blob: 114e3f2aa8895b9557b77d9dde1ac76a4b6b7f69 [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 Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * 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 Moolenaar329cc7e2005-08-10 07:51:35 +000053/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000054 * Only use it for small word lists!
55 * SPELL_COMPRESS_CNT is in how many words we compress the tree to limit the
56 * amount of memory used (esp. for Italian). */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000057#if 0
58# define SPELL_PRINTTREE
59# define SPELL_COMPRESS_CNT 1
60#else
61# define SPELL_COMPRESS_CNT 1000000
62#endif
63
Bram Moolenaar51485f02005-06-04 21:55:20 +000064/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000065 * Use this to adjust the score after finding suggestions, based on the
66 * suggested word sounding like the bad word. This is much faster than doing
67 * it for every possible suggestion.
68 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
69 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000070 * Used when 'spellsuggest' is set to "best".
71 */
72#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
73
74/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000075 * Vim spell file format: <HEADER>
76 * <SUGGEST>
77 * <LWORDTREE>
78 * <KWORDTREE>
79 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000080 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000081 * <HEADER>: <fileID>
82 * <regioncnt> <regionname> ...
83 * <charflagslen> <charflags>
84 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000085 * <midwordlen> <midword>
Bram Moolenaarae5bce12005-08-15 21:41:48 +000086 * <compoundlen> <compoundtype> <compoundinfo>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000088 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000089 * <fileID> 10 bytes "VIMspell10"
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000091 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 * First <regionname> is region 1.
93 *
94 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
95 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000096 * 0x01 word character CF_WORD
97 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +000098 * <fcharslen> 2 bytes Number of bytes in <fchars>.
99 * <fchars> N bytes Folded characters, first one is for character 128.
100 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000101 * <midwordlen> 2 bytes Number of bytes in <midword>.
102 * <midword> N bytes Characters that are word characters only when used
103 * in the middle of a word.
104 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000105 * <compoundlen> 2 bytes Number of bytes following for compound info (can
106 * be used to skip it when it's not understood).
107 *
108 * <compoundtype 1 byte 1: compound words using <comp1minlen> and
109 * <comp1flags>
110 *
111 * <comp1minlen> 1 byte minimal word length for compounding
112 *
113 * <comp1flags> N bytes flags used for compounding words
114 *
115 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000116 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
117 *
118 * <prefcond> : <condlen> <condstr>
119 *
120 * <condlen> 1 byte Length of <condstr>.
121 *
122 * <condstr> N bytes Condition for the prefix.
123 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000124 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000125 * <SUGGEST> : <repcount> <rep> ...
126 * <salflags> <salcount> <sal> ...
127 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000128 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000129 * <repcount> 2 bytes number of <rep> items, MSB first.
130 *
131 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
132 *
133 * <repfromlen> 1 byte length of <repfrom>
134 *
135 * <repfrom> N bytes "from" part of replacement
136 *
137 * <reptolen> 1 byte length of <repto>
138 *
139 * <repto> N bytes "to" part of replacement
140 *
141 * <salflags> 1 byte flags for soundsalike conversion:
142 * SAL_F0LLOWUP
143 * SAL_COLLAPSE
144 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000145 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
146 *
147 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000148 *
149 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
150 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000151 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000152 *
153 * <salfrom> N bytes "from" part of soundsalike
154 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000155 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000156 *
157 * <salto> N bytes "to" part of soundsalike
158 *
159 * <maplen> 2 bytes length of <mapstr>, MSB first
160 *
161 * <mapstr> N bytes String with sequences of similar characters,
162 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000163 *
164 *
165 * <LWORDTREE>: <wordtree>
166 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000167 * <KWORDTREE>: <wordtree>
168 *
169 * <PREFIXTREE>: <wordtree>
170 *
171 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000172 * <wordtree>: <nodecount> <nodedata> ...
173 *
174 * <nodecount> 4 bytes Number of nodes following. MSB first.
175 *
176 * <nodedata>: <siblingcount> <sibling> ...
177 *
178 * <siblingcount> 1 byte Number of siblings in this node. The siblings
179 * follow in sorted order.
180 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000181 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000182 * | <flags> [<flags2>] [<region>] [<affixID>]
183 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000184 *
185 * <byte> 1 byte Byte value of the sibling. Special cases:
186 * BY_NOFLAGS: End of word without flags and for all
187 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000188 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000189 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000190 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000191 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000192 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000193 * BY_FLAGS2: End of word, <flags> and <flags2>
194 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000195 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000196 * and <xbyte> follow.
197 *
198 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
199 *
200 * <xbyte> 1 byte byte value of the sibling.
201 *
202 * <flags> 1 byte bitmask of:
203 * WF_ALLCAP word must have only capitals
204 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000205 * WF_KEEPCAP keep-case word
206 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000207 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000208 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000210 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000211 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000212 * <flags2> 1 byte Only used when there are postponed prefixes.
213 * Bitmask of:
214 * WF_HAS_AFF >> 8 word includes affix
215 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000216 * <pflags> 1 byte bitmask of:
217 * WFP_RARE rare prefix
218 * WFP_NC non-combining prefix
219 * WFP_UP letter after prefix made upper case
220 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000221 * <region> 1 byte Bitmask for regions in which word is valid. When
222 * omitted it's valid in all regions.
223 * Lowest bit is for region 1.
224 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000225 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000226 * PREFIXTREE used for the required prefix ID.
227 *
228 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
229 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000230 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000231 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000232 */
233
Bram Moolenaare19defe2005-03-21 08:23:33 +0000234#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
235# include <io.h> /* for lseek(), must be before vim.h */
236#endif
237
238#include "vim.h"
239
240#if defined(FEAT_SYN_HL) || defined(PROTO)
241
242#ifdef HAVE_FCNTL_H
243# include <fcntl.h>
244#endif
245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000246#define MAXWLEN 250 /* Assume max. word len is this many bytes.
247 Some places assume a word length fits in a
248 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000249
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000250/* Type used for indexes in the word tree need to be at least 3 bytes. If int
251 * is 8 bytes we could use something smaller, but what? */
252#if SIZEOF_INT > 2
253typedef int idx_T;
254#else
255typedef long idx_T;
256#endif
257
258/* Flags used for a word. Only the lowest byte can be used, the region byte
259 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000260#define WF_REGION 0x01 /* region byte follows */
261#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
262#define WF_ALLCAP 0x04 /* word must be all capitals */
263#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000264#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000265#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000266#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000267#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000268
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000269/* for <flags2>, shifted up one byte to be used in wn_flags */
270#define WF_HAS_AFF 0x0100 /* word includes affix */
271
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000272#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000273
Bram Moolenaar53805d12005-08-01 07:08:33 +0000274/* flags for <pflags> */
275#define WFP_RARE 0x01 /* rare prefix */
276#define WFP_NC 0x02 /* prefix is not combining */
277#define WFP_UP 0x04 /* to-upper prefix */
278
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000279/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000280 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000281#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
282 * postponed prefix */
283#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
284 * postponed prefix */
285#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
286 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000287
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000288/* Special byte values for <byte>. Some are only used in the tree for
289 * postponed prefixes, some only in the other trees. This is a bit messy... */
290#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000291 * postponed prefix: no <pflags> */
292#define BY_INDEX 1 /* child is shared, index follows */
293#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
294 * postponed prefix: <pflags> follows */
295#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
296 * follow; never used in prefix tree */
297#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000299/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000300 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000301 * One replacement: from "ft_from" to "ft_to". */
302typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000303{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304 char_u *ft_from;
305 char_u *ft_to;
306} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000307
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000308/* Info from "SAL" entries in ".aff" file used in sl_sal.
309 * The info is split for quick processing by spell_soundfold().
310 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
311typedef struct salitem_S
312{
313 char_u *sm_lead; /* leading letters */
314 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000315 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000316 char_u *sm_rules; /* rules like ^, $, priority */
317 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000318#ifdef FEAT_MBYTE
319 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000321 int *sm_to_w; /* wide character copy of "sm_to" */
322#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000323} salitem_T;
324
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000325#ifdef FEAT_MBYTE
326typedef int salfirst_T;
327#else
328typedef short salfirst_T;
329#endif
330
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000331/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000332 * Structure used to store words and other info for one language, loaded from
333 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000334 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
335 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
336 *
337 * The "byts" array stores the possible bytes in each tree node, preceded by
338 * the number of possible bytes, sorted on byte value:
339 * <len> <byte1> <byte2> ...
340 * The "idxs" array stores the index of the child node corresponding to the
341 * byte in "byts".
342 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000343 * the flags, region mask and affixID for the word. There may be several
344 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000345 */
346typedef struct slang_S slang_T;
347struct slang_S
348{
349 slang_T *sl_next; /* next language */
350 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000351 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000352 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000353
Bram Moolenaar51485f02005-06-04 21:55:20 +0000354 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000355 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000356 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000357 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000358 char_u *sl_pbyts; /* prefix tree word bytes */
359 idx_T *sl_pidxs; /* prefix tree word indexes */
360
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000362
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000363 char_u *sl_midword; /* MIDWORD string or NULL */
364
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000365 int sl_compminlen; /* COMPOUNDMIN */
366 char_u *sl_compflags; /* COMPOUNDFLAGS (NULL when no compounding) */
367
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000368 int sl_prefixcnt; /* number of items in "sl_prefprog" */
369 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000371 garray_T sl_rep; /* list of fromto_T entries from REP lines */
372 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
373 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000374 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000375 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000376 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000377 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
378 * "sl_sal_first" maps chars, when has_mbyte
379 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000380 int sl_followup; /* SAL followup */
381 int sl_collapse; /* SAL collapse_result */
382 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000383 int sl_has_map; /* TRUE if there is a MAP line */
384#ifdef FEAT_MBYTE
385 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
386 int sl_map_array[256]; /* MAP for first 256 chars */
387#else
388 char_u sl_map_array[256]; /* MAP for first 256 chars */
389#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000390};
391
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000392/* First language that is loaded, start of the linked list of loaded
393 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000394static slang_T *first_lang = NULL;
395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000396/* Flags used in .spl file for soundsalike flags. */
397#define SAL_F0LLOWUP 1
398#define SAL_COLLAPSE 2
399#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000400#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000401
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000402/*
403 * Structure used in "b_langp", filled from 'spelllang'.
404 */
405typedef struct langp_S
406{
407 slang_T *lp_slang; /* info for this language (NULL for last one) */
408 int lp_region; /* bitmask for region or REGION_ALL */
409} langp_T;
410
411#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
412
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000413#define REGION_ALL 0xff /* word valid in all regions */
414
415/* Result values. Lower number is accepted over higher one. */
416#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000417#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000418#define SP_RARE 1
419#define SP_LOCAL 2
420#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000421
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000422#define VIMSPELLMAGIC "VIMspell10" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000423#define VIMSPELLMAGICL 10
424
Bram Moolenaar7887d882005-07-01 22:33:52 +0000425/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000426static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000427
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000429 * Information used when looking for suggestions.
430 */
431typedef struct suginfo_S
432{
433 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000434 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000435 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000436 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000437 char_u *su_badptr; /* start of bad word in line */
438 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000439 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000440 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
441 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
442 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000443} suginfo_T;
444
445/* One word suggestion. Used in "si_ga". */
446typedef struct suggest_S
447{
448 char_u *st_word; /* suggested word, allocated string */
449 int st_orglen; /* length of replaced text */
450 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000451 int st_altscore; /* used when st_score compares equal */
452 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000453 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454} suggest_T;
455
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000456#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000457
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000458/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
459 * called the score may change, thus we need to keep more than what is
460 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000461#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000462
463/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
464 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000465#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000466
467/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000468#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000469#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000470#define SCORE_REGION 70 /* word is for different region */
471#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000472#define SCORE_SWAP 90 /* swap two characters */
473#define SCORE_SWAP3 110 /* swap two characters in three */
474#define SCORE_REP 87 /* REP replacement */
475#define SCORE_SUBST 93 /* substitute a character */
476#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000477#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000478#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000479#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000480#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000481#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000482#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000483#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000484#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000486#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
488 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489
490#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000491#define SCORE_MAXMAX 999999 /* accept any score */
492
493/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000494 * Structure to store info for word matching.
495 */
496typedef struct matchinf_S
497{
498 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000499
500 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000501 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000502 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000503 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000504 char_u *mi_cend; /* char after what was used for
505 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000506
507 /* case-folded text */
508 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000509 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000510
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000511 /* for when checking word after a prefix */
512 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000513 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000514 int mi_prefcnt; /* number of entries at mi_prefarridx */
515 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000516#ifdef FEAT_MBYTE
517 int mi_cprefixlen; /* byte length of prefix in original
518 case */
519#else
520# define mi_cprefixlen mi_prefixlen /* it's the same value */
521#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000522
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000523 /* for when checking a compound word */
524 int mi_compoff; /* start of following word offset */
525
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000526 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000527 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000528 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000529 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000530} matchinf_T;
531
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000532/*
533 * The tables used for recognizing word characters according to spelling.
534 * These are only used for the first 256 characters of 'encoding'.
535 */
536typedef struct spelltab_S
537{
538 char_u st_isw[256]; /* flags: is word char */
539 char_u st_isu[256]; /* flags: is uppercase char */
540 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000541 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542} spelltab_T;
543
544static spelltab_T spelltab;
545static int did_set_spelltab;
546
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000547#define CF_WORD 0x01
548#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000549
550static void clear_spell_chartab __ARGS((spelltab_T *sp));
551static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000552static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
553static int spell_iswordp_nmw __ARGS((char_u *p));
554#ifdef FEAT_MBYTE
555static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
556#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000557static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000558
559/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000560 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000561 */
562typedef enum
563{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000564 STATE_START = 0, /* At start of node check for NUL bytes (goodword
565 * ends); if badword ends there is a match, otherwise
566 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000567 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000568 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000569 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
570 STATE_PLAIN, /* Use each byte of the node. */
571 STATE_DEL, /* Delete a byte from the bad word. */
572 STATE_INS, /* Insert a byte in the bad word. */
573 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000574 STATE_UNSWAP, /* Undo swap two characters. */
575 STATE_SWAP3, /* Swap two characters over three. */
576 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000577 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000578 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000579 STATE_REP_INI, /* Prepare for using REP items. */
580 STATE_REP, /* Use matching REP items from the .aff file. */
581 STATE_REP_UNDO, /* Undo a REP item replacement. */
582 STATE_FINAL /* End of this node. */
583} state_T;
584
585/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000586 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587 */
588typedef struct trystate_S
589{
Bram Moolenaarea424162005-06-16 21:51:00 +0000590 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000591 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000593 short ts_curi; /* index in list of child nodes */
594 char_u ts_fidx; /* index in fword[], case-folded bad word */
595 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
596 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000597 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
598 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000599#ifdef FEAT_MBYTE
600 char_u ts_tcharlen; /* number of bytes in tword character */
601 char_u ts_tcharidx; /* current byte index in tword character */
602 char_u ts_isdiff; /* DIFF_ values */
603 char_u ts_fcharstart; /* index in fword where badword char started */
604#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000606 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000607 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000608} trystate_T;
609
Bram Moolenaarea424162005-06-16 21:51:00 +0000610/* values for ts_isdiff */
611#define DIFF_NONE 0 /* no different byte (yet) */
612#define DIFF_YES 1 /* different byte found */
613#define DIFF_INSERT 2 /* inserting character */
614
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000615/* special values ts_prefixdepth */
616#define PREFIXTREE 0xfe /* walking through the prefix tree */
617#define NOPREFIX 0xff /* not using prefixes */
618
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000619/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000620#define FIND_FOLDWORD 0 /* find word case-folded */
621#define FIND_KEEPWORD 1 /* find keep-case word */
622#define FIND_PREFIX 2 /* find word after prefix */
623#define FIND_COMPOUND 3 /* find case-folded compound word */
624#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000625
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000626static slang_T *slang_alloc __ARGS((char_u *lang));
627static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000628static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000629static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000630static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000631static void find_prefix __ARGS((matchinf_T *mip));
632static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000633static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000634static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000635static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000636static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000637static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000638static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000639static 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 +0000640static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000641static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
642static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000643#ifdef FEAT_MBYTE
644static int *mb_str2wide __ARGS((char_u *s));
645#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000646static 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 +0000647static void clear_midword __ARGS((buf_T *buf));
648static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000649static int find_region __ARGS((char_u *rp, char_u *region));
650static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000651static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000652static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000653static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000654static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
655static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000656static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000657static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000658static 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 +0000659#ifdef FEAT_EVAL
660static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
661#endif
662static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
663static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000664static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000665static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000666static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000667static void suggest_try_special __ARGS((suginfo_T *su));
668static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000669static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000670#ifdef FEAT_MBYTE
671static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
672#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000673static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000674static void score_comp_sal __ARGS((suginfo_T *su));
675static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000676static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000677static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000678static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000679static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000681static 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 +0000682static void add_banned __ARGS((suginfo_T *su, char_u *word));
683static int was_banned __ARGS((suginfo_T *su, char_u *word));
684static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000685static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000686static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000687static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
688static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
689static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000690#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000691static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000692#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000693static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000694static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000695static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
696static 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 +0000697
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000698/*
699 * Use our own character-case definitions, because the current locale may
700 * differ from what the .spl file uses.
701 * These must not be called with negative number!
702 */
703#ifndef FEAT_MBYTE
704/* Non-multi-byte implementation. */
705# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
706# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
707# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
708#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000709# if defined(HAVE_WCHAR_H)
710# include <wchar.h> /* for towupper() and towlower() */
711# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000712/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
713 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
714 * the "w" library function for characters above 255 if available. */
715# ifdef HAVE_TOWLOWER
716# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
717 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
718# else
719# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
720 : (c) < 256 ? spelltab.st_fold[c] : (c))
721# endif
722
723# ifdef HAVE_TOWUPPER
724# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
725 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
726# else
727# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
728 : (c) < 256 ? spelltab.st_upper[c] : (c))
729# endif
730
731# ifdef HAVE_ISWUPPER
732# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
733 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
734# else
735# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000736 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000737# endif
738#endif
739
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000740
741static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000742static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000743static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000744
745/*
746 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000747 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000748 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
749 * or when it's OK it remains unchanged.
750 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000751 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000752 * "capcol" is used to check for a Capitalised word after the end of a
753 * sentence. If it's zero then perform the check. Return the column where to
754 * check next, or -1 when no sentence end was found. If it's NULL then don't
755 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000756 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000757 * Returns the length of the word in bytes, also when it's OK, so that the
758 * caller can skip over the word.
759 */
760 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000761spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000762 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000763 char_u *ptr;
764 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000765 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766{
767 matchinf_T mi; /* Most things are put in "mi" so that it can
768 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000770 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000771
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000772 /* A word never starts at a space or a control character. Return quickly
773 * then, skipping over the character. */
774 if (*ptr <= ' ')
775 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000776
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000778 * 0X99FF. But when a word character follows do check spelling to find
779 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000780 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000781 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000782 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
783 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000784 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000785 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000786 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000787 nrlen = mi.mi_end - ptr;
788 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000789 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000790 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000791
792 /* Try including the digits in the word. */
793 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000794 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000795 else
796 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000797
Bram Moolenaar0c405862005-06-22 22:26:26 +0000798 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000799 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000800 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000801 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000802 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000803 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000804 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000805 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000806
807 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
808 {
809 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000810 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000811 if (!SPELL_ISUPPER(c))
812 {
813 *attrp = highlight_attr[HLF_SPC];
814 return (int)(mi.mi_fend - ptr);
815 }
816 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000817 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000818 if (capcol != NULL)
819 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000820
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000821 /* We always use the characters up to the next non-word character,
822 * also for bad words. */
823 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000824
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000825 /* Check caps type later. */
826 mi.mi_capflags = 0;
827 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000828 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000829
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000830 /* Include one non-word character so that we can check for the
831 * word end. */
832 if (*mi.mi_fend != NUL)
833 mb_ptr_adv(mi.mi_fend);
834
835 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
836 MAXWLEN + 1);
837 mi.mi_fwordlen = STRLEN(mi.mi_fword);
838
839 /* The word is bad unless we recognize it. */
840 mi.mi_result = SP_BAD;
841
842 /*
843 * Loop over the languages specified in 'spelllang'.
844 * We check them all, because a matching word may be longer than an
845 * already found matching word.
846 */
847 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
848 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
849 {
850 /* Check for a matching word in case-folded words. */
851 find_word(&mi, FIND_FOLDWORD);
852
853 /* Check for a matching word in keep-case words. */
854 find_word(&mi, FIND_KEEPWORD);
855
856 /* Check for matching prefixes. */
857 find_prefix(&mi);
858 }
859
860 if (mi.mi_result != SP_OK)
861 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000862 /* If we found a number skip over it. Allows for "42nd". Do flag
863 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000864 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000865 {
866 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
867 return nrlen;
868 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000869
870 /* When we are at a non-word character there is no error, just
871 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000872 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000873 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000874 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
875 {
876 regmatch_T regmatch;
877
878 /* Check for end of sentence. */
879 regmatch.regprog = wp->w_buffer->b_cap_prog;
880 regmatch.rm_ic = FALSE;
881 if (vim_regexec(&regmatch, ptr, 0))
882 *capcol = (int)(regmatch.endp[0] - ptr);
883 }
884
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000885#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000886 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000887 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000888#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000889 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000890 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000891
892 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
893 *attrp = highlight_attr[HLF_SPB];
894 else if (mi.mi_result == SP_RARE)
895 *attrp = highlight_attr[HLF_SPR];
896 else
897 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000898 }
899
Bram Moolenaar51485f02005-06-04 21:55:20 +0000900 return (int)(mi.mi_end - ptr);
901}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000902
Bram Moolenaar51485f02005-06-04 21:55:20 +0000903/*
904 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000905 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
906 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
907 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
908 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909 *
910 * For a match mip->mi_result is updated.
911 */
912 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000913find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000914 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000915 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000916{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000917 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000918 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000919 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000920 int endidxcnt = 0;
921 int len;
922 int wlen = 0;
923 int flen;
924 int c;
925 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000926 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927#ifdef FEAT_MBYTE
928 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000929 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000930#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000931 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000932 slang_T *slang = mip->mi_lp->lp_slang;
933 unsigned flags;
934 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000935 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000936 int word_ends;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000937
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000938 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000939 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000940 /* Check for word with matching case in keep-case tree. */
941 ptr = mip->mi_word;
942 flen = 9999; /* no case folding, always enough bytes */
943 byts = slang->sl_kbyts;
944 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000945
946 if (mode == FIND_KEEPCOMPOUND)
947 /* Skip over the previously found word(s). */
948 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000949 }
950 else
951 {
952 /* Check for case-folded in case-folded tree. */
953 ptr = mip->mi_fword;
954 flen = mip->mi_fwordlen; /* available case-folded bytes */
955 byts = slang->sl_fbyts;
956 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000957
958 if (mode == FIND_PREFIX)
959 {
960 /* Skip over the prefix. */
961 wlen = mip->mi_prefixlen;
962 flen -= mip->mi_prefixlen;
963 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000964 else if (mode == FIND_COMPOUND)
965 {
966 /* Skip over the previously found word(s). */
967 wlen = mip->mi_compoff;
968 flen -= mip->mi_compoff;
969 }
970
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000971 }
972
Bram Moolenaar51485f02005-06-04 21:55:20 +0000973 if (byts == NULL)
974 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000975
Bram Moolenaar51485f02005-06-04 21:55:20 +0000976 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000977 * Repeat advancing in the tree until:
978 * - there is a byte that doesn't match,
979 * - we reach the end of the tree,
980 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000981 */
982 for (;;)
983 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000984 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000985 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000986
987 len = byts[arridx++];
988
989 /* If the first possible byte is a zero the word could end here.
990 * Remember this index, we first check for the longest word. */
991 if (byts[arridx] == 0)
992 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000993 if (endidxcnt == MAXWLEN)
994 {
995 /* Must be a corrupted spell file. */
996 EMSG(_(e_format));
997 return;
998 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000999 endlen[endidxcnt] = wlen;
1000 endidx[endidxcnt++] = arridx++;
1001 --len;
1002
1003 /* Skip over the zeros, there can be several flag/region
1004 * combinations. */
1005 while (len > 0 && byts[arridx] == 0)
1006 {
1007 ++arridx;
1008 --len;
1009 }
1010 if (len == 0)
1011 break; /* no children, word must end here */
1012 }
1013
1014 /* Stop looking at end of the line. */
1015 if (ptr[wlen] == NUL)
1016 break;
1017
1018 /* Perform a binary search in the list of accepted bytes. */
1019 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001020 if (c == TAB) /* <Tab> is handled like <Space> */
1021 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001022 lo = arridx;
1023 hi = arridx + len - 1;
1024 while (lo < hi)
1025 {
1026 m = (lo + hi) / 2;
1027 if (byts[m] > c)
1028 hi = m - 1;
1029 else if (byts[m] < c)
1030 lo = m + 1;
1031 else
1032 {
1033 lo = hi = m;
1034 break;
1035 }
1036 }
1037
1038 /* Stop if there is no matching byte. */
1039 if (hi < lo || byts[lo] != c)
1040 break;
1041
1042 /* Continue at the child (if there is one). */
1043 arridx = idxs[lo];
1044 ++wlen;
1045 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001046
1047 /* One space in the good word may stand for several spaces in the
1048 * checked word. */
1049 if (c == ' ')
1050 {
1051 for (;;)
1052 {
1053 if (flen <= 0 && *mip->mi_fend != NUL)
1054 flen = fold_more(mip);
1055 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1056 break;
1057 ++wlen;
1058 --flen;
1059 }
1060 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 }
1062
1063 /*
1064 * Verify that one of the possible endings is valid. Try the longest
1065 * first.
1066 */
1067 while (endidxcnt > 0)
1068 {
1069 --endidxcnt;
1070 arridx = endidx[endidxcnt];
1071 wlen = endlen[endidxcnt];
1072
1073#ifdef FEAT_MBYTE
1074 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1075 continue; /* not at first byte of character */
1076#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001077 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001078 {
1079 if (slang->sl_compflags == NULL)
1080 continue; /* next char is a word character */
1081 word_ends = FALSE;
1082 }
1083 else
1084 word_ends = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085
1086#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001087 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001088 {
1089 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001090 * when folding case. This can be slow, take a shortcut when the
1091 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001092 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001093 if (STRNCMP(ptr, p, wlen) != 0)
1094 {
1095 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1096 mb_ptr_adv(p);
1097 wlen = p - mip->mi_word;
1098 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001099 }
1100#endif
1101
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 /* Check flags and region. For FIND_PREFIX check the condition and
1103 * prefix ID.
1104 * Repeat this if there are more flags/region alternatives until there
1105 * is a match. */
1106 res = SP_BAD;
1107 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1108 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001109 {
1110 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001111
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001112 /* For the fold-case tree check that the case of the checked word
1113 * matches with what the word in the tree requires.
1114 * For keep-case tree the case is always right. For prefixes we
1115 * don't bother to check. */
1116 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001117 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001118 if (mip->mi_cend != mip->mi_word + wlen)
1119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001120 /* mi_capflags was set for a different word length, need
1121 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001123 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001124 }
1125
Bram Moolenaar0c405862005-06-22 22:26:26 +00001126 if (mip->mi_capflags == WF_KEEPCAP
1127 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001128 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001129 }
1130
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001131 /* When mode is FIND_PREFIX the word must support the prefix:
1132 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001133 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001134 else if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001135 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001136 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001137 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001138 mip->mi_word + mip->mi_cprefixlen, slang,
1139 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001140 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001141 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001142
1143 /* Use the WF_RARE flag for a rare prefix. */
1144 if (c & WF_RAREPFX)
1145 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001146 }
1147
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001148 if (mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1149 || !word_ends)
1150 {
1151 /* Makes you wonder why someone puts a compound flag on a word
1152 * that's too short... Myspell compatibility requires this
1153 * anyway. */
1154 if (wlen < slang->sl_compminlen)
1155 continue;
1156
1157 /* The word doesn't end or it comes after another: it must
1158 * have a compound flag. */
1159 /* TODO: check more flags */
1160 if (*slang->sl_compflags != ((unsigned)flags >> 24))
1161 continue;
1162 }
1163
1164 if (!word_ends)
1165 {
1166 /* Check that a valid word follows. If there is one, it will
1167 * set "mi_result", thus we are always finished here.
1168 * Recursive! */
1169
1170 /* Find following word in case-folded tree. */
1171 mip->mi_compoff = endlen[endidxcnt];
1172#ifdef FEAT_MBYTE
1173 if (has_mbyte && mode == FIND_KEEPWORD)
1174 {
1175 /* Compute byte length in case-folded word from "wlen":
1176 * byte length in keep-case word. Length may change when
1177 * folding case. This can be slow, take a shortcut when
1178 * the case-folded word is equal to the keep-case word. */
1179 p = mip->mi_fword;
1180 if (STRNCMP(ptr, p, wlen) != 0)
1181 {
1182 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1183 mb_ptr_adv(p);
1184 mip->mi_compoff = p - mip->mi_fword;
1185 }
1186 }
1187#endif
1188 find_word(mip, FIND_COMPOUND);
1189 if (mip->mi_result == SP_OK)
1190 break;
1191
1192 /* Find following word in keep-case tree. */
1193 mip->mi_compoff = wlen;
1194 find_word(mip, FIND_KEEPCOMPOUND);
1195 if (mip->mi_result == SP_OK)
1196 break;
1197 continue;
1198 }
1199
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001200 if (flags & WF_BANNED)
1201 res = SP_BANNED;
1202 else if (flags & WF_REGION)
1203 {
1204 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001205 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001206 res = SP_OK;
1207 else
1208 res = SP_LOCAL;
1209 }
1210 else if (flags & WF_RARE)
1211 res = SP_RARE;
1212 else
1213 res = SP_OK;
1214
1215 /* Always use the longest match and the best result. */
1216 if (mip->mi_result > res)
1217 {
1218 mip->mi_result = res;
1219 mip->mi_end = mip->mi_word + wlen;
1220 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001221 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001222 mip->mi_end = mip->mi_word + wlen;
1223
1224 if (res == SP_OK)
1225 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 }
1227
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001228 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001231}
1232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001233/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001234 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1235 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001236 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001237 */
1238 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001239valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001240 int totprefcnt; /* nr of prefix IDs */
1241 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001242 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001243 char_u *word;
1244 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001245 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001246{
1247 int prefcnt;
1248 int pidx;
1249 regprog_T *rp;
1250 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001251 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001252
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001253 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001254 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1255 {
1256 pidx = slang->sl_pidxs[arridx + prefcnt];
1257
1258 /* Check the prefix ID. */
1259 if (prefid != (pidx & 0xff))
1260 continue;
1261
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001262 /* Check if the prefix doesn't combine and the word already has a
1263 * suffix. */
1264 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1265 continue;
1266
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001267 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001268 * stored in the two bytes above the prefix ID byte. */
1269 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001270 if (rp != NULL)
1271 {
1272 regmatch.regprog = rp;
1273 regmatch.rm_ic = FALSE;
1274 if (!vim_regexec(&regmatch, word, 0))
1275 continue;
1276 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001277 else if (cond_req)
1278 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001279
Bram Moolenaar53805d12005-08-01 07:08:33 +00001280 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001281 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001282 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001283 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001284}
1285
1286/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001287 * Check if the word at "mip->mi_word" has a matching prefix.
1288 * If it does, then check the following word.
1289 *
1290 * For a match mip->mi_result is updated.
1291 */
1292 static void
1293find_prefix(mip)
1294 matchinf_T *mip;
1295{
1296 idx_T arridx = 0;
1297 int len;
1298 int wlen = 0;
1299 int flen;
1300 int c;
1301 char_u *ptr;
1302 idx_T lo, hi, m;
1303 slang_T *slang = mip->mi_lp->lp_slang;
1304 char_u *byts;
1305 idx_T *idxs;
1306
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001307 byts = slang->sl_pbyts;
1308 if (byts == NULL)
1309 return; /* array is empty */
1310
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001311 /* We use the case-folded word here, since prefixes are always
1312 * case-folded. */
1313 ptr = mip->mi_fword;
1314 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001315 idxs = slang->sl_pidxs;
1316
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001317 /*
1318 * Repeat advancing in the tree until:
1319 * - there is a byte that doesn't match,
1320 * - we reach the end of the tree,
1321 * - or we reach the end of the line.
1322 */
1323 for (;;)
1324 {
1325 if (flen == 0 && *mip->mi_fend != NUL)
1326 flen = fold_more(mip);
1327
1328 len = byts[arridx++];
1329
1330 /* If the first possible byte is a zero the prefix could end here.
1331 * Check if the following word matches and supports the prefix. */
1332 if (byts[arridx] == 0)
1333 {
1334 /* There can be several prefixes with different conditions. We
1335 * try them all, since we don't know which one will give the
1336 * longest match. The word is the same each time, pass the list
1337 * of possible prefixes to find_word(). */
1338 mip->mi_prefarridx = arridx;
1339 mip->mi_prefcnt = len;
1340 while (len > 0 && byts[arridx] == 0)
1341 {
1342 ++arridx;
1343 --len;
1344 }
1345 mip->mi_prefcnt -= len;
1346
1347 /* Find the word that comes after the prefix. */
1348 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001349#ifdef FEAT_MBYTE
1350 if (has_mbyte)
1351 {
1352 /* Case-folded length may differ from original length. */
1353 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1354 mip->mi_word);
1355 }
1356 else
1357 mip->mi_cprefixlen = wlen;
1358#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001359 find_word(mip, FIND_PREFIX);
1360
1361
1362 if (len == 0)
1363 break; /* no children, word must end here */
1364 }
1365
1366 /* Stop looking at end of the line. */
1367 if (ptr[wlen] == NUL)
1368 break;
1369
1370 /* Perform a binary search in the list of accepted bytes. */
1371 c = ptr[wlen];
1372 lo = arridx;
1373 hi = arridx + len - 1;
1374 while (lo < hi)
1375 {
1376 m = (lo + hi) / 2;
1377 if (byts[m] > c)
1378 hi = m - 1;
1379 else if (byts[m] < c)
1380 lo = m + 1;
1381 else
1382 {
1383 lo = hi = m;
1384 break;
1385 }
1386 }
1387
1388 /* Stop if there is no matching byte. */
1389 if (hi < lo || byts[lo] != c)
1390 break;
1391
1392 /* Continue at the child (if there is one). */
1393 arridx = idxs[lo];
1394 ++wlen;
1395 --flen;
1396 }
1397}
1398
1399/*
1400 * Need to fold at least one more character. Do until next non-word character
1401 * for efficiency.
1402 * Return the length of the folded chars in bytes.
1403 */
1404 static int
1405fold_more(mip)
1406 matchinf_T *mip;
1407{
1408 int flen;
1409 char_u *p;
1410
1411 p = mip->mi_fend;
1412 do
1413 {
1414 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001415 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001416
1417 /* Include the non-word character so that we can check for the
1418 * word end. */
1419 if (*mip->mi_fend != NUL)
1420 mb_ptr_adv(mip->mi_fend);
1421
1422 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1423 mip->mi_fword + mip->mi_fwordlen,
1424 MAXWLEN - mip->mi_fwordlen);
1425 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1426 mip->mi_fwordlen += flen;
1427 return flen;
1428}
1429
1430/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001431 * Check case flags for a word. Return TRUE if the word has the requested
1432 * case.
1433 */
1434 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001435spell_valid_case(wordflags, treeflags)
1436 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001437 int treeflags; /* flags for the word in the spell tree */
1438{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001439 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001440 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001441 && ((treeflags & WF_ONECAP) == 0
1442 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443}
1444
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001445/*
1446 * Return TRUE if spell checking is not enabled.
1447 */
1448 static int
1449no_spell_checking()
1450{
1451 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1452 {
1453 EMSG(_("E756: Spell checking is not enabled"));
1454 return TRUE;
1455 }
1456 return FALSE;
1457}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001459/*
1460 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001461 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001462 * Return OK if found, FAIL otherwise.
1463 */
1464 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001465spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001466 int dir; /* FORWARD or BACKWARD */
1467 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001468 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001469{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001470 linenr_T lnum;
1471 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001472 char_u *line;
1473 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001474 char_u *endp;
1475 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001477 int has_syntax = syntax_present(curbuf);
1478 int col;
1479 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001480 char_u *buf = NULL;
1481 int buflen = 0;
1482 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001483 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001484
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001485 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001486 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001487
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488 /*
1489 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001490 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001491 *
1492 * When searching backwards, we continue in the line to find the last
1493 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001494 *
1495 * We concatenate the start of the next line, so that wrapped words work
1496 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1497 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001498 */
1499 lnum = curwin->w_cursor.lnum;
1500 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001501
1502 while (!got_int)
1503 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001504 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001505
Bram Moolenaar0c405862005-06-22 22:26:26 +00001506 len = STRLEN(line);
1507 if (buflen < len + MAXWLEN + 2)
1508 {
1509 vim_free(buf);
1510 buflen = len + MAXWLEN + 2;
1511 buf = alloc(buflen);
1512 if (buf == NULL)
1513 break;
1514 }
1515
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001516 /* In first line check first word for Capital. */
1517 if (lnum == 1)
1518 capcol = 0;
1519
1520 /* For checking first word with a capital skip white space. */
1521 if (capcol == 0)
1522 capcol = skipwhite(line) - line;
1523
Bram Moolenaar0c405862005-06-22 22:26:26 +00001524 /* Copy the line into "buf" and append the start of the next line if
1525 * possible. */
1526 STRCPY(buf, line);
1527 if (lnum < curbuf->b_ml.ml_line_count)
1528 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1529
1530 p = buf + skip;
1531 endp = buf + len;
1532 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001533 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001534 /* When searching backward don't search after the cursor. */
1535 if (dir == BACKWARD
1536 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001537 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001538 break;
1539
1540 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001541 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001542 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001543
1544 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001545 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001546 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001547 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001548 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001549 /* When searching forward only accept a bad word after
1550 * the cursor. */
1551 if (dir == BACKWARD
1552 || lnum > curwin->w_cursor.lnum
1553 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001554 && (colnr_T)(curline ? p - buf + len
1555 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001556 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001557 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001558 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001559 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001560 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001561 (void)syn_get_id(lnum, (colnr_T)col,
1562 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001563 }
1564 else
1565 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001566
Bram Moolenaar51485f02005-06-04 21:55:20 +00001567 if (can_spell)
1568 {
1569 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001570 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001571#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001572 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001573#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001574 if (dir == FORWARD)
1575 {
1576 /* No need to search further. */
1577 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001578 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001579 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001580 }
1581 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001582 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001583 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001584 }
1585
Bram Moolenaar51485f02005-06-04 21:55:20 +00001586 /* advance to character after the word */
1587 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001588 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001589 }
1590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001591 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001592 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001594 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001595 if (dir == BACKWARD)
1596 {
1597 if (found_pos.lnum != 0)
1598 {
1599 /* Use the last match in the line. */
1600 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001601 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001602 return OK;
1603 }
1604 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001605 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001606 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001607 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001608 }
1609 else
1610 {
1611 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001612 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001613 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001614
1615 /* Skip the characters at the start of the next line that were
1616 * included in a match crossing line boundaries. */
1617 if (attr == 0)
1618 skip = p - endp;
1619 else
1620 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001621
1622 /* Capscol skips over the inserted space. */
1623 --capcol;
1624
1625 /* But after empty line check first word in next line */
1626 if (*skipwhite(line) == NUL)
1627 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001628 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001629
1630 line_breakcheck();
1631 }
1632
Bram Moolenaar0c405862005-06-22 22:26:26 +00001633 vim_free(buf);
1634 return FAIL;
1635}
1636
1637/*
1638 * For spell checking: concatenate the start of the following line "line" into
1639 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1640 */
1641 void
1642spell_cat_line(buf, line, maxlen)
1643 char_u *buf;
1644 char_u *line;
1645 int maxlen;
1646{
1647 char_u *p;
1648 int n;
1649
1650 p = skipwhite(line);
1651 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1652 p = skipwhite(p + 1);
1653
1654 if (*p != NUL)
1655 {
1656 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001657 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001658 n = p - line;
1659 if (n >= maxlen)
1660 n = maxlen - 1;
1661 vim_memset(buf + 1, ' ', n);
1662 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001663}
1664
1665/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001666 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001667 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001668 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001669 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001670spell_load_lang(lang)
1671 char_u *lang;
1672{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001673 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001674 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001675 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001676
Bram Moolenaarb765d632005-06-07 21:00:02 +00001677 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001678 * It's truncated when an error is detected. */
1679 STRCPY(langcp, lang);
1680
Bram Moolenaarb765d632005-06-07 21:00:02 +00001681 /*
1682 * Find the first spell file for "lang" in 'runtimepath' and load it.
1683 */
1684 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1685 "spell/%s.%s.spl", lang, spell_enc());
1686 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001687
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001688 if (r == FAIL && *langcp != NUL)
1689 {
1690 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001691 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001692 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001693 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001694 }
1695
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001696 if (r == FAIL)
1697 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1698 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001699 else if (*langcp != NUL)
1700 {
1701 /* Load all the additions. */
1702 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1703 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1704 }
1705}
1706
1707/*
1708 * Return the encoding used for spell checking: Use 'encoding', except that we
1709 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1710 */
1711 static char_u *
1712spell_enc()
1713{
1714
1715#ifdef FEAT_MBYTE
1716 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1717 return p_enc;
1718#endif
1719 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001720}
1721
1722/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001723 * Get the name of the .spl file for the internal wordlist into
1724 * "fname[MAXPATHL]".
1725 */
1726 static void
1727int_wordlist_spl(fname)
1728 char_u *fname;
1729{
1730 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1731 int_wordlist, spell_enc());
1732}
1733
1734/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001735 * Allocate a new slang_T.
1736 * Caller must fill "sl_next".
1737 */
1738 static slang_T *
1739slang_alloc(lang)
1740 char_u *lang;
1741{
1742 slang_T *lp;
1743
Bram Moolenaar51485f02005-06-04 21:55:20 +00001744 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001745 if (lp != NULL)
1746 {
1747 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001748 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001749 }
1750 return lp;
1751}
1752
1753/*
1754 * Free the contents of an slang_T and the structure itself.
1755 */
1756 static void
1757slang_free(lp)
1758 slang_T *lp;
1759{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001760 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001761 vim_free(lp->sl_fname);
1762 slang_clear(lp);
1763 vim_free(lp);
1764}
1765
1766/*
1767 * Clear an slang_T so that the file can be reloaded.
1768 */
1769 static void
1770slang_clear(lp)
1771 slang_T *lp;
1772{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001773 garray_T *gap;
1774 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001775 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001776 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001777
Bram Moolenaar51485f02005-06-04 21:55:20 +00001778 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001779 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001780 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001781 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001782 vim_free(lp->sl_pbyts);
1783 lp->sl_pbyts = NULL;
1784
Bram Moolenaar51485f02005-06-04 21:55:20 +00001785 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001786 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001787 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001788 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001789 vim_free(lp->sl_pidxs);
1790 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001791
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001792 gap = &lp->sl_rep;
1793 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001794 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001795 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1796 vim_free(ftp->ft_from);
1797 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001798 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001799 ga_clear(gap);
1800
1801 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001802 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001803 {
1804 /* "ga_len" is set to 1 without adding an item for latin1 */
1805 if (gap->ga_data != NULL)
1806 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1807 for (i = 0; i < gap->ga_len; ++i)
1808 vim_free(((int **)gap->ga_data)[i]);
1809 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001810 else
1811 /* SAL items: free salitem_T items */
1812 while (gap->ga_len > 0)
1813 {
1814 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1815 vim_free(smp->sm_lead);
1816 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1817 vim_free(smp->sm_to);
1818#ifdef FEAT_MBYTE
1819 vim_free(smp->sm_lead_w);
1820 vim_free(smp->sm_oneof_w);
1821 vim_free(smp->sm_to_w);
1822#endif
1823 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001824 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001825
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001826 for (i = 0; i < lp->sl_prefixcnt; ++i)
1827 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001828 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001829 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001830 lp->sl_prefprog = NULL;
1831
1832 vim_free(lp->sl_midword);
1833 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001834
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001835 vim_free(lp->sl_compflags);
1836 lp->sl_compflags = NULL;
1837
Bram Moolenaarea424162005-06-16 21:51:00 +00001838#ifdef FEAT_MBYTE
1839 {
1840 int todo = lp->sl_map_hash.ht_used;
1841 hashitem_T *hi;
1842
1843 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1844 if (!HASHITEM_EMPTY(hi))
1845 {
1846 --todo;
1847 vim_free(hi->hi_key);
1848 }
1849 }
1850 hash_clear(&lp->sl_map_hash);
1851#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001852}
1853
1854/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001855 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001856 * Invoked through do_in_runtimepath().
1857 */
1858 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001859spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001860 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001861 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001862{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001863 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001864}
1865
1866/*
1867 * Load one spell file and store the info into a slang_T.
1868 *
1869 * This is invoked in two ways:
1870 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1871 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1872 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1873 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001874 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001875 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001876 static slang_T *
1877spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001878 char_u *fname;
1879 char_u *lang;
1880 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001881 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001882{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001883 FILE *fd;
1884 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001885 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001886 char_u *bp;
1887 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001888 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001889 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001890 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001891 int round;
1892 char_u *save_sourcing_name = sourcing_name;
1893 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001894 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001895 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001896 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001897 garray_T *gap;
1898 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001899 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001900 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001901 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001902 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903
Bram Moolenaarb765d632005-06-07 21:00:02 +00001904 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001905 if (fd == NULL)
1906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001907 if (!silent)
1908 EMSG2(_(e_notopen), fname);
1909 else if (p_verbose > 2)
1910 {
1911 verbose_enter();
1912 smsg((char_u *)e_notopen, fname);
1913 verbose_leave();
1914 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001915 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001916 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001917 if (p_verbose > 2)
1918 {
1919 verbose_enter();
1920 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1921 verbose_leave();
1922 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001923
Bram Moolenaarb765d632005-06-07 21:00:02 +00001924 if (old_lp == NULL)
1925 {
1926 lp = slang_alloc(lang);
1927 if (lp == NULL)
1928 goto endFAIL;
1929
1930 /* Remember the file name, used to reload the file when it's updated. */
1931 lp->sl_fname = vim_strsave(fname);
1932 if (lp->sl_fname == NULL)
1933 goto endFAIL;
1934
1935 /* Check for .add.spl. */
1936 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1937 }
1938 else
1939 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001940
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001941 /* Set sourcing_name, so that error messages mention the file name. */
1942 sourcing_name = fname;
1943 sourcing_lnum = 0;
1944
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001945 /* <HEADER>: <fileID>
1946 * <regioncnt> <regionname> ...
1947 * <charflagslen> <charflags>
1948 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001949 * <midwordlen> <midword>
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001950 * <compoundlen> <compoundtype> <compoundinfo>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001951 * <prefcondcnt> <prefcond> ...
1952 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001953 for (i = 0; i < VIMSPELLMAGICL; ++i)
1954 buf[i] = getc(fd); /* <fileID> */
1955 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1956 {
1957 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001958 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001959 }
1960
1961 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001962 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001963 {
1964truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001965 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001966 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001967 }
1968 if (cnt > 8)
1969 {
1970formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001971 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001972 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001973 }
1974 for (i = 0; i < cnt; ++i)
1975 {
1976 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1977 lp->sl_regions[i * 2 + 1] = getc(fd);
1978 }
1979 lp->sl_regions[cnt * 2] = NUL;
1980
Bram Moolenaar7887d882005-07-01 22:33:52 +00001981 /* <charflagslen> <charflags> */
1982 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001983 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001984 goto endFAIL;
1985
1986 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001987 fol = read_cnt_string(fd, 2, &ccnt);
1988 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001989 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001990 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001991 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001992 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001993
1994 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1995 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001996 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001997
1998 vim_free(p);
1999 vim_free(fol);
2000
2001 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2002 if ((p == NULL) != (fol == NULL))
2003 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002004
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002005 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002006 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002007 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002008 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002009
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002010 /* <compoundlen> <compoundtype> <compoundinfo> */
2011 cnt = (getc(fd) << 8) + getc(fd); /* <compoundlen> */
2012 if (cnt < 0)
2013 goto endFAIL;
2014 if (cnt > 0)
2015 {
2016 --cnt;
2017 c = getc(fd); /* <compoundtype> */
2018 if (c != 1)
2019 {
2020 /* Unknown kind of compound words, skip the info. */
2021 while (cnt-- > 0)
2022 getc(fd);
2023 }
2024 else if (cnt < 2)
2025 goto formerr;
2026 else
2027 {
2028 --cnt;
2029 c = getc(fd); /* <comp1minlen> */
2030 if (c < 1 || c > 50)
2031 c = 3;
2032 lp->sl_compminlen = c;
2033
2034 p = alloc(cnt + 1);
2035 if (p == NULL)
2036 goto endFAIL;
2037 lp->sl_compflags = p;
2038 while (cnt-- > 0)
2039 *p++ = getc(fd); /* <comp1flags> */
2040 *p = NUL;
2041 }
2042 }
2043
2044
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002045 /* <prefcondcnt> <prefcond> ... */
2046 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2047 if (cnt > 0)
2048 {
2049 lp->sl_prefprog = (regprog_T **)alloc_clear(
2050 (unsigned)sizeof(regprog_T *) * cnt);
2051 if (lp->sl_prefprog == NULL)
2052 goto endFAIL;
2053 lp->sl_prefixcnt = cnt;
2054
2055 for (i = 0; i < cnt; ++i)
2056 {
2057 /* <prefcond> : <condlen> <condstr> */
2058 n = getc(fd); /* <condlen> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002059 if (n < 0 || n >= MAXWLEN)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002060 goto formerr;
2061 /* When <condlen> is zero we have an empty condition. Otherwise
2062 * compile the regexp program used to check for the condition. */
2063 if (n > 0)
2064 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002065 buf[0] = '^'; /* always match at one position only */
2066 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002067 while (n-- > 0)
2068 *p++ = getc(fd); /* <condstr> */
2069 *p = NUL;
2070 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2071 }
2072 }
2073 }
2074
2075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002076 /* <SUGGEST> : <repcount> <rep> ...
2077 * <salflags> <salcount> <sal> ...
2078 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002079
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002080 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2081 if (cnt < 0)
2082 goto formerr;
2083
2084 gap = &lp->sl_rep;
2085 if (ga_grow(gap, cnt) == FAIL)
2086 goto endFAIL;
2087
2088 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2089 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002090 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002091 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00002092 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002093 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002094 goto endFAIL;
2095 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002096 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002097 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002098 vim_free(ftp->ft_from);
2099 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002100 }
2101 }
2102
2103 /* Fill the first-index table. */
2104 first = lp->sl_rep_first;
2105 for (i = 0; i < 256; ++i)
2106 first[i] = -1;
2107 for (i = 0; i < gap->ga_len; ++i)
2108 {
2109 ftp = &((fromto_T *)gap->ga_data)[i];
2110 if (first[*ftp->ft_from] == -1)
2111 first[*ftp->ft_from] = i;
2112 }
2113
2114 i = getc(fd); /* <salflags> */
2115 if (i & SAL_F0LLOWUP)
2116 lp->sl_followup = TRUE;
2117 if (i & SAL_COLLAPSE)
2118 lp->sl_collapse = TRUE;
2119 if (i & SAL_REM_ACCENTS)
2120 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002121 if (i & SAL_SOFO)
2122 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002123 else
2124 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002125
2126 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2127 if (cnt < 0)
2128 goto formerr;
2129
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002130 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002131 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002132 /*
2133 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2134 */
2135 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002136 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002137
Bram Moolenaar7887d882005-07-01 22:33:52 +00002138 /* <salfromlen> <salfrom> */
2139 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002140 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002141 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002142
Bram Moolenaar7887d882005-07-01 22:33:52 +00002143 /* <saltolen> <salto> */
2144 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002145 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002146 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002147 vim_free(bp);
2148 goto endFAIL;
2149 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002150
Bram Moolenaar7887d882005-07-01 22:33:52 +00002151 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002152 if (bp != NULL && fol != NULL)
2153 i = set_sofo(lp, bp, fol);
2154 else if (bp != NULL || fol != NULL)
2155 i = FAIL; /* only one of two strings is an error */
2156 else
2157 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002158
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002159 vim_free(bp);
2160 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002161 if (i == FAIL)
2162 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002163 }
2164 else
2165 {
2166 /*
2167 * SAL items
2168 */
2169 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002170 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002171 if (ga_grow(gap, cnt) == FAIL)
2172 goto endFAIL;
2173
2174 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2175 for (; gap->ga_len < cnt; ++gap->ga_len)
2176 {
2177 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2178 ccnt = getc(fd); /* <salfromlen> */
2179 if (ccnt < 0)
2180 goto formerr;
2181 if ((p = alloc(ccnt + 2)) == NULL)
2182 goto endFAIL;
2183 smp->sm_lead = p;
2184
2185 /* Read up to the first special char into sm_lead. */
2186 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002187 {
2188 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002189 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002190 break;
2191 *p++ = c;
2192 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002193 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002194 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002195
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002196 /* Put (abc) chars in sm_oneof, if any. */
2197 if (c == '(')
2198 {
2199 smp->sm_oneof = p;
2200 for (++i; i < ccnt; ++i)
2201 {
2202 c = getc(fd); /* <salfrom> */
2203 if (c == ')')
2204 break;
2205 *p++ = c;
2206 }
2207 *p++ = NUL;
2208 if (++i < ccnt)
2209 c = getc(fd);
2210 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002211 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002212 smp->sm_oneof = NULL;
2213
2214 /* Any following chars go in sm_rules. */
2215 smp->sm_rules = p;
2216 if (i < ccnt)
2217 /* store the char we got while checking for end of sm_lead */
2218 *p++ = c;
2219 for (++i; i < ccnt; ++i)
2220 *p++ = getc(fd); /* <salfrom> */
2221 *p++ = NUL;
2222
Bram Moolenaar7887d882005-07-01 22:33:52 +00002223 /* <saltolen> <salto> */
2224 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002225 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002226 {
2227 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002228 goto formerr;
2229 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002230
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002231#ifdef FEAT_MBYTE
2232 if (has_mbyte)
2233 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002234 /* convert the multi-byte strings to wide char strings */
2235 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2236 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2237 if (smp->sm_oneof == NULL)
2238 smp->sm_oneof_w = NULL;
2239 else
2240 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002241 if (smp->sm_to == NULL)
2242 smp->sm_to_w = NULL;
2243 else
2244 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002245 if (smp->sm_lead_w == NULL
2246 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002247 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002248 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002249 vim_free(smp->sm_lead);
2250 vim_free(smp->sm_to);
2251 vim_free(smp->sm_lead_w);
2252 vim_free(smp->sm_oneof_w);
2253 vim_free(smp->sm_to_w);
2254 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002255 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002256 }
2257#endif
2258 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002259
2260 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002261 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 }
2263
Bram Moolenaar7887d882005-07-01 22:33:52 +00002264 /* <maplen> <mapstr> */
2265 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002266 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002268 if (p != NULL)
2269 {
2270 set_map_str(lp, p);
2271 vim_free(p);
2272 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002273
Bram Moolenaar51485f02005-06-04 21:55:20 +00002274 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002275 * round 2: <KWORDTREE>
2276 * round 3: <PREFIXTREE> */
2277 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002278 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002279 /* The tree size was computed when writing the file, so that we can
2280 * allocate it as one long block. <nodecount> */
2281 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2282 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002284 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002285 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002286 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002287 bp = lalloc((long_u)len, TRUE);
2288 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002289 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002290 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002291 lp->sl_fbyts = bp;
2292 else if (round == 2)
2293 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002294 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002295 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296
2297 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002298 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2299 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002300 goto endFAIL;
2301 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002302 lp->sl_fidxs = ip;
2303 else if (round == 2)
2304 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002305 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002306 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002307
2308 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002309 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002310 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002311 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002312 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002313 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002314 }
2315 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002316
Bram Moolenaarb765d632005-06-07 21:00:02 +00002317 /* For a new file link it in the list of spell files. */
2318 if (old_lp == NULL)
2319 {
2320 lp->sl_next = first_lang;
2321 first_lang = lp;
2322 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002323
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002324 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002326endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002327 if (lang != NULL)
2328 /* truncating the name signals the error to spell_load_lang() */
2329 *lang = NUL;
2330 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002331 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002332 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 lp = NULL;
2334 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002335
2336endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002337 if (fd != NULL)
2338 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002339 sourcing_name = save_sourcing_name;
2340 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002341
2342 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002343}
2344
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002345/*
2346 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002347 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002348 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002349 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002350 */
2351 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002352read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002353 FILE *fd;
2354 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002355 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002356{
2357 int cnt = 0;
2358 int i;
2359 char_u *str;
2360
2361 /* read the length bytes, MSB first */
2362 for (i = 0; i < cnt_bytes; ++i)
2363 cnt = (cnt << 8) + getc(fd);
2364 if (cnt < 0)
2365 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002366 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002367 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002368 return NULL;
2369 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002370 *cntp = cnt;
2371 if (cnt == 0)
2372 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002373
2374 /* allocate memory */
2375 str = alloc((unsigned)cnt + 1);
2376 if (str == NULL)
2377 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002378 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002379 return NULL;
2380 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002381
2382 /* Read the string. Doesn't check for truncated file. */
2383 for (i = 0; i < cnt; ++i)
2384 str[i] = getc(fd);
2385 str[i] = NUL;
2386
2387 return str;
2388}
2389
Bram Moolenaar7887d882005-07-01 22:33:52 +00002390/*
2391 * Set the SOFOFROM and SOFOTO items in language "lp".
2392 * Returns FAIL when there is something wrong.
2393 */
2394 static int
2395set_sofo(lp, from, to)
2396 slang_T *lp;
2397 char_u *from;
2398 char_u *to;
2399{
2400 int i;
2401
2402#ifdef FEAT_MBYTE
2403 garray_T *gap;
2404 char_u *s;
2405 char_u *p;
2406 int c;
2407 int *inp;
2408
2409 if (has_mbyte)
2410 {
2411 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2412 * characters. The index is the low byte of the character.
2413 * The list contains from-to pairs with a terminating NUL.
2414 * sl_sal_first[] is used for latin1 "from" characters. */
2415 gap = &lp->sl_sal;
2416 ga_init2(gap, sizeof(int *), 1);
2417 if (ga_grow(gap, 256) == FAIL)
2418 return FAIL;
2419 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2420 gap->ga_len = 256;
2421
2422 /* First count the number of items for each list. Temporarily use
2423 * sl_sal_first[] for this. */
2424 for (p = from, s = to; *p != NUL && *s != NUL; )
2425 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002426 c = mb_cptr2char_adv(&p);
2427 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002428 if (c >= 256)
2429 ++lp->sl_sal_first[c & 0xff];
2430 }
2431 if (*p != NUL || *s != NUL) /* lengths differ */
2432 return FAIL;
2433
2434 /* Allocate the lists. */
2435 for (i = 0; i < 256; ++i)
2436 if (lp->sl_sal_first[i] > 0)
2437 {
2438 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2439 if (p == NULL)
2440 return FAIL;
2441 ((int **)gap->ga_data)[i] = (int *)p;
2442 *(int *)p = 0;
2443 }
2444
2445 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2446 * list. */
2447 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2448 for (p = from, s = to; *p != NUL && *s != NUL; )
2449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002450 c = mb_cptr2char_adv(&p);
2451 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002452 if (c >= 256)
2453 {
2454 /* Append the from-to chars at the end of the list with
2455 * the low byte. */
2456 inp = ((int **)gap->ga_data)[c & 0xff];
2457 while (*inp != 0)
2458 ++inp;
2459 *inp++ = c; /* from char */
2460 *inp++ = i; /* to char */
2461 *inp++ = NUL; /* NUL at the end */
2462 }
2463 else
2464 /* mapping byte to char is done in sl_sal_first[] */
2465 lp->sl_sal_first[c] = i;
2466 }
2467 }
2468 else
2469#endif
2470 {
2471 /* mapping bytes to bytes is done in sl_sal_first[] */
2472 if (STRLEN(from) != STRLEN(to))
2473 return FAIL;
2474
2475 for (i = 0; to[i] != NUL; ++i)
2476 lp->sl_sal_first[from[i]] = to[i];
2477 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2478 }
2479
2480 return OK;
2481}
2482
2483/*
2484 * Fill the first-index table for "lp".
2485 */
2486 static void
2487set_sal_first(lp)
2488 slang_T *lp;
2489{
2490 salfirst_T *sfirst;
2491 int i;
2492 salitem_T *smp;
2493 int c;
2494 garray_T *gap = &lp->sl_sal;
2495
2496 sfirst = lp->sl_sal_first;
2497 for (i = 0; i < 256; ++i)
2498 sfirst[i] = -1;
2499 smp = (salitem_T *)gap->ga_data;
2500 for (i = 0; i < gap->ga_len; ++i)
2501 {
2502#ifdef FEAT_MBYTE
2503 if (has_mbyte)
2504 /* Use the lowest byte of the first character. For latin1 it's
2505 * the character, for other encodings it should differ for most
2506 * characters. */
2507 c = *smp[i].sm_lead_w & 0xff;
2508 else
2509#endif
2510 c = *smp[i].sm_lead;
2511 if (sfirst[c] == -1)
2512 {
2513 sfirst[c] = i;
2514#ifdef FEAT_MBYTE
2515 if (has_mbyte)
2516 {
2517 int n;
2518
2519 /* Make sure all entries with this byte are following each
2520 * other. Move the ones that are in the wrong position. Do
2521 * keep the same ordering! */
2522 while (i + 1 < gap->ga_len
2523 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2524 /* Skip over entry with same index byte. */
2525 ++i;
2526
2527 for (n = 1; i + n < gap->ga_len; ++n)
2528 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2529 {
2530 salitem_T tsal;
2531
2532 /* Move entry with same index byte after the entries
2533 * we already found. */
2534 ++i;
2535 --n;
2536 tsal = smp[i + n];
2537 mch_memmove(smp + i + 1, smp + i,
2538 sizeof(salitem_T) * n);
2539 smp[i] = tsal;
2540 }
2541 }
2542#endif
2543 }
2544 }
2545}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002546
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002547#ifdef FEAT_MBYTE
2548/*
2549 * Turn a multi-byte string into a wide character string.
2550 * Return it in allocated memory (NULL for out-of-memory)
2551 */
2552 static int *
2553mb_str2wide(s)
2554 char_u *s;
2555{
2556 int *res;
2557 char_u *p;
2558 int i = 0;
2559
2560 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2561 if (res != NULL)
2562 {
2563 for (p = s; *p != NUL; )
2564 res[i++] = mb_ptr2char_adv(&p);
2565 res[i] = NUL;
2566 }
2567 return res;
2568}
2569#endif
2570
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002571/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002572 * Read one row of siblings from the spell file and store it in the byte array
2573 * "byts" and index array "idxs". Recursively read the children.
2574 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002575 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002576 *
2577 * Returns the index follosing the siblings.
2578 * Returns -1 if the file is shorter than expected.
2579 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002581 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002582read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002583 FILE *fd;
2584 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002585 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002586 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002587 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002588 int prefixtree; /* TRUE for reading PREFIXTREE */
2589 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002591 int len;
2592 int i;
2593 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002594 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002595 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002596 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002597#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598
Bram Moolenaar51485f02005-06-04 21:55:20 +00002599 len = getc(fd); /* <siblingcount> */
2600 if (len <= 0)
2601 return -1;
2602
2603 if (startidx + len >= maxidx)
2604 return -2;
2605 byts[idx++] = len;
2606
2607 /* Read the byte values, flag/region bytes and shared indexes. */
2608 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002610 c = getc(fd); /* <byte> */
2611 if (c < 0)
2612 return -1;
2613 if (c <= BY_SPECIAL)
2614 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002615 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 {
2617 /* No flags, all regions. */
2618 idxs[idx] = 0;
2619 c = 0;
2620 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002621 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002622 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002623 if (prefixtree)
2624 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002625 /* Read the optional pflags byte, the prefix ID and the
2626 * condition nr. In idxs[] store the prefix ID in the low
2627 * byte, the condition index shifted up 8 bits, the flags
2628 * shifted up 24 bits. */
2629 if (c == BY_FLAGS)
2630 c = getc(fd) << 24; /* <pflags> */
2631 else
2632 c = 0;
2633
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002634 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00002635
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002636 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2637 if (n >= maxprefcondnr)
2638 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002639 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002640 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002641 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002642 {
2643 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002644 * idxs[] the flags go in the low two bytes, region above
2645 * that and prefix ID above the region. */
2646 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002647 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002648 if (c2 == BY_FLAGS2)
2649 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002650 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002651 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002652 if (c & WF_AFX)
2653 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002654 }
2655
Bram Moolenaar51485f02005-06-04 21:55:20 +00002656 idxs[idx] = c;
2657 c = 0;
2658 }
2659 else /* c == BY_INDEX */
2660 {
2661 /* <nodeidx> */
2662 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2663 if (n < 0 || n >= maxidx)
2664 return -2;
2665 idxs[idx] = n + SHARED_MASK;
2666 c = getc(fd); /* <xbyte> */
2667 }
2668 }
2669 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002670 }
2671
Bram Moolenaar51485f02005-06-04 21:55:20 +00002672 /* Recursively read the children for non-shared siblings.
2673 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2674 * remove SHARED_MASK) */
2675 for (i = 1; i <= len; ++i)
2676 if (byts[startidx + i] != 0)
2677 {
2678 if (idxs[startidx + i] & SHARED_MASK)
2679 idxs[startidx + i] &= ~SHARED_MASK;
2680 else
2681 {
2682 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002683 idx = read_tree(fd, byts, idxs, maxidx, idx,
2684 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002685 if (idx < 0)
2686 break;
2687 }
2688 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002689
Bram Moolenaar51485f02005-06-04 21:55:20 +00002690 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691}
2692
2693/*
2694 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002695 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002696 */
2697 char_u *
2698did_set_spelllang(buf)
2699 buf_T *buf;
2700{
2701 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002702 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002703 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002704 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002705 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002706 int region_mask;
2707 slang_T *lp;
2708 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002709 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002711 int len;
2712 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002713 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002714 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002715 char_u *use_region = NULL;
2716 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717
2718 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002719 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002720
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002721 /* loop over comma separated language names. */
2722 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002724 /* Get one language name. */
2725 copy_option_part(&splp, lang, MAXWLEN, ",");
2726
Bram Moolenaar5482f332005-04-17 20:18:43 +00002727 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002728 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002729
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002730 /* If the name ends in ".spl" use it as the name of the spell file.
2731 * If there is a region name let "region" point to it and remove it
2732 * from the name. */
2733 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2734 {
2735 filename = TRUE;
2736
Bram Moolenaarb6356332005-07-18 21:40:44 +00002737 /* Locate a region and remove it from the file name. */
2738 p = vim_strchr(gettail(lang), '_');
2739 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2740 && !ASCII_ISALPHA(p[3]))
2741 {
2742 vim_strncpy(region_cp, p + 1, 2);
2743 mch_memmove(p, p + 3, len - (p - lang) - 2);
2744 len -= 3;
2745 region = region_cp;
2746 }
2747 else
2748 dont_use_region = TRUE;
2749
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002750 /* Check if we loaded this language before. */
2751 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2752 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2753 break;
2754 }
2755 else
2756 {
2757 filename = FALSE;
2758 if (len > 3 && lang[len - 3] == '_')
2759 {
2760 region = lang + len - 2;
2761 len -= 3;
2762 lang[len] = NUL;
2763 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002764 else
2765 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002766
2767 /* Check if we loaded this language before. */
2768 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2769 if (STRICMP(lang, lp->sl_name) == 0)
2770 break;
2771 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002772
Bram Moolenaarb6356332005-07-18 21:40:44 +00002773 if (region != NULL)
2774 {
2775 /* If the region differs from what was used before then don't
2776 * use it for 'spellfile'. */
2777 if (use_region != NULL && STRCMP(region, use_region) != 0)
2778 dont_use_region = TRUE;
2779 use_region = region;
2780 }
2781
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002782 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002783 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002784 {
2785 if (filename)
2786 (void)spell_load_file(lang, lang, NULL, FALSE);
2787 else
2788 spell_load_lang(lang);
2789 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002790
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002791 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002792 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002793 */
2794 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002795 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2796 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002798 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002799 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002800 {
2801 /* find region in sl_regions */
2802 c = find_region(lp->sl_regions, region);
2803 if (c == REGION_ALL)
2804 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002805 if (lp->sl_add)
2806 {
2807 if (*lp->sl_regions != NUL)
2808 /* This addition file is for other regions. */
2809 region_mask = 0;
2810 }
2811 else
2812 /* This is probably an error. Give a warning and
2813 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002814 smsg((char_u *)
2815 _("Warning: region %s not supported"),
2816 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002817 }
2818 else
2819 region_mask = 1 << c;
2820 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002821
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002822 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002823 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002824 if (ga_grow(&ga, 1) == FAIL)
2825 {
2826 ga_clear(&ga);
2827 return e_outofmem;
2828 }
2829 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2830 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2831 ++ga.ga_len;
2832 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002833 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002834 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002835 }
2836
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002837 /* round 0: load int_wordlist, if possible.
2838 * round 1: load first name in 'spellfile'.
2839 * round 2: load second name in 'spellfile.
2840 * etc. */
2841 spf = curbuf->b_p_spf;
2842 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002844 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002845 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002846 /* Internal wordlist, if there is one. */
2847 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002848 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002849 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002850 }
2851 else
2852 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002853 /* One entry in 'spellfile'. */
2854 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2855 STRCAT(spf_name, ".spl");
2856
2857 /* If it was already found above then skip it. */
2858 for (c = 0; c < ga.ga_len; ++c)
2859 if (fullpathcmp(spf_name,
2860 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2861 FALSE) == FPC_SAME)
2862 break;
2863 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002864 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002865 }
2866
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002867 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002868 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2869 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2870 break;
2871 if (lp == NULL)
2872 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002873 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002874 * region name, the region is ignored otherwise. for int_wordlist
2875 * use an arbitrary name. */
2876 if (round == 0)
2877 STRCPY(lang, "internal wordlist");
2878 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002879 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002880 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002881 p = vim_strchr(lang, '.');
2882 if (p != NULL)
2883 *p = NUL; /* truncate at ".encoding.add" */
2884 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002885 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 }
2887 if (lp != NULL && ga_grow(&ga, 1) == OK)
2888 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002889 region_mask = REGION_ALL;
2890 if (use_region != NULL && !dont_use_region)
2891 {
2892 /* find region in sl_regions */
2893 c = find_region(lp->sl_regions, use_region);
2894 if (c != REGION_ALL)
2895 region_mask = 1 << c;
2896 else if (*lp->sl_regions != NUL)
2897 /* This spell file is for other regions. */
2898 region_mask = 0;
2899 }
2900
2901 if (region_mask != 0)
2902 {
2903 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2904 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2905 ++ga.ga_len;
2906 use_midword(lp, buf);
2907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002908 }
2909 }
2910
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002911 /* Add a NULL entry to mark the end of the list. */
2912 if (ga_grow(&ga, 1) == FAIL)
2913 {
2914 ga_clear(&ga);
2915 return e_outofmem;
2916 }
2917 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2918 ++ga.ga_len;
2919
2920 /* Everything is fine, store the new b_langp value. */
2921 ga_clear(&buf->b_langp);
2922 buf->b_langp = ga;
2923
2924 return NULL;
2925}
2926
2927/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002928 * Clear the midword characters for buffer "buf".
2929 */
2930 static void
2931clear_midword(buf)
2932 buf_T *buf;
2933{
2934 vim_memset(buf->b_spell_ismw, 0, 256);
2935#ifdef FEAT_MBYTE
2936 vim_free(buf->b_spell_ismw_mb);
2937 buf->b_spell_ismw_mb = NULL;
2938#endif
2939}
2940
2941/*
2942 * Use the "sl_midword" field of language "lp" for buffer "buf".
2943 * They add up to any currently used midword characters.
2944 */
2945 static void
2946use_midword(lp, buf)
2947 slang_T *lp;
2948 buf_T *buf;
2949{
2950 char_u *p;
2951
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002952 if (lp->sl_midword == NULL) /* there aren't any */
2953 return;
2954
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002955 for (p = lp->sl_midword; *p != NUL; )
2956#ifdef FEAT_MBYTE
2957 if (has_mbyte)
2958 {
2959 int c, l, n;
2960 char_u *bp;
2961
2962 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002963 l = (*mb_ptr2len)(p);
2964 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002965 buf->b_spell_ismw[c] = TRUE;
2966 else if (buf->b_spell_ismw_mb == NULL)
2967 /* First multi-byte char in "b_spell_ismw_mb". */
2968 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2969 else
2970 {
2971 /* Append multi-byte chars to "b_spell_ismw_mb". */
2972 n = STRLEN(buf->b_spell_ismw_mb);
2973 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2974 if (bp != NULL)
2975 {
2976 vim_free(buf->b_spell_ismw_mb);
2977 buf->b_spell_ismw_mb = bp;
2978 vim_strncpy(bp + n, p, l);
2979 }
2980 }
2981 p += l;
2982 }
2983 else
2984#endif
2985 buf->b_spell_ismw[*p++] = TRUE;
2986}
2987
2988/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002989 * Find the region "region[2]" in "rp" (points to "sl_regions").
2990 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002991 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002992 */
2993 static int
2994find_region(rp, region)
2995 char_u *rp;
2996 char_u *region;
2997{
2998 int i;
2999
3000 for (i = 0; ; i += 2)
3001 {
3002 if (rp[i] == NUL)
3003 return REGION_ALL;
3004 if (rp[i] == region[0] && rp[i + 1] == region[1])
3005 break;
3006 }
3007 return i / 2;
3008}
3009
3010/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003012 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003013 * Word WF_ONECAP
3014 * W WORD WF_ALLCAP
3015 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003016 */
3017 static int
3018captype(word, end)
3019 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003020 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003021{
3022 char_u *p;
3023 int c;
3024 int firstcap;
3025 int allcap;
3026 int past_second = FALSE; /* past second word char */
3027
3028 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003029 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003030 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003031 return 0; /* only non-word characters, illegal word */
3032#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003033 if (has_mbyte)
3034 c = mb_ptr2char_adv(&p);
3035 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003036#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003037 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003038 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003039
3040 /*
3041 * Need to check all letters to find a word with mixed upper/lower.
3042 * But a word with an upper char only at start is a ONECAP.
3043 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003045 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003046 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003047 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003048 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003049 {
3050 /* UUl -> KEEPCAP */
3051 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003052 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003053 allcap = FALSE;
3054 }
3055 else if (!allcap)
3056 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003057 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003058 past_second = TRUE;
3059 }
3060
3061 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003062 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003063 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003064 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003065 return 0;
3066}
3067
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003068/*
3069 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3070 * capital. So that make_case_word() can turn WOrd into Word.
3071 * Add ALLCAP for "WOrD".
3072 */
3073 static int
3074badword_captype(word, end)
3075 char_u *word;
3076 char_u *end;
3077{
3078 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003079 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003080 int l, u;
3081 int first;
3082 char_u *p;
3083
3084 if (flags & WF_KEEPCAP)
3085 {
3086 /* Count the number of UPPER and lower case letters. */
3087 l = u = 0;
3088 first = FALSE;
3089 for (p = word; p < end; mb_ptr_adv(p))
3090 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003091 c = PTR2CHAR(p);
3092 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003093 {
3094 ++u;
3095 if (p == word)
3096 first = TRUE;
3097 }
3098 else
3099 ++l;
3100 }
3101
3102 /* If there are more UPPER than lower case letters suggest an
3103 * ALLCAP word. Otherwise, if the first letter is UPPER then
3104 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3105 * require three upper case letters. */
3106 if (u > l && u > 2)
3107 flags |= WF_ALLCAP;
3108 else if (first)
3109 flags |= WF_ONECAP;
3110 }
3111 return flags;
3112}
3113
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003114# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3115/*
3116 * Free all languages.
3117 */
3118 void
3119spell_free_all()
3120{
3121 slang_T *lp;
3122 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003123 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003124
3125 /* Go through all buffers and handle 'spelllang'. */
3126 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3127 ga_clear(&buf->b_langp);
3128
3129 while (first_lang != NULL)
3130 {
3131 lp = first_lang;
3132 first_lang = lp->sl_next;
3133 slang_free(lp);
3134 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003135
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003136 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003137 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003138 /* Delete the internal wordlist and its .spl file */
3139 mch_remove(int_wordlist);
3140 int_wordlist_spl(fname);
3141 mch_remove(fname);
3142 vim_free(int_wordlist);
3143 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003144 }
3145
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003146 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003147}
3148# endif
3149
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003150# if defined(FEAT_MBYTE) || defined(PROTO)
3151/*
3152 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003153 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003154 */
3155 void
3156spell_reload()
3157{
3158 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003159 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003160
Bram Moolenaarea408852005-06-25 22:49:46 +00003161 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003162 init_spell_chartab();
3163
3164 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003165 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003166
3167 /* Go through all buffers and handle 'spelllang'. */
3168 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3169 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003170 /* Only load the wordlists when 'spelllang' is set and there is a
3171 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003172 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003173 {
3174 FOR_ALL_WINDOWS(wp)
3175 if (wp->w_buffer == buf && wp->w_p_spell)
3176 {
3177 (void)did_set_spelllang(buf);
3178# ifdef FEAT_WINDOWS
3179 break;
3180# endif
3181 }
3182 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003183 }
3184}
3185# endif
3186
Bram Moolenaarb765d632005-06-07 21:00:02 +00003187/*
3188 * Reload the spell file "fname" if it's loaded.
3189 */
3190 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003191spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003192 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003193 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003194{
3195 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003196 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003197
Bram Moolenaarb765d632005-06-07 21:00:02 +00003198 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3199 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3200 {
3201 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003202 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003203 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003204 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003206
3207 /* When "zg" was used and the file wasn't loaded yet, should redo
3208 * 'spelllang' to get it loaded. */
3209 if (added_word && !didit)
3210 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003211}
3212
3213
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003214/*
3215 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003216 */
3217
Bram Moolenaar51485f02005-06-04 21:55:20 +00003218#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003219 and .dic file. */
3220/*
3221 * Main structure to store the contents of a ".aff" file.
3222 */
3223typedef struct afffile_S
3224{
3225 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003226 int af_slash; /* character used in word for slash */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003227 int af_rar; /* RAR ID for rare word */
3228 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003229 int af_bad; /* BAD ID for banned word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003230 char_u *af_compflags; /* COMPOUNDFLAG or COMPOUNDFLAGS */
3231 int af_compminlen; /* COMPOUNDMIN */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003232 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003233 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3234 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003235} afffile_T;
3236
3237typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003238/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3239struct affentry_S
3240{
3241 affentry_T *ae_next; /* next affix with same name/number */
3242 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3243 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003244 char_u *ae_cond; /* condition (NULL for ".") */
3245 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003246 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003247};
3248
Bram Moolenaar53805d12005-08-01 07:08:33 +00003249#define AH_KEY_LEN 10
3250
Bram Moolenaar51485f02005-06-04 21:55:20 +00003251/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3252typedef struct affheader_S
3253{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003254 /* key for hashtable == name of affix entry */
3255#ifdef FEAT_MBYTE
3256 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3257#else
3258 char_u ah_key[2]; /* one byte char plus NUL */
3259#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003260 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003261 int ah_combine; /* suffix may combine with prefix */
3262 affentry_T *ah_first; /* first affix entry */
3263} affheader_T;
3264
3265#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3266
3267/*
3268 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003269 * the need to keep track of each allocated thing, everything is freed all at
3270 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003271 */
3272#define SBLOCKSIZE 16000 /* size of sb_data */
3273typedef struct sblock_S sblock_T;
3274struct sblock_S
3275{
3276 sblock_T *sb_next; /* next block in list */
3277 int sb_used; /* nr of bytes already in use */
3278 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003279};
3280
3281/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003282 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003283 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003284typedef struct wordnode_S wordnode_T;
3285struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003286{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003287 union /* shared to save space */
3288 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003289 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003290 int index; /* index in written nodes (valid after first
3291 round) */
3292 } wn_u1;
3293 union /* shared to save space */
3294 {
3295 wordnode_T *next; /* next node with same hash key */
3296 wordnode_T *wnode; /* parent node that will write this node */
3297 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003298 wordnode_T *wn_child; /* child (next byte in word) */
3299 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3300 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003301 int wn_refs; /* Nr. of references to this node. Only
3302 relevant for first node in a list of
3303 siblings, in following siblings it is
3304 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003305 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003306 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003307 prefix ID or 0 */
3308 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3309 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003310 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003311#ifdef SPELL_PRINTTREE
3312 int wn_nr; /* sequence nr for printing */
3313#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003314};
3315
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003316#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3317
Bram Moolenaar51485f02005-06-04 21:55:20 +00003318#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003319
Bram Moolenaar51485f02005-06-04 21:55:20 +00003320/*
3321 * Info used while reading the spell files.
3322 */
3323typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003324{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003325 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003326 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003327 int si_fold_added; /* nr of words added since compressing */
3328
Bram Moolenaar51485f02005-06-04 21:55:20 +00003329 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003330 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003331 int si_keep_added; /* nr of words added since compressing */
3332
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003333 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003334
Bram Moolenaar51485f02005-06-04 21:55:20 +00003335 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003336 wordnode_T *si_first_free; /* List of nodes that have been freed during
3337 compression, linked by "wn_child" field. */
3338#ifdef SPELL_PRINTTREE
3339 int si_wordnode_nr; /* sequence nr for nodes */
3340#endif
3341
3342
Bram Moolenaar51485f02005-06-04 21:55:20 +00003343 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003344 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003345 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003346 int si_region; /* region mask */
3347 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003348 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003349 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003350 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003351 int si_region_count; /* number of regions supported (1 when there
3352 are no regions) */
3353 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354
3355 garray_T si_rep; /* list of fromto_T entries from REP lines */
3356 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003357 char_u *si_sofofr; /* SOFOFROM text */
3358 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 int si_followup; /* soundsalike: ? */
3360 int si_collapse; /* soundsalike: ? */
3361 int si_rem_accents; /* soundsalike: remove accents */
3362 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003363 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003364 int si_compminlen; /* minimal length for compounding */
3365 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003366 garray_T si_prefcond; /* table with conditions for postponed
3367 * prefixes, each stored as a string */
3368 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003369} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003370
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003371static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003372static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3374static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003375static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003376static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003377static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
3378static char_u *get_pfxlist __ARGS((spellinfo_T *spin, afffile_T *affile, char_u *afflist));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003379static char_u *get_compflags __ARGS((spellinfo_T *spin, char_u *afflist));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003380static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist));
3381static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3382static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3383static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003384static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003385static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
3386static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003387static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003388static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3389static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3390static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3391static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3392static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003393static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003394static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003395static void clear_node __ARGS((wordnode_T *node));
3396static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003398static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003399
Bram Moolenaar53805d12005-08-01 07:08:33 +00003400/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3401 * but it must be negative to indicate the prefix tree to tree_add_word().
3402 * Use a negative number with the lower 8 bits zero. */
3403#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003404
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003405#ifdef SPELL_PRINTTREE
3406/*
3407 * For debugging the tree code: print the current tree in a (more or less)
3408 * readable format, so that we can see what happens when adding a word and/or
3409 * compressing the tree.
3410 * Based on code from Olaf Seibert.
3411 */
3412#define PRINTLINESIZE 1000
3413#define PRINTWIDTH 6
3414
3415#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3416 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3417
3418static char line1[PRINTLINESIZE];
3419static char line2[PRINTLINESIZE];
3420static char line3[PRINTLINESIZE];
3421
3422 static void
3423spell_clear_flags(wordnode_T *node)
3424{
3425 wordnode_T *np;
3426
3427 for (np = node; np != NULL; np = np->wn_sibling)
3428 {
3429 np->wn_u1.index = FALSE;
3430 spell_clear_flags(np->wn_child);
3431 }
3432}
3433
3434 static void
3435spell_print_node(wordnode_T *node, int depth)
3436{
3437 if (node->wn_u1.index)
3438 {
3439 /* Done this node before, print the reference. */
3440 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3441 PRINTSOME(line2, depth, " ", 0, 0);
3442 PRINTSOME(line3, depth, " ", 0, 0);
3443 msg(line1);
3444 msg(line2);
3445 msg(line3);
3446 }
3447 else
3448 {
3449 node->wn_u1.index = TRUE;
3450
3451 if (node->wn_byte != NUL)
3452 {
3453 if (node->wn_child != NULL)
3454 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3455 else
3456 /* Cannot happen? */
3457 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3458 }
3459 else
3460 PRINTSOME(line1, depth, " $ ", 0, 0);
3461
3462 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3463
3464 if (node->wn_sibling != NULL)
3465 PRINTSOME(line3, depth, " | ", 0, 0);
3466 else
3467 PRINTSOME(line3, depth, " ", 0, 0);
3468
3469 if (node->wn_byte == NUL)
3470 {
3471 msg(line1);
3472 msg(line2);
3473 msg(line3);
3474 }
3475
3476 /* do the children */
3477 if (node->wn_byte != NUL && node->wn_child != NULL)
3478 spell_print_node(node->wn_child, depth + 1);
3479
3480 /* do the siblings */
3481 if (node->wn_sibling != NULL)
3482 {
3483 /* get rid of all parent details except | */
3484 STRCPY(line1, line3);
3485 STRCPY(line2, line3);
3486 spell_print_node(node->wn_sibling, depth);
3487 }
3488 }
3489}
3490
3491 static void
3492spell_print_tree(wordnode_T *root)
3493{
3494 if (root != NULL)
3495 {
3496 /* Clear the "wn_u1.index" fields, used to remember what has been
3497 * done. */
3498 spell_clear_flags(root);
3499
3500 /* Recursively print the tree. */
3501 spell_print_node(root, 0);
3502 }
3503}
3504#endif /* SPELL_PRINTTREE */
3505
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003506/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003507 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003508 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003509 */
3510 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003511spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003512 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003513 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003514{
3515 FILE *fd;
3516 afffile_T *aff;
3517 char_u rline[MAXLINELEN];
3518 char_u *line;
3519 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003520#define MAXITEMCNT 7
3521 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003522 int itemcnt;
3523 char_u *p;
3524 int lnum = 0;
3525 affheader_T *cur_aff = NULL;
3526 int aff_todo = 0;
3527 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003528 char_u *low = NULL;
3529 char_u *fol = NULL;
3530 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003531 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 int do_rep;
3533 int do_sal;
3534 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003535 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003536 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003537 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003538 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003539 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003540
Bram Moolenaar51485f02005-06-04 21:55:20 +00003541 /*
3542 * Open the file.
3543 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003544 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003545 if (fd == NULL)
3546 {
3547 EMSG2(_(e_notopen), fname);
3548 return NULL;
3549 }
3550
Bram Moolenaarb765d632005-06-07 21:00:02 +00003551 if (spin->si_verbose || p_verbose > 2)
3552 {
3553 if (!spin->si_verbose)
3554 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003555 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003556 out_flush();
3557 if (!spin->si_verbose)
3558 verbose_leave();
3559 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003561 /* Only do REP lines when not done in another .aff file already. */
3562 do_rep = spin->si_rep.ga_len == 0;
3563
3564 /* Only do SAL lines when not done in another .aff file already. */
3565 do_sal = spin->si_sal.ga_len == 0;
3566
3567 /* Only do MAP lines when not done in another .aff file already. */
3568 do_map = spin->si_map.ga_len == 0;
3569
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003570 /* Only do MIDWORD line when not done in another .aff file already */
3571 do_midword = spin->si_midword == NULL;
3572
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003573 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3574 do_sofo = spin->si_sofofr == NULL;
3575
Bram Moolenaar51485f02005-06-04 21:55:20 +00003576 /*
3577 * Allocate and init the afffile_T structure.
3578 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003579 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003580 if (aff == NULL)
3581 return NULL;
3582 hash_init(&aff->af_pref);
3583 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003584
3585 /*
3586 * Read all the lines in the file one by one.
3587 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003588 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003589 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003590 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003591 ++lnum;
3592
3593 /* Skip comment lines. */
3594 if (*rline == '#')
3595 continue;
3596
3597 /* Convert from "SET" to 'encoding' when needed. */
3598 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003599#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003600 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003601 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003602 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003603 if (pc == NULL)
3604 {
3605 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3606 fname, lnum, rline);
3607 continue;
3608 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609 line = pc;
3610 }
3611 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003612#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003613 {
3614 pc = NULL;
3615 line = rline;
3616 }
3617
3618 /* Split the line up in white separated items. Put a NUL after each
3619 * item. */
3620 itemcnt = 0;
3621 for (p = line; ; )
3622 {
3623 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3624 ++p;
3625 if (*p == NUL)
3626 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003627 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003628 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003629 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003630 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003631 ++p;
3632 if (*p == NUL)
3633 break;
3634 *p++ = NUL;
3635 }
3636
3637 /* Handle non-empty lines. */
3638 if (itemcnt > 0)
3639 {
3640 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3641 && aff->af_enc == NULL)
3642 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003643#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003644 /* Setup for conversion from "ENC" to 'encoding'. */
3645 aff->af_enc = enc_canonize(items[1]);
3646 if (aff->af_enc != NULL && !spin->si_ascii
3647 && convert_setup(&spin->si_conv, aff->af_enc,
3648 p_enc) == FAIL)
3649 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3650 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003651 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003652#else
3653 smsg((char_u *)_("Conversion in %s not supported"), fname);
3654#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003655 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003656 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3657 {
3658 if (do_midword)
3659 spin->si_midword = vim_strsave(items[1]);
3660 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003661 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003665 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003667 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003668 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003669 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
3670 && aff->af_slash == 0)
3671 {
3672 aff->af_slash = items[1][0];
3673 if (items[1][1] != NUL)
3674 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
3675 fname, lnum, items[1]);
3676 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003677 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3678 && aff->af_rar == 0)
3679 {
3680 aff->af_rar = items[1][0];
3681 if (items[1][1] != NUL)
3682 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3683 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003684 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3685 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003686 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003687 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003688 if (items[1][1] != NUL)
3689 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3690 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003691 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3692 && aff->af_bad == 0)
3693 {
3694 aff->af_bad = items[1][0];
3695 if (items[1][1] != NUL)
3696 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3697 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003698 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
3699 && aff->af_compflags == 0)
3700 {
3701 aff->af_compflags = getroom_save(spin, items[1]);
3702 if (items[1][1] != NUL)
3703 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3704 }
3705 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2
3706 && aff->af_compflags == 0)
3707 {
3708 aff->af_compflags = getroom_save(spin, items[1]);
3709 }
3710 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
3711 && aff->af_compminlen == 0)
3712 {
3713 aff->af_compminlen = atoi((char *)items[1]);
3714 if (aff->af_compminlen == 0)
3715 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
3716 fname, lnum, items[1]);
3717 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003718 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3719 {
3720 aff->af_pfxpostpone = TRUE;
3721 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003722 else if ((STRCMP(items[0], "PFX") == 0
3723 || STRCMP(items[0], "SFX") == 0)
3724 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003725 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003726 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003727 /* Myspell allows extra text after the item, but that might
3728 * mean mistakes go unnoticed. Require a comment-starter. */
3729 if (itemcnt > 4 && *items[4] != '#')
3730 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3731 fname, lnum, items[4]);
3732
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003733 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003734 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003735 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003736 if (cur_aff == NULL)
3737 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003738#ifdef FEAT_MBYTE
3739 if (has_mbyte)
3740 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003741 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003742 if (l >= AH_KEY_LEN)
3743 l = 1; /* too long, must be an overlong sequence */
3744 else
3745 mch_memmove(cur_aff->ah_key, items[1], l);
3746 }
3747 else
3748#endif
3749 {
3750 *cur_aff->ah_key = *items[1];
3751 l = 1;
3752 }
3753 cur_aff->ah_key[l] = NUL;
3754 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003755 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003756 if (*items[2] == 'Y')
3757 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003758 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003759 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3760 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003761
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003762 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003763 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003764 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003765 /* Use a new number in the .spl file later, to be able to
3766 * handle multiple .aff files. */
3767 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003768 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003769 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003770 else
3771 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003772 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003773 hi = hash_find(tp, cur_aff->ah_key);
3774 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003775 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003776 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3777 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003778 aff_todo = 0;
3779 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003780 else
3781 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003782 }
3783 else if ((STRCMP(items[0], "PFX") == 0
3784 || STRCMP(items[0], "SFX") == 0)
3785 && aff_todo > 0
3786 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003787 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003788 {
3789 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003790 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003791 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003792 int lasti = 5;
3793
3794 /* Check for "rare" after the other info. */
3795 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3796 {
3797 rare = TRUE;
3798 lasti = 6;
3799 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003800
Bram Moolenaar8db73182005-06-17 21:51:16 +00003801 /* Myspell allows extra text after the item, but that might
3802 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003803 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003804 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003805 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003806
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003807 /* New item for an affix letter. */
3808 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003809 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003810 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003811 if (aff_entry == NULL)
3812 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003813 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003814
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003815 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003816 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003817 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003818 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003819
Bram Moolenaar51485f02005-06-04 21:55:20 +00003820 /* Don't use an affix entry with non-ASCII characters when
3821 * "spin->si_ascii" is TRUE. */
3822 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003823 || has_non_ascii(aff_entry->ae_add)))
3824 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003825 aff_entry->ae_next = cur_aff->ah_first;
3826 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003827
3828 if (STRCMP(items[4], ".") != 0)
3829 {
3830 char_u buf[MAXLINELEN];
3831
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003832 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003833 if (*items[0] == 'P')
3834 sprintf((char *)buf, "^%s", items[4]);
3835 else
3836 sprintf((char *)buf, "%s$", items[4]);
3837 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003838 RE_MAGIC + RE_STRING + RE_STRICT);
3839 if (aff_entry->ae_prog == NULL)
3840 smsg((char_u *)_("Broken condition in %s line %d: %s"),
3841 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003842 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003843
3844 /* For postponed prefixes we need an entry in si_prefcond
3845 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003846 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003847 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003848 /* When the chop string is one lower-case letter and
3849 * the add string ends in the upper-case letter we set
3850 * the "upper" flag, clear "ae_chop" and remove the
3851 * letters from "ae_add". The condition must either
3852 * be empty or start with the same letter. */
3853 if (aff_entry->ae_chop != NULL
3854 && aff_entry->ae_add != NULL
3855#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003856 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00003857 aff_entry->ae_chop)] == NUL
3858#else
3859 && aff_entry->ae_chop[1] == NUL
3860#endif
3861 )
3862 {
3863 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003864
Bram Moolenaar53805d12005-08-01 07:08:33 +00003865 c = PTR2CHAR(aff_entry->ae_chop);
3866 c_up = SPELL_TOUPPER(c);
3867 if (c_up != c
3868 && (aff_entry->ae_cond == NULL
3869 || PTR2CHAR(aff_entry->ae_cond) == c))
3870 {
3871 p = aff_entry->ae_add
3872 + STRLEN(aff_entry->ae_add);
3873 mb_ptr_back(aff_entry->ae_add, p);
3874 if (PTR2CHAR(p) == c_up)
3875 {
3876 upper = TRUE;
3877 aff_entry->ae_chop = NULL;
3878 *p = NUL;
3879
3880 /* The condition is matched with the
3881 * actual word, thus must check for the
3882 * upper-case letter. */
3883 if (aff_entry->ae_cond != NULL)
3884 {
3885 char_u buf[MAXLINELEN];
3886#ifdef FEAT_MBYTE
3887 if (has_mbyte)
3888 {
3889 onecap_copy(items[4], buf, TRUE);
3890 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003891 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003892 }
3893 else
3894#endif
3895 *aff_entry->ae_cond = c_up;
3896 if (aff_entry->ae_cond != NULL)
3897 {
3898 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003899 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003900 vim_free(aff_entry->ae_prog);
3901 aff_entry->ae_prog = vim_regcomp(
3902 buf, RE_MAGIC + RE_STRING);
3903 }
3904 }
3905 }
3906 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003907 }
3908
Bram Moolenaar53805d12005-08-01 07:08:33 +00003909 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003910 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003911 int idx;
3912 char_u **pp;
3913 int n;
3914
3915 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3916 --idx)
3917 {
3918 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3919 if (str_equal(p, aff_entry->ae_cond))
3920 break;
3921 }
3922 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3923 {
3924 /* Not found, add a new condition. */
3925 idx = spin->si_prefcond.ga_len++;
3926 pp = ((char_u **)spin->si_prefcond.ga_data)
3927 + idx;
3928 if (aff_entry->ae_cond == NULL)
3929 *pp = NULL;
3930 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003931 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00003932 aff_entry->ae_cond);
3933 }
3934
3935 /* Add the prefix to the prefix tree. */
3936 if (aff_entry->ae_add == NULL)
3937 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003938 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003939 p = aff_entry->ae_add;
3940 /* PFX_FLAGS is a negative number, so that
3941 * tree_add_word() knows this is the prefix tree. */
3942 n = PFX_FLAGS;
3943 if (rare)
3944 n |= WFP_RARE;
3945 if (!cur_aff->ah_combine)
3946 n |= WFP_NC;
3947 if (upper)
3948 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003949 tree_add_word(spin, p, spin->si_prefroot, n,
3950 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003951 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003952 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003953 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003954 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003955 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3956 {
3957 if (fol != NULL)
3958 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3959 fname, lnum);
3960 else
3961 fol = vim_strsave(items[1]);
3962 }
3963 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3964 {
3965 if (low != NULL)
3966 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3967 fname, lnum);
3968 else
3969 low = vim_strsave(items[1]);
3970 }
3971 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3972 {
3973 if (upp != NULL)
3974 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3975 fname, lnum);
3976 else
3977 upp = vim_strsave(items[1]);
3978 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003979 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003980 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003981 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003982 if (!isdigit(*items[1]))
3983 smsg((char_u *)_("Expected REP count in %s line %d"),
3984 fname, lnum);
3985 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003986 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3987 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003988 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989 if (do_rep)
3990 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3991 }
3992 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3993 {
3994 /* MAP item or count */
3995 if (!found_map)
3996 {
3997 /* First line contains the count. */
3998 found_map = TRUE;
3999 if (!isdigit(*items[1]))
4000 smsg((char_u *)_("Expected MAP count in %s line %d"),
4001 fname, lnum);
4002 }
4003 else if (do_map)
4004 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004005 int c;
4006
4007 /* Check that every character appears only once. */
4008 for (p = items[1]; *p != NUL; )
4009 {
4010#ifdef FEAT_MBYTE
4011 c = mb_ptr2char_adv(&p);
4012#else
4013 c = *p++;
4014#endif
4015 if ((spin->si_map.ga_len > 0
4016 && vim_strchr(spin->si_map.ga_data, c)
4017 != NULL)
4018 || vim_strchr(p, c) != NULL)
4019 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4020 fname, lnum);
4021 }
4022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004023 /* We simply concatenate all the MAP strings, separated by
4024 * slashes. */
4025 ga_concat(&spin->si_map, items[1]);
4026 ga_append(&spin->si_map, '/');
4027 }
4028 }
4029 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4030 {
4031 if (do_sal)
4032 {
4033 /* SAL item (sounds-a-like)
4034 * Either one of the known keys or a from-to pair. */
4035 if (STRCMP(items[1], "followup") == 0)
4036 spin->si_followup = sal_to_bool(items[2]);
4037 else if (STRCMP(items[1], "collapse_result") == 0)
4038 spin->si_collapse = sal_to_bool(items[2]);
4039 else if (STRCMP(items[1], "remove_accents") == 0)
4040 spin->si_rem_accents = sal_to_bool(items[2]);
4041 else
4042 /* when "to" is "_" it means empty */
4043 add_fromto(spin, &spin->si_sal, items[1],
4044 STRCMP(items[2], "_") == 0 ? (char_u *)""
4045 : items[2]);
4046 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004047 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004048 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
4049 && (!do_sofo || spin->si_sofofr == NULL))
4050 {
4051 if (do_sofo)
4052 spin->si_sofofr = vim_strsave(items[1]);
4053 }
4054 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
4055 && (!do_sofo || spin->si_sofoto == NULL))
4056 {
4057 if (do_sofo)
4058 spin->si_sofoto = vim_strsave(items[1]);
4059 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004060 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004061 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062 fname, lnum, items[0]);
4063 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 }
4065
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004066 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
4067 smsg((char_u *)_("Missing SOFO%s line in %s"),
4068 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
4069 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
4070 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
4071
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004072 if (fol != NULL || low != NULL || upp != NULL)
4073 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004074 if (spin->si_clear_chartab)
4075 {
4076 /* Clear the char type tables, don't want to use any of the
4077 * currently used spell properties. */
4078 init_spell_chartab();
4079 spin->si_clear_chartab = FALSE;
4080 }
4081
Bram Moolenaar3982c542005-06-08 21:56:31 +00004082 /*
4083 * Don't write a word table for an ASCII file, so that we don't check
4084 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004085 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004086 * mb_get_class(), the list of chars in the file will be incomplete.
4087 */
4088 if (!spin->si_ascii
4089#ifdef FEAT_MBYTE
4090 && !enc_utf8
4091#endif
4092 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004093 {
4094 if (fol == NULL || low == NULL || upp == NULL)
4095 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4096 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004097 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004098 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004099
4100 vim_free(fol);
4101 vim_free(low);
4102 vim_free(upp);
4103 }
4104
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004105 /* Use compound specifications of the .aff file for the spell info. */
4106 if (aff->af_compminlen != 0)
4107 {
4108 if (spin->si_compminlen != 0
4109 && spin->si_compminlen != aff->af_compminlen)
4110 smsg((char_u *)_("COMPOUNDMIN value differs from what is used in another .aff file"));
4111 else
4112 spin->si_compminlen = aff->af_compminlen;
4113 }
4114
4115 if (aff->af_compflags != NULL)
4116 {
4117 if (spin->si_compflags != NULL
4118 && STRCMP(spin->si_compflags, aff->af_compflags) != 0)
4119 smsg((char_u *)_("COMPOUNDFLAG(S) value differs from what is used in another .aff file"));
4120 else
4121 spin->si_compflags = aff->af_compflags;
4122
4123 if (aff->af_pfxpostpone)
4124 smsg((char_u *)_("Cannot use both PFXPOSTPONE and COMPOUNDFLAG(S)"));
4125 }
4126
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 vim_free(pc);
4128 fclose(fd);
4129 return aff;
4130}
4131
4132/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004133 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
4134 * NULL as equal.
4135 */
4136 static int
4137str_equal(s1, s2)
4138 char_u *s1;
4139 char_u *s2;
4140{
4141 if (s1 == NULL || s2 == NULL)
4142 return s1 == s2;
4143 return STRCMP(s1, s2) == 0;
4144}
4145
4146/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 * Add a from-to item to "gap". Used for REP and SAL items.
4148 * They are stored case-folded.
4149 */
4150 static void
4151add_fromto(spin, gap, from, to)
4152 spellinfo_T *spin;
4153 garray_T *gap;
4154 char_u *from;
4155 char_u *to;
4156{
4157 fromto_T *ftp;
4158 char_u word[MAXWLEN];
4159
4160 if (ga_grow(gap, 1) == OK)
4161 {
4162 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
4163 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004164 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004166 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 ++gap->ga_len;
4168 }
4169}
4170
4171/*
4172 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4173 */
4174 static int
4175sal_to_bool(s)
4176 char_u *s;
4177{
4178 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4179}
4180
4181/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004182 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4183 * When "s" is NULL FALSE is returned.
4184 */
4185 static int
4186has_non_ascii(s)
4187 char_u *s;
4188{
4189 char_u *p;
4190
4191 if (s != NULL)
4192 for (p = s; *p != NUL; ++p)
4193 if (*p >= 128)
4194 return TRUE;
4195 return FALSE;
4196}
4197
4198/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004199 * Free the structure filled by spell_read_aff().
4200 */
4201 static void
4202spell_free_aff(aff)
4203 afffile_T *aff;
4204{
4205 hashtab_T *ht;
4206 hashitem_T *hi;
4207 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004208 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004209 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004210
4211 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004212
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004213 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004214 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4215 {
4216 todo = ht->ht_used;
4217 for (hi = ht->ht_array; todo > 0; ++hi)
4218 {
4219 if (!HASHITEM_EMPTY(hi))
4220 {
4221 --todo;
4222 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004223 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4224 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004225 }
4226 }
4227 if (ht == &aff->af_suff)
4228 break;
4229 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004230
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004231 hash_clear(&aff->af_pref);
4232 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004233}
4234
4235/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004236 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004237 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004238 */
4239 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004240spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004242 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004243 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004244{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004245 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004246 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004247 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004248 char_u *afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004249 char_u *store_afflist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004250 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004251 char_u *pc;
4252 char_u *w;
4253 int l;
4254 hash_T hash;
4255 hashitem_T *hi;
4256 FILE *fd;
4257 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004258 int non_ascii = 0;
4259 int retval = OK;
4260 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004261 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004262 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004263
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 /*
4265 * Open the file.
4266 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004267 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004268 if (fd == NULL)
4269 {
4270 EMSG2(_(e_notopen), fname);
4271 return FAIL;
4272 }
4273
Bram Moolenaar51485f02005-06-04 21:55:20 +00004274 /* The hashtable is only used to detect duplicated words. */
4275 hash_init(&ht);
4276
Bram Moolenaarb765d632005-06-07 21:00:02 +00004277 if (spin->si_verbose || p_verbose > 2)
4278 {
4279 if (!spin->si_verbose)
4280 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004281 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004282 out_flush();
4283 if (!spin->si_verbose)
4284 verbose_leave();
4285 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004286
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004287 /* start with a message for the first line */
4288 spin->si_msg_count = 999999;
4289
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004290 /* Read and ignore the first line: word count. */
4291 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004292 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004293 EMSG2(_("E760: No word count in %s"), fname);
4294
4295 /*
4296 * Read all the lines in the file one by one.
4297 * The words are converted to 'encoding' here, before being added to
4298 * the hashtable.
4299 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004300 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004301 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004302 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004303 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004304 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004305 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004306
Bram Moolenaar51485f02005-06-04 21:55:20 +00004307 /* Remove CR, LF and white space from the end. White space halfway
4308 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004309 l = STRLEN(line);
4310 while (l > 0 && line[l - 1] <= ' ')
4311 --l;
4312 if (l == 0)
4313 continue; /* empty line */
4314 line[l] = NUL;
4315
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004316 /* Find the optional affix names. Replace the SLASH character by a
4317 * slash. */
4318 afflist = NULL;
4319 for (p = line; *p != NUL; mb_ptr_adv(p))
4320 {
4321 if (*p == affile->af_slash)
4322 *p = '/';
4323 else if (*p == '/')
4324 {
4325 *p = NUL;
4326 afflist = p + 1;
4327 break;
4328 }
4329 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004330
4331 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4332 if (spin->si_ascii && has_non_ascii(line))
4333 {
4334 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004335 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004336 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004337
Bram Moolenaarb765d632005-06-07 21:00:02 +00004338#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004339 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004340 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004341 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004342 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004343 if (pc == NULL)
4344 {
4345 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4346 fname, lnum, line);
4347 continue;
4348 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004349 w = pc;
4350 }
4351 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004352#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004353 {
4354 pc = NULL;
4355 w = line;
4356 }
4357
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004358 /* This takes time, print a message every 10000 words. */
4359 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004360 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004361 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004362 vim_snprintf((char *)message, sizeof(message),
4363 _("line %6d, word %6d - %s"),
4364 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4365 msg_start();
4366 msg_puts_long_attr(message, 0);
4367 msg_clr_eos();
4368 msg_didout = FALSE;
4369 msg_col = 0;
4370 out_flush();
4371 }
4372
Bram Moolenaar51485f02005-06-04 21:55:20 +00004373 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004374 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004375 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004376 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004378 if (retval == FAIL)
4379 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004380
Bram Moolenaar51485f02005-06-04 21:55:20 +00004381 hash = hash_hash(dw);
4382 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004383 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004384 {
4385 if (p_verbose > 0)
4386 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004387 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004388 else if (duplicate == 0)
4389 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
4390 fname, lnum, dw);
4391 ++duplicate;
4392 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004393 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004394 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004395
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004396 flags = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004397 store_afflist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004398 if (afflist != NULL)
4399 {
4400 /* Check for affix name that stands for keep-case word and stands
4401 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004402 if (affile->af_kep != NUL
4403 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004404 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004405 if (affile->af_rar != NUL
4406 && vim_strchr(afflist, affile->af_rar) != NULL)
4407 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004408 if (affile->af_bad != NUL
4409 && vim_strchr(afflist, affile->af_bad) != NULL)
4410 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004411
4412 if (affile->af_pfxpostpone)
4413 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004414 store_afflist = get_pfxlist(spin, affile, afflist);
4415 else if (spin->si_compflags)
4416 /* Need to store the list of affix IDs for compounding with
4417 * the word. */
4418 store_afflist = get_compflags(spin, afflist);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004419 }
4420
Bram Moolenaar51485f02005-06-04 21:55:20 +00004421 /* Add the word to the word tree(s). */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004422 if (store_word(spin, dw, flags, spin->si_region, store_afflist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004423 retval = FAIL;
4424
4425 if (afflist != NULL)
4426 {
4427 /* Find all matching suffixes and add the resulting words.
4428 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004429 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004430 &affile->af_suff, &affile->af_pref,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004431 FALSE, flags, store_afflist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004432 retval = FAIL;
4433
4434 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004435 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004436 &affile->af_pref, NULL,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004437 FALSE, flags, store_afflist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004438 retval = FAIL;
4439 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004440 }
4441
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004442 if (duplicate > 0)
4443 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004444 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004445 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
4446 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004447 hash_clear(&ht);
4448
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004449 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004450 return retval;
4451}
4452
4453/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004454 * Get the list of prefix IDs from the affix list "afflist".
4455 * Used for PFXPOSTPONE.
4456 * Returns a string allocated with getroom(). NULL when there are no prefixes
4457 * or when out of memory.
4458 */
4459 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004460get_pfxlist(spin, affile, afflist)
4461 spellinfo_T *spin;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004462 afffile_T *affile;
4463 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004464{
4465 char_u *p;
4466 int cnt;
4467 int round;
4468 char_u *res = NULL;
4469 char_u key[2];
4470 hashitem_T *hi;
4471
4472 key[1] = NUL;
4473
4474 /* round 1: count the number of prefix IDs.
4475 * round 2: move prefix IDs to "res" */
4476 for (round = 1; round <= 2; ++round)
4477 {
4478 cnt = 0;
4479 for (p = afflist; *p != NUL; ++p)
4480 {
4481 key[0] = *p;
4482 hi = hash_find(&affile->af_pref, key);
4483 if (!HASHITEM_EMPTY(hi))
4484 {
4485 /* This is a prefix ID, use the new number. */
4486 if (round == 2)
4487 res[cnt] = HI2AH(hi)->ah_newID;
4488 ++cnt;
4489 }
4490 }
4491 if (round == 1 && cnt > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004492 res = getroom(spin, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004493 if (res == NULL)
4494 break;
4495 }
4496
4497 if (res != NULL)
4498 res[cnt] = NUL;
4499 return res;
4500}
4501
4502/*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004503 * Get the list of affix IDs from the affix list "afflist" that are used for
4504 * compound words.
4505 * Returns a string allocated with getroom(). NULL when there are no relevant
4506 * affixes or when out of memory.
4507 */
4508 static char_u *
4509get_compflags(spin, afflist)
4510 spellinfo_T *spin;
4511 char_u *afflist;
4512{
4513 char_u *p;
4514 int cnt;
4515 int round;
4516 char_u *res = NULL;
4517
4518 /* round 1: count the number of affix IDs.
4519 * round 2: move affix IDs to "res" */
4520 for (round = 1; round <= 2; ++round)
4521 {
4522 cnt = 0;
4523 for (p = afflist; *p != NUL; ++p)
4524 {
4525 if (*p != ',' && *p != '-'
4526 && vim_strchr(spin->si_compflags, *p) != NULL)
4527 {
4528 /* This is a compount affix ID. */
4529 if (round == 2)
4530 res[cnt] = *p;
4531 ++cnt;
4532 }
4533 }
4534 if (round == 1 && cnt > 0)
4535 res = getroom(spin, cnt + 1, FALSE);
4536 if (res == NULL)
4537 break;
4538 }
4539
4540 if (res != NULL)
4541 res[cnt] = NUL;
4542 return res;
4543}
4544
4545/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004546 * Apply affixes to a word and store the resulting words.
4547 * "ht" is the hashtable with affentry_T that need to be applied, either
4548 * prefixes or suffixes.
4549 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4550 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004551 *
4552 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004553 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004554 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004555store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004556 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004557 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004558 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004559 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004560 hashtab_T *ht;
4561 hashtab_T *xht;
4562 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004563 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004564 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004565{
4566 int todo;
4567 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004568 affheader_T *ah;
4569 affentry_T *ae;
4570 regmatch_T regmatch;
4571 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004572 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004573 int i;
4574 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004575 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004576 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004577 int c;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004578 int wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579
Bram Moolenaar51485f02005-06-04 21:55:20 +00004580 todo = ht->ht_used;
4581 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004582 {
4583 if (!HASHITEM_EMPTY(hi))
4584 {
4585 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004586 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004587
Bram Moolenaar51485f02005-06-04 21:55:20 +00004588 /* Check that the affix combines, if required, and that the word
4589 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004590 c = PTR2CHAR(ah->ah_key);
4591 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004592 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004593 /* Loop over all affix entries with this name. */
4594 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004596 /* Check the condition. It's not logical to match case
4597 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004598 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004599 * Another requirement from Myspell is that the chop
4600 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004601 * For prefixes, when "PFXPOSTPONE" was used, only do
4602 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004603 regmatch.regprog = ae->ae_prog;
4604 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004605 if ((xht != NULL || !affile->af_pfxpostpone
4606 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004607 && (ae->ae_chop == NULL
4608 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004609 && (ae->ae_prog == NULL
4610 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004612 /* Match. Remove the chop and add the affix. */
4613 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004614 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004615 /* prefix: chop/add at the start of the word */
4616 if (ae->ae_add == NULL)
4617 *newword = NUL;
4618 else
4619 STRCPY(newword, ae->ae_add);
4620 p = word;
4621 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004622 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004623 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004624#ifdef FEAT_MBYTE
4625 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004626 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004627 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004628 for ( ; i > 0; --i)
4629 mb_ptr_adv(p);
4630 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004631 else
4632#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004633 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004634 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004635 STRCAT(newword, p);
4636 }
4637 else
4638 {
4639 /* suffix: chop/add at the end of the word */
4640 STRCPY(newword, word);
4641 if (ae->ae_chop != NULL)
4642 {
4643 /* Remove chop string. */
4644 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004645 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004646 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004647 mb_ptr_back(newword, p);
4648 *p = NUL;
4649 }
4650 if (ae->ae_add != NULL)
4651 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004652 }
4653
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004654 /* Obey the "rare" flag of the affix. */
4655 if (ae->ae_rare)
4656 use_flags = flags | WF_RARE;
4657 else
4658 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004659 use_pfxlist = pfxlist;
4660
4661 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004662 if (spin->si_prefroot != NULL
4663 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004664 {
4665 /* ... add a flag to indicate an affix was used. */
4666 use_flags |= WF_HAS_AFF;
4667
4668 /* ... don't use a prefix list if combining
4669 * affixes is not allowed */
4670 if (!ah->ah_combine || comb)
4671 use_pfxlist = NULL;
4672 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004673
Bram Moolenaar51485f02005-06-04 21:55:20 +00004674 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004675 if (store_word(spin, newword, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004676 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004677 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004678
Bram Moolenaar51485f02005-06-04 21:55:20 +00004679 /* When added a suffix and combining is allowed also
4680 * try adding prefixes additionally. */
4681 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004682 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004683 xht, NULL, TRUE,
4684 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004685 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004686 }
4687 }
4688 }
4689 }
4690 }
4691
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004692 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004693}
4694
4695/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004696 * Read a file with a list of words.
4697 */
4698 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004699spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004700 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004701 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004702{
4703 FILE *fd;
4704 long lnum = 0;
4705 char_u rline[MAXLINELEN];
4706 char_u *line;
4707 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004708 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004709 int l;
4710 int retval = OK;
4711 int did_word = FALSE;
4712 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004713 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004714 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004715
4716 /*
4717 * Open the file.
4718 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004719 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004720 if (fd == NULL)
4721 {
4722 EMSG2(_(e_notopen), fname);
4723 return FAIL;
4724 }
4725
Bram Moolenaarb765d632005-06-07 21:00:02 +00004726 if (spin->si_verbose || p_verbose > 2)
4727 {
4728 if (!spin->si_verbose)
4729 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004730 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004731 out_flush();
4732 if (!spin->si_verbose)
4733 verbose_leave();
4734 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004735
4736 /*
4737 * Read all the lines in the file one by one.
4738 */
4739 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4740 {
4741 line_breakcheck();
4742 ++lnum;
4743
4744 /* Skip comment lines. */
4745 if (*rline == '#')
4746 continue;
4747
4748 /* Remove CR, LF and white space from the end. */
4749 l = STRLEN(rline);
4750 while (l > 0 && rline[l - 1] <= ' ')
4751 --l;
4752 if (l == 0)
4753 continue; /* empty or blank line */
4754 rline[l] = NUL;
4755
4756 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4757 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004758#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004759 if (spin->si_conv.vc_type != CONV_NONE)
4760 {
4761 pc = string_convert(&spin->si_conv, rline, NULL);
4762 if (pc == NULL)
4763 {
4764 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4765 fname, lnum, rline);
4766 continue;
4767 }
4768 line = pc;
4769 }
4770 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004771#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004772 {
4773 pc = NULL;
4774 line = rline;
4775 }
4776
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004777 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004778 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004779 ++line;
4780 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004781 {
4782 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004783 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4784 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004785 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004786 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4787 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004788 else
4789 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004790#ifdef FEAT_MBYTE
4791 char_u *enc;
4792
Bram Moolenaar51485f02005-06-04 21:55:20 +00004793 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004794 line += 10;
4795 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004796 if (enc != NULL && !spin->si_ascii
4797 && convert_setup(&spin->si_conv, enc,
4798 p_enc) == FAIL)
4799 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004800 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004801 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004802 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004803#else
4804 smsg((char_u *)_("Conversion in %s not supported"), fname);
4805#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004806 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004807 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004808 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004809
Bram Moolenaar3982c542005-06-08 21:56:31 +00004810 if (STRNCMP(line, "regions=", 8) == 0)
4811 {
4812 if (spin->si_region_count > 1)
4813 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4814 fname, lnum, line);
4815 else
4816 {
4817 line += 8;
4818 if (STRLEN(line) > 16)
4819 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4820 fname, lnum, line);
4821 else
4822 {
4823 spin->si_region_count = STRLEN(line) / 2;
4824 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004825
4826 /* Adjust the mask for a word valid in all regions. */
4827 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004828 }
4829 }
4830 continue;
4831 }
4832
Bram Moolenaar7887d882005-07-01 22:33:52 +00004833 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4834 fname, lnum, line - 1);
4835 continue;
4836 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004837
Bram Moolenaar7887d882005-07-01 22:33:52 +00004838 flags = 0;
4839 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004840
Bram Moolenaar7887d882005-07-01 22:33:52 +00004841 /* Check for flags and region after a slash. */
4842 p = vim_strchr(line, '/');
4843 if (p != NULL)
4844 {
4845 *p++ = NUL;
4846 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004847 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004848 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004849 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004850 else if (*p == '!') /* Bad, bad, wicked word. */
4851 flags |= WF_BANNED;
4852 else if (*p == '?') /* Rare word. */
4853 flags |= WF_RARE;
4854 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004855 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004856 if ((flags & WF_REGION) == 0) /* first one */
4857 regionmask = 0;
4858 flags |= WF_REGION;
4859
4860 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004861 if (l > spin->si_region_count)
4862 {
4863 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004864 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004865 break;
4866 }
4867 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004868 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004869 else
4870 {
4871 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4872 fname, lnum, p);
4873 break;
4874 }
4875 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004876 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004877 }
4878
4879 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4880 if (spin->si_ascii && has_non_ascii(line))
4881 {
4882 ++non_ascii;
4883 continue;
4884 }
4885
4886 /* Normal word: store it. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004887 if (store_word(spin, line, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004888 {
4889 retval = FAIL;
4890 break;
4891 }
4892 did_word = TRUE;
4893 }
4894
4895 vim_free(pc);
4896 fclose(fd);
4897
Bram Moolenaarb765d632005-06-07 21:00:02 +00004898 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4899 {
4900 if (p_verbose > 2)
4901 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004902 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4903 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004904 if (p_verbose > 2)
4905 verbose_leave();
4906 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004907 return retval;
4908}
4909
4910/*
4911 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004912 * This avoids calling free() for every little struct we use (and keeping
4913 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004914 * The memory is cleared to all zeros.
4915 * Returns NULL when out of memory.
4916 */
4917 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004918getroom(spin, len, align)
4919 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004920 size_t len; /* length needed */
4921 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004922{
4923 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004924 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004925
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004926 if (align && bl != NULL)
4927 /* Round size up for alignment. On some systems structures need to be
4928 * aligned to the size of a pointer (e.g., SPARC). */
4929 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4930 & ~(sizeof(char *) - 1);
4931
Bram Moolenaar51485f02005-06-04 21:55:20 +00004932 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4933 {
4934 /* Allocate a block of memory. This is not freed until much later. */
4935 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4936 if (bl == NULL)
4937 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004938 bl->sb_next = spin->si_blocks;
4939 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004940 bl->sb_used = 0;
4941 }
4942
4943 p = bl->sb_data + bl->sb_used;
4944 bl->sb_used += len;
4945
4946 return p;
4947}
4948
4949/*
4950 * Make a copy of a string into memory allocated with getroom().
4951 */
4952 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004953getroom_save(spin, s)
4954 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004955 char_u *s;
4956{
4957 char_u *sc;
4958
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004959 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004960 if (sc != NULL)
4961 STRCPY(sc, s);
4962 return sc;
4963}
4964
4965
4966/*
4967 * Free the list of allocated sblock_T.
4968 */
4969 static void
4970free_blocks(bl)
4971 sblock_T *bl;
4972{
4973 sblock_T *next;
4974
4975 while (bl != NULL)
4976 {
4977 next = bl->sb_next;
4978 vim_free(bl);
4979 bl = next;
4980 }
4981}
4982
4983/*
4984 * Allocate the root of a word tree.
4985 */
4986 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004987wordtree_alloc(spin)
4988 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004989{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004990 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004991}
4992
4993/*
4994 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004995 * Always store it in the case-folded tree. For a keep-case word this is
4996 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4997 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004998 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004999 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005000 */
5001 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005002store_word(spin, word, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005003 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005004 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005005 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005006 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005007 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005008{
5009 int len = STRLEN(word);
5010 int ct = captype(word, word + len);
5011 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005012 int res = OK;
5013 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005015 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005016 for (p = pfxlist; res == OK; ++p)
5017 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005018 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
5019 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005020 if (p == NULL || *p == NUL)
5021 break;
5022 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005023 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005024
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005025 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00005026 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005027 for (p = pfxlist; res == OK; ++p)
5028 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005029 res = tree_add_word(spin, word, spin->si_keeproot, flags,
5030 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005031 if (p == NULL || *p == NUL)
5032 break;
5033 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005034 ++spin->si_keepwcount;
5035 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005036 return res;
5037}
5038
5039/*
5040 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005041 * When "flags" < 0 we are adding to the prefix tree where flags is used for
5042 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005043 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005044 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005045 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005046tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005047 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005048 char_u *word;
5049 wordnode_T *root;
5050 int flags;
5051 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005052 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005054 wordnode_T *node = root;
5055 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005056 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005057 wordnode_T **prev = NULL;
5058 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005059
Bram Moolenaar51485f02005-06-04 21:55:20 +00005060 /* Add each byte of the word to the tree, including the NUL at the end. */
5061 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005062 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005063 /* When there is more than one reference to this node we need to make
5064 * a copy, so that we can modify it. Copy the whole list of siblings
5065 * (we don't optimize for a partly shared list of siblings). */
5066 if (node != NULL && node->wn_refs > 1)
5067 {
5068 --node->wn_refs;
5069 copyprev = prev;
5070 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
5071 {
5072 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005073 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005074 if (np == NULL)
5075 return FAIL;
5076 np->wn_child = copyp->wn_child;
5077 if (np->wn_child != NULL)
5078 ++np->wn_child->wn_refs; /* child gets extra ref */
5079 np->wn_byte = copyp->wn_byte;
5080 if (np->wn_byte == NUL)
5081 {
5082 np->wn_flags = copyp->wn_flags;
5083 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005084 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005085 }
5086
5087 /* Link the new node in the list, there will be one ref. */
5088 np->wn_refs = 1;
5089 *copyprev = np;
5090 copyprev = &np->wn_sibling;
5091
5092 /* Let "node" point to the head of the copied list. */
5093 if (copyp == node)
5094 node = np;
5095 }
5096 }
5097
Bram Moolenaar51485f02005-06-04 21:55:20 +00005098 /* Look for the sibling that has the same character. They are sorted
5099 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005100 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005101 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005102 while (node != NULL
5103 && (node->wn_byte < word[i]
5104 || (node->wn_byte == NUL
5105 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005106 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005107 : node->wn_flags < (flags & WN_MASK)
5108 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005109 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005111 prev = &node->wn_sibling;
5112 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005113 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005114 if (node == NULL
5115 || node->wn_byte != word[i]
5116 || (word[i] == NUL
5117 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005118 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005119 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005120 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005121 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005122 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005123 if (np == NULL)
5124 return FAIL;
5125 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005126
5127 /* If "node" is NULL this is a new child or the end of the sibling
5128 * list: ref count is one. Otherwise use ref count of sibling and
5129 * make ref count of sibling one (matters when inserting in front
5130 * of the list of siblings). */
5131 if (node == NULL)
5132 np->wn_refs = 1;
5133 else
5134 {
5135 np->wn_refs = node->wn_refs;
5136 node->wn_refs = 1;
5137 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005138 *prev = np;
5139 np->wn_sibling = node;
5140 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005142
Bram Moolenaar51485f02005-06-04 21:55:20 +00005143 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005144 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005145 node->wn_flags = flags;
5146 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005147 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005148 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00005149 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005150 prev = &node->wn_child;
5151 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005152 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005153#ifdef SPELL_PRINTTREE
5154 smsg("Added \"%s\"", word);
5155 spell_print_tree(root->wn_sibling);
5156#endif
5157
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005158 /* count nr of words added since last message */
5159 ++spin->si_msg_count;
5160
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005161 /*
5162 * Every so many words compress the tree, so that we don't use too much
5163 * memory.
5164 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005165 i = FALSE;
5166 if (root == spin->si_foldroot)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005167 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005168 if (++spin->si_fold_added >= SPELL_COMPRESS_CNT)
5169 {
5170 i = TRUE;
5171 spin->si_fold_added = 0;
5172 }
5173 }
5174 else if (root == spin->si_keeproot)
5175 {
5176 if (++spin->si_keep_added >= SPELL_COMPRESS_CNT)
5177 {
5178 i = TRUE;
5179 spin->si_keep_added = 0;
5180 }
5181 }
5182 if (i)
5183 {
5184 if (spin->si_verbose)
5185 {
5186 msg_start();
5187 msg_puts((char_u *)_(msg_compressing));
5188 msg_clr_eos();
5189 msg_didout = FALSE;
5190 msg_col = 0;
5191 out_flush();
5192 }
5193 wordtree_compress(spin, root);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005194 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005195
5196 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005197}
5198
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005199/*
5200 * Get a wordnode_T, either from the list of previously freed nodes or
5201 * allocate a new one.
5202 */
5203 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005204get_wordnode(spin)
5205 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005206{
5207 wordnode_T *n;
5208
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005209 if (spin->si_first_free == NULL)
5210 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005211 else
5212 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005213 n = spin->si_first_free;
5214 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005215 vim_memset(n, 0, sizeof(wordnode_T));
5216 }
5217#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005218 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005219#endif
5220 return n;
5221}
5222
5223/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005224 * Decrement the reference count on a node (which is the head of a list of
5225 * siblings). If the reference count becomes zero free the node and its
5226 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005227 */
5228 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005229deref_wordnode(spin, node)
5230 spellinfo_T *spin;
5231 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005232{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005233 wordnode_T *np;
5234
5235 if (--node->wn_refs == 0)
5236 for (np = node; np != NULL; np = np->wn_sibling)
5237 {
5238 if (np->wn_child != NULL)
5239 deref_wordnode(spin, np->wn_child);
5240 free_wordnode(spin, np);
5241 }
5242}
5243
5244/*
5245 * Free a wordnode_T for re-use later.
5246 * Only the "wn_child" field becomes invalid.
5247 */
5248 static void
5249free_wordnode(spin, n)
5250 spellinfo_T *spin;
5251 wordnode_T *n;
5252{
5253 n->wn_child = spin->si_first_free;
5254 spin->si_first_free = n;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005255}
5256
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005257/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005258 * Compress a tree: find tails that are identical and can be shared.
5259 */
5260 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005261wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005262 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005263 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005264{
5265 hashtab_T ht;
5266 int n;
5267 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005268 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005269
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005270 /* Skip the root itself, it's not actually used. The first sibling is the
5271 * start of the tree. */
5272 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005273 {
5274 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005275 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005276
5277#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005278 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005279#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005280 {
5281 if (!spin->si_verbose)
5282 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005283 if (tot > 1000000)
5284 perc = (tot - n) / (tot / 100);
5285 else
5286 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005287 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005288 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005289 if (p_verbose > 2)
5290 verbose_leave();
5291 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005292#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005293 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005294#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005295 hash_clear(&ht);
5296 }
5297}
5298
5299/*
5300 * Compress a node, its siblings and its children, depth first.
5301 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005302 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005303 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005304node_compress(spin, node, ht, tot)
5305 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005306 wordnode_T *node;
5307 hashtab_T *ht;
5308 int *tot; /* total count of nodes before compressing,
5309 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005310{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005311 wordnode_T *np;
5312 wordnode_T *tp;
5313 wordnode_T *child;
5314 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005315 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005316 int len = 0;
5317 unsigned nr, n;
5318 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005319
Bram Moolenaar51485f02005-06-04 21:55:20 +00005320 /*
5321 * Go through the list of siblings. Compress each child and then try
5322 * finding an identical child to replace it.
5323 * Note that with "child" we mean not just the node that is pointed to,
5324 * but the whole list of siblings, of which the node is the first.
5325 */
5326 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005327 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005328 ++len;
5329 if ((child = np->wn_child) != NULL)
5330 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005331 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005332 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005333
5334 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005335 hash = hash_hash(child->wn_u1.hashkey);
5336 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005337 tp = NULL;
5338 if (!HASHITEM_EMPTY(hi))
5339 {
5340 /* There are children with an identical hash value. Now check
5341 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005342 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005343 if (node_equal(child, tp))
5344 {
5345 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005346 * current one. This means the current child and all
5347 * its siblings is unlinked from the tree. */
5348 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005349 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005350 np->wn_child = tp;
5351 ++compressed;
5352 break;
5353 }
5354 if (tp == NULL)
5355 {
5356 /* No other child with this hash value equals the child of
5357 * the node, add it to the linked list after the first
5358 * item. */
5359 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005360 child->wn_u2.next = tp->wn_u2.next;
5361 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005362 }
5363 }
5364 else
5365 /* No other child has this hash value, add it to the
5366 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005367 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005368 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005369 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005370 *tot += len;
5371
5372 /*
5373 * Make a hash key for the node and its siblings, so that we can quickly
5374 * find a lookalike node. This must be done after compressing the sibling
5375 * list, otherwise the hash key would become invalid by the compression.
5376 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005377 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005378 nr = 0;
5379 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005380 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005381 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005382 /* end node: use wn_flags, wn_region and wn_affixID */
5383 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005384 else
5385 /* byte node: use the byte value and the child pointer */
5386 n = np->wn_byte + ((long_u)np->wn_child << 8);
5387 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005388 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005389
5390 /* Avoid NUL bytes, it terminates the hash key. */
5391 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005392 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005393 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005394 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005395 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005396 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005397 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005398 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
5399 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005400
5401 return compressed;
5402}
5403
5404/*
5405 * Return TRUE when two nodes have identical siblings and children.
5406 */
5407 static int
5408node_equal(n1, n2)
5409 wordnode_T *n1;
5410 wordnode_T *n2;
5411{
5412 wordnode_T *p1;
5413 wordnode_T *p2;
5414
5415 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
5416 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
5417 if (p1->wn_byte != p2->wn_byte
5418 || (p1->wn_byte == NUL
5419 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005420 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005421 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005422 : (p1->wn_child != p2->wn_child)))
5423 break;
5424
5425 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005426}
5427
5428/*
5429 * Write a number to file "fd", MSB first, in "len" bytes.
5430 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005431 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005432put_bytes(fd, nr, len)
5433 FILE *fd;
5434 long_u nr;
5435 int len;
5436{
5437 int i;
5438
5439 for (i = len - 1; i >= 0; --i)
5440 putc((int)(nr >> (i * 8)), fd);
5441}
5442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005443static int
5444#ifdef __BORLANDC__
5445_RTLENTRYF
5446#endif
5447rep_compare __ARGS((const void *s1, const void *s2));
5448
5449/*
5450 * Function given to qsort() to sort the REP items on "from" string.
5451 */
5452 static int
5453#ifdef __BORLANDC__
5454_RTLENTRYF
5455#endif
5456rep_compare(s1, s2)
5457 const void *s1;
5458 const void *s2;
5459{
5460 fromto_T *p1 = (fromto_T *)s1;
5461 fromto_T *p2 = (fromto_T *)s2;
5462
5463 return STRCMP(p1->ft_from, p2->ft_from);
5464}
5465
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005466/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005467 * Write the Vim spell file "fname".
5468 */
5469 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005470write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005471 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005472 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005473{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005474 FILE *fd;
5475 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005476 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005477 wordnode_T *tree;
5478 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005479 int i;
5480 int l;
5481 garray_T *gap;
5482 fromto_T *ftp;
5483 char_u *p;
5484 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005485
Bram Moolenaarb765d632005-06-07 21:00:02 +00005486 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005487 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005488 {
5489 EMSG2(_(e_notopen), fname);
5490 return;
5491 }
5492
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005493 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005494 * <charflagslen> <charflags>
5495 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005496 * <midwordlen> <midword>
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005497 * <compoundlen> <compoundtype> <compoundinfo>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005498 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005499
5500 /* <fileID> */
5501 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
5502 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005503
5504 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005505 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005506 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005507 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
5508 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
5509 (size_t)1, fd);
5510 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005511 }
5512 else
5513 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005514 putc(0, fd);
5515 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005516 }
5517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005518 /*
5519 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005520 * <charflagslen> <charflags> <fcharlen> <fchars>
5521 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005522 * 'encoding'.
5523 * Also skip this for an .add.spl file, the main spell file must contain
5524 * the table (avoids that it conflicts). File is shorter too.
5525 */
5526 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005527 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005528 putc(0, fd);
5529 putc(0, fd);
5530 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005531 }
5532 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005533 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005534
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005535
5536 if (spin->si_midword == NULL)
5537 put_bytes(fd, 0L, 2); /* <midwordlen> */
5538 else
5539 {
5540 i = STRLEN(spin->si_midword);
5541 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
5542 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
5543 }
5544
5545
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005546 /* Write the compound info. */
5547 if (spin->si_compflags == NULL)
5548 put_bytes(fd, 0L, 2); /* <compoundlen> */
5549 else
5550 {
5551 l = STRLEN(spin->si_compflags);
5552 put_bytes(fd, (long_u)(l + 2), 2); /* <compoundlen> */
5553 putc(1, fd); /* <compoundtype> */
5554 putc(spin->si_compminlen, fd); /* <comp1minlen> */
5555 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
5556 /* <comp1flags> */
5557 }
5558
5559
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005560 /* Write the prefix conditions. */
5561 write_spell_prefcond(fd, &spin->si_prefcond);
5562
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005563 /* <SUGGEST> : <repcount> <rep> ...
5564 * <salflags> <salcount> <sal> ...
5565 * <maplen> <mapstr> */
5566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005567 /* Sort the REP items. */
5568 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
5569 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005570
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005571 /* round 1: REP items
5572 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005573 for (round = 1; round <= 2; ++round)
5574 {
5575 if (round == 1)
5576 gap = &spin->si_rep;
5577 else
5578 {
5579 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005581 i = 0;
5582 if (spin->si_followup)
5583 i |= SAL_F0LLOWUP;
5584 if (spin->si_collapse)
5585 i |= SAL_COLLAPSE;
5586 if (spin->si_rem_accents)
5587 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005588 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5589 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005590 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005591 if (i & SAL_SOFO)
5592 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005593 }
5594
5595 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
5596 for (i = 0; i < gap->ga_len; ++i)
5597 {
5598 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
5599 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
5600 ftp = &((fromto_T *)gap->ga_data)[i];
5601 for (rr = 1; rr <= 2; ++rr)
5602 {
5603 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5604 l = STRLEN(p);
5605 putc(l, fd);
5606 fwrite(p, l, (size_t)1, fd);
5607 }
5608 }
5609 }
5610
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005611 /* SOFOFROM and SOFOTO */
5612 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5613 {
5614 put_bytes(fd, 1L, 2); /* <salcount> */
5615
5616 l = STRLEN(spin->si_sofofr);
5617 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5618 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5619
5620 l = STRLEN(spin->si_sofoto);
5621 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5622 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5623 }
5624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005625 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5626 if (spin->si_map.ga_len > 0) /* <mapstr> */
5627 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5628 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005629
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005630 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005631 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005632 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005633 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005634 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005635 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005636 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005637 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005638 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005639 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005640 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005641 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005642
Bram Moolenaar0c405862005-06-22 22:26:26 +00005643 /* Clear the index and wnode fields in the tree. */
5644 clear_node(tree);
5645
Bram Moolenaar51485f02005-06-04 21:55:20 +00005646 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005647 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005648 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005649 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005650
Bram Moolenaar51485f02005-06-04 21:55:20 +00005651 /* number of nodes in 4 bytes */
5652 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005653 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005654
Bram Moolenaar51485f02005-06-04 21:55:20 +00005655 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005656 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005657 }
5658
Bram Moolenaar51485f02005-06-04 21:55:20 +00005659 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005660}
5661
5662/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005663 * Clear the index and wnode fields of "node", it siblings and its
5664 * children. This is needed because they are a union with other items to save
5665 * space.
5666 */
5667 static void
5668clear_node(node)
5669 wordnode_T *node;
5670{
5671 wordnode_T *np;
5672
5673 if (node != NULL)
5674 for (np = node; np != NULL; np = np->wn_sibling)
5675 {
5676 np->wn_u1.index = 0;
5677 np->wn_u2.wnode = NULL;
5678
5679 if (np->wn_byte != NUL)
5680 clear_node(np->wn_child);
5681 }
5682}
5683
5684
5685/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005686 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005687 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005688 * This first writes the list of possible bytes (siblings). Then for each
5689 * byte recursively write the children.
5690 *
5691 * NOTE: The code here must match the code in read_tree(), since assumptions
5692 * are made about the indexes (so that we don't have to write them in the
5693 * file).
5694 *
5695 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005697 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005698put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005699 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005700 wordnode_T *node;
5701 int index;
5702 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005703 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005704{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005705 int newindex = index;
5706 int siblingcount = 0;
5707 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005708 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005709
Bram Moolenaar51485f02005-06-04 21:55:20 +00005710 /* If "node" is zero the tree is empty. */
5711 if (node == NULL)
5712 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005713
Bram Moolenaar51485f02005-06-04 21:55:20 +00005714 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005715 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005716
5717 /* Count the number of siblings. */
5718 for (np = node; np != NULL; np = np->wn_sibling)
5719 ++siblingcount;
5720
5721 /* Write the sibling count. */
5722 if (fd != NULL)
5723 putc(siblingcount, fd); /* <siblingcount> */
5724
5725 /* Write each sibling byte and optionally extra info. */
5726 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005727 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005728 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005729 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005730 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005731 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005732 /* For a NUL byte (end of word) write the flags etc. */
5733 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005734 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005735 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005736 * associated condition nr (stored in wn_region). The
5737 * byte value is misused to store the "rare" and "not
5738 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005739 if (np->wn_flags == (short_u)PFX_FLAGS)
5740 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005741 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005742 {
5743 putc(BY_FLAGS, fd); /* <byte> */
5744 putc(np->wn_flags, fd); /* <pflags> */
5745 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005746 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005747 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005748 }
5749 else
5750 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005751 /* For word trees we write the flag/region items. */
5752 flags = np->wn_flags;
5753 if (regionmask != 0 && np->wn_region != regionmask)
5754 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005755 if (np->wn_affixID != 0)
5756 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005757 if (flags == 0)
5758 {
5759 /* word without flags or region */
5760 putc(BY_NOFLAGS, fd); /* <byte> */
5761 }
5762 else
5763 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005764 if (np->wn_flags >= 0x100)
5765 {
5766 putc(BY_FLAGS2, fd); /* <byte> */
5767 putc(flags, fd); /* <flags> */
5768 putc((unsigned)flags >> 8, fd); /* <flags2> */
5769 }
5770 else
5771 {
5772 putc(BY_FLAGS, fd); /* <byte> */
5773 putc(flags, fd); /* <flags> */
5774 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005775 if (flags & WF_REGION)
5776 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005777 if (flags & WF_AFX)
5778 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005779 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005780 }
5781 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005782 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005783 else
5784 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005785 if (np->wn_child->wn_u1.index != 0
5786 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005787 {
5788 /* The child is written elsewhere, write the reference. */
5789 if (fd != NULL)
5790 {
5791 putc(BY_INDEX, fd); /* <byte> */
5792 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005793 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005794 }
5795 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005796 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005797 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005798 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005799
Bram Moolenaar51485f02005-06-04 21:55:20 +00005800 if (fd != NULL)
5801 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5802 {
5803 EMSG(_(e_write));
5804 return 0;
5805 }
5806 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005807 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005808
5809 /* Space used in the array when reading: one for each sibling and one for
5810 * the count. */
5811 newindex += siblingcount + 1;
5812
5813 /* Recursively dump the children of each sibling. */
5814 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005815 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5816 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005817 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005818
5819 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005820}
5821
5822
5823/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005824 * ":mkspell [-ascii] outfile infile ..."
5825 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005826 */
5827 void
5828ex_mkspell(eap)
5829 exarg_T *eap;
5830{
5831 int fcount;
5832 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005833 char_u *arg = eap->arg;
5834 int ascii = FALSE;
5835
5836 if (STRNCMP(arg, "-ascii", 6) == 0)
5837 {
5838 ascii = TRUE;
5839 arg = skipwhite(arg + 6);
5840 }
5841
5842 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5843 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005845 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005846 FreeWild(fcount, fnames);
5847 }
5848}
5849
5850/*
5851 * Create a Vim spell file from one or more word lists.
5852 * "fnames[0]" is the output file name.
5853 * "fnames[fcount - 1]" is the last input file name.
5854 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5855 * and ".spl" is appended to make the output file name.
5856 */
5857 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005858mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005859 int fcount;
5860 char_u **fnames;
5861 int ascii; /* -ascii argument given */
5862 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005863 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005864{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005865 char_u fname[MAXPATHL];
5866 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005867 char_u **innames;
5868 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005869 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005870 int i;
5871 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005872 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005873 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005874 spellinfo_T spin;
5875
5876 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005877 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005878 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005879 spin.si_followup = TRUE;
5880 spin.si_rem_accents = TRUE;
5881 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5882 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5883 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005884 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005885
Bram Moolenaarb765d632005-06-07 21:00:02 +00005886 /* default: fnames[0] is output file, following are input files */
5887 innames = &fnames[1];
5888 incount = fcount - 1;
5889
5890 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005891 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005892 len = STRLEN(fnames[0]);
5893 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5894 {
5895 /* For ":mkspell path/en.latin1.add" output file is
5896 * "path/en.latin1.add.spl". */
5897 innames = &fnames[0];
5898 incount = 1;
5899 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5900 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005901 else if (fcount == 1)
5902 {
5903 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5904 innames = &fnames[0];
5905 incount = 1;
5906 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5907 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5908 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005909 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5910 {
5911 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005912 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005913 }
5914 else
5915 /* Name should be language, make the file name from it. */
5916 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5917 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5918
5919 /* Check for .ascii.spl. */
5920 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5921 spin.si_ascii = TRUE;
5922
5923 /* Check for .add.spl. */
5924 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5925 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005926 }
5927
Bram Moolenaarb765d632005-06-07 21:00:02 +00005928 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005929 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005930 else if (vim_strchr(gettail(wfname), '_') != NULL)
5931 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005932 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005933 EMSG(_("E754: Only up to 8 regions supported"));
5934 else
5935 {
5936 /* Check for overwriting before doing things that may take a lot of
5937 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005938 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005939 {
5940 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005941 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005942 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005943 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005944 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005945 EMSG2(_(e_isadir2), wfname);
5946 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005947 }
5948
5949 /*
5950 * Init the aff and dic pointers.
5951 * Get the region names if there are more than 2 arguments.
5952 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005953 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005954 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005955 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005956
Bram Moolenaar3982c542005-06-08 21:56:31 +00005957 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005958 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005959 len = STRLEN(innames[i]);
5960 if (STRLEN(gettail(innames[i])) < 5
5961 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005962 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005963 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5964 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005965 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005966 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5967 spin.si_region_name[i * 2 + 1] =
5968 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005969 }
5970 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005971 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005972
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005973 spin.si_foldroot = wordtree_alloc(&spin);
5974 spin.si_keeproot = wordtree_alloc(&spin);
5975 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005976 if (spin.si_foldroot == NULL
5977 || spin.si_keeproot == NULL
5978 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005979 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005980 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005981 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005982 }
5983
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005984 /* When not producing a .add.spl file clear the character table when
5985 * we encounter one in the .aff file. This means we dump the current
5986 * one in the .spl file if the .aff file doesn't define one. That's
5987 * better than guessing the contents, the table will match a
5988 * previously loaded spell file. */
5989 if (!spin.si_add)
5990 spin.si_clear_chartab = TRUE;
5991
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005992 /*
5993 * Read all the .aff and .dic files.
5994 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005995 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005996 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005997 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005998 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005999 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006000 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006001
Bram Moolenaarb765d632005-06-07 21:00:02 +00006002 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006003 if (mch_stat((char *)fname, &st) >= 0)
6004 {
6005 /* Read the .aff file. Will init "spin->si_conv" based on the
6006 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006007 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006008 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006009 error = TRUE;
6010 else
6011 {
6012 /* Read the .dic file and store the words in the trees. */
6013 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00006014 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006015 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006016 error = TRUE;
6017 }
6018 }
6019 else
6020 {
6021 /* No .aff file, try reading the file as a word list. Store
6022 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006023 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006024 error = TRUE;
6025 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006026
Bram Moolenaarb765d632005-06-07 21:00:02 +00006027#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006028 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006029 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006030#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006031 }
6032
Bram Moolenaar51485f02005-06-04 21:55:20 +00006033 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006034 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006035 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006036 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006037 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006038 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006039 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006040 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006041 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006042 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006043 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006044 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006045 verbose_leave();
6046 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006047 wordtree_compress(&spin, spin.si_foldroot);
6048 wordtree_compress(&spin, spin.si_keeproot);
6049 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006050 }
6051
Bram Moolenaar51485f02005-06-04 21:55:20 +00006052 if (!error)
6053 {
6054 /*
6055 * Write the info in the spell file.
6056 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006057 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006058 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006059 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006060 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006061 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006062 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006063 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006064 verbose_leave();
6065 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00006066
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006067 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006068
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006069 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006070 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006071 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006072 verbose_enter();
6073 MSG(_("Done!"));
6074 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00006075 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006076 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006077 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006078 verbose_leave();
6079 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006080
Bram Moolenaarb765d632005-06-07 21:00:02 +00006081 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006082 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006083 }
6084
6085 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006086 ga_clear(&spin.si_rep);
6087 ga_clear(&spin.si_sal);
6088 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006089 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006090 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006091 vim_free(spin.si_sofofr);
6092 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006093
6094 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006095 for (i = 0; i < incount; ++i)
6096 if (afile[i] != NULL)
6097 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006098
6099 /* Free all the bits and pieces at once. */
6100 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006101 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006102}
6103
Bram Moolenaarb765d632005-06-07 21:00:02 +00006104
6105/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006106 * ":[count]spellgood {word}"
6107 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00006108 */
6109 void
6110ex_spell(eap)
6111 exarg_T *eap;
6112{
Bram Moolenaar7887d882005-07-01 22:33:52 +00006113 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006114 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006115}
6116
6117/*
6118 * Add "word[len]" to 'spellfile' as a good or bad word.
6119 */
6120 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006121spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006122 char_u *word;
6123 int len;
6124 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006125 int index; /* "zG" and "zW": zero, otherwise index in
6126 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006127{
6128 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006129 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006130 int new_spf = FALSE;
6131 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006132 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006133 char_u fnamebuf[MAXPATHL];
6134 char_u line[MAXWLEN * 2];
6135 long fpos, fpos_next = 0;
6136 int i;
6137 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006138
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006139 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006140 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006141 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006142 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006143 int_wordlist = vim_tempname('s');
6144 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006145 return;
6146 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006147 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006148 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006149 else
6150 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006151 /* If 'spellfile' isn't set figure out a good default value. */
6152 if (*curbuf->b_p_spf == NUL)
6153 {
6154 init_spellfile();
6155 new_spf = TRUE;
6156 }
6157
6158 if (*curbuf->b_p_spf == NUL)
6159 {
6160 EMSG(_("E764: 'spellfile' is not set"));
6161 return;
6162 }
6163
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006164 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
6165 {
6166 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6167 if (i == index)
6168 break;
6169 if (*spf == NUL)
6170 {
6171 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
6172 return;
6173 }
6174 }
6175
Bram Moolenaarb765d632005-06-07 21:00:02 +00006176 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006177 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006178 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6179 buf = NULL;
6180 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00006181 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006182 EMSG(_(e_bufloaded));
6183 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006184 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006185
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006186 fname = fnamebuf;
6187 }
6188
6189 if (bad)
6190 {
6191 /* When the word also appears as good word we need to remove that one,
6192 * since its flags sort before the one with WF_BANNED. */
6193 fd = mch_fopen((char *)fname, "r");
6194 if (fd != NULL)
6195 {
6196 while (!vim_fgets(line, MAXWLEN * 2, fd))
6197 {
6198 fpos = fpos_next;
6199 fpos_next = ftell(fd);
6200 if (STRNCMP(word, line, len) == 0
6201 && (line[len] == '/' || line[len] < ' '))
6202 {
6203 /* Found duplicate word. Remove it by writing a '#' at
6204 * the start of the line. Mixing reading and writing
6205 * doesn't work for all systems, close the file first. */
6206 fclose(fd);
6207 fd = mch_fopen((char *)fname, "r+");
6208 if (fd == NULL)
6209 break;
6210 if (fseek(fd, fpos, SEEK_SET) == 0)
6211 fputc('#', fd);
6212 fseek(fd, fpos_next, SEEK_SET);
6213 }
6214 }
6215 fclose(fd);
6216 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006217 }
6218
6219 fd = mch_fopen((char *)fname, "a");
6220 if (fd == NULL && new_spf)
6221 {
6222 /* We just initialized the 'spellfile' option and can't open the file.
6223 * We may need to create the "spell" directory first. We already
6224 * checked the runtime directory is writable in init_spellfile(). */
6225 STRCPY(NameBuff, fname);
6226 *gettail_sep(NameBuff) = NUL;
6227 if (mch_stat((char *)NameBuff, &st) < 0)
6228 {
6229 /* The directory doesn't exist. Try creating it and opening the
6230 * file again. */
6231 vim_mkdir(NameBuff, 0755);
6232 fd = mch_fopen((char *)fname, "a");
6233 }
6234 }
6235
6236 if (fd == NULL)
6237 EMSG2(_(e_notopen), fname);
6238 else
6239 {
6240 if (bad)
6241 fprintf(fd, "%.*s/!\n", len, word);
6242 else
6243 fprintf(fd, "%.*s\n", len, word);
6244 fclose(fd);
6245
6246 /* Update the .add.spl file. */
6247 mkspell(1, &fname, FALSE, TRUE, TRUE);
6248
6249 /* If the .add file is edited somewhere, reload it. */
6250 if (buf != NULL)
6251 buf_reload(buf);
6252
6253 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006254 }
6255}
6256
6257/*
6258 * Initialize 'spellfile' for the current buffer.
6259 */
6260 static void
6261init_spellfile()
6262{
6263 char_u buf[MAXPATHL];
6264 int l;
6265 slang_T *sl;
6266 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006267 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006268
6269 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
6270 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006271 /* Find the end of the language name. Exclude the region. */
6272 for (lend = curbuf->b_p_spl; *lend != NUL
6273 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6274 ;
6275
6276 /* Loop over all entries in 'runtimepath'. Use the first one where we
6277 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006278 rtp = p_rtp;
6279 while (*rtp != NUL)
6280 {
6281 /* Copy the path from 'runtimepath' to buf[]. */
6282 copy_option_part(&rtp, buf, MAXPATHL, ",");
6283 if (filewritable(buf) == 2)
6284 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00006285 /* Use the first language name from 'spelllang' and the
6286 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006287 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
6288 l = STRLEN(buf);
6289 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00006290 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006291 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00006292 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
6293 ? (char_u *)"ascii" : spell_enc());
6294 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6295 break;
6296 }
6297 }
6298 }
6299}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006300
Bram Moolenaar51485f02005-06-04 21:55:20 +00006301
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006302/*
6303 * Init the chartab used for spelling for ASCII.
6304 * EBCDIC is not supported!
6305 */
6306 static void
6307clear_spell_chartab(sp)
6308 spelltab_T *sp;
6309{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006310 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006311
6312 /* Init everything to FALSE. */
6313 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
6314 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
6315 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006316 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006317 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006318 sp->st_upper[i] = i;
6319 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006320
6321 /* We include digits. A word shouldn't start with a digit, but handling
6322 * that is done separately. */
6323 for (i = '0'; i <= '9'; ++i)
6324 sp->st_isw[i] = TRUE;
6325 for (i = 'A'; i <= 'Z'; ++i)
6326 {
6327 sp->st_isw[i] = TRUE;
6328 sp->st_isu[i] = TRUE;
6329 sp->st_fold[i] = i + 0x20;
6330 }
6331 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006332 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006333 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006334 sp->st_upper[i] = i - 0x20;
6335 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006336}
6337
6338/*
6339 * Init the chartab used for spelling. Only depends on 'encoding'.
6340 * Called once while starting up and when 'encoding' changes.
6341 * The default is to use isalpha(), but the spell file should define the word
6342 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006343 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006344 */
6345 void
6346init_spell_chartab()
6347{
6348 int i;
6349
6350 did_set_spelltab = FALSE;
6351 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006352#ifdef FEAT_MBYTE
6353 if (enc_dbcs)
6354 {
6355 /* DBCS: assume double-wide characters are word characters. */
6356 for (i = 128; i <= 255; ++i)
6357 if (MB_BYTE2LEN(i) == 2)
6358 spelltab.st_isw[i] = TRUE;
6359 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006360 else if (enc_utf8)
6361 {
6362 for (i = 128; i < 256; ++i)
6363 {
6364 spelltab.st_isu[i] = utf_isupper(i);
6365 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
6366 spelltab.st_fold[i] = utf_fold(i);
6367 spelltab.st_upper[i] = utf_toupper(i);
6368 }
6369 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006370 else
6371#endif
6372 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006373 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006374 for (i = 128; i < 256; ++i)
6375 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006376 if (MB_ISUPPER(i))
6377 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006378 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006379 spelltab.st_isu[i] = TRUE;
6380 spelltab.st_fold[i] = MB_TOLOWER(i);
6381 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006382 else if (MB_ISLOWER(i))
6383 {
6384 spelltab.st_isw[i] = TRUE;
6385 spelltab.st_upper[i] = MB_TOUPPER(i);
6386 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006387 }
6388 }
6389}
6390
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006391static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
6392static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
6393
6394/*
6395 * Set the spell character tables from strings in the affix file.
6396 */
6397 static int
6398set_spell_chartab(fol, low, upp)
6399 char_u *fol;
6400 char_u *low;
6401 char_u *upp;
6402{
6403 /* We build the new tables here first, so that we can compare with the
6404 * previous one. */
6405 spelltab_T new_st;
6406 char_u *pf = fol, *pl = low, *pu = upp;
6407 int f, l, u;
6408
6409 clear_spell_chartab(&new_st);
6410
6411 while (*pf != NUL)
6412 {
6413 if (*pl == NUL || *pu == NUL)
6414 {
6415 EMSG(_(e_affform));
6416 return FAIL;
6417 }
6418#ifdef FEAT_MBYTE
6419 f = mb_ptr2char_adv(&pf);
6420 l = mb_ptr2char_adv(&pl);
6421 u = mb_ptr2char_adv(&pu);
6422#else
6423 f = *pf++;
6424 l = *pl++;
6425 u = *pu++;
6426#endif
6427 /* Every character that appears is a word character. */
6428 if (f < 256)
6429 new_st.st_isw[f] = TRUE;
6430 if (l < 256)
6431 new_st.st_isw[l] = TRUE;
6432 if (u < 256)
6433 new_st.st_isw[u] = TRUE;
6434
6435 /* if "LOW" and "FOL" are not the same the "LOW" char needs
6436 * case-folding */
6437 if (l < 256 && l != f)
6438 {
6439 if (f >= 256)
6440 {
6441 EMSG(_(e_affrange));
6442 return FAIL;
6443 }
6444 new_st.st_fold[l] = f;
6445 }
6446
6447 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006448 * case-folding, it's upper case and the "UPP" is the upper case of
6449 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006450 if (u < 256 && u != f)
6451 {
6452 if (f >= 256)
6453 {
6454 EMSG(_(e_affrange));
6455 return FAIL;
6456 }
6457 new_st.st_fold[u] = f;
6458 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006459 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006460 }
6461 }
6462
6463 if (*pl != NUL || *pu != NUL)
6464 {
6465 EMSG(_(e_affform));
6466 return FAIL;
6467 }
6468
6469 return set_spell_finish(&new_st);
6470}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006471
6472/*
6473 * Set the spell character tables from strings in the .spl file.
6474 */
6475 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006476set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006477 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006478 int cnt; /* length of "flags" */
6479 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006480{
6481 /* We build the new tables here first, so that we can compare with the
6482 * previous one. */
6483 spelltab_T new_st;
6484 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006485 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006486 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006487
6488 clear_spell_chartab(&new_st);
6489
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006490 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006491 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006492 if (i < cnt)
6493 {
6494 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6495 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6496 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006497
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006498 if (*p != NUL)
6499 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006500#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006501 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006502#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006503 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006504#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006505 new_st.st_fold[i + 128] = c;
6506 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6507 new_st.st_upper[c] = i + 128;
6508 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006509 }
6510
6511 return set_spell_finish(&new_st);
6512}
6513
6514 static int
6515set_spell_finish(new_st)
6516 spelltab_T *new_st;
6517{
6518 int i;
6519
6520 if (did_set_spelltab)
6521 {
6522 /* check that it's the same table */
6523 for (i = 0; i < 256; ++i)
6524 {
6525 if (spelltab.st_isw[i] != new_st->st_isw[i]
6526 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006527 || spelltab.st_fold[i] != new_st->st_fold[i]
6528 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006529 {
6530 EMSG(_("E763: Word characters differ between spell files"));
6531 return FAIL;
6532 }
6533 }
6534 }
6535 else
6536 {
6537 /* copy the new spelltab into the one being used */
6538 spelltab = *new_st;
6539 did_set_spelltab = TRUE;
6540 }
6541
6542 return OK;
6543}
6544
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006545/*
Bram Moolenaarea408852005-06-25 22:49:46 +00006546 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006547 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00006548 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006549 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00006550 */
6551 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006552spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00006553 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006554 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00006555{
Bram Moolenaarea408852005-06-25 22:49:46 +00006556#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006557 char_u *s;
6558 int l;
6559 int c;
6560
6561 if (has_mbyte)
6562 {
6563 l = MB_BYTE2LEN(*p);
6564 s = p;
6565 if (l == 1)
6566 {
6567 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006568 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006569 {
6570 s = p + 1; /* skip a mid-word character */
6571 l = MB_BYTE2LEN(*s);
6572 }
6573 }
6574 else
6575 {
6576 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006577 if (c < 256 ? buf->b_spell_ismw[c]
6578 : (buf->b_spell_ismw_mb != NULL
6579 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006580 {
6581 s = p + l;
6582 l = MB_BYTE2LEN(*s);
6583 }
6584 }
6585
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006586 c = mb_ptr2char(s);
6587 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006588 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006589 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006590 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006591#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006592
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006593 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
6594}
6595
6596/*
6597 * Return TRUE if "p" points to a word character.
6598 * Unlike spell_iswordp() this doesn't check for "midword" characters.
6599 */
6600 static int
6601spell_iswordp_nmw(p)
6602 char_u *p;
6603{
6604#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006605 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006606
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006607 if (has_mbyte)
6608 {
6609 c = mb_ptr2char(p);
6610 if (c > 255)
6611 return mb_get_class(p) >= 2;
6612 return spelltab.st_isw[c];
6613 }
6614#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006615 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006616}
6617
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006618#ifdef FEAT_MBYTE
6619/*
6620 * Return TRUE if "p" points to a word character.
6621 * Wide version of spell_iswordp().
6622 */
6623 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006624spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006625 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006626 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006627{
6628 int *s;
6629
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006630 if (*p < 256 ? buf->b_spell_ismw[*p]
6631 : (buf->b_spell_ismw_mb != NULL
6632 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006633 s = p + 1;
6634 else
6635 s = p;
6636
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006637 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006638 {
6639 if (enc_utf8)
6640 return utf_class(*s) >= 2;
6641 if (enc_dbcs)
6642 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6643 return 0;
6644 }
6645 return spelltab.st_isw[*s];
6646}
6647#endif
6648
Bram Moolenaarea408852005-06-25 22:49:46 +00006649/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006650 * Write the table with prefix conditions to the .spl file.
6651 */
6652 static void
6653write_spell_prefcond(fd, gap)
6654 FILE *fd;
6655 garray_T *gap;
6656{
6657 int i;
6658 char_u *p;
6659 int len;
6660
6661 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6662
6663 for (i = 0; i < gap->ga_len; ++i)
6664 {
6665 /* <prefcond> : <condlen> <condstr> */
6666 p = ((char_u **)gap->ga_data)[i];
6667 if (p == NULL)
6668 fputc(0, fd);
6669 else
6670 {
6671 len = STRLEN(p);
6672 fputc(len, fd);
6673 fwrite(p, (size_t)len, (size_t)1, fd);
6674 }
6675 }
6676}
6677
6678/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006679 * Write the current tables into the .spl file.
6680 * This makes sure the same characters are recognized as word characters when
6681 * generating an when using a spell file.
6682 */
6683 static void
6684write_spell_chartab(fd)
6685 FILE *fd;
6686{
6687 char_u charbuf[256 * 4];
6688 int len = 0;
6689 int flags;
6690 int i;
6691
6692 fputc(128, fd); /* <charflagslen> */
6693 for (i = 128; i < 256; ++i)
6694 {
6695 flags = 0;
6696 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006697 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006698 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006699 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006700 fputc(flags, fd); /* <charflags> */
6701
Bram Moolenaarb765d632005-06-07 21:00:02 +00006702#ifdef FEAT_MBYTE
6703 if (has_mbyte)
6704 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6705 else
6706#endif
6707 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006708 }
6709
6710 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6711 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6712}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006713
6714/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006715 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6716 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006717 * When using a multi-byte 'encoding' the length may change!
6718 * Returns FAIL when something wrong.
6719 */
6720 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006721spell_casefold(str, len, buf, buflen)
6722 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006723 int len;
6724 char_u *buf;
6725 int buflen;
6726{
6727 int i;
6728
6729 if (len >= buflen)
6730 {
6731 buf[0] = NUL;
6732 return FAIL; /* result will not fit */
6733 }
6734
6735#ifdef FEAT_MBYTE
6736 if (has_mbyte)
6737 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006738 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006739 char_u *p;
6740 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006741
6742 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006743 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006744 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006745 if (outi + MB_MAXBYTES > buflen)
6746 {
6747 buf[outi] = NUL;
6748 return FAIL;
6749 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006750 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006751 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006752 }
6753 buf[outi] = NUL;
6754 }
6755 else
6756#endif
6757 {
6758 /* Be quick for non-multibyte encodings. */
6759 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006760 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006761 buf[i] = NUL;
6762 }
6763
6764 return OK;
6765}
6766
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006767#define SPS_BEST 1
6768#define SPS_FAST 2
6769#define SPS_DOUBLE 4
6770
6771static int sps_flags = SPS_BEST;
6772
6773/*
6774 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6775 * Sets "sps_flags".
6776 */
6777 int
6778spell_check_sps()
6779{
6780 char_u *p;
6781 char_u buf[MAXPATHL];
6782 int f;
6783
6784 sps_flags = 0;
6785
6786 for (p = p_sps; *p != NUL; )
6787 {
6788 copy_option_part(&p, buf, MAXPATHL, ",");
6789
6790 f = 0;
6791 if (STRCMP(buf, "best") == 0)
6792 f = SPS_BEST;
6793 else if (STRCMP(buf, "fast") == 0)
6794 f = SPS_FAST;
6795 else if (STRCMP(buf, "double") == 0)
6796 f = SPS_DOUBLE;
6797 else if (STRNCMP(buf, "expr:", 5) != 0
6798 && STRNCMP(buf, "file:", 5) != 0)
6799 f = -1;
6800
6801 if (f == -1 || (sps_flags != 0 && f != 0))
6802 {
6803 sps_flags = SPS_BEST;
6804 return FAIL;
6805 }
6806 if (f != 0)
6807 sps_flags = f;
6808 }
6809
6810 if (sps_flags == 0)
6811 sps_flags = SPS_BEST;
6812
6813 return OK;
6814}
6815
6816/* Remember what "z?" replaced. */
6817static char_u *repl_from = NULL;
6818static char_u *repl_to = NULL;
6819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006820/*
6821 * "z?": Find badly spelled word under or after the cursor.
6822 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006823 */
6824 void
6825spell_suggest()
6826{
6827 char_u *line;
6828 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006829 char_u wcopy[MAXWLEN + 2];
6830 char_u *p;
6831 int i;
6832 int c;
6833 suginfo_T sug;
6834 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006835 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006836 int need_cap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006837
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006838 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006839 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6840 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006841 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006842 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6843 return;
6844
6845 /* No bad word or it starts after the cursor: use the word under the
6846 * cursor. */
6847 curwin->w_cursor = prev_cursor;
6848 line = ml_get_curline();
6849 p = line + curwin->w_cursor.col;
6850 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006851 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006852 mb_ptr_back(line, p);
6853 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006854 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006855 mb_ptr_adv(p);
6856
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006857 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006858 {
6859 beep_flush();
6860 return;
6861 }
6862 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006863 }
6864
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006865 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006866
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006867 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006868 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006869
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006870 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006871
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006872 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006873 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6874 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006875
6876 if (sug.su_ga.ga_len == 0)
6877 MSG(_("Sorry, no suggestions"));
6878 else
6879 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006880 vim_free(repl_from);
6881 repl_from = NULL;
6882 vim_free(repl_to);
6883 repl_to = NULL;
6884
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006885#ifdef FEAT_RIGHTLEFT
6886 /* When 'rightleft' is set the list is drawn right-left. */
6887 cmdmsg_rl = curwin->w_p_rl;
6888 if (cmdmsg_rl)
6889 msg_col = Columns - 1;
6890#endif
6891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006892 /* List the suggestions. */
6893 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006894 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006895 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6896 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006897#ifdef FEAT_RIGHTLEFT
6898 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
6899 {
6900 /* And now the rabbit from the high hat: Avoid showing the
6901 * untranslated message rightleft. */
6902 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
6903 sug.su_badlen, sug.su_badptr);
6904 }
6905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006906 msg_puts(IObuff);
6907 msg_clr_eos();
6908 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006910 msg_scroll = TRUE;
6911 for (i = 0; i < sug.su_ga.ga_len; ++i)
6912 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006913 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006914
6915 /* The suggested word may replace only part of the bad word, add
6916 * the not replaced part. */
6917 STRCPY(wcopy, stp->st_word);
6918 if (sug.su_badlen > stp->st_orglen)
6919 vim_strncpy(wcopy + STRLEN(wcopy),
6920 sug.su_badptr + stp->st_orglen,
6921 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006922 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
6923#ifdef FEAT_RIGHTLEFT
6924 if (cmdmsg_rl)
6925 rl_mirror(IObuff);
6926#endif
6927 msg_puts(IObuff);
6928
6929 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006930 msg_puts(IObuff);
6931
6932 /* The word may replace more than "su_badlen". */
6933 if (sug.su_badlen < stp->st_orglen)
6934 {
6935 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6936 stp->st_orglen, sug.su_badptr);
6937 msg_puts(IObuff);
6938 }
6939
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006940 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006941 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006942 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006943 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006944 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006945 stp->st_salscore ? "s " : "",
6946 stp->st_score, stp->st_altscore);
6947 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006948 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00006949 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006950#ifdef FEAT_RIGHTLEFT
6951 if (cmdmsg_rl)
6952 /* Mirror the numbers, but keep the leading space. */
6953 rl_mirror(IObuff + 1);
6954#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00006955 msg_advance(30);
6956 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006958 msg_putchar('\n');
6959 }
6960
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006961#ifdef FEAT_RIGHTLEFT
6962 cmdmsg_rl = FALSE;
6963 msg_col = 0;
6964#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006966 i = prompt_for_number(&mouse_used);
6967 if (mouse_used)
6968 i -= lines_left;
6969
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006970 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006971 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006972 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006973 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006974 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6975 repl_to = vim_strsave(stp->st_word);
6976
6977 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006978 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6979 if (p != NULL)
6980 {
6981 c = sug.su_badptr - line;
6982 mch_memmove(p, line, c);
6983 STRCPY(p + c, stp->st_word);
6984 STRCAT(p, sug.su_badptr + stp->st_orglen);
6985 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6986 curwin->w_cursor.col = c;
6987 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006988
6989 /* For redo we use a change-word command. */
6990 ResetRedobuff();
6991 AppendToRedobuff((char_u *)"ciw");
6992 AppendToRedobuff(stp->st_word);
6993 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006994 }
6995 }
6996 else
6997 curwin->w_cursor = prev_cursor;
6998 }
6999
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007000 spell_find_cleanup(&sug);
7001}
7002
7003/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007004 * Check if the word at line "lnum" column "col" is required to start with a
7005 * capital. This uses 'spellcapcheck' of the current buffer.
7006 */
7007 static int
7008check_need_cap(lnum, col)
7009 linenr_T lnum;
7010 colnr_T col;
7011{
7012 int need_cap = FALSE;
7013 char_u *line;
7014 char_u *line_copy = NULL;
7015 char_u *p;
7016 colnr_T endcol;
7017 regmatch_T regmatch;
7018
7019 if (curbuf->b_cap_prog == NULL)
7020 return FALSE;
7021
7022 line = ml_get_curline();
7023 endcol = 0;
7024 if ((int)(skipwhite(line) - line) >= (int)col)
7025 {
7026 /* At start of line, check if previous line is empty or sentence
7027 * ends there. */
7028 if (lnum == 1)
7029 need_cap = TRUE;
7030 else
7031 {
7032 line = ml_get(lnum - 1);
7033 if (*skipwhite(line) == NUL)
7034 need_cap = TRUE;
7035 else
7036 {
7037 /* Append a space in place of the line break. */
7038 line_copy = concat_str(line, (char_u *)" ");
7039 line = line_copy;
7040 endcol = STRLEN(line);
7041 }
7042 }
7043 }
7044 else
7045 endcol = col;
7046
7047 if (endcol > 0)
7048 {
7049 /* Check if sentence ends before the bad word. */
7050 regmatch.regprog = curbuf->b_cap_prog;
7051 regmatch.rm_ic = FALSE;
7052 p = line + endcol;
7053 for (;;)
7054 {
7055 mb_ptr_back(line, p);
7056 if (p == line || spell_iswordp_nmw(p))
7057 break;
7058 if (vim_regexec(&regmatch, p, 0)
7059 && regmatch.endp[0] == line + endcol)
7060 {
7061 need_cap = TRUE;
7062 break;
7063 }
7064 }
7065 }
7066
7067 vim_free(line_copy);
7068
7069 return need_cap;
7070}
7071
7072
7073/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007074 * ":spellrepall"
7075 */
7076/*ARGSUSED*/
7077 void
7078ex_spellrepall(eap)
7079 exarg_T *eap;
7080{
7081 pos_T pos = curwin->w_cursor;
7082 char_u *frompat;
7083 int addlen;
7084 char_u *line;
7085 char_u *p;
7086 int didone = FALSE;
7087 int save_ws = p_ws;
7088
7089 if (repl_from == NULL || repl_to == NULL)
7090 {
7091 EMSG(_("E752: No previous spell replacement"));
7092 return;
7093 }
7094 addlen = STRLEN(repl_to) - STRLEN(repl_from);
7095
7096 frompat = alloc(STRLEN(repl_from) + 7);
7097 if (frompat == NULL)
7098 return;
7099 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
7100 p_ws = FALSE;
7101
7102 curwin->w_cursor.lnum = 0;
7103 while (!got_int)
7104 {
7105 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
7106 || u_save_cursor() == FAIL)
7107 break;
7108
7109 /* Only replace when the right word isn't there yet. This happens
7110 * when changing "etc" to "etc.". */
7111 line = ml_get_curline();
7112 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
7113 repl_to, STRLEN(repl_to)) != 0)
7114 {
7115 p = alloc(STRLEN(line) + addlen + 1);
7116 if (p == NULL)
7117 break;
7118 mch_memmove(p, line, curwin->w_cursor.col);
7119 STRCPY(p + curwin->w_cursor.col, repl_to);
7120 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
7121 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7122 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7123 didone = TRUE;
7124 }
7125 curwin->w_cursor.col += STRLEN(repl_to);
7126 }
7127
7128 p_ws = save_ws;
7129 curwin->w_cursor = pos;
7130 vim_free(frompat);
7131
7132 if (!didone)
7133 EMSG2(_("E753: Not found: %s"), repl_from);
7134}
7135
7136/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007137 * Find spell suggestions for "word". Return them in the growarray "*gap" as
7138 * a list of allocated strings.
7139 */
7140 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007141spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007142 garray_T *gap;
7143 char_u *word;
7144 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007145 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007146{
7147 suginfo_T sug;
7148 int i;
7149 suggest_T *stp;
7150 char_u *wcopy;
7151
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007152 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007153
7154 /* Make room in "gap". */
7155 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
7156 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
7157 return;
7158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007159 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007160 {
7161 stp = &SUG(sug.su_ga, i);
7162
7163 /* The suggested word may replace only part of "word", add the not
7164 * replaced part. */
7165 wcopy = alloc(STRLEN(stp->st_word)
7166 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
7167 if (wcopy == NULL)
7168 break;
7169 STRCPY(wcopy, stp->st_word);
7170 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
7171 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
7172 }
7173
7174 spell_find_cleanup(&sug);
7175}
7176
7177/*
7178 * Find spell suggestions for the word at the start of "badptr".
7179 * Return the suggestions in "su->su_ga".
7180 * The maximum number of suggestions is "maxcount".
7181 * Note: does use info for the current window.
7182 * This is based on the mechanisms of Aspell, but completely reimplemented.
7183 */
7184 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007185spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007186 char_u *badptr;
7187 suginfo_T *su;
7188 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00007189 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007190 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007191{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007192 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007193 char_u buf[MAXPATHL];
7194 char_u *p;
7195 int do_combine = FALSE;
7196 char_u *sps_copy;
7197#ifdef FEAT_EVAL
7198 static int expr_busy = FALSE;
7199#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007200 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007201
7202 /*
7203 * Set the info in "*su".
7204 */
7205 vim_memset(su, 0, sizeof(suginfo_T));
7206 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
7207 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00007208 if (*badptr == NUL)
7209 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007210 hash_init(&su->su_banned);
7211
7212 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007213 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007214 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007215 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007216
7217 if (su->su_badlen >= MAXWLEN)
7218 su->su_badlen = MAXWLEN - 1; /* just in case */
7219 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
7220 (void)spell_casefold(su->su_badptr, su->su_badlen,
7221 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007222 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007223 su->su_badflags = badword_captype(su->su_badptr,
7224 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007225 if (need_cap)
7226 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007227
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007228 /* If the word is not capitalised and spell_check() doesn't consider the
7229 * word to be bad then it might need to be capitalised. Add a suggestion
7230 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007231 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007232 if (!SPELL_ISUPPER(c) && attr == 0)
7233 {
7234 make_case_word(su->su_badword, buf, WF_ONECAP);
7235 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
7236 0, TRUE);
7237 }
7238
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007239 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00007240 if (banbadword)
7241 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007242
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007243 /* Make a copy of 'spellsuggest', because the expression may change it. */
7244 sps_copy = vim_strsave(p_sps);
7245 if (sps_copy == NULL)
7246 return;
7247
7248 /* Loop over the items in 'spellsuggest'. */
7249 for (p = sps_copy; *p != NUL; )
7250 {
7251 copy_option_part(&p, buf, MAXPATHL, ",");
7252
7253 if (STRNCMP(buf, "expr:", 5) == 0)
7254 {
7255#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007256 /* Evaluate an expression. Skip this when called recursively,
7257 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007258 if (!expr_busy)
7259 {
7260 expr_busy = TRUE;
7261 spell_suggest_expr(su, buf + 5);
7262 expr_busy = FALSE;
7263 }
7264#endif
7265 }
7266 else if (STRNCMP(buf, "file:", 5) == 0)
7267 /* Use list of suggestions in a file. */
7268 spell_suggest_file(su, buf + 5);
7269 else
7270 {
7271 /* Use internal method. */
7272 spell_suggest_intern(su);
7273 if (sps_flags & SPS_DOUBLE)
7274 do_combine = TRUE;
7275 }
7276 }
7277
7278 vim_free(sps_copy);
7279
7280 if (do_combine)
7281 /* Combine the two list of suggestions. This must be done last,
7282 * because sorting changes the order again. */
7283 score_combine(su);
7284}
7285
7286#ifdef FEAT_EVAL
7287/*
7288 * Find suggestions by evaluating expression "expr".
7289 */
7290 static void
7291spell_suggest_expr(su, expr)
7292 suginfo_T *su;
7293 char_u *expr;
7294{
7295 list_T *list;
7296 listitem_T *li;
7297 int score;
7298 char_u *p;
7299
7300 /* The work is split up in a few parts to avoid having to export
7301 * suginfo_T.
7302 * First evaluate the expression and get the resulting list. */
7303 list = eval_spell_expr(su->su_badword, expr);
7304 if (list != NULL)
7305 {
7306 /* Loop over the items in the list. */
7307 for (li = list->lv_first; li != NULL; li = li->li_next)
7308 if (li->li_tv.v_type == VAR_LIST)
7309 {
7310 /* Get the word and the score from the items. */
7311 score = get_spellword(li->li_tv.vval.v_list, &p);
7312 if (score >= 0)
7313 add_suggestion(su, &su->su_ga, p,
7314 su->su_badlen, score, 0, TRUE);
7315 }
7316 list_unref(list);
7317 }
7318
7319 /* Sort the suggestions and truncate at "maxcount". */
7320 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7321}
7322#endif
7323
7324/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007325 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007326 */
7327 static void
7328spell_suggest_file(su, fname)
7329 suginfo_T *su;
7330 char_u *fname;
7331{
7332 FILE *fd;
7333 char_u line[MAXWLEN * 2];
7334 char_u *p;
7335 int len;
7336 char_u cword[MAXWLEN];
7337
7338 /* Open the file. */
7339 fd = mch_fopen((char *)fname, "r");
7340 if (fd == NULL)
7341 {
7342 EMSG2(_(e_notopen), fname);
7343 return;
7344 }
7345
7346 /* Read it line by line. */
7347 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
7348 {
7349 line_breakcheck();
7350
7351 p = vim_strchr(line, '/');
7352 if (p == NULL)
7353 continue; /* No Tab found, just skip the line. */
7354 *p++ = NUL;
7355 if (STRICMP(su->su_badword, line) == 0)
7356 {
7357 /* Match! Isolate the good word, until CR or NL. */
7358 for (len = 0; p[len] >= ' '; ++len)
7359 ;
7360 p[len] = NUL;
7361
7362 /* If the suggestion doesn't have specific case duplicate the case
7363 * of the bad word. */
7364 if (captype(p, NULL) == 0)
7365 {
7366 make_case_word(p, cword, su->su_badflags);
7367 p = cword;
7368 }
7369
7370 add_suggestion(su, &su->su_ga, p, su->su_badlen,
7371 SCORE_FILE, 0, TRUE);
7372 }
7373 }
7374
7375 fclose(fd);
7376
7377 /* Sort the suggestions and truncate at "maxcount". */
7378 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7379}
7380
7381/*
7382 * Find suggestions for the internal method indicated by "sps_flags".
7383 */
7384 static void
7385spell_suggest_intern(su)
7386 suginfo_T *su;
7387{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007388 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007389 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007390 *
7391 * Set a maximum score to limit the combination of operations that is
7392 * tried.
7393 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007394 suggest_try_special(su);
7395
7396 /*
7397 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
7398 * from the .aff file and inserting a space (split the word).
7399 */
7400 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007401
7402 /* For the resulting top-scorers compute the sound-a-like score. */
7403 if (sps_flags & SPS_DOUBLE)
7404 score_comp_sal(su);
7405
7406 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007407 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007408 *
7409 * Only do this when we don't have a lot of suggestions yet, because it's
7410 * very slow and often doesn't find new suggestions.
7411 */
7412 if ((sps_flags & SPS_DOUBLE)
7413 || (!(sps_flags & SPS_FAST)
7414 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
7415 {
7416 /* Allow a higher score now. */
7417 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007418 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007419 }
7420
7421 /* When CTRL-C was hit while searching do show the results. */
7422 ui_breakcheck();
7423 if (got_int)
7424 {
7425 (void)vgetc();
7426 got_int = FALSE;
7427 }
7428
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007429 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007430 {
7431 if (sps_flags & SPS_BEST)
7432 /* Adjust the word score for how it sounds like. */
7433 rescore_suggestions(su);
7434
7435 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007436 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007437 }
7438}
7439
7440/*
7441 * Free the info put in "*su" by spell_find_suggest().
7442 */
7443 static void
7444spell_find_cleanup(su)
7445 suginfo_T *su;
7446{
7447 int i;
7448
7449 /* Free the suggestions. */
7450 for (i = 0; i < su->su_ga.ga_len; ++i)
7451 vim_free(SUG(su->su_ga, i).st_word);
7452 ga_clear(&su->su_ga);
7453 for (i = 0; i < su->su_sga.ga_len; ++i)
7454 vim_free(SUG(su->su_sga, i).st_word);
7455 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007456
7457 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007458 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007459}
7460
7461/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007462 * Make a copy of "word", with the first letter upper or lower cased, to
7463 * "wcopy[MAXWLEN]". "word" must not be empty.
7464 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007465 */
7466 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007467onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007468 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007469 char_u *wcopy;
7470 int upper; /* TRUE: first letter made upper case */
7471{
7472 char_u *p;
7473 int c;
7474 int l;
7475
7476 p = word;
7477#ifdef FEAT_MBYTE
7478 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007479 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007480 else
7481#endif
7482 c = *p++;
7483 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007484 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007485 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007486 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007487#ifdef FEAT_MBYTE
7488 if (has_mbyte)
7489 l = mb_char2bytes(c, wcopy);
7490 else
7491#endif
7492 {
7493 l = 1;
7494 wcopy[0] = c;
7495 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007496 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497}
7498
7499/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007500 * Make a copy of "word" with all the letters upper cased into
7501 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007502 */
7503 static void
7504allcap_copy(word, wcopy)
7505 char_u *word;
7506 char_u *wcopy;
7507{
7508 char_u *s;
7509 char_u *d;
7510 int c;
7511
7512 d = wcopy;
7513 for (s = word; *s != NUL; )
7514 {
7515#ifdef FEAT_MBYTE
7516 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007517 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 else
7519#endif
7520 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007521 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007522
7523#ifdef FEAT_MBYTE
7524 if (has_mbyte)
7525 {
7526 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
7527 break;
7528 d += mb_char2bytes(c, d);
7529 }
7530 else
7531#endif
7532 {
7533 if (d - wcopy >= MAXWLEN - 1)
7534 break;
7535 *d++ = c;
7536 }
7537 }
7538 *d = NUL;
7539}
7540
7541/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007542 * Try finding suggestions by recognizing specific situations.
7543 */
7544 static void
7545suggest_try_special(su)
7546 suginfo_T *su;
7547{
7548 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007549 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007550 int c;
7551 char_u word[MAXWLEN];
7552
7553 /*
7554 * Recognize a word that is repeated: "the the".
7555 */
7556 p = skiptowhite(su->su_fbadword);
7557 len = p - su->su_fbadword;
7558 p = skipwhite(p);
7559 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
7560 {
7561 /* Include badflags: if the badword is onecap or allcap
7562 * use that for the goodword too: "The the" -> "The". */
7563 c = su->su_fbadword[len];
7564 su->su_fbadword[len] = NUL;
7565 make_case_word(su->su_fbadword, word, su->su_badflags);
7566 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007567 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007568 }
7569}
7570
7571/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007572 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00007573 *
7574 * This uses a state machine. At each node in the tree we try various
7575 * operations. When trying if an operation work "depth" is increased and the
7576 * stack[] is used to store info. This allows combinations, thus insert one
7577 * character, replace one and delete another. The number of changes is
7578 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007579 */
7580 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007581suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007582 suginfo_T *su;
7583{
7584 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
7585 char_u tword[MAXWLEN]; /* good word collected so far */
7586 trystate_T stack[MAXWLEN];
7587 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
7588 * to for word split) */
7589 char_u prewordlen = 0; /* length of word in "preword" */
7590 int splitoff = 0; /* index in tword after last split */
7591 trystate_T *sp;
7592 int newscore;
7593 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007594 char_u *byts, *fbyts, *pbyts;
7595 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007596 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007597 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007598 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007600 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007601 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007602 int len;
7603 char_u *p;
7604 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00007605 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007606 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007607
7608 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00007609 * to find matches (esp. REP items). Append some more text, changing
7610 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007611 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007612 n = STRLEN(fword);
7613 p = su->su_badptr + su->su_badlen;
7614 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007615
7616 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7617 lp->lp_slang != NULL; ++lp)
7618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007619 /*
7620 * Go through the whole case-fold tree, try changes at each node.
7621 * "tword[]" contains the word collected from nodes in the tree.
7622 * "fword[]" the word we are trying to match with (initially the bad
7623 * word).
7624 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007625 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007626 sp = &stack[0];
7627 sp->ts_state = STATE_START;
7628 sp->ts_score = 0;
7629 sp->ts_curi = 1;
7630 sp->ts_fidx = 0;
7631 sp->ts_fidxtry = 0;
7632 sp->ts_twordlen = 0;
7633 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007634#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007635 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007636#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007637
Bram Moolenaarea424162005-06-16 21:51:00 +00007638 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007639 * When there are postponed prefixes we need to use these first. At
7640 * the end of the prefix we continue in the case-fold tree.
7641 */
7642 fbyts = lp->lp_slang->sl_fbyts;
7643 fidxs = lp->lp_slang->sl_fidxs;
7644 pbyts = lp->lp_slang->sl_pbyts;
7645 pidxs = lp->lp_slang->sl_pidxs;
7646 if (pbyts != NULL)
7647 {
7648 byts = pbyts;
7649 idxs = pidxs;
7650 sp->ts_prefixdepth = PREFIXTREE;
7651 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
7652 }
7653 else
7654 {
7655 byts = fbyts;
7656 idxs = fidxs;
7657 sp->ts_prefixdepth = NOPREFIX;
7658 }
7659
7660 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007661 * Loop to find all suggestions. At each round we either:
7662 * - For the current state try one operation, advance "ts_curi",
7663 * increase "depth".
7664 * - When a state is done go to the next, set "ts_state".
7665 * - When all states are tried decrease "depth".
7666 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667 while (depth >= 0 && !got_int)
7668 {
7669 sp = &stack[depth];
7670 switch (sp->ts_state)
7671 {
7672 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007673 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007674 /*
7675 * Start of node: Deal with NUL bytes, which means
7676 * tword[] may end here.
7677 */
7678 arridx = sp->ts_arridx; /* current node in the tree */
7679 len = byts[arridx]; /* bytes in this node */
7680 arridx += sp->ts_curi; /* index of current byte */
7681
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007682 if (sp->ts_prefixdepth == PREFIXTREE)
7683 {
7684 /* Skip over the NUL bytes, we use them later. */
7685 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7686 ;
7687 sp->ts_curi += n;
7688
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007689 /* Always past NUL bytes now. */
7690 n = (int)sp->ts_state;
7691 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007692 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007693
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007694 /* At end of a prefix or at start of prefixtree: check for
7695 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007696 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007697 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007698 /* Set su->su_badflags to the caps type at this
7699 * position. Use the caps type until here for the
7700 * prefix itself. */
7701#ifdef FEAT_MBYTE
7702 if (has_mbyte)
7703 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7704 else
7705#endif
7706 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007707 flags = badword_captype(su->su_badptr,
7708 su->su_badptr + n);
7709 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007710 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007711 ++depth;
7712 stack[depth] = stack[depth - 1];
7713 sp = &stack[depth];
7714 sp->ts_prefixdepth = depth - 1;
7715 byts = fbyts;
7716 idxs = fidxs;
7717 sp->ts_state = STATE_START;
7718 sp->ts_curi = 1; /* start just after length byte */
7719 sp->ts_arridx = 0;
7720
Bram Moolenaar53805d12005-08-01 07:08:33 +00007721 /* Move the prefix to preword[] with the right case
7722 * and make find_keepcap_word() works. */
7723 splitoff = sp->ts_twordlen;
7724 tword[splitoff] = NUL;
7725 make_case_word(tword, preword, flags);
7726 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007727 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007728 break;
7729 }
7730
Bram Moolenaar0c405862005-06-22 22:26:26 +00007731 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007732 {
7733 /* Past bytes in node and/or past NUL bytes. */
7734 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007735 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007736 break;
7737 }
7738
7739 /*
7740 * End of word in tree.
7741 */
7742 ++sp->ts_curi; /* eat one NUL byte */
7743
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007744 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007745
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007746 if (sp->ts_prefixdepth < MAXWLEN)
7747 {
7748 /* There was a prefix before the word. Check that the
7749 * prefix can be used with this word. */
7750 /* Count the length of the NULs in the prefix. If there
7751 * are none this must be the first try without a prefix.
7752 */
7753 n = stack[sp->ts_prefixdepth].ts_arridx;
7754 len = pbyts[n++];
7755 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7756 ;
7757 if (c > 0)
7758 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007759 /* The prefix ID is stored three bytes above the
7760 * flags. */
7761 c = valid_word_prefix(c, n, flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007762 tword + splitoff, lp->lp_slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007763 if (c == 0)
7764 break;
7765
7766 /* Use the WF_RARE flag for a rare prefix. */
7767 if (c & WF_RAREPFX)
7768 flags |= WF_RARE;
7769 }
7770 }
7771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772 /*
7773 * Form the word with proper case in preword.
7774 * If there is a word from a previous split, append.
7775 */
7776 tword[sp->ts_twordlen] = NUL;
7777 if (flags & WF_KEEPCAP)
7778 /* Must find the word in the keep-case tree. */
7779 find_keepcap_word(lp->lp_slang, tword + splitoff,
7780 preword + prewordlen);
7781 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007784 * use that for the goodword too. But if the badword is
7785 * allcap and it's only one char long use onecap. */
7786 c = su->su_badflags;
7787 if ((c & WF_ALLCAP)
7788#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007789 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007790#else
7791 && su->su_badlen == 1
7792#endif
7793 )
7794 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007795 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007796 preword + prewordlen, flags | c);
7797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007798
7799 /* Don't use a banned word. It may appear again as a good
7800 * word, thus remember it. */
7801 if (flags & WF_BANNED)
7802 {
7803 add_banned(su, preword + prewordlen);
7804 break;
7805 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007806 if (was_banned(su, preword + prewordlen)
7807 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007808 break;
7809
7810 newscore = 0;
7811 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007812 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007813 newscore += SCORE_REGION;
7814 if (flags & WF_RARE)
7815 newscore += SCORE_RARE;
7816
Bram Moolenaar0c405862005-06-22 22:26:26 +00007817 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007818 captype(preword + prewordlen, NULL)))
7819 newscore += SCORE_ICASE;
7820
Bram Moolenaar0c405862005-06-22 22:26:26 +00007821 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007822 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007823 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007824 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007825 /* The badword also ends: add suggestions. Give a penalty
7826 * when changing non-word char to word char, e.g., "thes,"
7827 * -> "these". */
7828 p = fword + sp->ts_fidx;
7829#ifdef FEAT_MBYTE
7830 if (has_mbyte)
7831 mb_ptr_back(fword, p);
7832 else
7833#endif
7834 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007835 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007836 {
7837 p = preword + STRLEN(preword);
7838#ifdef FEAT_MBYTE
7839 if (has_mbyte)
7840 mb_ptr_back(preword, p);
7841 else
7842#endif
7843 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007844 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007845 newscore += SCORE_NONWORD;
7846 }
7847
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007848 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007849 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007850 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007851 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007852 else if (sp->ts_fidx >= sp->ts_fidxtry
7853#ifdef FEAT_MBYTE
7854 /* Don't split halfway a character. */
7855 && (!has_mbyte || sp->ts_tcharlen == 0)
7856#endif
7857 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858 {
7859 /* The word in the tree ends but the badword
7860 * continues: try inserting a space and check that a valid
7861 * words starts at fword[sp->ts_fidx]. */
7862 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7863 {
7864 /* Save things to be restored at STATE_SPLITUNDO. */
7865 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007866 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007867 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007868 sp->ts_state = STATE_SPLITUNDO;
7869
7870 ++depth;
7871 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872
7873 /* Append a space to preword. */
7874 STRCAT(preword, " ");
7875 prewordlen = STRLEN(preword);
7876 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007877
7878 /* If the badword has a non-word character at this
7879 * position skip it. That means replacing the
7880 * non-word character with a space. */
7881 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7882 {
7883 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7884#ifdef FEAT_MBYTE
7885 if (has_mbyte)
7886 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7887 else
7888#endif
7889 ++sp->ts_fidx;
7890 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00007891
7892 /* set su->su_badflags to the caps type at this
7893 * position */
7894
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007895#ifdef FEAT_MBYTE
7896 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00007897 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007898 else
7899#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00007900 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007901 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007902 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007904 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007905 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007906 }
7907 }
7908 break;
7909
7910 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007911 /* Undo the changes done for word split. */
7912 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007913 splitoff = sp->ts_save_splitoff;
7914 prewordlen = sp->ts_save_prewordlen;
7915
7916 /* Continue looking for NUL bytes. */
7917 sp->ts_state = STATE_START;
7918 break;
7919
7920 case STATE_ENDNUL:
7921 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007922 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007923 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007924 {
7925 /* The badword ends, can't use the bytes in this node. */
7926 sp->ts_state = STATE_DEL;
7927 break;
7928 }
7929 sp->ts_state = STATE_PLAIN;
7930 /*FALLTHROUGH*/
7931
7932 case STATE_PLAIN:
7933 /*
7934 * Go over all possible bytes at this node, add each to
7935 * tword[] and use child node. "ts_curi" is the index.
7936 */
7937 arridx = sp->ts_arridx;
7938 if (sp->ts_curi > byts[arridx])
7939 {
7940 /* Done all bytes at this node, do next state. When still
7941 * at already changed bytes skip the other tricks. */
7942 if (sp->ts_fidx >= sp->ts_fidxtry)
7943 sp->ts_state = STATE_DEL;
7944 else
7945 sp->ts_state = STATE_FINAL;
7946 }
7947 else
7948 {
7949 arridx += sp->ts_curi++;
7950 c = byts[arridx];
7951
7952 /* Normal byte, go one level deeper. If it's not equal to
7953 * the byte in the bad word adjust the score. But don't
7954 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007955 if (c == fword[sp->ts_fidx]
7956#ifdef FEAT_MBYTE
7957 || (sp->ts_tcharlen > 0
7958 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007959#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007960 )
7961 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007962 else
7963 newscore = SCORE_SUBST;
7964 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7965 && try_deeper(su, stack, depth, newscore))
7966 {
7967 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007968 sp = &stack[depth];
7969 ++sp->ts_fidx;
7970 tword[sp->ts_twordlen++] = c;
7971 sp->ts_arridx = idxs[arridx];
7972#ifdef FEAT_MBYTE
7973 if (newscore == SCORE_SUBST)
7974 sp->ts_isdiff = DIFF_YES;
7975 if (has_mbyte)
7976 {
7977 /* Multi-byte characters are a bit complicated to
7978 * handle: They differ when any of the bytes
7979 * differ and then their length may also differ. */
7980 if (sp->ts_tcharlen == 0)
7981 {
7982 /* First byte. */
7983 sp->ts_tcharidx = 0;
7984 sp->ts_tcharlen = MB_BYTE2LEN(c);
7985 sp->ts_fcharstart = sp->ts_fidx - 1;
7986 sp->ts_isdiff = (newscore != 0)
7987 ? DIFF_YES : DIFF_NONE;
7988 }
7989 else if (sp->ts_isdiff == DIFF_INSERT)
7990 /* When inserting trail bytes don't advance in
7991 * the bad word. */
7992 --sp->ts_fidx;
7993 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7994 {
7995 /* Last byte of character. */
7996 if (sp->ts_isdiff == DIFF_YES)
7997 {
7998 /* Correct ts_fidx for the byte length of
7999 * the character (we didn't check that
8000 * before). */
8001 sp->ts_fidx = sp->ts_fcharstart
8002 + MB_BYTE2LEN(
8003 fword[sp->ts_fcharstart]);
8004
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008005 /* For changing a composing character
8006 * adjust the score from SCORE_SUBST to
8007 * SCORE_SUBCOMP. */
8008 if (enc_utf8
8009 && utf_iscomposing(
8010 mb_ptr2char(tword
8011 + sp->ts_twordlen
8012 - sp->ts_tcharlen))
8013 && utf_iscomposing(
8014 mb_ptr2char(fword
8015 + sp->ts_fcharstart)))
8016 sp->ts_score -=
8017 SCORE_SUBST - SCORE_SUBCOMP;
8018
Bram Moolenaarea424162005-06-16 21:51:00 +00008019 /* For a similar character adjust score
8020 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008021 else if (lp->lp_slang->sl_has_map
Bram Moolenaarea424162005-06-16 21:51:00 +00008022 && similar_chars(lp->lp_slang,
8023 mb_ptr2char(tword
8024 + sp->ts_twordlen
8025 - sp->ts_tcharlen),
8026 mb_ptr2char(fword
8027 + sp->ts_fcharstart)))
8028 sp->ts_score -=
8029 SCORE_SUBST - SCORE_SIMILAR;
8030 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008031 else if (sp->ts_isdiff == DIFF_INSERT
8032 && sp->ts_twordlen > sp->ts_tcharlen)
8033 {
Bram Moolenaarea408852005-06-25 22:49:46 +00008034 p = tword + sp->ts_twordlen
8035 - sp->ts_tcharlen;
8036 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008037 if (enc_utf8 && utf_iscomposing(c))
8038 {
8039 /* Inserting a composing char doesn't
8040 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008041 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008042 - SCORE_INSCOMP;
8043 }
8044 else
8045 {
8046 /* If the previous character was the
8047 * same, thus doubling a character,
8048 * give a bonus to the score. */
8049 mb_ptr_back(tword, p);
8050 if (c == mb_ptr2char(p))
8051 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00008052 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008053 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008054 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008055
8056 /* Starting a new char, reset the length. */
8057 sp->ts_tcharlen = 0;
8058 }
8059 }
8060 else
8061#endif
8062 {
8063 /* If we found a similar char adjust the score.
8064 * We do this after calling try_deeper() because
8065 * it's slow. */
8066 if (newscore != 0
8067 && lp->lp_slang->sl_has_map
8068 && similar_chars(lp->lp_slang,
8069 c, fword[sp->ts_fidx - 1]))
8070 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
8071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008072 }
8073 }
8074 break;
8075
8076 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00008077#ifdef FEAT_MBYTE
8078 /* When past the first byte of a multi-byte char don't try
8079 * delete/insert/swap a character. */
8080 if (has_mbyte && sp->ts_tcharlen > 0)
8081 {
8082 sp->ts_state = STATE_FINAL;
8083 break;
8084 }
8085#endif
8086 /*
8087 * Try skipping one character in the bad word (delete it).
8088 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 sp->ts_state = STATE_INS;
8090 sp->ts_curi = 1;
8091 if (fword[sp->ts_fidx] != NUL
8092 && try_deeper(su, stack, depth, SCORE_DEL))
8093 {
8094 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00008095
8096 /* Advance over the character in fword[]. Give a bonus to
8097 * the score if the same character is following "nn" ->
8098 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00008099#ifdef FEAT_MBYTE
8100 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00008101 {
8102 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00008103 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008104 if (enc_utf8 && utf_iscomposing(c))
8105 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
8106 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00008107 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
8108 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008109 else
8110#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00008111 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008112 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00008113 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
8114 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
8115 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 break;
8117 }
8118 /*FALLTHROUGH*/
8119
8120 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00008121 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 * node. */
8123 n = sp->ts_arridx;
8124 if (sp->ts_curi > byts[n])
8125 {
8126 /* Done all bytes at this node, do next state. */
8127 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008128 }
8129 else
8130 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008131 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008132 n += sp->ts_curi++;
8133 c = byts[n];
8134 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
8135 {
8136 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008137 sp = &stack[depth];
8138 tword[sp->ts_twordlen++] = c;
8139 sp->ts_arridx = idxs[n];
8140#ifdef FEAT_MBYTE
8141 if (has_mbyte)
8142 {
8143 fl = MB_BYTE2LEN(c);
8144 if (fl > 1)
8145 {
8146 /* There are following bytes for the same
8147 * character. We must find all bytes before
8148 * trying delete/insert/swap/etc. */
8149 sp->ts_tcharlen = fl;
8150 sp->ts_tcharidx = 1;
8151 sp->ts_isdiff = DIFF_INSERT;
8152 }
8153 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008154 else
8155 fl = 1;
8156 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00008157#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00008158 {
8159 /* If the previous character was the same, thus
8160 * doubling a character, give a bonus to the
8161 * score. */
8162 if (sp->ts_twordlen >= 2
8163 && tword[sp->ts_twordlen - 2] == c)
8164 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
8165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008166 }
8167 }
8168 break;
8169
8170 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00008171 /*
8172 * Swap two bytes in the bad word: "12" -> "21".
8173 * We change "fword" here, it's changed back afterwards.
8174 */
8175 p = fword + sp->ts_fidx;
8176 c = *p;
8177 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008179 /* End of word, can't swap or replace. */
8180 sp->ts_state = STATE_FINAL;
8181 break;
8182 }
8183#ifdef FEAT_MBYTE
8184 if (has_mbyte)
8185 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008186 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008187 c = mb_ptr2char(p);
8188 c2 = mb_ptr2char(p + n);
8189 }
8190 else
8191#endif
8192 c2 = p[1];
8193 if (c == c2)
8194 {
8195 /* Characters are identical, swap won't do anything. */
8196 sp->ts_state = STATE_SWAP3;
8197 break;
8198 }
8199 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
8200 {
8201 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008202 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008203#ifdef FEAT_MBYTE
8204 if (has_mbyte)
8205 {
8206 fl = mb_char2len(c2);
8207 mch_memmove(p, p + n, fl);
8208 mb_char2bytes(c, p + fl);
8209 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8210 }
8211 else
8212#endif
8213 {
8214 p[0] = c2;
8215 p[1] = c;
8216 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
8217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008218 }
8219 else
8220 /* If this swap doesn't work then SWAP3 won't either. */
8221 sp->ts_state = STATE_REP_INI;
8222 break;
8223
Bram Moolenaarea424162005-06-16 21:51:00 +00008224 case STATE_UNSWAP:
8225 /* Undo the STATE_SWAP swap: "21" -> "12". */
8226 p = fword + sp->ts_fidx;
8227#ifdef FEAT_MBYTE
8228 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008229 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008230 n = MB_BYTE2LEN(*p);
8231 c = mb_ptr2char(p + n);
8232 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
8233 mb_char2bytes(c, p);
8234 }
8235 else
8236#endif
8237 {
8238 c = *p;
8239 *p = p[1];
8240 p[1] = c;
8241 }
8242 /*FALLTHROUGH*/
8243
8244 case STATE_SWAP3:
8245 /* Swap two bytes, skipping one: "123" -> "321". We change
8246 * "fword" here, it's changed back afterwards. */
8247 p = fword + sp->ts_fidx;
8248#ifdef FEAT_MBYTE
8249 if (has_mbyte)
8250 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008251 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008252 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008253 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008254 c2 = mb_ptr2char(p + n);
8255 c3 = mb_ptr2char(p + n + fl);
8256 }
8257 else
8258#endif
8259 {
8260 c = *p;
8261 c2 = p[1];
8262 c3 = p[2];
8263 }
8264
8265 /* When characters are identical: "121" then SWAP3 result is
8266 * identical, ROT3L result is same as SWAP: "211", ROT3L
8267 * result is same as SWAP on next char: "112". Thus skip all
8268 * swapping. Also skip when c3 is NUL. */
8269 if (c == c3 || c3 == NUL)
8270 {
8271 sp->ts_state = STATE_REP_INI;
8272 break;
8273 }
8274 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8275 {
8276 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008277 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008278#ifdef FEAT_MBYTE
8279 if (has_mbyte)
8280 {
8281 tl = mb_char2len(c3);
8282 mch_memmove(p, p + n + fl, tl);
8283 mb_char2bytes(c2, p + tl);
8284 mb_char2bytes(c, p + fl + tl);
8285 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
8286 }
8287 else
8288#endif
8289 {
8290 p[0] = p[2];
8291 p[2] = c;
8292 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008294 }
8295 else
8296 sp->ts_state = STATE_REP_INI;
8297 break;
8298
Bram Moolenaarea424162005-06-16 21:51:00 +00008299 case STATE_UNSWAP3:
8300 /* Undo STATE_SWAP3: "321" -> "123" */
8301 p = fword + sp->ts_fidx;
8302#ifdef FEAT_MBYTE
8303 if (has_mbyte)
8304 {
8305 n = MB_BYTE2LEN(*p);
8306 c2 = mb_ptr2char(p + n);
8307 fl = MB_BYTE2LEN(p[n]);
8308 c = mb_ptr2char(p + n + fl);
8309 tl = MB_BYTE2LEN(p[n + fl]);
8310 mch_memmove(p + fl + tl, p, n);
8311 mb_char2bytes(c, p);
8312 mb_char2bytes(c2, p + tl);
8313 }
8314 else
8315#endif
8316 {
8317 c = *p;
8318 *p = p[2];
8319 p[2] = c;
8320 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008321
Bram Moolenaarea424162005-06-16 21:51:00 +00008322 /* Rotate three characters left: "123" -> "231". We change
8323 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8325 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008326 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008328 p = fword + sp->ts_fidx;
8329#ifdef FEAT_MBYTE
8330 if (has_mbyte)
8331 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008332 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008333 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008334 fl = mb_cptr2len(p + n);
8335 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00008336 mch_memmove(p, p + n, fl);
8337 mb_char2bytes(c, p + fl);
8338 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8339 }
8340 else
8341#endif
8342 {
8343 c = *p;
8344 *p = p[1];
8345 p[1] = p[2];
8346 p[2] = c;
8347 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008349 }
8350 else
8351 sp->ts_state = STATE_REP_INI;
8352 break;
8353
Bram Moolenaarea424162005-06-16 21:51:00 +00008354 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008355 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008356 p = fword + sp->ts_fidx;
8357#ifdef FEAT_MBYTE
8358 if (has_mbyte)
8359 {
8360 n = MB_BYTE2LEN(*p);
8361 n += MB_BYTE2LEN(p[n]);
8362 c = mb_ptr2char(p + n);
8363 tl = MB_BYTE2LEN(p[n]);
8364 mch_memmove(p + tl, p, n);
8365 mb_char2bytes(c, p);
8366 }
8367 else
8368#endif
8369 {
8370 c = p[2];
8371 p[2] = p[1];
8372 p[1] = *p;
8373 *p = c;
8374 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008376 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00008377 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8379 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008380 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008381 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008382 p = fword + sp->ts_fidx;
8383#ifdef FEAT_MBYTE
8384 if (has_mbyte)
8385 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008386 n = mb_cptr2len(p);
8387 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008388 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008389 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008390 mch_memmove(p + tl, p, n);
8391 mb_char2bytes(c, p);
8392 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
8393 }
8394 else
8395#endif
8396 {
8397 c = p[2];
8398 p[2] = p[1];
8399 p[1] = *p;
8400 *p = c;
8401 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008403 }
8404 else
8405 sp->ts_state = STATE_REP_INI;
8406 break;
8407
Bram Moolenaarea424162005-06-16 21:51:00 +00008408 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008409 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008410 p = fword + sp->ts_fidx;
8411#ifdef FEAT_MBYTE
8412 if (has_mbyte)
8413 {
8414 c = mb_ptr2char(p);
8415 tl = MB_BYTE2LEN(*p);
8416 n = MB_BYTE2LEN(p[tl]);
8417 n += MB_BYTE2LEN(p[tl + n]);
8418 mch_memmove(p, p + tl, n);
8419 mb_char2bytes(c, p + n);
8420 }
8421 else
8422#endif
8423 {
8424 c = *p;
8425 *p = p[1];
8426 p[1] = p[2];
8427 p[2] = c;
8428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008429 /*FALLTHROUGH*/
8430
8431 case STATE_REP_INI:
8432 /* Check if matching with REP items from the .aff file would
8433 * work. Quickly skip if there are no REP items or the score
8434 * is going to be too high anyway. */
8435 gap = &lp->lp_slang->sl_rep;
8436 if (gap->ga_len == 0
8437 || sp->ts_score + SCORE_REP >= su->su_maxscore)
8438 {
8439 sp->ts_state = STATE_FINAL;
8440 break;
8441 }
8442
8443 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00008444 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
8446 if (sp->ts_curi < 0)
8447 {
8448 sp->ts_state = STATE_FINAL;
8449 break;
8450 }
8451
8452 sp->ts_state = STATE_REP;
8453 /*FALLTHROUGH*/
8454
8455 case STATE_REP:
8456 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00008457 * match replace the characters and check if the resulting
8458 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008459 p = fword + sp->ts_fidx;
8460
8461 gap = &lp->lp_slang->sl_rep;
8462 while (sp->ts_curi < gap->ga_len)
8463 {
8464 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
8465 if (*ftp->ft_from != *p)
8466 {
8467 /* past possible matching entries */
8468 sp->ts_curi = gap->ga_len;
8469 break;
8470 }
8471 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
8472 && try_deeper(su, stack, depth, SCORE_REP))
8473 {
8474 /* Need to undo this afterwards. */
8475 sp->ts_state = STATE_REP_UNDO;
8476
8477 /* Change the "from" to the "to" string. */
8478 ++depth;
8479 fl = STRLEN(ftp->ft_from);
8480 tl = STRLEN(ftp->ft_to);
8481 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008484 repextra += tl - fl;
8485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008486 mch_memmove(p, ftp->ft_to, tl);
8487 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00008488#ifdef FEAT_MBYTE
8489 stack[depth].ts_tcharlen = 0;
8490#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 break;
8492 }
8493 }
8494
8495 if (sp->ts_curi >= gap->ga_len)
8496 /* No (more) matches. */
8497 sp->ts_state = STATE_FINAL;
8498
8499 break;
8500
8501 case STATE_REP_UNDO:
8502 /* Undo a REP replacement and continue with the next one. */
8503 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
8504 + sp->ts_curi - 1;
8505 fl = STRLEN(ftp->ft_from);
8506 tl = STRLEN(ftp->ft_to);
8507 p = fword + sp->ts_fidx;
8508 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008511 repextra -= tl - fl;
8512 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 mch_memmove(p, ftp->ft_from, fl);
8514 sp->ts_state = STATE_REP;
8515 break;
8516
8517 default:
8518 /* Did all possible states at this level, go up one level. */
8519 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008520
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008521 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
8522 {
8523 /* Continue in or go back to the prefix tree. */
8524 byts = pbyts;
8525 idxs = pidxs;
8526 splitoff = 0;
8527 }
8528
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008529 /* Don't check for CTRL-C too often, it takes time. */
8530 line_breakcheck();
8531 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008532 }
8533 }
8534}
8535
8536/*
8537 * Try going one level deeper in the tree.
8538 */
8539 static int
8540try_deeper(su, stack, depth, score_add)
8541 suginfo_T *su;
8542 trystate_T *stack;
8543 int depth;
8544 int score_add;
8545{
8546 int newscore;
8547
8548 /* Refuse to go deeper if the scrore is getting too big. */
8549 newscore = stack[depth].ts_score + score_add;
8550 if (newscore >= su->su_maxscore)
8551 return FALSE;
8552
Bram Moolenaarea424162005-06-16 21:51:00 +00008553 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008554 stack[depth + 1].ts_state = STATE_START;
8555 stack[depth + 1].ts_score = newscore;
8556 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008557 return TRUE;
8558}
8559
Bram Moolenaar53805d12005-08-01 07:08:33 +00008560#ifdef FEAT_MBYTE
8561/*
8562 * Case-folding may change the number of bytes: Count nr of chars in
8563 * fword[flen] and return the byte length of that many chars in "word".
8564 */
8565 static int
8566nofold_len(fword, flen, word)
8567 char_u *fword;
8568 int flen;
8569 char_u *word;
8570{
8571 char_u *p;
8572 int i = 0;
8573
8574 for (p = fword; p < fword + flen; mb_ptr_adv(p))
8575 ++i;
8576 for (p = word; i > 0; mb_ptr_adv(p))
8577 --i;
8578 return (int)(p - word);
8579}
8580#endif
8581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582/*
8583 * "fword" is a good word with case folded. Find the matching keep-case
8584 * words and put it in "kword".
8585 * Theoretically there could be several keep-case words that result in the
8586 * same case-folded word, but we only find one...
8587 */
8588 static void
8589find_keepcap_word(slang, fword, kword)
8590 slang_T *slang;
8591 char_u *fword;
8592 char_u *kword;
8593{
8594 char_u uword[MAXWLEN]; /* "fword" in upper-case */
8595 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008596 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008597
8598 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008599 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008600 int round[MAXWLEN];
8601 int fwordidx[MAXWLEN];
8602 int uwordidx[MAXWLEN];
8603 int kwordlen[MAXWLEN];
8604
8605 int flen, ulen;
8606 int l;
8607 int len;
8608 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008609 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610 char_u *p;
8611 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008612 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008613
8614 if (byts == NULL)
8615 {
8616 /* array is empty: "cannot happen" */
8617 *kword = NUL;
8618 return;
8619 }
8620
8621 /* Make an all-cap version of "fword". */
8622 allcap_copy(fword, uword);
8623
8624 /*
8625 * Each character needs to be tried both case-folded and upper-case.
8626 * All this gets very complicated if we keep in mind that changing case
8627 * may change the byte length of a multi-byte character...
8628 */
8629 depth = 0;
8630 arridx[0] = 0;
8631 round[0] = 0;
8632 fwordidx[0] = 0;
8633 uwordidx[0] = 0;
8634 kwordlen[0] = 0;
8635 while (depth >= 0)
8636 {
8637 if (fword[fwordidx[depth]] == NUL)
8638 {
8639 /* We are at the end of "fword". If the tree allows a word to end
8640 * here we have found a match. */
8641 if (byts[arridx[depth] + 1] == 0)
8642 {
8643 kword[kwordlen[depth]] = NUL;
8644 return;
8645 }
8646
8647 /* kword is getting too long, continue one level up */
8648 --depth;
8649 }
8650 else if (++round[depth] > 2)
8651 {
8652 /* tried both fold-case and upper-case character, continue one
8653 * level up */
8654 --depth;
8655 }
8656 else
8657 {
8658 /*
8659 * round[depth] == 1: Try using the folded-case character.
8660 * round[depth] == 2: Try using the upper-case character.
8661 */
8662#ifdef FEAT_MBYTE
8663 if (has_mbyte)
8664 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008665 flen = mb_cptr2len(fword + fwordidx[depth]);
8666 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 }
8668 else
8669#endif
8670 ulen = flen = 1;
8671 if (round[depth] == 1)
8672 {
8673 p = fword + fwordidx[depth];
8674 l = flen;
8675 }
8676 else
8677 {
8678 p = uword + uwordidx[depth];
8679 l = ulen;
8680 }
8681
8682 for (tryidx = arridx[depth]; l > 0; --l)
8683 {
8684 /* Perform a binary search in the list of accepted bytes. */
8685 len = byts[tryidx++];
8686 c = *p++;
8687 lo = tryidx;
8688 hi = tryidx + len - 1;
8689 while (lo < hi)
8690 {
8691 m = (lo + hi) / 2;
8692 if (byts[m] > c)
8693 hi = m - 1;
8694 else if (byts[m] < c)
8695 lo = m + 1;
8696 else
8697 {
8698 lo = hi = m;
8699 break;
8700 }
8701 }
8702
8703 /* Stop if there is no matching byte. */
8704 if (hi < lo || byts[lo] != c)
8705 break;
8706
8707 /* Continue at the child (if there is one). */
8708 tryidx = idxs[lo];
8709 }
8710
8711 if (l == 0)
8712 {
8713 /*
8714 * Found the matching char. Copy it to "kword" and go a
8715 * level deeper.
8716 */
8717 if (round[depth] == 1)
8718 {
8719 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8720 flen);
8721 kwordlen[depth + 1] = kwordlen[depth] + flen;
8722 }
8723 else
8724 {
8725 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8726 ulen);
8727 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8728 }
8729 fwordidx[depth + 1] = fwordidx[depth] + flen;
8730 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8731
8732 ++depth;
8733 arridx[depth] = tryidx;
8734 round[depth] = 0;
8735 }
8736 }
8737 }
8738
8739 /* Didn't find it: "cannot happen". */
8740 *kword = NUL;
8741}
8742
8743/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008744 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8745 * su->su_sga.
8746 */
8747 static void
8748score_comp_sal(su)
8749 suginfo_T *su;
8750{
8751 langp_T *lp;
8752 char_u badsound[MAXWLEN];
8753 int i;
8754 suggest_T *stp;
8755 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008756 int score;
8757
8758 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8759 return;
8760
8761 /* Use the sound-folding of the first language that supports it. */
8762 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8763 lp->lp_slang != NULL; ++lp)
8764 if (lp->lp_slang->sl_sal.ga_len > 0)
8765 {
8766 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008767 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008768
8769 for (i = 0; i < su->su_ga.ga_len; ++i)
8770 {
8771 stp = &SUG(su->su_ga, i);
8772
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008773 /* Case-fold the suggested word, sound-fold it and compute the
8774 * sound-a-like score. */
8775 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008776 if (score < SCORE_MAXMAX)
8777 {
8778 /* Add the suggestion. */
8779 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8780 sstp->st_word = vim_strsave(stp->st_word);
8781 if (sstp->st_word != NULL)
8782 {
8783 sstp->st_score = score;
8784 sstp->st_altscore = 0;
8785 sstp->st_orglen = stp->st_orglen;
8786 ++su->su_sga.ga_len;
8787 }
8788 }
8789 }
8790 break;
8791 }
8792}
8793
8794/*
8795 * Combine the list of suggestions in su->su_ga and su->su_sga.
8796 * They are intwined.
8797 */
8798 static void
8799score_combine(su)
8800 suginfo_T *su;
8801{
8802 int i;
8803 int j;
8804 garray_T ga;
8805 garray_T *gap;
8806 langp_T *lp;
8807 suggest_T *stp;
8808 char_u *p;
8809 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008810 int round;
8811
8812 /* Add the alternate score to su_ga. */
8813 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8814 lp->lp_slang != NULL; ++lp)
8815 {
8816 if (lp->lp_slang->sl_sal.ga_len > 0)
8817 {
8818 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008819 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008820
8821 for (i = 0; i < su->su_ga.ga_len; ++i)
8822 {
8823 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008824 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8825 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008826 if (stp->st_altscore == SCORE_MAXMAX)
8827 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8828 else
8829 stp->st_score = (stp->st_score * 3
8830 + stp->st_altscore) / 4;
8831 stp->st_salscore = FALSE;
8832 }
8833 break;
8834 }
8835 }
8836
8837 /* Add the alternate score to su_sga. */
8838 for (i = 0; i < su->su_sga.ga_len; ++i)
8839 {
8840 stp = &SUG(su->su_sga, i);
8841 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8842 if (stp->st_score == SCORE_MAXMAX)
8843 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8844 else
8845 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8846 stp->st_salscore = TRUE;
8847 }
8848
8849 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8850 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8851 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8852
8853 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8854 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8855 return;
8856
8857 stp = &SUG(ga, 0);
8858 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8859 {
8860 /* round 1: get a suggestion from su_ga
8861 * round 2: get a suggestion from su_sga */
8862 for (round = 1; round <= 2; ++round)
8863 {
8864 gap = round == 1 ? &su->su_ga : &su->su_sga;
8865 if (i < gap->ga_len)
8866 {
8867 /* Don't add a word if it's already there. */
8868 p = SUG(*gap, i).st_word;
8869 for (j = 0; j < ga.ga_len; ++j)
8870 if (STRCMP(stp[j].st_word, p) == 0)
8871 break;
8872 if (j == ga.ga_len)
8873 stp[ga.ga_len++] = SUG(*gap, i);
8874 else
8875 vim_free(p);
8876 }
8877 }
8878 }
8879
8880 ga_clear(&su->su_ga);
8881 ga_clear(&su->su_sga);
8882
8883 /* Truncate the list to the number of suggestions that will be displayed. */
8884 if (ga.ga_len > su->su_maxcount)
8885 {
8886 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8887 vim_free(stp[i].st_word);
8888 ga.ga_len = su->su_maxcount;
8889 }
8890
8891 su->su_ga = ga;
8892}
8893
8894/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008895 * For the goodword in "stp" compute the soundalike score compared to the
8896 * badword.
8897 */
8898 static int
8899stp_sal_score(stp, su, slang, badsound)
8900 suggest_T *stp;
8901 suginfo_T *su;
8902 slang_T *slang;
8903 char_u *badsound; /* sound-folded badword */
8904{
8905 char_u *p;
8906 char_u badsound2[MAXWLEN];
8907 char_u fword[MAXWLEN];
8908 char_u goodsound[MAXWLEN];
8909
8910 if (stp->st_orglen <= su->su_badlen)
8911 p = badsound;
8912 else
8913 {
8914 /* soundfold the bad word with more characters following */
8915 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8916
8917 /* When joining two words the sound often changes a lot. E.g., "t he"
8918 * sounds like "t h" while "the" sounds like "@". Avoid that by
8919 * removing the space. Don't do it when the good word also contains a
8920 * space. */
8921 if (vim_iswhite(su->su_badptr[su->su_badlen])
8922 && *skiptowhite(stp->st_word) == NUL)
8923 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8924 mch_memmove(p, p + 1, STRLEN(p));
8925
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008926 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008927 p = badsound2;
8928 }
8929
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008930 /* Sound-fold the word and compute the score for the difference. */
8931 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008932
8933 return soundalike_score(goodsound, p);
8934}
8935
8936/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 * Find suggestions by comparing the word in a sound-a-like form.
8938 */
8939 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008940suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 suginfo_T *su;
8942{
8943 char_u salword[MAXWLEN];
8944 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008946 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 int curi[MAXWLEN];
8948 langp_T *lp;
8949 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008950 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 int depth;
8952 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008953 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 int round;
8955 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008956 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008958 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8960 lp->lp_slang != NULL; ++lp)
8961 {
8962 if (lp->lp_slang->sl_sal.ga_len > 0)
8963 {
8964 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008965 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966
8967 /*
8968 * Go through the whole tree, soundfold each word and compare.
8969 * round 1: use the case-folded tree.
8970 * round 2: use the keep-case tree.
8971 */
8972 for (round = 1; round <= 2; ++round)
8973 {
8974 if (round == 1)
8975 {
8976 byts = lp->lp_slang->sl_fbyts;
8977 idxs = lp->lp_slang->sl_fidxs;
8978 }
8979 else
8980 {
8981 byts = lp->lp_slang->sl_kbyts;
8982 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008983 if (byts == NULL) /* no keep-case words */
8984 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 }
8986
8987 depth = 0;
8988 arridx[0] = 0;
8989 curi[0] = 1;
8990 while (depth >= 0 && !got_int)
8991 {
8992 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 /* Done all bytes at this node, go up one level. */
8995 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008996 line_breakcheck();
8997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 else
8999 {
9000 /* Do one more byte at this node. */
9001 n = arridx[depth] + curi[depth];
9002 ++curi[depth];
9003 c = byts[n];
9004 if (c == 0)
9005 {
9006 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009007 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 if (round == 2 || (flags & WF_KEEPCAP) == 0)
9009 {
9010 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009011 /* Sound-fold. Only in keep-case tree need to
9012 * case-fold the word. */
9013 spell_soundfold(lp->lp_slang, tword,
9014 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009016 /* Compute the edit distance between the
9017 * sound-a-like words. */
9018 sound_score = soundalike_score(salword,
9019 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009020 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009022 char_u cword[MAXWLEN];
9023 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009024 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009025
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009026 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009027 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009028 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009029 /* Need to fix case according to
9030 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009032 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 }
9034 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009035 p = tword;
9036
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009037 if (sps_flags & SPS_DOUBLE)
9038 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009039 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009040 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009041 else
9042 {
9043 /* Compute the score. */
9044 score = spell_edit_score(
9045 su->su_badword, p);
9046 if (sps_flags & SPS_BEST)
9047 /* give a bonus for the good word
9048 * sounding the same as the bad
9049 * word */
9050 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009051 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009052 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009053 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009054 else
9055 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009056 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009057 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 }
9060 }
9061
9062 /* Skip over other NUL bytes. */
9063 while (byts[n + 1] == 0)
9064 {
9065 ++n;
9066 ++curi[depth];
9067 }
9068 }
9069 else
9070 {
9071 /* Normal char, go one level deeper. */
9072 tword[depth++] = c;
9073 arridx[depth] = idxs[n];
9074 curi[depth] = 1;
9075 }
9076 }
9077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078 }
9079 }
9080 }
9081}
9082
9083/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009084 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 */
9086 static void
9087make_case_word(fword, cword, flags)
9088 char_u *fword;
9089 char_u *cword;
9090 int flags;
9091{
9092 if (flags & WF_ALLCAP)
9093 /* Make it all upper-case */
9094 allcap_copy(fword, cword);
9095 else if (flags & WF_ONECAP)
9096 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009097 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 else
9099 /* Use goodword as-is. */
9100 STRCPY(cword, fword);
9101}
9102
Bram Moolenaarea424162005-06-16 21:51:00 +00009103/*
9104 * Use map string "map" for languages "lp".
9105 */
9106 static void
9107set_map_str(lp, map)
9108 slang_T *lp;
9109 char_u *map;
9110{
9111 char_u *p;
9112 int headc = 0;
9113 int c;
9114 int i;
9115
9116 if (*map == NUL)
9117 {
9118 lp->sl_has_map = FALSE;
9119 return;
9120 }
9121 lp->sl_has_map = TRUE;
9122
9123 /* Init the array and hash table empty. */
9124 for (i = 0; i < 256; ++i)
9125 lp->sl_map_array[i] = 0;
9126#ifdef FEAT_MBYTE
9127 hash_init(&lp->sl_map_hash);
9128#endif
9129
9130 /*
9131 * The similar characters are stored separated with slashes:
9132 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
9133 * before the same slash. For characters above 255 sl_map_hash is used.
9134 */
9135 for (p = map; *p != NUL; )
9136 {
9137#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009138 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009139#else
9140 c = *p++;
9141#endif
9142 if (c == '/')
9143 headc = 0;
9144 else
9145 {
9146 if (headc == 0)
9147 headc = c;
9148
9149#ifdef FEAT_MBYTE
9150 /* Characters above 255 don't fit in sl_map_array[], put them in
9151 * the hash table. Each entry is the char, a NUL the headchar and
9152 * a NUL. */
9153 if (c >= 256)
9154 {
9155 int cl = mb_char2len(c);
9156 int headcl = mb_char2len(headc);
9157 char_u *b;
9158 hash_T hash;
9159 hashitem_T *hi;
9160
9161 b = alloc((unsigned)(cl + headcl + 2));
9162 if (b == NULL)
9163 return;
9164 mb_char2bytes(c, b);
9165 b[cl] = NUL;
9166 mb_char2bytes(headc, b + cl + 1);
9167 b[cl + 1 + headcl] = NUL;
9168 hash = hash_hash(b);
9169 hi = hash_lookup(&lp->sl_map_hash, b, hash);
9170 if (HASHITEM_EMPTY(hi))
9171 hash_add_item(&lp->sl_map_hash, hi, b, hash);
9172 else
9173 {
9174 /* This should have been checked when generating the .spl
9175 * file. */
9176 EMSG(_("E999: duplicate char in MAP entry"));
9177 vim_free(b);
9178 }
9179 }
9180 else
9181#endif
9182 lp->sl_map_array[c] = headc;
9183 }
9184 }
9185}
9186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009187/*
9188 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
9189 * lines in the .aff file.
9190 */
9191 static int
9192similar_chars(slang, c1, c2)
9193 slang_T *slang;
9194 int c1;
9195 int c2;
9196{
Bram Moolenaarea424162005-06-16 21:51:00 +00009197 int m1, m2;
9198#ifdef FEAT_MBYTE
9199 char_u buf[MB_MAXBYTES];
9200 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201
Bram Moolenaarea424162005-06-16 21:51:00 +00009202 if (c1 >= 256)
9203 {
9204 buf[mb_char2bytes(c1, buf)] = 0;
9205 hi = hash_find(&slang->sl_map_hash, buf);
9206 if (HASHITEM_EMPTY(hi))
9207 m1 = 0;
9208 else
9209 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
9210 }
9211 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009212#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009213 m1 = slang->sl_map_array[c1];
9214 if (m1 == 0)
9215 return FALSE;
9216
9217
9218#ifdef FEAT_MBYTE
9219 if (c2 >= 256)
9220 {
9221 buf[mb_char2bytes(c2, buf)] = 0;
9222 hi = hash_find(&slang->sl_map_hash, buf);
9223 if (HASHITEM_EMPTY(hi))
9224 m2 = 0;
9225 else
9226 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
9227 }
9228 else
9229#endif
9230 m2 = slang->sl_map_array[c2];
9231
9232 return m1 == m2;
9233}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234
9235/*
9236 * Add a suggestion to the list of suggestions.
9237 * Do not add a duplicate suggestion or suggestions with a bad score.
9238 * When "use_score" is not zero it's used, otherwise the score is computed
9239 * with spell_edit_score().
9240 */
9241 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009242add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009244 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009246 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009247 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009248 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009249 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250{
9251 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009253 char_u *p = NULL;
9254 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255
9256 /* Check that the word wasn't banned. */
9257 if (was_banned(su, goodword))
9258 return;
9259
Bram Moolenaar0c405862005-06-22 22:26:26 +00009260 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
9261 * Remove the common part from "goodword". */
9262 i = badlen - su->su_badlen;
9263 if (i > 0)
9264 {
9265 /* This assumes there was no case folding or it didn't change the
9266 * length... */
9267 p = goodword + STRLEN(goodword) - i;
9268 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
9269 {
9270 badlen = su->su_badlen;
9271 c = *p;
9272 *p = NUL;
9273 }
9274 else
9275 p = NULL;
9276 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009277 else if (i < 0)
9278 {
9279 /* When replacing part of the word check that we actually change
9280 * something. For "the the" a suggestion can be replacing the first
9281 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009282 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009283 && STRNCMP(su->su_badword, goodword, badlen) == 0)
9284 return;
9285 }
9286
Bram Moolenaar0c405862005-06-22 22:26:26 +00009287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 if (score <= su->su_maxscore)
9289 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009290 /* Check if the word is already there. Also check the length that is
9291 * being replaced "thes," -> "these" is a different suggestion from
9292 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009293 stp = &SUG(*gap, 0);
9294 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009295 if (STRCMP(stp[i].st_word, goodword) == 0
9296 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 {
9298 /* Found it. Remember the lowest score. */
9299 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009302 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009303 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 break;
9306 }
9307
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009308 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009309 {
9310 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009311 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009312 stp->st_word = vim_strsave(goodword);
9313 if (stp->st_word != NULL)
9314 {
9315 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009316 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009317 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009318 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009319 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320
9321 /* If we have too many suggestions now, sort the list and keep
9322 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009323 if (gap->ga_len > SUG_MAX_COUNT(su))
9324 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
9325 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 }
9327 }
9328 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00009329
9330 if (p != NULL)
9331 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332}
9333
9334/*
9335 * Add a word to be banned.
9336 */
9337 static void
9338add_banned(su, word)
9339 suginfo_T *su;
9340 char_u *word;
9341{
9342 char_u *s = vim_strsave(word);
9343 hash_T hash;
9344 hashitem_T *hi;
9345
9346 if (s != NULL)
9347 {
9348 hash = hash_hash(s);
9349 hi = hash_lookup(&su->su_banned, s, hash);
9350 if (HASHITEM_EMPTY(hi))
9351 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009352 else
9353 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 }
9355}
9356
9357/*
9358 * Return TRUE if a word appears in the list of banned words.
9359 */
9360 static int
9361was_banned(su, word)
9362 suginfo_T *su;
9363 char_u *word;
9364{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009365 hashitem_T *hi = hash_find(&su->su_banned, word);
9366
9367 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368}
9369
9370/*
9371 * Free the banned words in "su".
9372 */
9373 static void
9374free_banned(su)
9375 suginfo_T *su;
9376{
9377 int todo;
9378 hashitem_T *hi;
9379
9380 todo = su->su_banned.ht_used;
9381 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
9382 {
9383 if (!HASHITEM_EMPTY(hi))
9384 {
9385 vim_free(hi->hi_key);
9386 --todo;
9387 }
9388 }
9389 hash_clear(&su->su_banned);
9390}
9391
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009392/*
9393 * Recompute the score if sound-folding is possible. This is slow,
9394 * thus only done for the final results.
9395 */
9396 static void
9397rescore_suggestions(su)
9398 suginfo_T *su;
9399{
9400 langp_T *lp;
9401 suggest_T *stp;
9402 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009403 int i;
9404
9405 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9406 lp->lp_slang != NULL; ++lp)
9407 {
9408 if (lp->lp_slang->sl_sal.ga_len > 0)
9409 {
9410 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009411 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009412
9413 for (i = 0; i < su->su_ga.ga_len; ++i)
9414 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009415 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009416 if (!stp->st_had_bonus)
9417 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009418 stp->st_altscore = stp_sal_score(stp, su,
9419 lp->lp_slang, sal_badword);
9420 if (stp->st_altscore == SCORE_MAXMAX)
9421 stp->st_altscore = SCORE_BIG;
9422 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009423 }
9424 }
9425 break;
9426 }
9427 }
9428}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009430static int
9431#ifdef __BORLANDC__
9432_RTLENTRYF
9433#endif
9434sug_compare __ARGS((const void *s1, const void *s2));
9435
9436/*
9437 * Function given to qsort() to sort the suggestions on st_score.
9438 */
9439 static int
9440#ifdef __BORLANDC__
9441_RTLENTRYF
9442#endif
9443sug_compare(s1, s2)
9444 const void *s1;
9445 const void *s2;
9446{
9447 suggest_T *p1 = (suggest_T *)s1;
9448 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009449 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009450
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009451 if (n == 0)
9452 return p1->st_altscore - p2->st_altscore;
9453 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009454}
9455
9456/*
9457 * Cleanup the suggestions:
9458 * - Sort on score.
9459 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009460 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009462 static int
9463cleanup_suggestions(gap, maxscore, keep)
9464 garray_T *gap;
9465 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009466 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009468 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009469 int i;
9470
9471 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009472 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473
9474 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009475 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009477 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009479 gap->ga_len = keep;
9480 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009481 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009482 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009483}
9484
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009485#if defined(FEAT_EVAL) || defined(PROTO)
9486/*
9487 * Soundfold a string, for soundfold().
9488 * Result is in allocated memory, NULL for an error.
9489 */
9490 char_u *
9491eval_soundfold(word)
9492 char_u *word;
9493{
9494 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009495 char_u sound[MAXWLEN];
9496
9497 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
9498 /* Use the sound-folding of the first language that supports it. */
9499 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9500 lp->lp_slang != NULL; ++lp)
9501 if (lp->lp_slang->sl_sal.ga_len > 0)
9502 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009503 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009504 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009505 return vim_strsave(sound);
9506 }
9507
9508 /* No language with sound folding, return word as-is. */
9509 return vim_strsave(word);
9510}
9511#endif
9512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513/*
9514 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9515 */
9516 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009517spell_soundfold(slang, inword, folded, res)
9518 slang_T *slang;
9519 char_u *inword;
9520 int folded; /* "inword" is already case-folded */
9521 char_u *res;
9522{
9523 char_u fword[MAXWLEN];
9524 char_u *word;
9525
9526 if (slang->sl_sofo)
9527 /* SOFOFROM and SOFOTO used */
9528 spell_soundfold_sofo(slang, inword, res);
9529 else
9530 {
9531 /* SAL items used. Requires the word to be case-folded. */
9532 if (folded)
9533 word = inword;
9534 else
9535 {
9536 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
9537 word = fword;
9538 }
9539
9540#ifdef FEAT_MBYTE
9541 if (has_mbyte)
9542 spell_soundfold_wsal(slang, word, res);
9543 else
9544#endif
9545 spell_soundfold_sal(slang, word, res);
9546 }
9547}
9548
9549/*
9550 * Perform sound folding of "inword" into "res" according to SOFOFROM and
9551 * SOFOTO lines.
9552 */
9553 static void
9554spell_soundfold_sofo(slang, inword, res)
9555 slang_T *slang;
9556 char_u *inword;
9557 char_u *res;
9558{
9559 char_u *s;
9560 int ri = 0;
9561 int c;
9562
9563#ifdef FEAT_MBYTE
9564 if (has_mbyte)
9565 {
9566 int prevc = 0;
9567 int *ip;
9568
9569 /* The sl_sal_first[] table contains the translation for chars up to
9570 * 255, sl_sal the rest. */
9571 for (s = inword; *s != NUL; )
9572 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009573 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009574 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9575 c = ' ';
9576 else if (c < 256)
9577 c = slang->sl_sal_first[c];
9578 else
9579 {
9580 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
9581 if (ip == NULL) /* empty list, can't match */
9582 c = NUL;
9583 else
9584 for (;;) /* find "c" in the list */
9585 {
9586 if (*ip == 0) /* not found */
9587 {
9588 c = NUL;
9589 break;
9590 }
9591 if (*ip == c) /* match! */
9592 {
9593 c = ip[1];
9594 break;
9595 }
9596 ip += 2;
9597 }
9598 }
9599
9600 if (c != NUL && c != prevc)
9601 {
9602 ri += mb_char2bytes(c, res + ri);
9603 if (ri + MB_MAXBYTES > MAXWLEN)
9604 break;
9605 prevc = c;
9606 }
9607 }
9608 }
9609 else
9610#endif
9611 {
9612 /* The sl_sal_first[] table contains the translation. */
9613 for (s = inword; (c = *s) != NUL; ++s)
9614 {
9615 if (vim_iswhite(c))
9616 c = ' ';
9617 else
9618 c = slang->sl_sal_first[c];
9619 if (c != NUL && (ri == 0 || res[ri - 1] != c))
9620 res[ri++] = c;
9621 }
9622 }
9623
9624 res[ri] = NUL;
9625}
9626
9627 static void
9628spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 slang_T *slang;
9630 char_u *inword;
9631 char_u *res;
9632{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009633 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009635 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009637 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009639 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 int n, k = 0;
9641 int z0;
9642 int k0;
9643 int n0;
9644 int c;
9645 int pri;
9646 int p0 = -333;
9647 int c0;
9648
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009649 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009650 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 if (slang->sl_rem_accents)
9652 {
9653 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009654 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009656 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009657 {
9658 *t++ = ' ';
9659 s = skipwhite(s);
9660 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009661 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009662 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009663 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 *t++ = *s;
9665 ++s;
9666 }
9667 }
9668 *t = NUL;
9669 }
9670 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009671 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009673 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674
9675 /*
9676 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009677 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009678 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009679 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 while ((c = word[i]) != NUL)
9681 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009682 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009683 n = slang->sl_sal_first[c];
9684 z0 = 0;
9685
9686 if (n >= 0)
9687 {
9688 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009689 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009691 /* Quickly skip entries that don't match the word. Most
9692 * entries are less then three chars, optimize for that. */
9693 k = smp[n].sm_leadlen;
9694 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009696 if (word[i + 1] != s[1])
9697 continue;
9698 if (k > 2)
9699 {
9700 for (j = 2; j < k; ++j)
9701 if (word[i + j] != s[j])
9702 break;
9703 if (j < k)
9704 continue;
9705 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009706 }
9707
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009708 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009710 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009711 while (*pf != NUL && *pf != word[i + k])
9712 ++pf;
9713 if (*pf == NUL)
9714 continue;
9715 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009716 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009717 s = smp[n].sm_rules;
9718 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009719
9720 p0 = *s;
9721 k0 = k;
9722 while (*s == '-' && k > 1)
9723 {
9724 k--;
9725 s++;
9726 }
9727 if (*s == '<')
9728 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009729 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009730 {
9731 /* determine priority */
9732 pri = *s - '0';
9733 s++;
9734 }
9735 if (*s == '^' && *(s + 1) == '^')
9736 s++;
9737
9738 if (*s == NUL
9739 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009740 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009741 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009743 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009745 && spell_iswordp(word + i - 1, curbuf)
9746 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 {
9748 /* search for followup rules, if: */
9749 /* followup and k > 1 and NO '-' in searchstring */
9750 c0 = word[i + k - 1];
9751 n0 = slang->sl_sal_first[c0];
9752
9753 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009754 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 {
9756 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009757 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009759 /* Quickly skip entries that don't match the word.
9760 * */
9761 k0 = smp[n0].sm_leadlen;
9762 if (k0 > 1)
9763 {
9764 if (word[i + k] != s[1])
9765 continue;
9766 if (k0 > 2)
9767 {
9768 pf = word + i + k + 1;
9769 for (j = 2; j < k0; ++j)
9770 if (*pf++ != s[j])
9771 break;
9772 if (j < k0)
9773 continue;
9774 }
9775 }
9776 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009778 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009779 {
9780 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009781 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009782 while (*pf != NUL && *pf != word[i + k0])
9783 ++pf;
9784 if (*pf == NUL)
9785 continue;
9786 ++k0;
9787 }
9788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009790 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009791 while (*s == '-')
9792 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009793 /* "k0" gets NOT reduced because
9794 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009795 s++;
9796 }
9797 if (*s == '<')
9798 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009799 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009800 {
9801 p0 = *s - '0';
9802 s++;
9803 }
9804
9805 if (*s == NUL
9806 /* *s == '^' cuts */
9807 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009808 && !spell_iswordp(word + i + k0,
9809 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009810 {
9811 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814
9815 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009817 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 /* rule fits; stop search */
9819 break;
9820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009821 }
9822
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009823 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 }
9826
9827 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009828 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009829 if (s == NULL)
9830 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009831 pf = smp[n].sm_rules;
9832 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009833 if (p0 == 1 && z == 0)
9834 {
9835 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009836 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9837 || res[reslen - 1] == *s))
9838 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 z0 = 1;
9840 z = 1;
9841 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009842 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 {
9844 word[i + k0] = *s;
9845 k0++;
9846 s++;
9847 }
9848 if (k > k0)
9849 mch_memmove(word + i + k0, word + i + k,
9850 STRLEN(word + i + k) + 1);
9851
9852 /* new "actual letter" */
9853 c = word[i];
9854 }
9855 else
9856 {
9857 /* no '<' rule used */
9858 i += k - 1;
9859 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009860 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009862 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009863 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 s++;
9865 }
9866 /* new "actual letter" */
9867 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009868 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 {
9870 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009871 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 mch_memmove(word, word + i + 1,
9873 STRLEN(word + i + 1) + 1);
9874 i = 0;
9875 z0 = 1;
9876 }
9877 }
9878 break;
9879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 }
9881 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009882 else if (vim_iswhite(c))
9883 {
9884 c = ' ';
9885 k = 1;
9886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887
9888 if (z0 == 0)
9889 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009890 if (k && !p0 && reslen < MAXWLEN && c != NUL
9891 && (!slang->sl_collapse || reslen == 0
9892 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009894 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895
9896 i++;
9897 z = 0;
9898 k = 0;
9899 }
9900 }
9901
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009902 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903}
9904
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009905#ifdef FEAT_MBYTE
9906/*
9907 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9908 * Multi-byte version of spell_soundfold().
9909 */
9910 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009911spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009912 slang_T *slang;
9913 char_u *inword;
9914 char_u *res;
9915{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009916 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009917 int word[MAXWLEN];
9918 int wres[MAXWLEN];
9919 int l;
9920 char_u *s;
9921 int *ws;
9922 char_u *t;
9923 int *pf;
9924 int i, j, z;
9925 int reslen;
9926 int n, k = 0;
9927 int z0;
9928 int k0;
9929 int n0;
9930 int c;
9931 int pri;
9932 int p0 = -333;
9933 int c0;
9934 int did_white = FALSE;
9935
9936 /*
9937 * Convert the multi-byte string to a wide-character string.
9938 * Remove accents, if wanted. We actually remove all non-word characters.
9939 * But keep white space.
9940 */
9941 n = 0;
9942 for (s = inword; *s != NUL; )
9943 {
9944 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009945 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009946 if (slang->sl_rem_accents)
9947 {
9948 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9949 {
9950 if (did_white)
9951 continue;
9952 c = ' ';
9953 did_white = TRUE;
9954 }
9955 else
9956 {
9957 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009958 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009959 continue;
9960 }
9961 }
9962 word[n++] = c;
9963 }
9964 word[n] = NUL;
9965
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009966 /*
9967 * This comes from Aspell phonet.cpp.
9968 * Converted from C++ to C. Added support for multi-byte chars.
9969 * Changed to keep spaces.
9970 */
9971 i = reslen = z = 0;
9972 while ((c = word[i]) != NUL)
9973 {
9974 /* Start with the first rule that has the character in the word. */
9975 n = slang->sl_sal_first[c & 0xff];
9976 z0 = 0;
9977
9978 if (n >= 0)
9979 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009980 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009981 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9982 {
9983 /* Quickly skip entries that don't match the word. Most
9984 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009985 if (c != ws[0])
9986 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009987 k = smp[n].sm_leadlen;
9988 if (k > 1)
9989 {
9990 if (word[i + 1] != ws[1])
9991 continue;
9992 if (k > 2)
9993 {
9994 for (j = 2; j < k; ++j)
9995 if (word[i + j] != ws[j])
9996 break;
9997 if (j < k)
9998 continue;
9999 }
10000 }
10001
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010002 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010003 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010004 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010005 while (*pf != NUL && *pf != word[i + k])
10006 ++pf;
10007 if (*pf == NUL)
10008 continue;
10009 ++k;
10010 }
10011 s = smp[n].sm_rules;
10012 pri = 5; /* default priority */
10013
10014 p0 = *s;
10015 k0 = k;
10016 while (*s == '-' && k > 1)
10017 {
10018 k--;
10019 s++;
10020 }
10021 if (*s == '<')
10022 s++;
10023 if (VIM_ISDIGIT(*s))
10024 {
10025 /* determine priority */
10026 pri = *s - '0';
10027 s++;
10028 }
10029 if (*s == '^' && *(s + 1) == '^')
10030 s++;
10031
10032 if (*s == NUL
10033 || (*s == '^'
10034 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010035 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010036 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010037 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010038 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010039 && spell_iswordp_w(word + i - 1, curbuf)
10040 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010041 {
10042 /* search for followup rules, if: */
10043 /* followup and k > 1 and NO '-' in searchstring */
10044 c0 = word[i + k - 1];
10045 n0 = slang->sl_sal_first[c0 & 0xff];
10046
10047 if (slang->sl_followup && k > 1 && n0 >= 0
10048 && p0 != '-' && word[i + k] != NUL)
10049 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010050 /* Test follow-up rule for "word[i + k]"; loop over
10051 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010052 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
10053 == (c0 & 0xff); ++n0)
10054 {
10055 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010056 */
10057 if (c0 != ws[0])
10058 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010059 k0 = smp[n0].sm_leadlen;
10060 if (k0 > 1)
10061 {
10062 if (word[i + k] != ws[1])
10063 continue;
10064 if (k0 > 2)
10065 {
10066 pf = word + i + k + 1;
10067 for (j = 2; j < k0; ++j)
10068 if (*pf++ != ws[j])
10069 break;
10070 if (j < k0)
10071 continue;
10072 }
10073 }
10074 k0 += k - 1;
10075
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010076 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010077 {
10078 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010079 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010080 while (*pf != NUL && *pf != word[i + k0])
10081 ++pf;
10082 if (*pf == NUL)
10083 continue;
10084 ++k0;
10085 }
10086
10087 p0 = 5;
10088 s = smp[n0].sm_rules;
10089 while (*s == '-')
10090 {
10091 /* "k0" gets NOT reduced because
10092 * "if (k0 == k)" */
10093 s++;
10094 }
10095 if (*s == '<')
10096 s++;
10097 if (VIM_ISDIGIT(*s))
10098 {
10099 p0 = *s - '0';
10100 s++;
10101 }
10102
10103 if (*s == NUL
10104 /* *s == '^' cuts */
10105 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010106 && !spell_iswordp_w(word + i + k0,
10107 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010108 {
10109 if (k0 == k)
10110 /* this is just a piece of the string */
10111 continue;
10112
10113 if (p0 < pri)
10114 /* priority too low */
10115 continue;
10116 /* rule fits; stop search */
10117 break;
10118 }
10119 }
10120
10121 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
10122 == (c0 & 0xff))
10123 continue;
10124 }
10125
10126 /* replace string */
10127 ws = smp[n].sm_to_w;
10128 s = smp[n].sm_rules;
10129 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
10130 if (p0 == 1 && z == 0)
10131 {
10132 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010133 if (reslen > 0 && ws != NULL && *ws != NUL
10134 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010135 || wres[reslen - 1] == *ws))
10136 reslen--;
10137 z0 = 1;
10138 z = 1;
10139 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010140 if (ws != NULL)
10141 while (*ws != NUL && word[i + k0] != NUL)
10142 {
10143 word[i + k0] = *ws;
10144 k0++;
10145 ws++;
10146 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010147 if (k > k0)
10148 mch_memmove(word + i + k0, word + i + k,
10149 sizeof(int) * (STRLEN(word + i + k) + 1));
10150
10151 /* new "actual letter" */
10152 c = word[i];
10153 }
10154 else
10155 {
10156 /* no '<' rule used */
10157 i += k - 1;
10158 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010159 if (ws != NULL)
10160 while (*ws != NUL && ws[1] != NUL
10161 && reslen < MAXWLEN)
10162 {
10163 if (reslen == 0 || wres[reslen - 1] != *ws)
10164 wres[reslen++] = *ws;
10165 ws++;
10166 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010167 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010168 if (ws == NULL)
10169 c = NUL;
10170 else
10171 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010172 if (strstr((char *)s, "^^") != NULL)
10173 {
10174 if (c != NUL)
10175 wres[reslen++] = c;
10176 mch_memmove(word, word + i + 1,
10177 sizeof(int) * (STRLEN(word + i + 1) + 1));
10178 i = 0;
10179 z0 = 1;
10180 }
10181 }
10182 break;
10183 }
10184 }
10185 }
10186 else if (vim_iswhite(c))
10187 {
10188 c = ' ';
10189 k = 1;
10190 }
10191
10192 if (z0 == 0)
10193 {
10194 if (k && !p0 && reslen < MAXWLEN && c != NUL
10195 && (!slang->sl_collapse || reslen == 0
10196 || wres[reslen - 1] != c))
10197 /* condense only double letters */
10198 wres[reslen++] = c;
10199
10200 i++;
10201 z = 0;
10202 k = 0;
10203 }
10204 }
10205
10206 /* Convert wide characters in "wres" to a multi-byte string in "res". */
10207 l = 0;
10208 for (n = 0; n < reslen; ++n)
10209 {
10210 l += mb_char2bytes(wres[n], res + l);
10211 if (l + MB_MAXBYTES > MAXWLEN)
10212 break;
10213 }
10214 res[l] = NUL;
10215}
10216#endif
10217
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010218/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010219 * Compute a score for two sound-a-like words.
10220 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
10221 * Instead of a generic loop we write out the code. That keeps it fast by
10222 * avoiding checks that will not be possible.
10223 */
10224 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010225soundalike_score(goodstart, badstart)
10226 char_u *goodstart; /* sound-folded good word */
10227 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010228{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010229 char_u *goodsound = goodstart;
10230 char_u *badsound = badstart;
10231 int goodlen;
10232 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010233 int n;
10234 char_u *pl, *ps;
10235 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010236 int score = 0;
10237
10238 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
10239 * counted so much, vowels halfway the word aren't counted at all. */
10240 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
10241 {
10242 score = SCORE_DEL / 2;
10243 if (*badsound == '*')
10244 ++badsound;
10245 else
10246 ++goodsound;
10247 }
10248
10249 goodlen = STRLEN(goodsound);
10250 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010251
10252 /* Return quickly if the lenghts are too different to be fixed by two
10253 * changes. */
10254 n = goodlen - badlen;
10255 if (n < -2 || n > 2)
10256 return SCORE_MAXMAX;
10257
10258 if (n > 0)
10259 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010260 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010261 ps = badsound;
10262 }
10263 else
10264 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010265 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010266 ps = goodsound;
10267 }
10268
10269 /* Skip over the identical part. */
10270 while (*pl == *ps && *pl != NUL)
10271 {
10272 ++pl;
10273 ++ps;
10274 }
10275
10276 switch (n)
10277 {
10278 case -2:
10279 case 2:
10280 /*
10281 * Must delete two characters from "pl".
10282 */
10283 ++pl; /* first delete */
10284 while (*pl == *ps)
10285 {
10286 ++pl;
10287 ++ps;
10288 }
10289 /* strings must be equal after second delete */
10290 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010291 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010292
10293 /* Failed to compare. */
10294 break;
10295
10296 case -1:
10297 case 1:
10298 /*
10299 * Minimal one delete from "pl" required.
10300 */
10301
10302 /* 1: delete */
10303 pl2 = pl + 1;
10304 ps2 = ps;
10305 while (*pl2 == *ps2)
10306 {
10307 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010308 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010309 ++pl2;
10310 ++ps2;
10311 }
10312
10313 /* 2: delete then swap, then rest must be equal */
10314 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10315 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010316 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010317
10318 /* 3: delete then substitute, then the rest must be equal */
10319 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010320 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010321
10322 /* 4: first swap then delete */
10323 if (pl[0] == ps[1] && pl[1] == ps[0])
10324 {
10325 pl2 = pl + 2; /* swap, skip two chars */
10326 ps2 = ps + 2;
10327 while (*pl2 == *ps2)
10328 {
10329 ++pl2;
10330 ++ps2;
10331 }
10332 /* delete a char and then strings must be equal */
10333 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010334 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010335 }
10336
10337 /* 5: first substitute then delete */
10338 pl2 = pl + 1; /* substitute, skip one char */
10339 ps2 = ps + 1;
10340 while (*pl2 == *ps2)
10341 {
10342 ++pl2;
10343 ++ps2;
10344 }
10345 /* delete a char and then strings must be equal */
10346 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010347 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010348
10349 /* Failed to compare. */
10350 break;
10351
10352 case 0:
10353 /*
10354 * Lenghts are equal, thus changes must result in same length: An
10355 * insert is only possible in combination with a delete.
10356 * 1: check if for identical strings
10357 */
10358 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010359 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010360
10361 /* 2: swap */
10362 if (pl[0] == ps[1] && pl[1] == ps[0])
10363 {
10364 pl2 = pl + 2; /* swap, skip two chars */
10365 ps2 = ps + 2;
10366 while (*pl2 == *ps2)
10367 {
10368 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010369 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010370 ++pl2;
10371 ++ps2;
10372 }
10373 /* 3: swap and swap again */
10374 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10375 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010376 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010377
10378 /* 4: swap and substitute */
10379 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010380 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010381 }
10382
10383 /* 5: substitute */
10384 pl2 = pl + 1;
10385 ps2 = ps + 1;
10386 while (*pl2 == *ps2)
10387 {
10388 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010389 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010390 ++pl2;
10391 ++ps2;
10392 }
10393
10394 /* 6: substitute and swap */
10395 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10396 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010397 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010398
10399 /* 7: substitute and substitute */
10400 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010401 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010402
10403 /* 8: insert then delete */
10404 pl2 = pl;
10405 ps2 = ps + 1;
10406 while (*pl2 == *ps2)
10407 {
10408 ++pl2;
10409 ++ps2;
10410 }
10411 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010412 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010413
10414 /* 9: delete then insert */
10415 pl2 = pl + 1;
10416 ps2 = ps;
10417 while (*pl2 == *ps2)
10418 {
10419 ++pl2;
10420 ++ps2;
10421 }
10422 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010423 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010424
10425 /* Failed to compare. */
10426 break;
10427 }
10428
10429 return SCORE_MAXMAX;
10430}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432/*
10433 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010434 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010435 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010437 * It has been converted from C++ to C and modified to support multi-byte
10438 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 */
10440 static int
10441spell_edit_score(badword, goodword)
10442 char_u *badword;
10443 char_u *goodword;
10444{
10445 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010446 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 int j, i;
10448 int t;
10449 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010450 int pbc, pgc;
10451#ifdef FEAT_MBYTE
10452 char_u *p;
10453 int wbadword[MAXWLEN];
10454 int wgoodword[MAXWLEN];
10455
10456 if (has_mbyte)
10457 {
10458 /* Get the characters from the multi-byte strings and put them in an
10459 * int array for easy access. */
10460 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010461 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010462 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010463 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010464 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010465 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010466 }
10467 else
10468#endif
10469 {
10470 badlen = STRLEN(badword) + 1;
10471 goodlen = STRLEN(goodword) + 1;
10472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473
10474 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
10475#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
10477 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010478 if (cnt == NULL)
10479 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480
10481 CNT(0, 0) = 0;
10482 for (j = 1; j <= goodlen; ++j)
10483 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
10484
10485 for (i = 1; i <= badlen; ++i)
10486 {
10487 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
10488 for (j = 1; j <= goodlen; ++j)
10489 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010490#ifdef FEAT_MBYTE
10491 if (has_mbyte)
10492 {
10493 bc = wbadword[i - 1];
10494 gc = wgoodword[j - 1];
10495 }
10496 else
10497#endif
10498 {
10499 bc = badword[i - 1];
10500 gc = goodword[j - 1];
10501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 if (bc == gc)
10503 CNT(i, j) = CNT(i - 1, j - 1);
10504 else
10505 {
10506 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010507 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
10509 else
10510 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
10511
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010512 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010514#ifdef FEAT_MBYTE
10515 if (has_mbyte)
10516 {
10517 pbc = wbadword[i - 2];
10518 pgc = wgoodword[j - 2];
10519 }
10520 else
10521#endif
10522 {
10523 pbc = badword[i - 2];
10524 pgc = goodword[j - 2];
10525 }
10526 if (bc == pgc && pbc == gc)
10527 {
10528 t = SCORE_SWAP + CNT(i - 2, j - 2);
10529 if (t < CNT(i, j))
10530 CNT(i, j) = t;
10531 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 }
10533 t = SCORE_DEL + CNT(i - 1, j);
10534 if (t < CNT(i, j))
10535 CNT(i, j) = t;
10536 t = SCORE_INS + CNT(i, j - 1);
10537 if (t < CNT(i, j))
10538 CNT(i, j) = t;
10539 }
10540 }
10541 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010542
10543 i = CNT(badlen - 1, goodlen - 1);
10544 vim_free(cnt);
10545 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010547
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010548/*
10549 * ":spelldump"
10550 */
10551/*ARGSUSED*/
10552 void
10553ex_spelldump(eap)
10554 exarg_T *eap;
10555{
10556 buf_T *buf = curbuf;
10557 langp_T *lp;
10558 slang_T *slang;
10559 idx_T arridx[MAXWLEN];
10560 int curi[MAXWLEN];
10561 char_u word[MAXWLEN];
10562 int c;
10563 char_u *byts;
10564 idx_T *idxs;
10565 linenr_T lnum = 0;
10566 int round;
10567 int depth;
10568 int n;
10569 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010570 char_u *region_names = NULL; /* region names being used */
10571 int do_region = TRUE; /* dump region names and numbers */
10572 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010573
10574 if (no_spell_checking())
10575 return;
10576
10577 /* Create a new empty buffer by splitting the window. */
10578 do_cmdline_cmd((char_u *)"new");
10579 if (!bufempty() || !buf_valid(buf))
10580 return;
10581
Bram Moolenaar7887d882005-07-01 22:33:52 +000010582 /* Find out if we can support regions: All languages must support the same
10583 * regions or none at all. */
10584 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10585 {
10586 p = lp->lp_slang->sl_regions;
10587 if (p[0] != 0)
10588 {
10589 if (region_names == NULL) /* first language with regions */
10590 region_names = p;
10591 else if (STRCMP(region_names, p) != 0)
10592 {
10593 do_region = FALSE; /* region names are different */
10594 break;
10595 }
10596 }
10597 }
10598
10599 if (do_region && region_names != NULL)
10600 {
10601 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
10602 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10603 }
10604 else
10605 do_region = FALSE;
10606
10607 /*
10608 * Loop over all files loaded for the entries in 'spelllang'.
10609 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010610 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10611 {
10612 slang = lp->lp_slang;
10613
10614 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
10615 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10616
10617 /* round 1: case-folded tree
10618 * round 2: keep-case tree */
10619 for (round = 1; round <= 2; ++round)
10620 {
10621 if (round == 1)
10622 {
10623 byts = slang->sl_fbyts;
10624 idxs = slang->sl_fidxs;
10625 }
10626 else
10627 {
10628 byts = slang->sl_kbyts;
10629 idxs = slang->sl_kidxs;
10630 }
10631 if (byts == NULL)
10632 continue; /* array is empty */
10633
10634 depth = 0;
10635 arridx[0] = 0;
10636 curi[0] = 1;
10637 while (depth >= 0 && !got_int)
10638 {
10639 if (curi[depth] > byts[arridx[depth]])
10640 {
10641 /* Done all bytes at this node, go up one level. */
10642 --depth;
10643 line_breakcheck();
10644 }
10645 else
10646 {
10647 /* Do one more byte at this node. */
10648 n = arridx[depth] + curi[depth];
10649 ++curi[depth];
10650 c = byts[n];
10651 if (c == 0)
10652 {
10653 /* End of word, deal with the word.
10654 * Don't use keep-case words in the fold-case tree,
10655 * they will appear in the keep-case tree.
10656 * Only use the word when the region matches. */
10657 flags = (int)idxs[n];
10658 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000010659 && (do_region
10660 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010661 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010662 & lp->lp_region) != 0))
10663 {
10664 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010665 if (!do_region)
10666 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010667
10668 /* Dump the basic word if there is no prefix or
10669 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010670 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010671 if (c == 0 || curi[depth] == 2)
10672 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010673
10674 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010675 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010676 lnum = apply_prefixes(slang, word, round,
10677 flags, lnum);
10678 }
10679 }
10680 else
10681 {
10682 /* Normal char, go one level deeper. */
10683 word[depth++] = c;
10684 arridx[depth] = idxs[n];
10685 curi[depth] = 1;
10686 }
10687 }
10688 }
10689 }
10690 }
10691
10692 /* Delete the empty line that we started with. */
10693 if (curbuf->b_ml.ml_line_count > 1)
10694 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10695
10696 redraw_later(NOT_VALID);
10697}
10698
10699/*
10700 * Dump one word: apply case modifications and append a line to the buffer.
10701 */
10702 static void
10703dump_word(word, round, flags, lnum)
10704 char_u *word;
10705 int round;
10706 int flags;
10707 linenr_T lnum;
10708{
10709 int keepcap = FALSE;
10710 char_u *p;
10711 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010712 char_u badword[MAXWLEN + 10];
10713 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010714
10715 if (round == 1 && (flags & WF_CAPMASK) != 0)
10716 {
10717 /* Need to fix case according to "flags". */
10718 make_case_word(word, cword, flags);
10719 p = cword;
10720 }
10721 else
10722 {
10723 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010724 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10725 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010726 keepcap = TRUE;
10727 }
10728
Bram Moolenaar7887d882005-07-01 22:33:52 +000010729 /* Add flags and regions after a slash. */
10730 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010731 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010732 STRCPY(badword, p);
10733 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010734 if (keepcap)
10735 STRCAT(badword, "=");
10736 if (flags & WF_BANNED)
10737 STRCAT(badword, "!");
10738 else if (flags & WF_RARE)
10739 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010740 if (flags & WF_REGION)
10741 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010742 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010743 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010744 p = badword;
10745 }
10746
10747 ml_append(lnum, p, (colnr_T)0, FALSE);
10748}
10749
10750/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010751 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10752 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010753 * Return the updated line number.
10754 */
10755 static linenr_T
10756apply_prefixes(slang, word, round, flags, startlnum)
10757 slang_T *slang;
10758 char_u *word; /* case-folded word */
10759 int round;
10760 int flags; /* flags with prefix ID */
10761 linenr_T startlnum;
10762{
10763 idx_T arridx[MAXWLEN];
10764 int curi[MAXWLEN];
10765 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010766 char_u word_up[MAXWLEN];
10767 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010768 int c;
10769 char_u *byts;
10770 idx_T *idxs;
10771 linenr_T lnum = startlnum;
10772 int depth;
10773 int n;
10774 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010775 int i;
10776
Bram Moolenaar53805d12005-08-01 07:08:33 +000010777 /* if the word starts with a lower-case letter make the word with an
10778 * upper-case letter in word_up[]. */
10779 c = PTR2CHAR(word);
10780 if (SPELL_TOUPPER(c) != c)
10781 {
10782 onecap_copy(word, word_up, TRUE);
10783 has_word_up = TRUE;
10784 }
10785
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010786 byts = slang->sl_pbyts;
10787 idxs = slang->sl_pidxs;
10788 if (byts != NULL) /* array not is empty */
10789 {
10790 /*
10791 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010792 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010793 */
10794 depth = 0;
10795 arridx[0] = 0;
10796 curi[0] = 1;
10797 while (depth >= 0 && !got_int)
10798 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010799 n = arridx[depth];
10800 len = byts[n];
10801 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010802 {
10803 /* Done all bytes at this node, go up one level. */
10804 --depth;
10805 line_breakcheck();
10806 }
10807 else
10808 {
10809 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010810 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010811 ++curi[depth];
10812 c = byts[n];
10813 if (c == 0)
10814 {
10815 /* End of prefix, find out how many IDs there are. */
10816 for (i = 1; i < len; ++i)
10817 if (byts[n + i] != 0)
10818 break;
10819 curi[depth] += i - 1;
10820
Bram Moolenaar53805d12005-08-01 07:08:33 +000010821 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10822 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010823 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010824 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010825 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010826 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010827 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010828 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010829
10830 /* Check for prefix that matches the word when the
10831 * first letter is upper-case, but only if the prefix has
10832 * a condition. */
10833 if (has_word_up)
10834 {
10835 c = valid_word_prefix(i, n, flags, word_up, slang,
10836 TRUE);
10837 if (c != 0)
10838 {
10839 vim_strncpy(prefix + depth, word_up,
10840 MAXWLEN - depth - 1);
10841 dump_word(prefix, round,
10842 (c & WF_RAREPFX) ? (flags | WF_RARE)
10843 : flags, lnum++);
10844 }
10845 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010846 }
10847 else
10848 {
10849 /* Normal char, go one level deeper. */
10850 prefix[depth++] = c;
10851 arridx[depth] = idxs[n];
10852 curi[depth] = 1;
10853 }
10854 }
10855 }
10856 }
10857
10858 return lnum;
10859}
10860
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010861#if defined(FEAT_INS_EXPAND) || defined(PROTO)
10862static int spell_expand_need_cap;
10863
10864/*
10865 * Find start of the word in front of the cursor. We don't check if it is
10866 * badly spelled, with completion we can only change the word in front of the
10867 * cursor.
10868 * Used for Insert mode completion CTRL-X ?.
10869 * Returns the column number of the word.
10870 */
10871 int
10872spell_word_start(startcol)
10873 int startcol;
10874{
10875 char_u *line;
10876 char_u *p;
10877 int col = 0;
10878
10879 if (no_spell_checking())
10880 return startcol;
10881
10882 /* Find a word character before "startcol". */
10883 line = ml_get_curline();
10884 for (p = line + startcol; p > line; )
10885 {
10886 mb_ptr_back(line, p);
10887 if (spell_iswordp_nmw(p))
10888 break;
10889 }
10890
10891 /* Go back to start of the word. */
10892 while (p > line)
10893 {
10894 col = p - line;
10895 mb_ptr_back(line, p);
10896 if (!spell_iswordp(p, curbuf))
10897 break;
10898 col = 0;
10899 }
10900
10901 /* Need to check for 'spellcapcheck' now, the word is removed before
10902 * expand_spelling() is called. Therefore the ugly global variable. */
10903 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
10904
10905 return col;
10906}
10907
10908/*
10909 * Get list of spelling suggestions.
10910 * Used for Insert mode completion CTRL-X ?.
10911 * Returns the number of matches. The matches are in "matchp[]", array of
10912 * allocated strings.
10913 */
10914/*ARGSUSED*/
10915 int
10916expand_spelling(lnum, col, pat, matchp)
10917 linenr_T lnum;
10918 int col;
10919 char_u *pat;
10920 char_u ***matchp;
10921{
10922 garray_T ga;
10923
10924 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
10925 *matchp = ga.ga_data;
10926 return ga.ga_len;
10927}
10928#endif
10929
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010930#endif /* FEAT_SYN_HL */