blob: 7513d0a73d3b2e158d1b7bbaee7ca525d82f76f4 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram 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/*
75 * The double scoring mechanism is based on the principle that there are two
76 * kinds of spelling mistakes:
77 * 1. You know how to spell the word, but mistype something. This results in
78 * a small editing distance (character swapped/omitted/inserted) and
79 * possibly a word that sounds completely different.
80 * 2. You don't know how to spell the word and type something that sounds
81 * right. The edit distance can be big but the word is similar after
82 * sound-folding.
83 * Since scores for these two mistakes will be very different we use a list
84 * for each.
85 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
86 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000087 */
88
89/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000090 * Vim spell file format: <HEADER>
91 * <SUGGEST>
92 * <LWORDTREE>
93 * <KWORDTREE>
94 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000096 * <HEADER>: <fileID>
97 * <regioncnt> <regionname> ...
98 * <charflagslen> <charflags>
99 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000100 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000101 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +0000102 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000103 * <fileID> 10 bytes "VIMspell09"
Bram Moolenaar51485f02005-06-04 21:55:20 +0000104 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000105 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000106 * First <regionname> is region 1.
107 *
108 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
109 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000110 * 0x01 word character CF_WORD
111 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000112 * <fcharslen> 2 bytes Number of bytes in <fchars>.
113 * <fchars> N bytes Folded characters, first one is for character 128.
114 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000115 * <midwordlen> 2 bytes Number of bytes in <midword>.
116 * <midword> N bytes Characters that are word characters only when used
117 * in the middle of a word.
118 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000119 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
120 *
121 * <prefcond> : <condlen> <condstr>
122 *
123 * <condlen> 1 byte Length of <condstr>.
124 *
125 * <condstr> N bytes Condition for the prefix.
126 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000127 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000128 * <SUGGEST> : <repcount> <rep> ...
129 * <salflags> <salcount> <sal> ...
130 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000131 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000132 * <repcount> 2 bytes number of <rep> items, MSB first.
133 *
134 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
135 *
136 * <repfromlen> 1 byte length of <repfrom>
137 *
138 * <repfrom> N bytes "from" part of replacement
139 *
140 * <reptolen> 1 byte length of <repto>
141 *
142 * <repto> N bytes "to" part of replacement
143 *
144 * <salflags> 1 byte flags for soundsalike conversion:
145 * SAL_F0LLOWUP
146 * SAL_COLLAPSE
147 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000148 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
149 *
150 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000151 *
152 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
153 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000154 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000155 *
156 * <salfrom> N bytes "from" part of soundsalike
157 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000158 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000159 *
160 * <salto> N bytes "to" part of soundsalike
161 *
162 * <maplen> 2 bytes length of <mapstr>, MSB first
163 *
164 * <mapstr> N bytes String with sequences of similar characters,
165 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000166 *
167 *
168 * <LWORDTREE>: <wordtree>
169 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000170 * <KWORDTREE>: <wordtree>
171 *
172 * <PREFIXTREE>: <wordtree>
173 *
174 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000175 * <wordtree>: <nodecount> <nodedata> ...
176 *
177 * <nodecount> 4 bytes Number of nodes following. MSB first.
178 *
179 * <nodedata>: <siblingcount> <sibling> ...
180 *
181 * <siblingcount> 1 byte Number of siblings in this node. The siblings
182 * follow in sorted order.
183 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000184 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000185 * | <flags> [<flags2>] [<region>] [<prefixID>]
186 * | [<pflags>] <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
188 * <byte> 1 byte Byte value of the sibling. Special cases:
189 * BY_NOFLAGS: End of word without flags and for all
190 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000191 * For PREFIXTREE <prefixID> and
192 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000193 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000194 * For PREFIXTREE <pflags>, <prefixID>
195 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000196 * BY_FLAGS2: End of word, <flags> and <flags2>
197 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000198 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 * and <xbyte> follow.
200 *
201 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
202 *
203 * <xbyte> 1 byte byte value of the sibling.
204 *
205 * <flags> 1 byte bitmask of:
206 * WF_ALLCAP word must have only capitals
207 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000208 * WF_KEEPCAP keep-case word
209 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000210 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000211 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000213 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000215 * <flags2> 1 byte Only used when there are postponed prefixes.
216 * Bitmask of:
217 * WF_HAS_AFF >> 8 word includes affix
218 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000219 * <pflags> 1 byte bitmask of:
220 * WFP_RARE rare prefix
221 * WFP_NC non-combining prefix
222 * WFP_UP letter after prefix made upper case
223 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000224 * <region> 1 byte Bitmask for regions in which word is valid. When
225 * omitted it's valid in all regions.
226 * Lowest bit is for region 1.
227 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000228 * <prefixID> 1 byte ID of prefix that can be used with this word. For
229 * PREFIXTREE used for the required prefix ID.
230 *
231 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
232 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000233 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000234 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235 */
236
Bram Moolenaare19defe2005-03-21 08:23:33 +0000237#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
238# include <io.h> /* for lseek(), must be before vim.h */
239#endif
240
241#include "vim.h"
242
243#if defined(FEAT_SYN_HL) || defined(PROTO)
244
245#ifdef HAVE_FCNTL_H
246# include <fcntl.h>
247#endif
248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000249#define MAXWLEN 250 /* Assume max. word len is this many bytes.
250 Some places assume a word length fits in a
251 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000252
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000253/* Type used for indexes in the word tree need to be at least 3 bytes. If int
254 * is 8 bytes we could use something smaller, but what? */
255#if SIZEOF_INT > 2
256typedef int idx_T;
257#else
258typedef long idx_T;
259#endif
260
261/* Flags used for a word. Only the lowest byte can be used, the region byte
262 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000263#define WF_REGION 0x01 /* region byte follows */
264#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
265#define WF_ALLCAP 0x04 /* word must be all capitals */
266#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000267#define WF_BANNED 0x10 /* bad word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000268#define WF_PFX 0x20 /* prefix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000269#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000270#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000271
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000272/* for <flags2>, shifted up one byte to be used in wn_flags */
273#define WF_HAS_AFF 0x0100 /* word includes affix */
274
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000275#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276
Bram Moolenaar53805d12005-08-01 07:08:33 +0000277/* flags for <pflags> */
278#define WFP_RARE 0x01 /* rare prefix */
279#define WFP_NC 0x02 /* prefix is not combining */
280#define WFP_UP 0x04 /* to-upper prefix */
281
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000282/* flags for postponed prefixes. Must be above prefixID (one byte)
283 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000284#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
285 * postponed prefix */
286#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
287 * postponed prefix */
288#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
289 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000290
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000291/* Special byte values for <byte>. Some are only used in the tree for
292 * postponed prefixes, some only in the other trees. This is a bit messy... */
293#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000294 * postponed prefix: no <pflags> */
295#define BY_INDEX 1 /* child is shared, index follows */
296#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
297 * postponed prefix: <pflags> follows */
298#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
299 * follow; never used in prefix tree */
300#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000302/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000303 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304 * One replacement: from "ft_from" to "ft_to". */
305typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000306{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000307 char_u *ft_from;
308 char_u *ft_to;
309} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000310
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000311/* Info from "SAL" entries in ".aff" file used in sl_sal.
312 * The info is split for quick processing by spell_soundfold().
313 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
314typedef struct salitem_S
315{
316 char_u *sm_lead; /* leading letters */
317 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000318 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000319 char_u *sm_rules; /* rules like ^, $, priority */
320 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000321#ifdef FEAT_MBYTE
322 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000323 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000324 int *sm_to_w; /* wide character copy of "sm_to" */
325#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000326} salitem_T;
327
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000328#ifdef FEAT_MBYTE
329typedef int salfirst_T;
330#else
331typedef short salfirst_T;
332#endif
333
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000334/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000335 * Structure used to store words and other info for one language, loaded from
336 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000337 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
338 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
339 *
340 * The "byts" array stores the possible bytes in each tree node, preceded by
341 * the number of possible bytes, sorted on byte value:
342 * <len> <byte1> <byte2> ...
343 * The "idxs" array stores the index of the child node corresponding to the
344 * byte in "byts".
345 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000346 * the flags, region mask and prefixID for the word. There may be several
347 * zeros in sequence for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000348 */
349typedef struct slang_S slang_T;
350struct slang_S
351{
352 slang_T *sl_next; /* next language */
353 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000354 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000355 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000356
Bram Moolenaar51485f02005-06-04 21:55:20 +0000357 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000358 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000359 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000360 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000361 char_u *sl_pbyts; /* prefix tree word bytes */
362 idx_T *sl_pidxs; /* prefix tree word indexes */
363
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000364 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000365
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000366 char_u *sl_midword; /* MIDWORD string or NULL */
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 Moolenaar53805d12005-08-01 07:08:33 +0000422#define VIMSPELLMAGIC "VIMspell09" /* 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 Moolenaar9f30f502005-06-14 22:01:04 +0000477#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000478#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000479#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000480#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000481#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000482#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000483
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000484#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
486 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000487
488#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000489#define SCORE_MAXMAX 999999 /* accept any score */
490
491/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000492 * Structure to store info for word matching.
493 */
494typedef struct matchinf_S
495{
496 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000497
498 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000499 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000500 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000501 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000502 char_u *mi_cend; /* char after what was used for
503 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000504
505 /* case-folded text */
506 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000507 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000508
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000509 /* for when checking word after a prefix */
510 int mi_prefarridx; /* index in sl_pidxs with list of
511 prefixID/condition */
512 int mi_prefcnt; /* number of entries at mi_prefarridx */
513 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000514#ifdef FEAT_MBYTE
515 int mi_cprefixlen; /* byte length of prefix in original
516 case */
517#else
518# define mi_cprefixlen mi_prefixlen /* it's the same value */
519#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000520
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000521 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000522 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000523 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000524 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525} matchinf_T;
526
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000527/*
528 * The tables used for recognizing word characters according to spelling.
529 * These are only used for the first 256 characters of 'encoding'.
530 */
531typedef struct spelltab_S
532{
533 char_u st_isw[256]; /* flags: is word char */
534 char_u st_isu[256]; /* flags: is uppercase char */
535 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000536 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000537} spelltab_T;
538
539static spelltab_T spelltab;
540static int did_set_spelltab;
541
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000542#define CF_WORD 0x01
543#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000544
545static void clear_spell_chartab __ARGS((spelltab_T *sp));
546static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000547static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
548static int spell_iswordp_nmw __ARGS((char_u *p));
549#ifdef FEAT_MBYTE
550static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
551#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000552static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000553
554/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000555 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000556 */
557typedef enum
558{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000559 STATE_START = 0, /* At start of node check for NUL bytes (goodword
560 * ends); if badword ends there is a match, otherwise
561 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000562 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000563 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000564 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
565 STATE_PLAIN, /* Use each byte of the node. */
566 STATE_DEL, /* Delete a byte from the bad word. */
567 STATE_INS, /* Insert a byte in the bad word. */
568 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000569 STATE_UNSWAP, /* Undo swap two characters. */
570 STATE_SWAP3, /* Swap two characters over three. */
571 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000572 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000573 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000574 STATE_REP_INI, /* Prepare for using REP items. */
575 STATE_REP, /* Use matching REP items from the .aff file. */
576 STATE_REP_UNDO, /* Undo a REP item replacement. */
577 STATE_FINAL /* End of this node. */
578} state_T;
579
580/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000581 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000582 */
583typedef struct trystate_S
584{
Bram Moolenaarea424162005-06-16 21:51:00 +0000585 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000586 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000587 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000588 short ts_curi; /* index in list of child nodes */
589 char_u ts_fidx; /* index in fword[], case-folded bad word */
590 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
591 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000592 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
593 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000594#ifdef FEAT_MBYTE
595 char_u ts_tcharlen; /* number of bytes in tword character */
596 char_u ts_tcharidx; /* current byte index in tword character */
597 char_u ts_isdiff; /* DIFF_ values */
598 char_u ts_fcharstart; /* index in fword where badword char started */
599#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000600 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000601 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000602 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603} trystate_T;
604
Bram Moolenaarea424162005-06-16 21:51:00 +0000605/* values for ts_isdiff */
606#define DIFF_NONE 0 /* no different byte (yet) */
607#define DIFF_YES 1 /* different byte found */
608#define DIFF_INSERT 2 /* inserting character */
609
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000610/* special values ts_prefixdepth */
611#define PREFIXTREE 0xfe /* walking through the prefix tree */
612#define NOPREFIX 0xff /* not using prefixes */
613
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000614/* mode values for find_word */
615#define FIND_FOLDWORD 0 /* find word case-folded */
616#define FIND_KEEPWORD 1 /* find keep-case word */
617#define FIND_PREFIX 2 /* find word after prefix */
618
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000619static slang_T *slang_alloc __ARGS((char_u *lang));
620static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000621static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000622static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000623static 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 +0000624static void find_prefix __ARGS((matchinf_T *mip));
625static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000626static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000627static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000628static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000629static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000630static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000631static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000632static 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 +0000633static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000634static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
635static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000636#ifdef FEAT_MBYTE
637static int *mb_str2wide __ARGS((char_u *s));
638#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000639static 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 +0000640static void clear_midword __ARGS((buf_T *buf));
641static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000642static int find_region __ARGS((char_u *rp, char_u *region));
643static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000644static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000645static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000646static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000647static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
648static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000649static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000650static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000651static 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 +0000652#ifdef FEAT_EVAL
653static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
654#endif
655static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
656static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000657static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000658static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000659static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000660static void suggest_try_special __ARGS((suginfo_T *su));
661static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000663#ifdef FEAT_MBYTE
664static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
665#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000666static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000667static void score_comp_sal __ARGS((suginfo_T *su));
668static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000669static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000670static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000671static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000672static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000673static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000674static 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 +0000675static void add_banned __ARGS((suginfo_T *su, char_u *word));
676static int was_banned __ARGS((suginfo_T *su, char_u *word));
677static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000678static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000679static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000680static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
681static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
682static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000683#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000684static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000685#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000686static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000687static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000688static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
689static 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 +0000690
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000691/*
692 * Use our own character-case definitions, because the current locale may
693 * differ from what the .spl file uses.
694 * These must not be called with negative number!
695 */
696#ifndef FEAT_MBYTE
697/* Non-multi-byte implementation. */
698# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
699# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
700# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
701#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000702# if defined(HAVE_WCHAR_H)
703# include <wchar.h> /* for towupper() and towlower() */
704# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000705/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
706 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
707 * the "w" library function for characters above 255 if available. */
708# ifdef HAVE_TOWLOWER
709# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
710 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
711# else
712# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
713 : (c) < 256 ? spelltab.st_fold[c] : (c))
714# endif
715
716# ifdef HAVE_TOWUPPER
717# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
718 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
719# else
720# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
721 : (c) < 256 ? spelltab.st_upper[c] : (c))
722# endif
723
724# ifdef HAVE_ISWUPPER
725# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
726 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
727# else
728# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000729 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000730# endif
731#endif
732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000733
734static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000735static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000736static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000737
738/*
739 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000740 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000741 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
742 * or when it's OK it remains unchanged.
743 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000744 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000745 * "capcol" is used to check for a Capitalised word after the end of a
746 * sentence. If it's zero then perform the check. Return the column where to
747 * check next, or -1 when no sentence end was found. If it's NULL then don't
748 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000749 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000750 * Returns the length of the word in bytes, also when it's OK, so that the
751 * caller can skip over the word.
752 */
753 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000754spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000755 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000756 char_u *ptr;
757 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000758 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000759{
760 matchinf_T mi; /* Most things are put in "mi" so that it can
761 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000762 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000763 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000764
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000765 /* A word never starts at a space or a control character. Return quickly
766 * then, skipping over the character. */
767 if (*ptr <= ' ')
768 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000769
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000771 * 0X99FF. But when a word character follows do check spelling to find
772 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000773 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000774 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000775 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
776 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000777 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000778 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000779 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 nrlen = mi.mi_end - ptr;
781 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000782 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000784
785 /* Try including the digits in the word. */
786 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000787 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000788 else
789 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000790
Bram Moolenaar0c405862005-06-22 22:26:26 +0000791 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000792 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000793 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000794 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000795 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000796 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000797 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000798 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000799
800 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
801 {
802 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000803 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000804 if (!SPELL_ISUPPER(c))
805 {
806 *attrp = highlight_attr[HLF_SPC];
807 return (int)(mi.mi_fend - ptr);
808 }
809 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000810 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000811 if (capcol != NULL)
812 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000813
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000814 /* We always use the characters up to the next non-word character,
815 * also for bad words. */
816 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000817
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000818 /* Check caps type later. */
819 mi.mi_capflags = 0;
820 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000821 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000822
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000823 /* Include one non-word character so that we can check for the
824 * word end. */
825 if (*mi.mi_fend != NUL)
826 mb_ptr_adv(mi.mi_fend);
827
828 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
829 MAXWLEN + 1);
830 mi.mi_fwordlen = STRLEN(mi.mi_fword);
831
832 /* The word is bad unless we recognize it. */
833 mi.mi_result = SP_BAD;
834
835 /*
836 * Loop over the languages specified in 'spelllang'.
837 * We check them all, because a matching word may be longer than an
838 * already found matching word.
839 */
840 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
841 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
842 {
843 /* Check for a matching word in case-folded words. */
844 find_word(&mi, FIND_FOLDWORD);
845
846 /* Check for a matching word in keep-case words. */
847 find_word(&mi, FIND_KEEPWORD);
848
849 /* Check for matching prefixes. */
850 find_prefix(&mi);
851 }
852
853 if (mi.mi_result != SP_OK)
854 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000855 /* If we found a number skip over it. Allows for "42nd". Do flag
856 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000857 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000858 {
859 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
860 return nrlen;
861 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000862
863 /* When we are at a non-word character there is no error, just
864 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000865 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000866 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000867 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
868 {
869 regmatch_T regmatch;
870
871 /* Check for end of sentence. */
872 regmatch.regprog = wp->w_buffer->b_cap_prog;
873 regmatch.rm_ic = FALSE;
874 if (vim_regexec(&regmatch, ptr, 0))
875 *capcol = (int)(regmatch.endp[0] - ptr);
876 }
877
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000878#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000879 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000880 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000881#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000882 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000883 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000884
885 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
886 *attrp = highlight_attr[HLF_SPB];
887 else if (mi.mi_result == SP_RARE)
888 *attrp = highlight_attr[HLF_SPR];
889 else
890 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000891 }
892
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893 return (int)(mi.mi_end - ptr);
894}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000895
Bram Moolenaar51485f02005-06-04 21:55:20 +0000896/*
897 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000898 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
899 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
900 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
901 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000902 *
903 * For a match mip->mi_result is updated.
904 */
905 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000906find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000907 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000908 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000910 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000911 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000912 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000913 int endidxcnt = 0;
914 int len;
915 int wlen = 0;
916 int flen;
917 int c;
918 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000919 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000920#ifdef FEAT_MBYTE
921 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000922 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000923#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000924 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000925 slang_T *slang = mip->mi_lp->lp_slang;
926 unsigned flags;
927 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000928 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000929
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000930 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000931 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000932 /* Check for word with matching case in keep-case tree. */
933 ptr = mip->mi_word;
934 flen = 9999; /* no case folding, always enough bytes */
935 byts = slang->sl_kbyts;
936 idxs = slang->sl_kidxs;
937 }
938 else
939 {
940 /* Check for case-folded in case-folded tree. */
941 ptr = mip->mi_fword;
942 flen = mip->mi_fwordlen; /* available case-folded bytes */
943 byts = slang->sl_fbyts;
944 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000945
946 if (mode == FIND_PREFIX)
947 {
948 /* Skip over the prefix. */
949 wlen = mip->mi_prefixlen;
950 flen -= mip->mi_prefixlen;
951 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000952 }
953
Bram Moolenaar51485f02005-06-04 21:55:20 +0000954 if (byts == NULL)
955 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000956
Bram Moolenaar51485f02005-06-04 21:55:20 +0000957 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000958 * Repeat advancing in the tree until:
959 * - there is a byte that doesn't match,
960 * - we reach the end of the tree,
961 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000962 */
963 for (;;)
964 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000965 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000966 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000967
968 len = byts[arridx++];
969
970 /* If the first possible byte is a zero the word could end here.
971 * Remember this index, we first check for the longest word. */
972 if (byts[arridx] == 0)
973 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000974 if (endidxcnt == MAXWLEN)
975 {
976 /* Must be a corrupted spell file. */
977 EMSG(_(e_format));
978 return;
979 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000980 endlen[endidxcnt] = wlen;
981 endidx[endidxcnt++] = arridx++;
982 --len;
983
984 /* Skip over the zeros, there can be several flag/region
985 * combinations. */
986 while (len > 0 && byts[arridx] == 0)
987 {
988 ++arridx;
989 --len;
990 }
991 if (len == 0)
992 break; /* no children, word must end here */
993 }
994
995 /* Stop looking at end of the line. */
996 if (ptr[wlen] == NUL)
997 break;
998
999 /* Perform a binary search in the list of accepted bytes. */
1000 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001001 if (c == TAB) /* <Tab> is handled like <Space> */
1002 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001003 lo = arridx;
1004 hi = arridx + len - 1;
1005 while (lo < hi)
1006 {
1007 m = (lo + hi) / 2;
1008 if (byts[m] > c)
1009 hi = m - 1;
1010 else if (byts[m] < c)
1011 lo = m + 1;
1012 else
1013 {
1014 lo = hi = m;
1015 break;
1016 }
1017 }
1018
1019 /* Stop if there is no matching byte. */
1020 if (hi < lo || byts[lo] != c)
1021 break;
1022
1023 /* Continue at the child (if there is one). */
1024 arridx = idxs[lo];
1025 ++wlen;
1026 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001027
1028 /* One space in the good word may stand for several spaces in the
1029 * checked word. */
1030 if (c == ' ')
1031 {
1032 for (;;)
1033 {
1034 if (flen <= 0 && *mip->mi_fend != NUL)
1035 flen = fold_more(mip);
1036 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1037 break;
1038 ++wlen;
1039 --flen;
1040 }
1041 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 }
1043
1044 /*
1045 * Verify that one of the possible endings is valid. Try the longest
1046 * first.
1047 */
1048 while (endidxcnt > 0)
1049 {
1050 --endidxcnt;
1051 arridx = endidx[endidxcnt];
1052 wlen = endlen[endidxcnt];
1053
1054#ifdef FEAT_MBYTE
1055 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1056 continue; /* not at first byte of character */
1057#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001058 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059 continue; /* next char is a word character */
1060
1061#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001062 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001063 {
1064 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001065 * when folding case. This can be slow, take a shortcut when the
1066 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001067 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001068 if (STRNCMP(ptr, p, wlen) != 0)
1069 {
1070 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1071 mb_ptr_adv(p);
1072 wlen = p - mip->mi_word;
1073 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001074 }
1075#endif
1076
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001077 /* Check flags and region. For FIND_PREFIX check the condition and
1078 * prefix ID.
1079 * Repeat this if there are more flags/region alternatives until there
1080 * is a match. */
1081 res = SP_BAD;
1082 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1083 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001084 {
1085 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001086
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001087 /* For the fold-case tree check that the case of the checked word
1088 * matches with what the word in the tree requires.
1089 * For keep-case tree the case is always right. For prefixes we
1090 * don't bother to check. */
1091 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001092 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001093 if (mip->mi_cend != mip->mi_word + wlen)
1094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001095 /* mi_capflags was set for a different word length, need
1096 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001097 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001098 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001099 }
1100
Bram Moolenaar0c405862005-06-22 22:26:26 +00001101 if (mip->mi_capflags == WF_KEEPCAP
1102 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001103 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001104 }
1105
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001106 /* When mode is FIND_PREFIX the word must support the prefix:
1107 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001108 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001109 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001110 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001111 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001112 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001113 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001114 mip->mi_word + mip->mi_cprefixlen, slang,
1115 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001116 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001117 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001118
1119 /* Use the WF_RARE flag for a rare prefix. */
1120 if (c & WF_RAREPFX)
1121 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001122 }
1123
1124 if (flags & WF_BANNED)
1125 res = SP_BANNED;
1126 else if (flags & WF_REGION)
1127 {
1128 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001129 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001130 res = SP_OK;
1131 else
1132 res = SP_LOCAL;
1133 }
1134 else if (flags & WF_RARE)
1135 res = SP_RARE;
1136 else
1137 res = SP_OK;
1138
1139 /* Always use the longest match and the best result. */
1140 if (mip->mi_result > res)
1141 {
1142 mip->mi_result = res;
1143 mip->mi_end = mip->mi_word + wlen;
1144 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001145 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001146 mip->mi_end = mip->mi_word + wlen;
1147
1148 if (res == SP_OK)
1149 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001150 }
1151
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001152 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001153 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001154 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001155}
1156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001158 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1159 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001160 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001161 */
1162 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001163valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001164 int totprefcnt; /* nr of prefix IDs */
1165 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001166 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001167 char_u *word;
1168 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001169 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001170{
1171 int prefcnt;
1172 int pidx;
1173 regprog_T *rp;
1174 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001175 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001176
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001177 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001178 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1179 {
1180 pidx = slang->sl_pidxs[arridx + prefcnt];
1181
1182 /* Check the prefix ID. */
1183 if (prefid != (pidx & 0xff))
1184 continue;
1185
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001186 /* Check if the prefix doesn't combine and the word already has a
1187 * suffix. */
1188 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1189 continue;
1190
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001191 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001192 * stored in the two bytes above the prefix ID byte. */
1193 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001194 if (rp != NULL)
1195 {
1196 regmatch.regprog = rp;
1197 regmatch.rm_ic = FALSE;
1198 if (!vim_regexec(&regmatch, word, 0))
1199 continue;
1200 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001201 else if (cond_req)
1202 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001203
Bram Moolenaar53805d12005-08-01 07:08:33 +00001204 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001205 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001206 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001207 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001208}
1209
1210/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001211 * Check if the word at "mip->mi_word" has a matching prefix.
1212 * If it does, then check the following word.
1213 *
1214 * For a match mip->mi_result is updated.
1215 */
1216 static void
1217find_prefix(mip)
1218 matchinf_T *mip;
1219{
1220 idx_T arridx = 0;
1221 int len;
1222 int wlen = 0;
1223 int flen;
1224 int c;
1225 char_u *ptr;
1226 idx_T lo, hi, m;
1227 slang_T *slang = mip->mi_lp->lp_slang;
1228 char_u *byts;
1229 idx_T *idxs;
1230
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001231 byts = slang->sl_pbyts;
1232 if (byts == NULL)
1233 return; /* array is empty */
1234
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001235 /* We use the case-folded word here, since prefixes are always
1236 * case-folded. */
1237 ptr = mip->mi_fword;
1238 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001239 idxs = slang->sl_pidxs;
1240
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001241 /*
1242 * Repeat advancing in the tree until:
1243 * - there is a byte that doesn't match,
1244 * - we reach the end of the tree,
1245 * - or we reach the end of the line.
1246 */
1247 for (;;)
1248 {
1249 if (flen == 0 && *mip->mi_fend != NUL)
1250 flen = fold_more(mip);
1251
1252 len = byts[arridx++];
1253
1254 /* If the first possible byte is a zero the prefix could end here.
1255 * Check if the following word matches and supports the prefix. */
1256 if (byts[arridx] == 0)
1257 {
1258 /* There can be several prefixes with different conditions. We
1259 * try them all, since we don't know which one will give the
1260 * longest match. The word is the same each time, pass the list
1261 * of possible prefixes to find_word(). */
1262 mip->mi_prefarridx = arridx;
1263 mip->mi_prefcnt = len;
1264 while (len > 0 && byts[arridx] == 0)
1265 {
1266 ++arridx;
1267 --len;
1268 }
1269 mip->mi_prefcnt -= len;
1270
1271 /* Find the word that comes after the prefix. */
1272 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001273#ifdef FEAT_MBYTE
1274 if (has_mbyte)
1275 {
1276 /* Case-folded length may differ from original length. */
1277 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1278 mip->mi_word);
1279 }
1280 else
1281 mip->mi_cprefixlen = wlen;
1282#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001283 find_word(mip, FIND_PREFIX);
1284
1285
1286 if (len == 0)
1287 break; /* no children, word must end here */
1288 }
1289
1290 /* Stop looking at end of the line. */
1291 if (ptr[wlen] == NUL)
1292 break;
1293
1294 /* Perform a binary search in the list of accepted bytes. */
1295 c = ptr[wlen];
1296 lo = arridx;
1297 hi = arridx + len - 1;
1298 while (lo < hi)
1299 {
1300 m = (lo + hi) / 2;
1301 if (byts[m] > c)
1302 hi = m - 1;
1303 else if (byts[m] < c)
1304 lo = m + 1;
1305 else
1306 {
1307 lo = hi = m;
1308 break;
1309 }
1310 }
1311
1312 /* Stop if there is no matching byte. */
1313 if (hi < lo || byts[lo] != c)
1314 break;
1315
1316 /* Continue at the child (if there is one). */
1317 arridx = idxs[lo];
1318 ++wlen;
1319 --flen;
1320 }
1321}
1322
1323/*
1324 * Need to fold at least one more character. Do until next non-word character
1325 * for efficiency.
1326 * Return the length of the folded chars in bytes.
1327 */
1328 static int
1329fold_more(mip)
1330 matchinf_T *mip;
1331{
1332 int flen;
1333 char_u *p;
1334
1335 p = mip->mi_fend;
1336 do
1337 {
1338 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001339 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001340
1341 /* Include the non-word character so that we can check for the
1342 * word end. */
1343 if (*mip->mi_fend != NUL)
1344 mb_ptr_adv(mip->mi_fend);
1345
1346 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1347 mip->mi_fword + mip->mi_fwordlen,
1348 MAXWLEN - mip->mi_fwordlen);
1349 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1350 mip->mi_fwordlen += flen;
1351 return flen;
1352}
1353
1354/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001355 * Check case flags for a word. Return TRUE if the word has the requested
1356 * case.
1357 */
1358 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001359spell_valid_case(wordflags, treeflags)
1360 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001361 int treeflags; /* flags for the word in the spell tree */
1362{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001363 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001364 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001365 && ((treeflags & WF_ONECAP) == 0
1366 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001367}
1368
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001369/*
1370 * Return TRUE if spell checking is not enabled.
1371 */
1372 static int
1373no_spell_checking()
1374{
1375 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1376 {
1377 EMSG(_("E756: Spell checking is not enabled"));
1378 return TRUE;
1379 }
1380 return FALSE;
1381}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001382
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001383/*
1384 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001385 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001386 * Return OK if found, FAIL otherwise.
1387 */
1388 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001389spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001390 int dir; /* FORWARD or BACKWARD */
1391 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001392 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001393{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001394 linenr_T lnum;
1395 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001396 char_u *line;
1397 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001398 char_u *endp;
1399 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001400 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001401 int has_syntax = syntax_present(curbuf);
1402 int col;
1403 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001404 char_u *buf = NULL;
1405 int buflen = 0;
1406 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001407 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001408
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001409 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001410 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001411
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001412 /*
1413 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001414 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001415 *
1416 * When searching backwards, we continue in the line to find the last
1417 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001418 *
1419 * We concatenate the start of the next line, so that wrapped words work
1420 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1421 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001422 */
1423 lnum = curwin->w_cursor.lnum;
1424 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001425
1426 while (!got_int)
1427 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001428 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001429
Bram Moolenaar0c405862005-06-22 22:26:26 +00001430 len = STRLEN(line);
1431 if (buflen < len + MAXWLEN + 2)
1432 {
1433 vim_free(buf);
1434 buflen = len + MAXWLEN + 2;
1435 buf = alloc(buflen);
1436 if (buf == NULL)
1437 break;
1438 }
1439
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001440 /* In first line check first word for Capital. */
1441 if (lnum == 1)
1442 capcol = 0;
1443
1444 /* For checking first word with a capital skip white space. */
1445 if (capcol == 0)
1446 capcol = skipwhite(line) - line;
1447
Bram Moolenaar0c405862005-06-22 22:26:26 +00001448 /* Copy the line into "buf" and append the start of the next line if
1449 * possible. */
1450 STRCPY(buf, line);
1451 if (lnum < curbuf->b_ml.ml_line_count)
1452 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1453
1454 p = buf + skip;
1455 endp = buf + len;
1456 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001457 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458 /* When searching backward don't search after the cursor. */
1459 if (dir == BACKWARD
1460 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001461 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001462 break;
1463
1464 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001465 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001466 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001467
1468 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001469 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001470 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001471 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001472 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001473 /* When searching forward only accept a bad word after
1474 * the cursor. */
1475 if (dir == BACKWARD
1476 || lnum > curwin->w_cursor.lnum
1477 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001478 && (colnr_T)(curline ? p - buf + len
1479 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001480 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001481 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001482 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001483 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001484 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001485 (void)syn_get_id(lnum, (colnr_T)col,
1486 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001487 }
1488 else
1489 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001490
Bram Moolenaar51485f02005-06-04 21:55:20 +00001491 if (can_spell)
1492 {
1493 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001494 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001495#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001496 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001497#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001498 if (dir == FORWARD)
1499 {
1500 /* No need to search further. */
1501 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001502 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001503 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001504 }
1505 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001506 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001507 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508 }
1509
Bram Moolenaar51485f02005-06-04 21:55:20 +00001510 /* advance to character after the word */
1511 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001512 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001513 }
1514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001515 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001516 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001517
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001518 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001519 if (dir == BACKWARD)
1520 {
1521 if (found_pos.lnum != 0)
1522 {
1523 /* Use the last match in the line. */
1524 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001525 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001526 return OK;
1527 }
1528 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001529 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001530 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001531 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001532 }
1533 else
1534 {
1535 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001536 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001537 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001538
1539 /* Skip the characters at the start of the next line that were
1540 * included in a match crossing line boundaries. */
1541 if (attr == 0)
1542 skip = p - endp;
1543 else
1544 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001545
1546 /* Capscol skips over the inserted space. */
1547 --capcol;
1548
1549 /* But after empty line check first word in next line */
1550 if (*skipwhite(line) == NUL)
1551 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001552 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001553
1554 line_breakcheck();
1555 }
1556
Bram Moolenaar0c405862005-06-22 22:26:26 +00001557 vim_free(buf);
1558 return FAIL;
1559}
1560
1561/*
1562 * For spell checking: concatenate the start of the following line "line" into
1563 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1564 */
1565 void
1566spell_cat_line(buf, line, maxlen)
1567 char_u *buf;
1568 char_u *line;
1569 int maxlen;
1570{
1571 char_u *p;
1572 int n;
1573
1574 p = skipwhite(line);
1575 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1576 p = skipwhite(p + 1);
1577
1578 if (*p != NUL)
1579 {
1580 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001581 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001582 n = p - line;
1583 if (n >= maxlen)
1584 n = maxlen - 1;
1585 vim_memset(buf + 1, ' ', n);
1586 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001587}
1588
1589/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001590 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001591 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001592 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001593 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001594spell_load_lang(lang)
1595 char_u *lang;
1596{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001597 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001598 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001599 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001600
Bram Moolenaarb765d632005-06-07 21:00:02 +00001601 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001602 * It's truncated when an error is detected. */
1603 STRCPY(langcp, lang);
1604
Bram Moolenaarb765d632005-06-07 21:00:02 +00001605 /*
1606 * Find the first spell file for "lang" in 'runtimepath' and load it.
1607 */
1608 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1609 "spell/%s.%s.spl", lang, spell_enc());
1610 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001611
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001612 if (r == FAIL && *langcp != NUL)
1613 {
1614 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001615 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001616 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001617 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001618 }
1619
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001620 if (r == FAIL)
1621 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1622 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001623 else if (*langcp != NUL)
1624 {
1625 /* Load all the additions. */
1626 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1627 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1628 }
1629}
1630
1631/*
1632 * Return the encoding used for spell checking: Use 'encoding', except that we
1633 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1634 */
1635 static char_u *
1636spell_enc()
1637{
1638
1639#ifdef FEAT_MBYTE
1640 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1641 return p_enc;
1642#endif
1643 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001644}
1645
1646/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001647 * Get the name of the .spl file for the internal wordlist into
1648 * "fname[MAXPATHL]".
1649 */
1650 static void
1651int_wordlist_spl(fname)
1652 char_u *fname;
1653{
1654 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1655 int_wordlist, spell_enc());
1656}
1657
1658/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001659 * Allocate a new slang_T.
1660 * Caller must fill "sl_next".
1661 */
1662 static slang_T *
1663slang_alloc(lang)
1664 char_u *lang;
1665{
1666 slang_T *lp;
1667
Bram Moolenaar51485f02005-06-04 21:55:20 +00001668 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001669 if (lp != NULL)
1670 {
1671 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001672 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001673 }
1674 return lp;
1675}
1676
1677/*
1678 * Free the contents of an slang_T and the structure itself.
1679 */
1680 static void
1681slang_free(lp)
1682 slang_T *lp;
1683{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001684 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001685 vim_free(lp->sl_fname);
1686 slang_clear(lp);
1687 vim_free(lp);
1688}
1689
1690/*
1691 * Clear an slang_T so that the file can be reloaded.
1692 */
1693 static void
1694slang_clear(lp)
1695 slang_T *lp;
1696{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001697 garray_T *gap;
1698 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001699 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001700 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001701
Bram Moolenaar51485f02005-06-04 21:55:20 +00001702 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001703 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001704 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001705 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001706 vim_free(lp->sl_pbyts);
1707 lp->sl_pbyts = NULL;
1708
Bram Moolenaar51485f02005-06-04 21:55:20 +00001709 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001710 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001711 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001712 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001713 vim_free(lp->sl_pidxs);
1714 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001715
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001716 gap = &lp->sl_rep;
1717 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001719 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1720 vim_free(ftp->ft_from);
1721 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001722 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001723 ga_clear(gap);
1724
1725 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001726 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001727 {
1728 /* "ga_len" is set to 1 without adding an item for latin1 */
1729 if (gap->ga_data != NULL)
1730 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1731 for (i = 0; i < gap->ga_len; ++i)
1732 vim_free(((int **)gap->ga_data)[i]);
1733 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001734 else
1735 /* SAL items: free salitem_T items */
1736 while (gap->ga_len > 0)
1737 {
1738 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1739 vim_free(smp->sm_lead);
1740 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1741 vim_free(smp->sm_to);
1742#ifdef FEAT_MBYTE
1743 vim_free(smp->sm_lead_w);
1744 vim_free(smp->sm_oneof_w);
1745 vim_free(smp->sm_to_w);
1746#endif
1747 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001748 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001749
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001750 for (i = 0; i < lp->sl_prefixcnt; ++i)
1751 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001752 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001753 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001754 lp->sl_prefprog = NULL;
1755
1756 vim_free(lp->sl_midword);
1757 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001758
Bram Moolenaarea424162005-06-16 21:51:00 +00001759#ifdef FEAT_MBYTE
1760 {
1761 int todo = lp->sl_map_hash.ht_used;
1762 hashitem_T *hi;
1763
1764 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1765 if (!HASHITEM_EMPTY(hi))
1766 {
1767 --todo;
1768 vim_free(hi->hi_key);
1769 }
1770 }
1771 hash_clear(&lp->sl_map_hash);
1772#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001773}
1774
1775/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001776 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001777 * Invoked through do_in_runtimepath().
1778 */
1779 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001780spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001782 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001783{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001784 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001785}
1786
1787/*
1788 * Load one spell file and store the info into a slang_T.
1789 *
1790 * This is invoked in two ways:
1791 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1792 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1793 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1794 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001795 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001796 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797 static slang_T *
1798spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001799 char_u *fname;
1800 char_u *lang;
1801 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001802 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001803{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804 FILE *fd;
1805 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001807 char_u *bp;
1808 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001809 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001810 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001811 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001812 int round;
1813 char_u *save_sourcing_name = sourcing_name;
1814 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001815 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001816 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001817 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001818 garray_T *gap;
1819 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001820 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001821 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001822 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001823 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001824
Bram Moolenaarb765d632005-06-07 21:00:02 +00001825 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826 if (fd == NULL)
1827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001828 if (!silent)
1829 EMSG2(_(e_notopen), fname);
1830 else if (p_verbose > 2)
1831 {
1832 verbose_enter();
1833 smsg((char_u *)e_notopen, fname);
1834 verbose_leave();
1835 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001836 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001837 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001838 if (p_verbose > 2)
1839 {
1840 verbose_enter();
1841 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1842 verbose_leave();
1843 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001844
Bram Moolenaarb765d632005-06-07 21:00:02 +00001845 if (old_lp == NULL)
1846 {
1847 lp = slang_alloc(lang);
1848 if (lp == NULL)
1849 goto endFAIL;
1850
1851 /* Remember the file name, used to reload the file when it's updated. */
1852 lp->sl_fname = vim_strsave(fname);
1853 if (lp->sl_fname == NULL)
1854 goto endFAIL;
1855
1856 /* Check for .add.spl. */
1857 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1858 }
1859 else
1860 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001861
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001862 /* Set sourcing_name, so that error messages mention the file name. */
1863 sourcing_name = fname;
1864 sourcing_lnum = 0;
1865
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001866 /* <HEADER>: <fileID>
1867 * <regioncnt> <regionname> ...
1868 * <charflagslen> <charflags>
1869 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001870 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001871 * <prefcondcnt> <prefcond> ...
1872 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001873 for (i = 0; i < VIMSPELLMAGICL; ++i)
1874 buf[i] = getc(fd); /* <fileID> */
1875 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1876 {
1877 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001878 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001879 }
1880
1881 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001882 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001883 {
1884truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001885 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001886 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001887 }
1888 if (cnt > 8)
1889 {
1890formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001891 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001892 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001893 }
1894 for (i = 0; i < cnt; ++i)
1895 {
1896 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1897 lp->sl_regions[i * 2 + 1] = getc(fd);
1898 }
1899 lp->sl_regions[cnt * 2] = NUL;
1900
Bram Moolenaar7887d882005-07-01 22:33:52 +00001901 /* <charflagslen> <charflags> */
1902 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001903 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001904 goto endFAIL;
1905
1906 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001907 fol = read_cnt_string(fd, 2, &ccnt);
1908 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001909 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001910 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001911 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001912 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001913
1914 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1915 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001916 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001917
1918 vim_free(p);
1919 vim_free(fol);
1920
1921 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1922 if ((p == NULL) != (fol == NULL))
1923 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001924
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001925 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001926 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001927 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001928 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001929
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001930 /* <prefcondcnt> <prefcond> ... */
1931 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1932 if (cnt > 0)
1933 {
1934 lp->sl_prefprog = (regprog_T **)alloc_clear(
1935 (unsigned)sizeof(regprog_T *) * cnt);
1936 if (lp->sl_prefprog == NULL)
1937 goto endFAIL;
1938 lp->sl_prefixcnt = cnt;
1939
1940 for (i = 0; i < cnt; ++i)
1941 {
1942 /* <prefcond> : <condlen> <condstr> */
1943 n = getc(fd); /* <condlen> */
1944 if (n < 0)
1945 goto formerr;
1946 /* When <condlen> is zero we have an empty condition. Otherwise
1947 * compile the regexp program used to check for the condition. */
1948 if (n > 0)
1949 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001950 buf[0] = '^'; /* always match at one position only */
1951 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001952 while (n-- > 0)
1953 *p++ = getc(fd); /* <condstr> */
1954 *p = NUL;
1955 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1956 }
1957 }
1958 }
1959
1960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001961 /* <SUGGEST> : <repcount> <rep> ...
1962 * <salflags> <salcount> <sal> ...
1963 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001964
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001965 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1966 if (cnt < 0)
1967 goto formerr;
1968
1969 gap = &lp->sl_rep;
1970 if (ga_grow(gap, cnt) == FAIL)
1971 goto endFAIL;
1972
1973 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1974 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001975 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001976 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001977 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001978 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001979 goto endFAIL;
1980 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001981 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001983 vim_free(ftp->ft_from);
1984 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001985 }
1986 }
1987
1988 /* Fill the first-index table. */
1989 first = lp->sl_rep_first;
1990 for (i = 0; i < 256; ++i)
1991 first[i] = -1;
1992 for (i = 0; i < gap->ga_len; ++i)
1993 {
1994 ftp = &((fromto_T *)gap->ga_data)[i];
1995 if (first[*ftp->ft_from] == -1)
1996 first[*ftp->ft_from] = i;
1997 }
1998
1999 i = getc(fd); /* <salflags> */
2000 if (i & SAL_F0LLOWUP)
2001 lp->sl_followup = TRUE;
2002 if (i & SAL_COLLAPSE)
2003 lp->sl_collapse = TRUE;
2004 if (i & SAL_REM_ACCENTS)
2005 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002006 if (i & SAL_SOFO)
2007 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002008 else
2009 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002010
2011 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2012 if (cnt < 0)
2013 goto formerr;
2014
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002015 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002016 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002017 /*
2018 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2019 */
2020 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002021 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002022
Bram Moolenaar7887d882005-07-01 22:33:52 +00002023 /* <salfromlen> <salfrom> */
2024 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002025 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002026 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002027
Bram Moolenaar7887d882005-07-01 22:33:52 +00002028 /* <saltolen> <salto> */
2029 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002030 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002031 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002032 vim_free(bp);
2033 goto endFAIL;
2034 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002035
Bram Moolenaar7887d882005-07-01 22:33:52 +00002036 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002037 if (bp != NULL && fol != NULL)
2038 i = set_sofo(lp, bp, fol);
2039 else if (bp != NULL || fol != NULL)
2040 i = FAIL; /* only one of two strings is an error */
2041 else
2042 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002043
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002044 vim_free(bp);
2045 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002046 if (i == FAIL)
2047 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002048 }
2049 else
2050 {
2051 /*
2052 * SAL items
2053 */
2054 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002055 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002056 if (ga_grow(gap, cnt) == FAIL)
2057 goto endFAIL;
2058
2059 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2060 for (; gap->ga_len < cnt; ++gap->ga_len)
2061 {
2062 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2063 ccnt = getc(fd); /* <salfromlen> */
2064 if (ccnt < 0)
2065 goto formerr;
2066 if ((p = alloc(ccnt + 2)) == NULL)
2067 goto endFAIL;
2068 smp->sm_lead = p;
2069
2070 /* Read up to the first special char into sm_lead. */
2071 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002072 {
2073 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002074 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002075 break;
2076 *p++ = c;
2077 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002078 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002079 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002080
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002081 /* Put (abc) chars in sm_oneof, if any. */
2082 if (c == '(')
2083 {
2084 smp->sm_oneof = p;
2085 for (++i; i < ccnt; ++i)
2086 {
2087 c = getc(fd); /* <salfrom> */
2088 if (c == ')')
2089 break;
2090 *p++ = c;
2091 }
2092 *p++ = NUL;
2093 if (++i < ccnt)
2094 c = getc(fd);
2095 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002096 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002097 smp->sm_oneof = NULL;
2098
2099 /* Any following chars go in sm_rules. */
2100 smp->sm_rules = p;
2101 if (i < ccnt)
2102 /* store the char we got while checking for end of sm_lead */
2103 *p++ = c;
2104 for (++i; i < ccnt; ++i)
2105 *p++ = getc(fd); /* <salfrom> */
2106 *p++ = NUL;
2107
Bram Moolenaar7887d882005-07-01 22:33:52 +00002108 /* <saltolen> <salto> */
2109 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002110 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002111 {
2112 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002113 goto formerr;
2114 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002115
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002116#ifdef FEAT_MBYTE
2117 if (has_mbyte)
2118 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002119 /* convert the multi-byte strings to wide char strings */
2120 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2121 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2122 if (smp->sm_oneof == NULL)
2123 smp->sm_oneof_w = NULL;
2124 else
2125 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002126 if (smp->sm_to == NULL)
2127 smp->sm_to_w = NULL;
2128 else
2129 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002130 if (smp->sm_lead_w == NULL
2131 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002132 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002133 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002134 vim_free(smp->sm_lead);
2135 vim_free(smp->sm_to);
2136 vim_free(smp->sm_lead_w);
2137 vim_free(smp->sm_oneof_w);
2138 vim_free(smp->sm_to_w);
2139 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002140 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002141 }
2142#endif
2143 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002144
2145 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002146 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 }
2148
Bram Moolenaar7887d882005-07-01 22:33:52 +00002149 /* <maplen> <mapstr> */
2150 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002151 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002153 if (p != NULL)
2154 {
2155 set_map_str(lp, p);
2156 vim_free(p);
2157 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002158
Bram Moolenaar51485f02005-06-04 21:55:20 +00002159 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002160 * round 2: <KWORDTREE>
2161 * round 3: <PREFIXTREE> */
2162 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002164 /* The tree size was computed when writing the file, so that we can
2165 * allocate it as one long block. <nodecount> */
2166 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2167 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002169 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002170 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002171 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002172 bp = lalloc((long_u)len, TRUE);
2173 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002174 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002175 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002176 lp->sl_fbyts = bp;
2177 else if (round == 2)
2178 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002179 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002180 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002181
2182 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002183 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2184 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002185 goto endFAIL;
2186 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002187 lp->sl_fidxs = ip;
2188 else if (round == 2)
2189 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002190 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002191 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002192
2193 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002194 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002195 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002196 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002197 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002198 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002199 }
2200 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002201
Bram Moolenaarb765d632005-06-07 21:00:02 +00002202 /* For a new file link it in the list of spell files. */
2203 if (old_lp == NULL)
2204 {
2205 lp->sl_next = first_lang;
2206 first_lang = lp;
2207 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002208
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002209 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002210
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002211endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002212 if (lang != NULL)
2213 /* truncating the name signals the error to spell_load_lang() */
2214 *lang = NUL;
2215 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002217 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002218 lp = NULL;
2219 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002220
2221endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002222 if (fd != NULL)
2223 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002224 sourcing_name = save_sourcing_name;
2225 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226
2227 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002228}
2229
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002230/*
2231 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002232 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002233 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002234 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002235 */
2236 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002237read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002238 FILE *fd;
2239 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002240 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002241{
2242 int cnt = 0;
2243 int i;
2244 char_u *str;
2245
2246 /* read the length bytes, MSB first */
2247 for (i = 0; i < cnt_bytes; ++i)
2248 cnt = (cnt << 8) + getc(fd);
2249 if (cnt < 0)
2250 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002251 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002252 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002253 return NULL;
2254 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002255 *cntp = cnt;
2256 if (cnt == 0)
2257 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002258
2259 /* allocate memory */
2260 str = alloc((unsigned)cnt + 1);
2261 if (str == NULL)
2262 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002263 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002264 return NULL;
2265 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002266
2267 /* Read the string. Doesn't check for truncated file. */
2268 for (i = 0; i < cnt; ++i)
2269 str[i] = getc(fd);
2270 str[i] = NUL;
2271
2272 return str;
2273}
2274
Bram Moolenaar7887d882005-07-01 22:33:52 +00002275/*
2276 * Set the SOFOFROM and SOFOTO items in language "lp".
2277 * Returns FAIL when there is something wrong.
2278 */
2279 static int
2280set_sofo(lp, from, to)
2281 slang_T *lp;
2282 char_u *from;
2283 char_u *to;
2284{
2285 int i;
2286
2287#ifdef FEAT_MBYTE
2288 garray_T *gap;
2289 char_u *s;
2290 char_u *p;
2291 int c;
2292 int *inp;
2293
2294 if (has_mbyte)
2295 {
2296 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2297 * characters. The index is the low byte of the character.
2298 * The list contains from-to pairs with a terminating NUL.
2299 * sl_sal_first[] is used for latin1 "from" characters. */
2300 gap = &lp->sl_sal;
2301 ga_init2(gap, sizeof(int *), 1);
2302 if (ga_grow(gap, 256) == FAIL)
2303 return FAIL;
2304 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2305 gap->ga_len = 256;
2306
2307 /* First count the number of items for each list. Temporarily use
2308 * sl_sal_first[] for this. */
2309 for (p = from, s = to; *p != NUL && *s != NUL; )
2310 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002311 c = mb_cptr2char_adv(&p);
2312 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002313 if (c >= 256)
2314 ++lp->sl_sal_first[c & 0xff];
2315 }
2316 if (*p != NUL || *s != NUL) /* lengths differ */
2317 return FAIL;
2318
2319 /* Allocate the lists. */
2320 for (i = 0; i < 256; ++i)
2321 if (lp->sl_sal_first[i] > 0)
2322 {
2323 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2324 if (p == NULL)
2325 return FAIL;
2326 ((int **)gap->ga_data)[i] = (int *)p;
2327 *(int *)p = 0;
2328 }
2329
2330 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2331 * list. */
2332 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2333 for (p = from, s = to; *p != NUL && *s != NUL; )
2334 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002335 c = mb_cptr2char_adv(&p);
2336 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002337 if (c >= 256)
2338 {
2339 /* Append the from-to chars at the end of the list with
2340 * the low byte. */
2341 inp = ((int **)gap->ga_data)[c & 0xff];
2342 while (*inp != 0)
2343 ++inp;
2344 *inp++ = c; /* from char */
2345 *inp++ = i; /* to char */
2346 *inp++ = NUL; /* NUL at the end */
2347 }
2348 else
2349 /* mapping byte to char is done in sl_sal_first[] */
2350 lp->sl_sal_first[c] = i;
2351 }
2352 }
2353 else
2354#endif
2355 {
2356 /* mapping bytes to bytes is done in sl_sal_first[] */
2357 if (STRLEN(from) != STRLEN(to))
2358 return FAIL;
2359
2360 for (i = 0; to[i] != NUL; ++i)
2361 lp->sl_sal_first[from[i]] = to[i];
2362 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2363 }
2364
2365 return OK;
2366}
2367
2368/*
2369 * Fill the first-index table for "lp".
2370 */
2371 static void
2372set_sal_first(lp)
2373 slang_T *lp;
2374{
2375 salfirst_T *sfirst;
2376 int i;
2377 salitem_T *smp;
2378 int c;
2379 garray_T *gap = &lp->sl_sal;
2380
2381 sfirst = lp->sl_sal_first;
2382 for (i = 0; i < 256; ++i)
2383 sfirst[i] = -1;
2384 smp = (salitem_T *)gap->ga_data;
2385 for (i = 0; i < gap->ga_len; ++i)
2386 {
2387#ifdef FEAT_MBYTE
2388 if (has_mbyte)
2389 /* Use the lowest byte of the first character. For latin1 it's
2390 * the character, for other encodings it should differ for most
2391 * characters. */
2392 c = *smp[i].sm_lead_w & 0xff;
2393 else
2394#endif
2395 c = *smp[i].sm_lead;
2396 if (sfirst[c] == -1)
2397 {
2398 sfirst[c] = i;
2399#ifdef FEAT_MBYTE
2400 if (has_mbyte)
2401 {
2402 int n;
2403
2404 /* Make sure all entries with this byte are following each
2405 * other. Move the ones that are in the wrong position. Do
2406 * keep the same ordering! */
2407 while (i + 1 < gap->ga_len
2408 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2409 /* Skip over entry with same index byte. */
2410 ++i;
2411
2412 for (n = 1; i + n < gap->ga_len; ++n)
2413 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2414 {
2415 salitem_T tsal;
2416
2417 /* Move entry with same index byte after the entries
2418 * we already found. */
2419 ++i;
2420 --n;
2421 tsal = smp[i + n];
2422 mch_memmove(smp + i + 1, smp + i,
2423 sizeof(salitem_T) * n);
2424 smp[i] = tsal;
2425 }
2426 }
2427#endif
2428 }
2429 }
2430}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002431
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002432#ifdef FEAT_MBYTE
2433/*
2434 * Turn a multi-byte string into a wide character string.
2435 * Return it in allocated memory (NULL for out-of-memory)
2436 */
2437 static int *
2438mb_str2wide(s)
2439 char_u *s;
2440{
2441 int *res;
2442 char_u *p;
2443 int i = 0;
2444
2445 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2446 if (res != NULL)
2447 {
2448 for (p = s; *p != NUL; )
2449 res[i++] = mb_ptr2char_adv(&p);
2450 res[i] = NUL;
2451 }
2452 return res;
2453}
2454#endif
2455
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002456/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002457 * Read one row of siblings from the spell file and store it in the byte array
2458 * "byts" and index array "idxs". Recursively read the children.
2459 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002460 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002461 *
2462 * Returns the index follosing the siblings.
2463 * Returns -1 if the file is shorter than expected.
2464 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002466 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002467read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002468 FILE *fd;
2469 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002470 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002471 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002472 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002473 int prefixtree; /* TRUE for reading PREFIXTREE */
2474 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002475{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002476 int len;
2477 int i;
2478 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002479 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002480 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002481 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002482#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002483
Bram Moolenaar51485f02005-06-04 21:55:20 +00002484 len = getc(fd); /* <siblingcount> */
2485 if (len <= 0)
2486 return -1;
2487
2488 if (startidx + len >= maxidx)
2489 return -2;
2490 byts[idx++] = len;
2491
2492 /* Read the byte values, flag/region bytes and shared indexes. */
2493 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002494 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002495 c = getc(fd); /* <byte> */
2496 if (c < 0)
2497 return -1;
2498 if (c <= BY_SPECIAL)
2499 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002500 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002501 {
2502 /* No flags, all regions. */
2503 idxs[idx] = 0;
2504 c = 0;
2505 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002506 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002507 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002508 if (prefixtree)
2509 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002510 /* Read the optional pflags byte, the prefix ID and the
2511 * condition nr. In idxs[] store the prefix ID in the low
2512 * byte, the condition index shifted up 8 bits, the flags
2513 * shifted up 24 bits. */
2514 if (c == BY_FLAGS)
2515 c = getc(fd) << 24; /* <pflags> */
2516 else
2517 c = 0;
2518
2519 c |= getc(fd); /* <prefixID> */
2520
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002521 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2522 if (n >= maxprefcondnr)
2523 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002524 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002525 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002526 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002527 {
2528 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002529 * idxs[] the flags go in the low two bytes, region above
2530 * that and prefix ID above the region. */
2531 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002532 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002533 if (c2 == BY_FLAGS2)
2534 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002535 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002536 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002537 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002538 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002539 }
2540
Bram Moolenaar51485f02005-06-04 21:55:20 +00002541 idxs[idx] = c;
2542 c = 0;
2543 }
2544 else /* c == BY_INDEX */
2545 {
2546 /* <nodeidx> */
2547 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2548 if (n < 0 || n >= maxidx)
2549 return -2;
2550 idxs[idx] = n + SHARED_MASK;
2551 c = getc(fd); /* <xbyte> */
2552 }
2553 }
2554 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555 }
2556
Bram Moolenaar51485f02005-06-04 21:55:20 +00002557 /* Recursively read the children for non-shared siblings.
2558 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2559 * remove SHARED_MASK) */
2560 for (i = 1; i <= len; ++i)
2561 if (byts[startidx + i] != 0)
2562 {
2563 if (idxs[startidx + i] & SHARED_MASK)
2564 idxs[startidx + i] &= ~SHARED_MASK;
2565 else
2566 {
2567 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002568 idx = read_tree(fd, byts, idxs, maxidx, idx,
2569 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002570 if (idx < 0)
2571 break;
2572 }
2573 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002574
Bram Moolenaar51485f02005-06-04 21:55:20 +00002575 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002576}
2577
2578/*
2579 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002580 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002581 */
2582 char_u *
2583did_set_spelllang(buf)
2584 buf_T *buf;
2585{
2586 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002587 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002588 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002589 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002590 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002591 int region_mask;
2592 slang_T *lp;
2593 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002594 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002596 int len;
2597 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002598 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002599 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002600 char_u *use_region = NULL;
2601 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002602
2603 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002604 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002605
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002606 /* loop over comma separated language names. */
2607 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002608 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002609 /* Get one language name. */
2610 copy_option_part(&splp, lang, MAXWLEN, ",");
2611
Bram Moolenaar5482f332005-04-17 20:18:43 +00002612 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002613 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002614
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002615 /* If the name ends in ".spl" use it as the name of the spell file.
2616 * If there is a region name let "region" point to it and remove it
2617 * from the name. */
2618 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2619 {
2620 filename = TRUE;
2621
Bram Moolenaarb6356332005-07-18 21:40:44 +00002622 /* Locate a region and remove it from the file name. */
2623 p = vim_strchr(gettail(lang), '_');
2624 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2625 && !ASCII_ISALPHA(p[3]))
2626 {
2627 vim_strncpy(region_cp, p + 1, 2);
2628 mch_memmove(p, p + 3, len - (p - lang) - 2);
2629 len -= 3;
2630 region = region_cp;
2631 }
2632 else
2633 dont_use_region = TRUE;
2634
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002635 /* Check if we loaded this language before. */
2636 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2637 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2638 break;
2639 }
2640 else
2641 {
2642 filename = FALSE;
2643 if (len > 3 && lang[len - 3] == '_')
2644 {
2645 region = lang + len - 2;
2646 len -= 3;
2647 lang[len] = NUL;
2648 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002649 else
2650 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002651
2652 /* Check if we loaded this language before. */
2653 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2654 if (STRICMP(lang, lp->sl_name) == 0)
2655 break;
2656 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002657
Bram Moolenaarb6356332005-07-18 21:40:44 +00002658 if (region != NULL)
2659 {
2660 /* If the region differs from what was used before then don't
2661 * use it for 'spellfile'. */
2662 if (use_region != NULL && STRCMP(region, use_region) != 0)
2663 dont_use_region = TRUE;
2664 use_region = region;
2665 }
2666
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002667 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002668 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002669 {
2670 if (filename)
2671 (void)spell_load_file(lang, lang, NULL, FALSE);
2672 else
2673 spell_load_lang(lang);
2674 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002675
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002676 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002677 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002678 */
2679 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002680 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2681 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002682 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002683 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002684 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002685 {
2686 /* find region in sl_regions */
2687 c = find_region(lp->sl_regions, region);
2688 if (c == REGION_ALL)
2689 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002690 if (lp->sl_add)
2691 {
2692 if (*lp->sl_regions != NUL)
2693 /* This addition file is for other regions. */
2694 region_mask = 0;
2695 }
2696 else
2697 /* This is probably an error. Give a warning and
2698 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002699 smsg((char_u *)
2700 _("Warning: region %s not supported"),
2701 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002702 }
2703 else
2704 region_mask = 1 << c;
2705 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002706
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002707 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002708 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002709 if (ga_grow(&ga, 1) == FAIL)
2710 {
2711 ga_clear(&ga);
2712 return e_outofmem;
2713 }
2714 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2715 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2716 ++ga.ga_len;
2717 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002718 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002719 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002720 }
2721
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002722 /* round 0: load int_wordlist, if possible.
2723 * round 1: load first name in 'spellfile'.
2724 * round 2: load second name in 'spellfile.
2725 * etc. */
2726 spf = curbuf->b_p_spf;
2727 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002729 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002730 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002731 /* Internal wordlist, if there is one. */
2732 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002733 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002734 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002735 }
2736 else
2737 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002738 /* One entry in 'spellfile'. */
2739 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2740 STRCAT(spf_name, ".spl");
2741
2742 /* If it was already found above then skip it. */
2743 for (c = 0; c < ga.ga_len; ++c)
2744 if (fullpathcmp(spf_name,
2745 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2746 FALSE) == FPC_SAME)
2747 break;
2748 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002749 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002750 }
2751
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002752 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2754 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2755 break;
2756 if (lp == NULL)
2757 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002758 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002759 * region name, the region is ignored otherwise. for int_wordlist
2760 * use an arbitrary name. */
2761 if (round == 0)
2762 STRCPY(lang, "internal wordlist");
2763 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002764 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002765 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002766 p = vim_strchr(lang, '.');
2767 if (p != NULL)
2768 *p = NUL; /* truncate at ".encoding.add" */
2769 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002770 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 }
2772 if (lp != NULL && ga_grow(&ga, 1) == OK)
2773 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002774 region_mask = REGION_ALL;
2775 if (use_region != NULL && !dont_use_region)
2776 {
2777 /* find region in sl_regions */
2778 c = find_region(lp->sl_regions, use_region);
2779 if (c != REGION_ALL)
2780 region_mask = 1 << c;
2781 else if (*lp->sl_regions != NUL)
2782 /* This spell file is for other regions. */
2783 region_mask = 0;
2784 }
2785
2786 if (region_mask != 0)
2787 {
2788 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2789 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2790 ++ga.ga_len;
2791 use_midword(lp, buf);
2792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002793 }
2794 }
2795
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002796 /* Add a NULL entry to mark the end of the list. */
2797 if (ga_grow(&ga, 1) == FAIL)
2798 {
2799 ga_clear(&ga);
2800 return e_outofmem;
2801 }
2802 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2803 ++ga.ga_len;
2804
2805 /* Everything is fine, store the new b_langp value. */
2806 ga_clear(&buf->b_langp);
2807 buf->b_langp = ga;
2808
2809 return NULL;
2810}
2811
2812/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002813 * Clear the midword characters for buffer "buf".
2814 */
2815 static void
2816clear_midword(buf)
2817 buf_T *buf;
2818{
2819 vim_memset(buf->b_spell_ismw, 0, 256);
2820#ifdef FEAT_MBYTE
2821 vim_free(buf->b_spell_ismw_mb);
2822 buf->b_spell_ismw_mb = NULL;
2823#endif
2824}
2825
2826/*
2827 * Use the "sl_midword" field of language "lp" for buffer "buf".
2828 * They add up to any currently used midword characters.
2829 */
2830 static void
2831use_midword(lp, buf)
2832 slang_T *lp;
2833 buf_T *buf;
2834{
2835 char_u *p;
2836
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002837 if (lp->sl_midword == NULL) /* there aren't any */
2838 return;
2839
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002840 for (p = lp->sl_midword; *p != NUL; )
2841#ifdef FEAT_MBYTE
2842 if (has_mbyte)
2843 {
2844 int c, l, n;
2845 char_u *bp;
2846
2847 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002848 l = (*mb_ptr2len)(p);
2849 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002850 buf->b_spell_ismw[c] = TRUE;
2851 else if (buf->b_spell_ismw_mb == NULL)
2852 /* First multi-byte char in "b_spell_ismw_mb". */
2853 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2854 else
2855 {
2856 /* Append multi-byte chars to "b_spell_ismw_mb". */
2857 n = STRLEN(buf->b_spell_ismw_mb);
2858 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2859 if (bp != NULL)
2860 {
2861 vim_free(buf->b_spell_ismw_mb);
2862 buf->b_spell_ismw_mb = bp;
2863 vim_strncpy(bp + n, p, l);
2864 }
2865 }
2866 p += l;
2867 }
2868 else
2869#endif
2870 buf->b_spell_ismw[*p++] = TRUE;
2871}
2872
2873/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002874 * Find the region "region[2]" in "rp" (points to "sl_regions").
2875 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002876 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002877 */
2878 static int
2879find_region(rp, region)
2880 char_u *rp;
2881 char_u *region;
2882{
2883 int i;
2884
2885 for (i = 0; ; i += 2)
2886 {
2887 if (rp[i] == NUL)
2888 return REGION_ALL;
2889 if (rp[i] == region[0] && rp[i + 1] == region[1])
2890 break;
2891 }
2892 return i / 2;
2893}
2894
2895/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002896 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002897 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002898 * Word WF_ONECAP
2899 * W WORD WF_ALLCAP
2900 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002901 */
2902 static int
2903captype(word, end)
2904 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002905 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002906{
2907 char_u *p;
2908 int c;
2909 int firstcap;
2910 int allcap;
2911 int past_second = FALSE; /* past second word char */
2912
2913 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002914 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002915 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002916 return 0; /* only non-word characters, illegal word */
2917#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002918 if (has_mbyte)
2919 c = mb_ptr2char_adv(&p);
2920 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002921#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002922 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002923 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002924
2925 /*
2926 * Need to check all letters to find a word with mixed upper/lower.
2927 * But a word with an upper char only at start is a ONECAP.
2928 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002929 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002930 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002931 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002932 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002933 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002934 {
2935 /* UUl -> KEEPCAP */
2936 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002937 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002938 allcap = FALSE;
2939 }
2940 else if (!allcap)
2941 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002942 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002943 past_second = TRUE;
2944 }
2945
2946 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002947 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002948 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002949 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002950 return 0;
2951}
2952
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002953/*
2954 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2955 * capital. So that make_case_word() can turn WOrd into Word.
2956 * Add ALLCAP for "WOrD".
2957 */
2958 static int
2959badword_captype(word, end)
2960 char_u *word;
2961 char_u *end;
2962{
2963 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002964 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002965 int l, u;
2966 int first;
2967 char_u *p;
2968
2969 if (flags & WF_KEEPCAP)
2970 {
2971 /* Count the number of UPPER and lower case letters. */
2972 l = u = 0;
2973 first = FALSE;
2974 for (p = word; p < end; mb_ptr_adv(p))
2975 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002976 c = PTR2CHAR(p);
2977 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002978 {
2979 ++u;
2980 if (p == word)
2981 first = TRUE;
2982 }
2983 else
2984 ++l;
2985 }
2986
2987 /* If there are more UPPER than lower case letters suggest an
2988 * ALLCAP word. Otherwise, if the first letter is UPPER then
2989 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2990 * require three upper case letters. */
2991 if (u > l && u > 2)
2992 flags |= WF_ALLCAP;
2993 else if (first)
2994 flags |= WF_ONECAP;
2995 }
2996 return flags;
2997}
2998
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002999# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3000/*
3001 * Free all languages.
3002 */
3003 void
3004spell_free_all()
3005{
3006 slang_T *lp;
3007 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003008 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003009
3010 /* Go through all buffers and handle 'spelllang'. */
3011 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3012 ga_clear(&buf->b_langp);
3013
3014 while (first_lang != NULL)
3015 {
3016 lp = first_lang;
3017 first_lang = lp->sl_next;
3018 slang_free(lp);
3019 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003020
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003021 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003022 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003023 /* Delete the internal wordlist and its .spl file */
3024 mch_remove(int_wordlist);
3025 int_wordlist_spl(fname);
3026 mch_remove(fname);
3027 vim_free(int_wordlist);
3028 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003029 }
3030
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003031 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003032}
3033# endif
3034
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003035# if defined(FEAT_MBYTE) || defined(PROTO)
3036/*
3037 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003038 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003039 */
3040 void
3041spell_reload()
3042{
3043 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003044 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003045
Bram Moolenaarea408852005-06-25 22:49:46 +00003046 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003047 init_spell_chartab();
3048
3049 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003050 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003051
3052 /* Go through all buffers and handle 'spelllang'. */
3053 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3054 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003055 /* Only load the wordlists when 'spelllang' is set and there is a
3056 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003057 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003058 {
3059 FOR_ALL_WINDOWS(wp)
3060 if (wp->w_buffer == buf && wp->w_p_spell)
3061 {
3062 (void)did_set_spelllang(buf);
3063# ifdef FEAT_WINDOWS
3064 break;
3065# endif
3066 }
3067 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003068 }
3069}
3070# endif
3071
Bram Moolenaarb765d632005-06-07 21:00:02 +00003072/*
3073 * Reload the spell file "fname" if it's loaded.
3074 */
3075 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003076spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003077 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003079{
3080 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003082
Bram Moolenaarb765d632005-06-07 21:00:02 +00003083 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3084 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3085 {
3086 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003087 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003088 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003090 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091
3092 /* When "zg" was used and the file wasn't loaded yet, should redo
3093 * 'spelllang' to get it loaded. */
3094 if (added_word && !didit)
3095 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003096}
3097
3098
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003099/*
3100 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003101 */
3102
Bram Moolenaar51485f02005-06-04 21:55:20 +00003103#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003104 and .dic file. */
3105/*
3106 * Main structure to store the contents of a ".aff" file.
3107 */
3108typedef struct afffile_S
3109{
3110 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003111 int af_rar; /* RAR ID for rare word */
3112 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003113 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003114 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003115 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3116 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003117} afffile_T;
3118
3119typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003120/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3121struct affentry_S
3122{
3123 affentry_T *ae_next; /* next affix with same name/number */
3124 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3125 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003126 char_u *ae_cond; /* condition (NULL for ".") */
3127 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003128 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003129};
3130
Bram Moolenaar53805d12005-08-01 07:08:33 +00003131#define AH_KEY_LEN 10
3132
Bram Moolenaar51485f02005-06-04 21:55:20 +00003133/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3134typedef struct affheader_S
3135{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003136 /* key for hashtable == name of affix entry */
3137#ifdef FEAT_MBYTE
3138 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3139#else
3140 char_u ah_key[2]; /* one byte char plus NUL */
3141#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003142 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003143 int ah_combine; /* suffix may combine with prefix */
3144 affentry_T *ah_first; /* first affix entry */
3145} affheader_T;
3146
3147#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3148
3149/*
3150 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003151 * the need to keep track of each allocated thing, everything is freed all at
3152 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003153 */
3154#define SBLOCKSIZE 16000 /* size of sb_data */
3155typedef struct sblock_S sblock_T;
3156struct sblock_S
3157{
3158 sblock_T *sb_next; /* next block in list */
3159 int sb_used; /* nr of bytes already in use */
3160 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003161};
3162
3163/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003164 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003165 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003166typedef struct wordnode_S wordnode_T;
3167struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003168{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003169 union /* shared to save space */
3170 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003171 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003172 int index; /* index in written nodes (valid after first
3173 round) */
3174 } wn_u1;
3175 union /* shared to save space */
3176 {
3177 wordnode_T *next; /* next node with same hash key */
3178 wordnode_T *wnode; /* parent node that will write this node */
3179 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003180 wordnode_T *wn_child; /* child (next byte in word) */
3181 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3182 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003183 int wn_refs; /* Nr. of references to this node. Only
3184 relevant for first node in a list of
3185 siblings, in following siblings it is
3186 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003187 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003188 char_u wn_prefixID; /* when "wn_byte" is NUL: supported/required
3189 prefix ID or 0 */
3190 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3191 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003192 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003193#ifdef SPELL_PRINTTREE
3194 int wn_nr; /* sequence nr for printing */
3195#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003196};
3197
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003198#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3199
Bram Moolenaar51485f02005-06-04 21:55:20 +00003200#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003201
Bram Moolenaar51485f02005-06-04 21:55:20 +00003202/*
3203 * Info used while reading the spell files.
3204 */
3205typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003206{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003207 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003208 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003209 int si_fold_added; /* nr of words added since compressing */
3210
Bram Moolenaar51485f02005-06-04 21:55:20 +00003211 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003212 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003213 int si_keep_added; /* nr of words added since compressing */
3214
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003215 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003216
Bram Moolenaar51485f02005-06-04 21:55:20 +00003217 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003218 wordnode_T *si_first_free; /* List of nodes that have been freed during
3219 compression, linked by "wn_child" field. */
3220#ifdef SPELL_PRINTTREE
3221 int si_wordnode_nr; /* sequence nr for nodes */
3222#endif
3223
3224
Bram Moolenaar51485f02005-06-04 21:55:20 +00003225 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003226 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003227 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003228 int si_region; /* region mask */
3229 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003230 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003231 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003232 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003233 int si_region_count; /* number of regions supported (1 when there
3234 are no regions) */
3235 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003236
3237 garray_T si_rep; /* list of fromto_T entries from REP lines */
3238 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003239 char_u *si_sofofr; /* SOFOFROM text */
3240 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003241 int si_followup; /* soundsalike: ? */
3242 int si_collapse; /* soundsalike: ? */
3243 int si_rem_accents; /* soundsalike: remove accents */
3244 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003245 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003246 garray_T si_prefcond; /* table with conditions for postponed
3247 * prefixes, each stored as a string */
3248 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003249} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003250
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003251static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003252static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3254static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003255static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003256static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003257static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
3258static char_u *get_pfxlist __ARGS((spellinfo_T *spin, afffile_T *affile, char_u *afflist));
3259static 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));
3260static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3261static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3262static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003263static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003264static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
3265static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist));
3266static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int prefixID));
3267static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3268static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3269static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3270static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3271static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003272static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003273static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003274static void clear_node __ARGS((wordnode_T *node));
3275static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003276static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003277static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003278
Bram Moolenaar53805d12005-08-01 07:08:33 +00003279/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3280 * but it must be negative to indicate the prefix tree to tree_add_word().
3281 * Use a negative number with the lower 8 bits zero. */
3282#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003283
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003284#ifdef SPELL_PRINTTREE
3285/*
3286 * For debugging the tree code: print the current tree in a (more or less)
3287 * readable format, so that we can see what happens when adding a word and/or
3288 * compressing the tree.
3289 * Based on code from Olaf Seibert.
3290 */
3291#define PRINTLINESIZE 1000
3292#define PRINTWIDTH 6
3293
3294#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3295 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3296
3297static char line1[PRINTLINESIZE];
3298static char line2[PRINTLINESIZE];
3299static char line3[PRINTLINESIZE];
3300
3301 static void
3302spell_clear_flags(wordnode_T *node)
3303{
3304 wordnode_T *np;
3305
3306 for (np = node; np != NULL; np = np->wn_sibling)
3307 {
3308 np->wn_u1.index = FALSE;
3309 spell_clear_flags(np->wn_child);
3310 }
3311}
3312
3313 static void
3314spell_print_node(wordnode_T *node, int depth)
3315{
3316 if (node->wn_u1.index)
3317 {
3318 /* Done this node before, print the reference. */
3319 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3320 PRINTSOME(line2, depth, " ", 0, 0);
3321 PRINTSOME(line3, depth, " ", 0, 0);
3322 msg(line1);
3323 msg(line2);
3324 msg(line3);
3325 }
3326 else
3327 {
3328 node->wn_u1.index = TRUE;
3329
3330 if (node->wn_byte != NUL)
3331 {
3332 if (node->wn_child != NULL)
3333 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3334 else
3335 /* Cannot happen? */
3336 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3337 }
3338 else
3339 PRINTSOME(line1, depth, " $ ", 0, 0);
3340
3341 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3342
3343 if (node->wn_sibling != NULL)
3344 PRINTSOME(line3, depth, " | ", 0, 0);
3345 else
3346 PRINTSOME(line3, depth, " ", 0, 0);
3347
3348 if (node->wn_byte == NUL)
3349 {
3350 msg(line1);
3351 msg(line2);
3352 msg(line3);
3353 }
3354
3355 /* do the children */
3356 if (node->wn_byte != NUL && node->wn_child != NULL)
3357 spell_print_node(node->wn_child, depth + 1);
3358
3359 /* do the siblings */
3360 if (node->wn_sibling != NULL)
3361 {
3362 /* get rid of all parent details except | */
3363 STRCPY(line1, line3);
3364 STRCPY(line2, line3);
3365 spell_print_node(node->wn_sibling, depth);
3366 }
3367 }
3368}
3369
3370 static void
3371spell_print_tree(wordnode_T *root)
3372{
3373 if (root != NULL)
3374 {
3375 /* Clear the "wn_u1.index" fields, used to remember what has been
3376 * done. */
3377 spell_clear_flags(root);
3378
3379 /* Recursively print the tree. */
3380 spell_print_node(root, 0);
3381 }
3382}
3383#endif /* SPELL_PRINTTREE */
3384
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003385/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003386 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003387 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003388 */
3389 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003390spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003391 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003393{
3394 FILE *fd;
3395 afffile_T *aff;
3396 char_u rline[MAXLINELEN];
3397 char_u *line;
3398 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003399#define MAXITEMCNT 7
3400 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003401 int itemcnt;
3402 char_u *p;
3403 int lnum = 0;
3404 affheader_T *cur_aff = NULL;
3405 int aff_todo = 0;
3406 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003407 char_u *low = NULL;
3408 char_u *fol = NULL;
3409 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003410 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 int do_rep;
3412 int do_sal;
3413 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003414 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003415 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003417 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003418 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003419
Bram Moolenaar51485f02005-06-04 21:55:20 +00003420 /*
3421 * Open the file.
3422 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003423 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003424 if (fd == NULL)
3425 {
3426 EMSG2(_(e_notopen), fname);
3427 return NULL;
3428 }
3429
Bram Moolenaarb765d632005-06-07 21:00:02 +00003430 if (spin->si_verbose || p_verbose > 2)
3431 {
3432 if (!spin->si_verbose)
3433 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003434 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003435 out_flush();
3436 if (!spin->si_verbose)
3437 verbose_leave();
3438 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003440 /* Only do REP lines when not done in another .aff file already. */
3441 do_rep = spin->si_rep.ga_len == 0;
3442
3443 /* Only do SAL lines when not done in another .aff file already. */
3444 do_sal = spin->si_sal.ga_len == 0;
3445
3446 /* Only do MAP lines when not done in another .aff file already. */
3447 do_map = spin->si_map.ga_len == 0;
3448
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003449 /* Only do MIDWORD line when not done in another .aff file already */
3450 do_midword = spin->si_midword == NULL;
3451
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003452 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3453 do_sofo = spin->si_sofofr == NULL;
3454
Bram Moolenaar51485f02005-06-04 21:55:20 +00003455 /*
3456 * Allocate and init the afffile_T structure.
3457 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003458 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003459 if (aff == NULL)
3460 return NULL;
3461 hash_init(&aff->af_pref);
3462 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003463
3464 /*
3465 * Read all the lines in the file one by one.
3466 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003467 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003468 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003469 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003470 ++lnum;
3471
3472 /* Skip comment lines. */
3473 if (*rline == '#')
3474 continue;
3475
3476 /* Convert from "SET" to 'encoding' when needed. */
3477 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003478#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003479 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003480 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003481 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003482 if (pc == NULL)
3483 {
3484 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3485 fname, lnum, rline);
3486 continue;
3487 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003488 line = pc;
3489 }
3490 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003491#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003492 {
3493 pc = NULL;
3494 line = rline;
3495 }
3496
3497 /* Split the line up in white separated items. Put a NUL after each
3498 * item. */
3499 itemcnt = 0;
3500 for (p = line; ; )
3501 {
3502 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3503 ++p;
3504 if (*p == NUL)
3505 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003506 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003507 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003508 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003509 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003510 ++p;
3511 if (*p == NUL)
3512 break;
3513 *p++ = NUL;
3514 }
3515
3516 /* Handle non-empty lines. */
3517 if (itemcnt > 0)
3518 {
3519 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3520 && aff->af_enc == NULL)
3521 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003522#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003523 /* Setup for conversion from "ENC" to 'encoding'. */
3524 aff->af_enc = enc_canonize(items[1]);
3525 if (aff->af_enc != NULL && !spin->si_ascii
3526 && convert_setup(&spin->si_conv, aff->af_enc,
3527 p_enc) == FAIL)
3528 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3529 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003530 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003531#else
3532 smsg((char_u *)_("Conversion in %s not supported"), fname);
3533#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003534 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003535 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3536 {
3537 if (do_midword)
3538 spin->si_midword = vim_strsave(items[1]);
3539 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003540 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003542 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003543 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003544 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003546 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003547 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003548 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3549 && aff->af_rar == 0)
3550 {
3551 aff->af_rar = items[1][0];
3552 if (items[1][1] != NUL)
3553 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3554 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003555 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3556 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003557 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003558 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003559 if (items[1][1] != NUL)
3560 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3561 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003562 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3563 && aff->af_bad == 0)
3564 {
3565 aff->af_bad = items[1][0];
3566 if (items[1][1] != NUL)
3567 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3568 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003569 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3570 {
3571 aff->af_pfxpostpone = TRUE;
3572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573 else if ((STRCMP(items[0], "PFX") == 0
3574 || STRCMP(items[0], "SFX") == 0)
3575 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003576 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003577 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003578 /* Myspell allows extra text after the item, but that might
3579 * mean mistakes go unnoticed. Require a comment-starter. */
3580 if (itemcnt > 4 && *items[4] != '#')
3581 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3582 fname, lnum, items[4]);
3583
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003584 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003585 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003586 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003587 if (cur_aff == NULL)
3588 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003589#ifdef FEAT_MBYTE
3590 if (has_mbyte)
3591 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003592 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003593 if (l >= AH_KEY_LEN)
3594 l = 1; /* too long, must be an overlong sequence */
3595 else
3596 mch_memmove(cur_aff->ah_key, items[1], l);
3597 }
3598 else
3599#endif
3600 {
3601 *cur_aff->ah_key = *items[1];
3602 l = 1;
3603 }
3604 cur_aff->ah_key[l] = NUL;
3605 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003606 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003607 if (*items[2] == 'Y')
3608 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003609 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003610 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3611 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003612
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003613 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003614 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003615 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003616 /* Use a new number in the .spl file later, to be able to
3617 * handle multiple .aff files. */
3618 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003619 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003620 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003621 else
3622 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003623 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003624 hi = hash_find(tp, cur_aff->ah_key);
3625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003626 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003627 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3628 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003629 aff_todo = 0;
3630 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003631 else
3632 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003633 }
3634 else if ((STRCMP(items[0], "PFX") == 0
3635 || STRCMP(items[0], "SFX") == 0)
3636 && aff_todo > 0
3637 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003638 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003639 {
3640 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003641 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003642 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003643 int lasti = 5;
3644
3645 /* Check for "rare" after the other info. */
3646 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3647 {
3648 rare = TRUE;
3649 lasti = 6;
3650 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003651
Bram Moolenaar8db73182005-06-17 21:51:16 +00003652 /* Myspell allows extra text after the item, but that might
3653 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003654 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003655 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003656 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003657
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003658 /* New item for an affix letter. */
3659 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003660 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003661 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003662 if (aff_entry == NULL)
3663 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003664 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003665
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003666 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003667 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003668 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003669 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003670
Bram Moolenaar51485f02005-06-04 21:55:20 +00003671 /* Don't use an affix entry with non-ASCII characters when
3672 * "spin->si_ascii" is TRUE. */
3673 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003674 || has_non_ascii(aff_entry->ae_add)))
3675 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003676 aff_entry->ae_next = cur_aff->ah_first;
3677 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003678
3679 if (STRCMP(items[4], ".") != 0)
3680 {
3681 char_u buf[MAXLINELEN];
3682
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003683 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003684 if (*items[0] == 'P')
3685 sprintf((char *)buf, "^%s", items[4]);
3686 else
3687 sprintf((char *)buf, "%s$", items[4]);
3688 aff_entry->ae_prog = vim_regcomp(buf,
3689 RE_MAGIC + RE_STRING);
3690 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003691
3692 /* For postponed prefixes we need an entry in si_prefcond
3693 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003694 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003695 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003696 /* When the chop string is one lower-case letter and
3697 * the add string ends in the upper-case letter we set
3698 * the "upper" flag, clear "ae_chop" and remove the
3699 * letters from "ae_add". The condition must either
3700 * be empty or start with the same letter. */
3701 if (aff_entry->ae_chop != NULL
3702 && aff_entry->ae_add != NULL
3703#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003704 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00003705 aff_entry->ae_chop)] == NUL
3706#else
3707 && aff_entry->ae_chop[1] == NUL
3708#endif
3709 )
3710 {
3711 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003712
Bram Moolenaar53805d12005-08-01 07:08:33 +00003713 c = PTR2CHAR(aff_entry->ae_chop);
3714 c_up = SPELL_TOUPPER(c);
3715 if (c_up != c
3716 && (aff_entry->ae_cond == NULL
3717 || PTR2CHAR(aff_entry->ae_cond) == c))
3718 {
3719 p = aff_entry->ae_add
3720 + STRLEN(aff_entry->ae_add);
3721 mb_ptr_back(aff_entry->ae_add, p);
3722 if (PTR2CHAR(p) == c_up)
3723 {
3724 upper = TRUE;
3725 aff_entry->ae_chop = NULL;
3726 *p = NUL;
3727
3728 /* The condition is matched with the
3729 * actual word, thus must check for the
3730 * upper-case letter. */
3731 if (aff_entry->ae_cond != NULL)
3732 {
3733 char_u buf[MAXLINELEN];
3734#ifdef FEAT_MBYTE
3735 if (has_mbyte)
3736 {
3737 onecap_copy(items[4], buf, TRUE);
3738 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003739 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003740 }
3741 else
3742#endif
3743 *aff_entry->ae_cond = c_up;
3744 if (aff_entry->ae_cond != NULL)
3745 {
3746 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003747 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003748 vim_free(aff_entry->ae_prog);
3749 aff_entry->ae_prog = vim_regcomp(
3750 buf, RE_MAGIC + RE_STRING);
3751 }
3752 }
3753 }
3754 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003755 }
3756
Bram Moolenaar53805d12005-08-01 07:08:33 +00003757 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003758 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003759 int idx;
3760 char_u **pp;
3761 int n;
3762
3763 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3764 --idx)
3765 {
3766 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3767 if (str_equal(p, aff_entry->ae_cond))
3768 break;
3769 }
3770 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3771 {
3772 /* Not found, add a new condition. */
3773 idx = spin->si_prefcond.ga_len++;
3774 pp = ((char_u **)spin->si_prefcond.ga_data)
3775 + idx;
3776 if (aff_entry->ae_cond == NULL)
3777 *pp = NULL;
3778 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003779 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00003780 aff_entry->ae_cond);
3781 }
3782
3783 /* Add the prefix to the prefix tree. */
3784 if (aff_entry->ae_add == NULL)
3785 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003786 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003787 p = aff_entry->ae_add;
3788 /* PFX_FLAGS is a negative number, so that
3789 * tree_add_word() knows this is the prefix tree. */
3790 n = PFX_FLAGS;
3791 if (rare)
3792 n |= WFP_RARE;
3793 if (!cur_aff->ah_combine)
3794 n |= WFP_NC;
3795 if (upper)
3796 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003797 tree_add_word(spin, p, spin->si_prefroot, n,
3798 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003799 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003800 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003801 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003802 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003803 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3804 {
3805 if (fol != NULL)
3806 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3807 fname, lnum);
3808 else
3809 fol = vim_strsave(items[1]);
3810 }
3811 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3812 {
3813 if (low != NULL)
3814 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3815 fname, lnum);
3816 else
3817 low = vim_strsave(items[1]);
3818 }
3819 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3820 {
3821 if (upp != NULL)
3822 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3823 fname, lnum);
3824 else
3825 upp = vim_strsave(items[1]);
3826 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003827 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003829 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 if (!isdigit(*items[1]))
3831 smsg((char_u *)_("Expected REP count in %s line %d"),
3832 fname, lnum);
3833 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003834 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3835 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003836 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 if (do_rep)
3838 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3839 }
3840 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3841 {
3842 /* MAP item or count */
3843 if (!found_map)
3844 {
3845 /* First line contains the count. */
3846 found_map = TRUE;
3847 if (!isdigit(*items[1]))
3848 smsg((char_u *)_("Expected MAP count in %s line %d"),
3849 fname, lnum);
3850 }
3851 else if (do_map)
3852 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003853 int c;
3854
3855 /* Check that every character appears only once. */
3856 for (p = items[1]; *p != NUL; )
3857 {
3858#ifdef FEAT_MBYTE
3859 c = mb_ptr2char_adv(&p);
3860#else
3861 c = *p++;
3862#endif
3863 if ((spin->si_map.ga_len > 0
3864 && vim_strchr(spin->si_map.ga_data, c)
3865 != NULL)
3866 || vim_strchr(p, c) != NULL)
3867 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3868 fname, lnum);
3869 }
3870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 /* We simply concatenate all the MAP strings, separated by
3872 * slashes. */
3873 ga_concat(&spin->si_map, items[1]);
3874 ga_append(&spin->si_map, '/');
3875 }
3876 }
3877 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3878 {
3879 if (do_sal)
3880 {
3881 /* SAL item (sounds-a-like)
3882 * Either one of the known keys or a from-to pair. */
3883 if (STRCMP(items[1], "followup") == 0)
3884 spin->si_followup = sal_to_bool(items[2]);
3885 else if (STRCMP(items[1], "collapse_result") == 0)
3886 spin->si_collapse = sal_to_bool(items[2]);
3887 else if (STRCMP(items[1], "remove_accents") == 0)
3888 spin->si_rem_accents = sal_to_bool(items[2]);
3889 else
3890 /* when "to" is "_" it means empty */
3891 add_fromto(spin, &spin->si_sal, items[1],
3892 STRCMP(items[2], "_") == 0 ? (char_u *)""
3893 : items[2]);
3894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003895 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003896 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3897 && (!do_sofo || spin->si_sofofr == NULL))
3898 {
3899 if (do_sofo)
3900 spin->si_sofofr = vim_strsave(items[1]);
3901 }
3902 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3903 && (!do_sofo || spin->si_sofoto == NULL))
3904 {
3905 if (do_sofo)
3906 spin->si_sofoto = vim_strsave(items[1]);
3907 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003908 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003909 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3910 fname, lnum, items[0]);
3911 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003912 }
3913
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003914 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3915 smsg((char_u *)_("Missing SOFO%s line in %s"),
3916 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3917 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3918 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3919
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003920 if (fol != NULL || low != NULL || upp != NULL)
3921 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003922 if (spin->si_clear_chartab)
3923 {
3924 /* Clear the char type tables, don't want to use any of the
3925 * currently used spell properties. */
3926 init_spell_chartab();
3927 spin->si_clear_chartab = FALSE;
3928 }
3929
Bram Moolenaar3982c542005-06-08 21:56:31 +00003930 /*
3931 * Don't write a word table for an ASCII file, so that we don't check
3932 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003933 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003934 * mb_get_class(), the list of chars in the file will be incomplete.
3935 */
3936 if (!spin->si_ascii
3937#ifdef FEAT_MBYTE
3938 && !enc_utf8
3939#endif
3940 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003941 {
3942 if (fol == NULL || low == NULL || upp == NULL)
3943 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3944 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003945 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003946 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003947
3948 vim_free(fol);
3949 vim_free(low);
3950 vim_free(upp);
3951 }
3952
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003953 vim_free(pc);
3954 fclose(fd);
3955 return aff;
3956}
3957
3958/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003959 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3960 * NULL as equal.
3961 */
3962 static int
3963str_equal(s1, s2)
3964 char_u *s1;
3965 char_u *s2;
3966{
3967 if (s1 == NULL || s2 == NULL)
3968 return s1 == s2;
3969 return STRCMP(s1, s2) == 0;
3970}
3971
3972/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003973 * Add a from-to item to "gap". Used for REP and SAL items.
3974 * They are stored case-folded.
3975 */
3976 static void
3977add_fromto(spin, gap, from, to)
3978 spellinfo_T *spin;
3979 garray_T *gap;
3980 char_u *from;
3981 char_u *to;
3982{
3983 fromto_T *ftp;
3984 char_u word[MAXWLEN];
3985
3986 if (ga_grow(gap, 1) == OK)
3987 {
3988 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3989 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003990 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003992 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 ++gap->ga_len;
3994 }
3995}
3996
3997/*
3998 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3999 */
4000 static int
4001sal_to_bool(s)
4002 char_u *s;
4003{
4004 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4005}
4006
4007/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004008 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4009 * When "s" is NULL FALSE is returned.
4010 */
4011 static int
4012has_non_ascii(s)
4013 char_u *s;
4014{
4015 char_u *p;
4016
4017 if (s != NULL)
4018 for (p = s; *p != NUL; ++p)
4019 if (*p >= 128)
4020 return TRUE;
4021 return FALSE;
4022}
4023
4024/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004025 * Free the structure filled by spell_read_aff().
4026 */
4027 static void
4028spell_free_aff(aff)
4029 afffile_T *aff;
4030{
4031 hashtab_T *ht;
4032 hashitem_T *hi;
4033 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004034 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004035 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004036
4037 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004038
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004039 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004040 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4041 {
4042 todo = ht->ht_used;
4043 for (hi = ht->ht_array; todo > 0; ++hi)
4044 {
4045 if (!HASHITEM_EMPTY(hi))
4046 {
4047 --todo;
4048 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004049 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4050 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004051 }
4052 }
4053 if (ht == &aff->af_suff)
4054 break;
4055 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004056
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004057 hash_clear(&aff->af_pref);
4058 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059}
4060
4061/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 */
4065 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004066spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004067 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004068 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004070{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004072 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004074 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004076 char_u *pc;
4077 char_u *w;
4078 int l;
4079 hash_T hash;
4080 hashitem_T *hi;
4081 FILE *fd;
4082 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 int non_ascii = 0;
4084 int retval = OK;
4085 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004086 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087
Bram Moolenaar51485f02005-06-04 21:55:20 +00004088 /*
4089 * Open the file.
4090 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004091 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092 if (fd == NULL)
4093 {
4094 EMSG2(_(e_notopen), fname);
4095 return FAIL;
4096 }
4097
Bram Moolenaar51485f02005-06-04 21:55:20 +00004098 /* The hashtable is only used to detect duplicated words. */
4099 hash_init(&ht);
4100
Bram Moolenaarb765d632005-06-07 21:00:02 +00004101 if (spin->si_verbose || p_verbose > 2)
4102 {
4103 if (!spin->si_verbose)
4104 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004105 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004106 out_flush();
4107 if (!spin->si_verbose)
4108 verbose_leave();
4109 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004111 /* start with a message for the first line */
4112 spin->si_msg_count = 999999;
4113
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004114 /* Read and ignore the first line: word count. */
4115 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004116 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004117 EMSG2(_("E760: No word count in %s"), fname);
4118
4119 /*
4120 * Read all the lines in the file one by one.
4121 * The words are converted to 'encoding' here, before being added to
4122 * the hashtable.
4123 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004124 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004125 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004126 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004128 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004129 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004130
Bram Moolenaar51485f02005-06-04 21:55:20 +00004131 /* Remove CR, LF and white space from the end. White space halfway
4132 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 l = STRLEN(line);
4134 while (l > 0 && line[l - 1] <= ' ')
4135 --l;
4136 if (l == 0)
4137 continue; /* empty line */
4138 line[l] = NUL;
4139
Bram Moolenaar51485f02005-06-04 21:55:20 +00004140 /* Find the optional affix names. */
4141 afflist = vim_strchr(line, '/');
4142 if (afflist != NULL)
4143 *afflist++ = NUL;
4144
4145 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4146 if (spin->si_ascii && has_non_ascii(line))
4147 {
4148 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004149 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004150 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004151
Bram Moolenaarb765d632005-06-07 21:00:02 +00004152#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004153 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004154 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004155 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004156 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004157 if (pc == NULL)
4158 {
4159 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4160 fname, lnum, line);
4161 continue;
4162 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004163 w = pc;
4164 }
4165 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004166#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 {
4168 pc = NULL;
4169 w = line;
4170 }
4171
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004172 /* This takes time, print a message every 10000 words. */
4173 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004174 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004175 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004176 vim_snprintf((char *)message, sizeof(message),
4177 _("line %6d, word %6d - %s"),
4178 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4179 msg_start();
4180 msg_puts_long_attr(message, 0);
4181 msg_clr_eos();
4182 msg_didout = FALSE;
4183 msg_col = 0;
4184 out_flush();
4185 }
4186
Bram Moolenaar51485f02005-06-04 21:55:20 +00004187 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004188 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004189 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004190 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004191 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004192 if (retval == FAIL)
4193 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004194
Bram Moolenaar51485f02005-06-04 21:55:20 +00004195 hash = hash_hash(dw);
4196 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197 if (!HASHITEM_EMPTY(hi))
4198 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004199 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004201 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004202
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004203 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004204 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004205 if (afflist != NULL)
4206 {
4207 /* Check for affix name that stands for keep-case word and stands
4208 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004209 if (affile->af_kep != NUL
4210 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004211 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004212 if (affile->af_rar != NUL
4213 && vim_strchr(afflist, affile->af_rar) != NULL)
4214 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004215 if (affile->af_bad != NUL
4216 && vim_strchr(afflist, affile->af_bad) != NULL)
4217 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004218
4219 if (affile->af_pfxpostpone)
4220 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004221 pfxlist = get_pfxlist(spin, affile, afflist);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004222 }
4223
Bram Moolenaar51485f02005-06-04 21:55:20 +00004224 /* Add the word to the word tree(s). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004225 if (store_word(spin, dw, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004226 retval = FAIL;
4227
4228 if (afflist != NULL)
4229 {
4230 /* Find all matching suffixes and add the resulting words.
4231 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004232 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004233 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004234 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004235 retval = FAIL;
4236
4237 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004238 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004239 &affile->af_pref, NULL,
4240 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 retval = FAIL;
4242 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243 }
4244
Bram Moolenaar51485f02005-06-04 21:55:20 +00004245 if (spin->si_ascii && non_ascii > 0)
4246 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4247 non_ascii);
4248 hash_clear(&ht);
4249
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004250 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004251 return retval;
4252}
4253
4254/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004255 * Get the list of prefix IDs from the affix list "afflist".
4256 * Used for PFXPOSTPONE.
4257 * Returns a string allocated with getroom(). NULL when there are no prefixes
4258 * or when out of memory.
4259 */
4260 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004261get_pfxlist(spin, affile, afflist)
4262 spellinfo_T *spin;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004263 afffile_T *affile;
4264 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004265{
4266 char_u *p;
4267 int cnt;
4268 int round;
4269 char_u *res = NULL;
4270 char_u key[2];
4271 hashitem_T *hi;
4272
4273 key[1] = NUL;
4274
4275 /* round 1: count the number of prefix IDs.
4276 * round 2: move prefix IDs to "res" */
4277 for (round = 1; round <= 2; ++round)
4278 {
4279 cnt = 0;
4280 for (p = afflist; *p != NUL; ++p)
4281 {
4282 key[0] = *p;
4283 hi = hash_find(&affile->af_pref, key);
4284 if (!HASHITEM_EMPTY(hi))
4285 {
4286 /* This is a prefix ID, use the new number. */
4287 if (round == 2)
4288 res[cnt] = HI2AH(hi)->ah_newID;
4289 ++cnt;
4290 }
4291 }
4292 if (round == 1 && cnt > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004293 res = getroom(spin, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004294 if (res == NULL)
4295 break;
4296 }
4297
4298 if (res != NULL)
4299 res[cnt] = NUL;
4300 return res;
4301}
4302
4303/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004304 * Apply affixes to a word and store the resulting words.
4305 * "ht" is the hashtable with affentry_T that need to be applied, either
4306 * prefixes or suffixes.
4307 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4308 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004309 *
4310 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004311 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004312 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004313store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004314 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004315 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004316 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004317 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004318 hashtab_T *ht;
4319 hashtab_T *xht;
4320 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004321 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004322 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004323{
4324 int todo;
4325 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004326 affheader_T *ah;
4327 affentry_T *ae;
4328 regmatch_T regmatch;
4329 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004330 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004331 int i;
4332 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004333 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004334 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004335 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004336
Bram Moolenaar51485f02005-06-04 21:55:20 +00004337 todo = ht->ht_used;
4338 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004339 {
4340 if (!HASHITEM_EMPTY(hi))
4341 {
4342 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004343 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004344
Bram Moolenaar51485f02005-06-04 21:55:20 +00004345 /* Check that the affix combines, if required, and that the word
4346 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004347 c = PTR2CHAR(ah->ah_key);
4348 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004349 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004350 /* Loop over all affix entries with this name. */
4351 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004352 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004353 /* Check the condition. It's not logical to match case
4354 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004355 * Myspell.
4356 * For prefixes, when "PFXPOSTPONE" was used, only do
4357 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004358 regmatch.regprog = ae->ae_prog;
4359 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004360 if ((xht != NULL || !affile->af_pfxpostpone
4361 || ae->ae_chop != NULL)
4362 && (ae->ae_prog == NULL
4363 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004365 /* Match. Remove the chop and add the affix. */
4366 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004367 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004368 /* prefix: chop/add at the start of the word */
4369 if (ae->ae_add == NULL)
4370 *newword = NUL;
4371 else
4372 STRCPY(newword, ae->ae_add);
4373 p = word;
4374 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004375 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004376 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004377#ifdef FEAT_MBYTE
4378 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004379 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004380 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004381 for ( ; i > 0; --i)
4382 mb_ptr_adv(p);
4383 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004384 else
4385#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004386 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004387 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004388 STRCAT(newword, p);
4389 }
4390 else
4391 {
4392 /* suffix: chop/add at the end of the word */
4393 STRCPY(newword, word);
4394 if (ae->ae_chop != NULL)
4395 {
4396 /* Remove chop string. */
4397 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004398 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004399 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004400 mb_ptr_back(newword, p);
4401 *p = NUL;
4402 }
4403 if (ae->ae_add != NULL)
4404 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004405 }
4406
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004407 /* Obey the "rare" flag of the affix. */
4408 if (ae->ae_rare)
4409 use_flags = flags | WF_RARE;
4410 else
4411 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004412 use_pfxlist = pfxlist;
4413
4414 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004415 if (spin->si_prefroot != NULL
4416 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004417 {
4418 /* ... add a flag to indicate an affix was used. */
4419 use_flags |= WF_HAS_AFF;
4420
4421 /* ... don't use a prefix list if combining
4422 * affixes is not allowed */
4423 if (!ah->ah_combine || comb)
4424 use_pfxlist = NULL;
4425 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004426
Bram Moolenaar51485f02005-06-04 21:55:20 +00004427 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004428 if (store_word(spin, newword, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004429 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004430 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004431
Bram Moolenaar51485f02005-06-04 21:55:20 +00004432 /* When added a suffix and combining is allowed also
4433 * try adding prefixes additionally. */
4434 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004435 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004436 xht, NULL, TRUE,
4437 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004438 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004439 }
4440 }
4441 }
4442 }
4443 }
4444
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004445 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004446}
4447
4448/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004449 * Read a file with a list of words.
4450 */
4451 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004452spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004453 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004454 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004455{
4456 FILE *fd;
4457 long lnum = 0;
4458 char_u rline[MAXLINELEN];
4459 char_u *line;
4460 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004461 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004462 int l;
4463 int retval = OK;
4464 int did_word = FALSE;
4465 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004466 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004467 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004468
4469 /*
4470 * Open the file.
4471 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004472 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004473 if (fd == NULL)
4474 {
4475 EMSG2(_(e_notopen), fname);
4476 return FAIL;
4477 }
4478
Bram Moolenaarb765d632005-06-07 21:00:02 +00004479 if (spin->si_verbose || p_verbose > 2)
4480 {
4481 if (!spin->si_verbose)
4482 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004483 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004484 out_flush();
4485 if (!spin->si_verbose)
4486 verbose_leave();
4487 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004488
4489 /*
4490 * Read all the lines in the file one by one.
4491 */
4492 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4493 {
4494 line_breakcheck();
4495 ++lnum;
4496
4497 /* Skip comment lines. */
4498 if (*rline == '#')
4499 continue;
4500
4501 /* Remove CR, LF and white space from the end. */
4502 l = STRLEN(rline);
4503 while (l > 0 && rline[l - 1] <= ' ')
4504 --l;
4505 if (l == 0)
4506 continue; /* empty or blank line */
4507 rline[l] = NUL;
4508
4509 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4510 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004511#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004512 if (spin->si_conv.vc_type != CONV_NONE)
4513 {
4514 pc = string_convert(&spin->si_conv, rline, NULL);
4515 if (pc == NULL)
4516 {
4517 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4518 fname, lnum, rline);
4519 continue;
4520 }
4521 line = pc;
4522 }
4523 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004524#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004525 {
4526 pc = NULL;
4527 line = rline;
4528 }
4529
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004530 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004531 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004532 ++line;
4533 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004534 {
4535 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004536 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4537 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004538 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004539 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4540 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004541 else
4542 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004543#ifdef FEAT_MBYTE
4544 char_u *enc;
4545
Bram Moolenaar51485f02005-06-04 21:55:20 +00004546 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004547 line += 10;
4548 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004549 if (enc != NULL && !spin->si_ascii
4550 && convert_setup(&spin->si_conv, enc,
4551 p_enc) == FAIL)
4552 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004553 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004554 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004555 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004556#else
4557 smsg((char_u *)_("Conversion in %s not supported"), fname);
4558#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004559 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004560 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004561 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004562
Bram Moolenaar3982c542005-06-08 21:56:31 +00004563 if (STRNCMP(line, "regions=", 8) == 0)
4564 {
4565 if (spin->si_region_count > 1)
4566 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4567 fname, lnum, line);
4568 else
4569 {
4570 line += 8;
4571 if (STRLEN(line) > 16)
4572 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4573 fname, lnum, line);
4574 else
4575 {
4576 spin->si_region_count = STRLEN(line) / 2;
4577 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004578
4579 /* Adjust the mask for a word valid in all regions. */
4580 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004581 }
4582 }
4583 continue;
4584 }
4585
Bram Moolenaar7887d882005-07-01 22:33:52 +00004586 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4587 fname, lnum, line - 1);
4588 continue;
4589 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004590
Bram Moolenaar7887d882005-07-01 22:33:52 +00004591 flags = 0;
4592 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004593
Bram Moolenaar7887d882005-07-01 22:33:52 +00004594 /* Check for flags and region after a slash. */
4595 p = vim_strchr(line, '/');
4596 if (p != NULL)
4597 {
4598 *p++ = NUL;
4599 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004600 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004601 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004602 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004603 else if (*p == '!') /* Bad, bad, wicked word. */
4604 flags |= WF_BANNED;
4605 else if (*p == '?') /* Rare word. */
4606 flags |= WF_RARE;
4607 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004608 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004609 if ((flags & WF_REGION) == 0) /* first one */
4610 regionmask = 0;
4611 flags |= WF_REGION;
4612
4613 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004614 if (l > spin->si_region_count)
4615 {
4616 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004617 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004618 break;
4619 }
4620 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004621 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004622 else
4623 {
4624 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4625 fname, lnum, p);
4626 break;
4627 }
4628 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004629 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004630 }
4631
4632 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4633 if (spin->si_ascii && has_non_ascii(line))
4634 {
4635 ++non_ascii;
4636 continue;
4637 }
4638
4639 /* Normal word: store it. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004640 if (store_word(spin, line, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004641 {
4642 retval = FAIL;
4643 break;
4644 }
4645 did_word = TRUE;
4646 }
4647
4648 vim_free(pc);
4649 fclose(fd);
4650
Bram Moolenaarb765d632005-06-07 21:00:02 +00004651 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4652 {
4653 if (p_verbose > 2)
4654 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004655 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4656 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004657 if (p_verbose > 2)
4658 verbose_leave();
4659 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004660 return retval;
4661}
4662
4663/*
4664 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004665 * This avoids calling free() for every little struct we use (and keeping
4666 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004667 * The memory is cleared to all zeros.
4668 * Returns NULL when out of memory.
4669 */
4670 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004671getroom(spin, len, align)
4672 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004673 size_t len; /* length needed */
4674 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004675{
4676 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004677 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004678
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004679 if (align && bl != NULL)
4680 /* Round size up for alignment. On some systems structures need to be
4681 * aligned to the size of a pointer (e.g., SPARC). */
4682 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4683 & ~(sizeof(char *) - 1);
4684
Bram Moolenaar51485f02005-06-04 21:55:20 +00004685 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4686 {
4687 /* Allocate a block of memory. This is not freed until much later. */
4688 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4689 if (bl == NULL)
4690 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004691 bl->sb_next = spin->si_blocks;
4692 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004693 bl->sb_used = 0;
4694 }
4695
4696 p = bl->sb_data + bl->sb_used;
4697 bl->sb_used += len;
4698
4699 return p;
4700}
4701
4702/*
4703 * Make a copy of a string into memory allocated with getroom().
4704 */
4705 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004706getroom_save(spin, s)
4707 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004708 char_u *s;
4709{
4710 char_u *sc;
4711
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004712 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004713 if (sc != NULL)
4714 STRCPY(sc, s);
4715 return sc;
4716}
4717
4718
4719/*
4720 * Free the list of allocated sblock_T.
4721 */
4722 static void
4723free_blocks(bl)
4724 sblock_T *bl;
4725{
4726 sblock_T *next;
4727
4728 while (bl != NULL)
4729 {
4730 next = bl->sb_next;
4731 vim_free(bl);
4732 bl = next;
4733 }
4734}
4735
4736/*
4737 * Allocate the root of a word tree.
4738 */
4739 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004740wordtree_alloc(spin)
4741 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004742{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004743 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004744}
4745
4746/*
4747 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004748 * Always store it in the case-folded tree. For a keep-case word this is
4749 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4750 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004751 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004752 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004753 */
4754 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004755store_word(spin, word, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004756 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004757 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004758 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004759 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004760 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004761{
4762 int len = STRLEN(word);
4763 int ct = captype(word, word + len);
4764 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004765 int res = OK;
4766 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004769 for (p = pfxlist; res == OK; ++p)
4770 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004771 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4772 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004773 if (p == NULL || *p == NUL)
4774 break;
4775 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004776 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004777
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004778 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004779 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004780 for (p = pfxlist; res == OK; ++p)
4781 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004782 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4783 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004784 if (p == NULL || *p == NUL)
4785 break;
4786 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004787 ++spin->si_keepwcount;
4788 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004789 return res;
4790}
4791
4792/*
4793 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004794 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4795 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004796 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004797 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004798 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004799tree_add_word(spin, word, root, flags, region, prefixID)
4800 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004801 char_u *word;
4802 wordnode_T *root;
4803 int flags;
4804 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004805 int prefixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004807 wordnode_T *node = root;
4808 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004809 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004810 wordnode_T **prev = NULL;
4811 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004812
Bram Moolenaar51485f02005-06-04 21:55:20 +00004813 /* Add each byte of the word to the tree, including the NUL at the end. */
4814 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004815 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004816 /* When there is more than one reference to this node we need to make
4817 * a copy, so that we can modify it. Copy the whole list of siblings
4818 * (we don't optimize for a partly shared list of siblings). */
4819 if (node != NULL && node->wn_refs > 1)
4820 {
4821 --node->wn_refs;
4822 copyprev = prev;
4823 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4824 {
4825 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004826 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004827 if (np == NULL)
4828 return FAIL;
4829 np->wn_child = copyp->wn_child;
4830 if (np->wn_child != NULL)
4831 ++np->wn_child->wn_refs; /* child gets extra ref */
4832 np->wn_byte = copyp->wn_byte;
4833 if (np->wn_byte == NUL)
4834 {
4835 np->wn_flags = copyp->wn_flags;
4836 np->wn_region = copyp->wn_region;
4837 np->wn_prefixID = copyp->wn_prefixID;
4838 }
4839
4840 /* Link the new node in the list, there will be one ref. */
4841 np->wn_refs = 1;
4842 *copyprev = np;
4843 copyprev = &np->wn_sibling;
4844
4845 /* Let "node" point to the head of the copied list. */
4846 if (copyp == node)
4847 node = np;
4848 }
4849 }
4850
Bram Moolenaar51485f02005-06-04 21:55:20 +00004851 /* Look for the sibling that has the same character. They are sorted
4852 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004853 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004854 * done on flags and then on prefixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004855 while (node != NULL
4856 && (node->wn_byte < word[i]
4857 || (node->wn_byte == NUL
4858 && (flags < 0
4859 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004860 : node->wn_flags < (flags & WN_MASK)
4861 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004862 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004863 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864 prev = &node->wn_sibling;
4865 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004866 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004867 if (node == NULL
4868 || node->wn_byte != word[i]
4869 || (word[i] == NUL
4870 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004871 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004872 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004873 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004874 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004875 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004876 if (np == NULL)
4877 return FAIL;
4878 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004879
4880 /* If "node" is NULL this is a new child or the end of the sibling
4881 * list: ref count is one. Otherwise use ref count of sibling and
4882 * make ref count of sibling one (matters when inserting in front
4883 * of the list of siblings). */
4884 if (node == NULL)
4885 np->wn_refs = 1;
4886 else
4887 {
4888 np->wn_refs = node->wn_refs;
4889 node->wn_refs = 1;
4890 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004891 *prev = np;
4892 np->wn_sibling = node;
4893 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004895
Bram Moolenaar51485f02005-06-04 21:55:20 +00004896 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004898 node->wn_flags = flags;
4899 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004900 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004901 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004902 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004903 prev = &node->wn_child;
4904 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004905 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004906#ifdef SPELL_PRINTTREE
4907 smsg("Added \"%s\"", word);
4908 spell_print_tree(root->wn_sibling);
4909#endif
4910
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004911 /* count nr of words added since last message */
4912 ++spin->si_msg_count;
4913
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004914 /*
4915 * Every so many words compress the tree, so that we don't use too much
4916 * memory.
4917 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004918 i = FALSE;
4919 if (root == spin->si_foldroot)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004921 if (++spin->si_fold_added >= SPELL_COMPRESS_CNT)
4922 {
4923 i = TRUE;
4924 spin->si_fold_added = 0;
4925 }
4926 }
4927 else if (root == spin->si_keeproot)
4928 {
4929 if (++spin->si_keep_added >= SPELL_COMPRESS_CNT)
4930 {
4931 i = TRUE;
4932 spin->si_keep_added = 0;
4933 }
4934 }
4935 if (i)
4936 {
4937 if (spin->si_verbose)
4938 {
4939 msg_start();
4940 msg_puts((char_u *)_(msg_compressing));
4941 msg_clr_eos();
4942 msg_didout = FALSE;
4943 msg_col = 0;
4944 out_flush();
4945 }
4946 wordtree_compress(spin, root);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004947 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004948
4949 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950}
4951
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004952/*
4953 * Get a wordnode_T, either from the list of previously freed nodes or
4954 * allocate a new one.
4955 */
4956 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004957get_wordnode(spin)
4958 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004959{
4960 wordnode_T *n;
4961
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004962 if (spin->si_first_free == NULL)
4963 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004964 else
4965 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004966 n = spin->si_first_free;
4967 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004968 vim_memset(n, 0, sizeof(wordnode_T));
4969 }
4970#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004971 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004972#endif
4973 return n;
4974}
4975
4976/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004977 * Decrement the reference count on a node (which is the head of a list of
4978 * siblings). If the reference count becomes zero free the node and its
4979 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004980 */
4981 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004982deref_wordnode(spin, node)
4983 spellinfo_T *spin;
4984 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004985{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004986 wordnode_T *np;
4987
4988 if (--node->wn_refs == 0)
4989 for (np = node; np != NULL; np = np->wn_sibling)
4990 {
4991 if (np->wn_child != NULL)
4992 deref_wordnode(spin, np->wn_child);
4993 free_wordnode(spin, np);
4994 }
4995}
4996
4997/*
4998 * Free a wordnode_T for re-use later.
4999 * Only the "wn_child" field becomes invalid.
5000 */
5001 static void
5002free_wordnode(spin, n)
5003 spellinfo_T *spin;
5004 wordnode_T *n;
5005{
5006 n->wn_child = spin->si_first_free;
5007 spin->si_first_free = n;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005008}
5009
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005011 * Compress a tree: find tails that are identical and can be shared.
5012 */
5013 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005014wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005015 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005016 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005017{
5018 hashtab_T ht;
5019 int n;
5020 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005021 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005022
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005023 /* Skip the root itself, it's not actually used. The first sibling is the
5024 * start of the tree. */
5025 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005026 {
5027 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005028 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005029
5030#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005031 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005032#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005033 {
5034 if (!spin->si_verbose)
5035 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005036 if (tot > 1000000)
5037 perc = (tot - n) / (tot / 100);
5038 else
5039 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005040 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005041 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005042 if (p_verbose > 2)
5043 verbose_leave();
5044 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005045#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005046 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005047#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005048 hash_clear(&ht);
5049 }
5050}
5051
5052/*
5053 * Compress a node, its siblings and its children, depth first.
5054 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005055 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005056 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005057node_compress(spin, node, ht, tot)
5058 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005059 wordnode_T *node;
5060 hashtab_T *ht;
5061 int *tot; /* total count of nodes before compressing,
5062 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005063{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005064 wordnode_T *np;
5065 wordnode_T *tp;
5066 wordnode_T *child;
5067 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005069 int len = 0;
5070 unsigned nr, n;
5071 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005072
Bram Moolenaar51485f02005-06-04 21:55:20 +00005073 /*
5074 * Go through the list of siblings. Compress each child and then try
5075 * finding an identical child to replace it.
5076 * Note that with "child" we mean not just the node that is pointed to,
5077 * but the whole list of siblings, of which the node is the first.
5078 */
5079 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005080 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005081 ++len;
5082 if ((child = np->wn_child) != NULL)
5083 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005084 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005085 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005086
5087 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005088 hash = hash_hash(child->wn_u1.hashkey);
5089 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005090 tp = NULL;
5091 if (!HASHITEM_EMPTY(hi))
5092 {
5093 /* There are children with an identical hash value. Now check
5094 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005095 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005096 if (node_equal(child, tp))
5097 {
5098 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005099 * current one. This means the current child and all
5100 * its siblings is unlinked from the tree. */
5101 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005102 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005103 np->wn_child = tp;
5104 ++compressed;
5105 break;
5106 }
5107 if (tp == NULL)
5108 {
5109 /* No other child with this hash value equals the child of
5110 * the node, add it to the linked list after the first
5111 * item. */
5112 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005113 child->wn_u2.next = tp->wn_u2.next;
5114 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005115 }
5116 }
5117 else
5118 /* No other child has this hash value, add it to the
5119 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005120 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005121 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005122 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005123 *tot += len;
5124
5125 /*
5126 * Make a hash key for the node and its siblings, so that we can quickly
5127 * find a lookalike node. This must be done after compressing the sibling
5128 * list, otherwise the hash key would become invalid by the compression.
5129 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005130 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005131 nr = 0;
5132 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005133 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005134 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005135 /* end node: use wn_flags, wn_region and wn_prefixID */
5136 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005137 else
5138 /* byte node: use the byte value and the child pointer */
5139 n = np->wn_byte + ((long_u)np->wn_child << 8);
5140 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005142
5143 /* Avoid NUL bytes, it terminates the hash key. */
5144 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005145 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005146 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005147 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005148 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005149 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005150 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005151 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
5152 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005153
5154 return compressed;
5155}
5156
5157/*
5158 * Return TRUE when two nodes have identical siblings and children.
5159 */
5160 static int
5161node_equal(n1, n2)
5162 wordnode_T *n1;
5163 wordnode_T *n2;
5164{
5165 wordnode_T *p1;
5166 wordnode_T *p2;
5167
5168 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
5169 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
5170 if (p1->wn_byte != p2->wn_byte
5171 || (p1->wn_byte == NUL
5172 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005173 || p1->wn_region != p2->wn_region
5174 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005175 : (p1->wn_child != p2->wn_child)))
5176 break;
5177
5178 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005179}
5180
5181/*
5182 * Write a number to file "fd", MSB first, in "len" bytes.
5183 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005184 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005185put_bytes(fd, nr, len)
5186 FILE *fd;
5187 long_u nr;
5188 int len;
5189{
5190 int i;
5191
5192 for (i = len - 1; i >= 0; --i)
5193 putc((int)(nr >> (i * 8)), fd);
5194}
5195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196static int
5197#ifdef __BORLANDC__
5198_RTLENTRYF
5199#endif
5200rep_compare __ARGS((const void *s1, const void *s2));
5201
5202/*
5203 * Function given to qsort() to sort the REP items on "from" string.
5204 */
5205 static int
5206#ifdef __BORLANDC__
5207_RTLENTRYF
5208#endif
5209rep_compare(s1, s2)
5210 const void *s1;
5211 const void *s2;
5212{
5213 fromto_T *p1 = (fromto_T *)s1;
5214 fromto_T *p2 = (fromto_T *)s2;
5215
5216 return STRCMP(p1->ft_from, p2->ft_from);
5217}
5218
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005219/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005220 * Write the Vim spell file "fname".
5221 */
5222 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005223write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005224 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005225 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005226{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005227 FILE *fd;
5228 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005229 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005230 wordnode_T *tree;
5231 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005232 int i;
5233 int l;
5234 garray_T *gap;
5235 fromto_T *ftp;
5236 char_u *p;
5237 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005238
Bram Moolenaarb765d632005-06-07 21:00:02 +00005239 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005240 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005241 {
5242 EMSG2(_(e_notopen), fname);
5243 return;
5244 }
5245
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005246 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005247 * <charflagslen> <charflags>
5248 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005249 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005250 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005251
5252 /* <fileID> */
5253 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
5254 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005255
5256 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005257 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005258 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005259 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
5260 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
5261 (size_t)1, fd);
5262 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005263 }
5264 else
5265 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005266 putc(0, fd);
5267 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005268 }
5269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 /*
5271 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005272 * <charflagslen> <charflags> <fcharlen> <fchars>
5273 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005274 * 'encoding'.
5275 * Also skip this for an .add.spl file, the main spell file must contain
5276 * the table (avoids that it conflicts). File is shorter too.
5277 */
5278 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005279 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005280 putc(0, fd);
5281 putc(0, fd);
5282 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005283 }
5284 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005285 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005286
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005287
5288 if (spin->si_midword == NULL)
5289 put_bytes(fd, 0L, 2); /* <midwordlen> */
5290 else
5291 {
5292 i = STRLEN(spin->si_midword);
5293 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
5294 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
5295 }
5296
5297
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005298 /* Write the prefix conditions. */
5299 write_spell_prefcond(fd, &spin->si_prefcond);
5300
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005301 /* <SUGGEST> : <repcount> <rep> ...
5302 * <salflags> <salcount> <sal> ...
5303 * <maplen> <mapstr> */
5304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 /* Sort the REP items. */
5306 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
5307 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005308
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005309 /* round 1: REP items
5310 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 for (round = 1; round <= 2; ++round)
5312 {
5313 if (round == 1)
5314 gap = &spin->si_rep;
5315 else
5316 {
5317 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 i = 0;
5320 if (spin->si_followup)
5321 i |= SAL_F0LLOWUP;
5322 if (spin->si_collapse)
5323 i |= SAL_COLLAPSE;
5324 if (spin->si_rem_accents)
5325 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005326 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5327 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005329 if (i & SAL_SOFO)
5330 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
5332
5333 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
5334 for (i = 0; i < gap->ga_len; ++i)
5335 {
5336 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
5337 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
5338 ftp = &((fromto_T *)gap->ga_data)[i];
5339 for (rr = 1; rr <= 2; ++rr)
5340 {
5341 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5342 l = STRLEN(p);
5343 putc(l, fd);
5344 fwrite(p, l, (size_t)1, fd);
5345 }
5346 }
5347 }
5348
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005349 /* SOFOFROM and SOFOTO */
5350 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5351 {
5352 put_bytes(fd, 1L, 2); /* <salcount> */
5353
5354 l = STRLEN(spin->si_sofofr);
5355 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5356 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5357
5358 l = STRLEN(spin->si_sofoto);
5359 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5360 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5361 }
5362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5364 if (spin->si_map.ga_len > 0) /* <mapstr> */
5365 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5366 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005367
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005368 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005369 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005370 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005371 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005372 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005373 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005374 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005375 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005376 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005377 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005378 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005379 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005380
Bram Moolenaar0c405862005-06-22 22:26:26 +00005381 /* Clear the index and wnode fields in the tree. */
5382 clear_node(tree);
5383
Bram Moolenaar51485f02005-06-04 21:55:20 +00005384 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005385 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005386 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005387 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005388
Bram Moolenaar51485f02005-06-04 21:55:20 +00005389 /* number of nodes in 4 bytes */
5390 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005391 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005392
Bram Moolenaar51485f02005-06-04 21:55:20 +00005393 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005394 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005395 }
5396
Bram Moolenaar51485f02005-06-04 21:55:20 +00005397 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005398}
5399
5400/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005401 * Clear the index and wnode fields of "node", it siblings and its
5402 * children. This is needed because they are a union with other items to save
5403 * space.
5404 */
5405 static void
5406clear_node(node)
5407 wordnode_T *node;
5408{
5409 wordnode_T *np;
5410
5411 if (node != NULL)
5412 for (np = node; np != NULL; np = np->wn_sibling)
5413 {
5414 np->wn_u1.index = 0;
5415 np->wn_u2.wnode = NULL;
5416
5417 if (np->wn_byte != NUL)
5418 clear_node(np->wn_child);
5419 }
5420}
5421
5422
5423/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005424 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005425 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005426 * This first writes the list of possible bytes (siblings). Then for each
5427 * byte recursively write the children.
5428 *
5429 * NOTE: The code here must match the code in read_tree(), since assumptions
5430 * are made about the indexes (so that we don't have to write them in the
5431 * file).
5432 *
5433 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005434 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005435 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005436put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005437 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005438 wordnode_T *node;
5439 int index;
5440 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005441 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005442{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005443 int newindex = index;
5444 int siblingcount = 0;
5445 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005446 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005447
Bram Moolenaar51485f02005-06-04 21:55:20 +00005448 /* If "node" is zero the tree is empty. */
5449 if (node == NULL)
5450 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005451
Bram Moolenaar51485f02005-06-04 21:55:20 +00005452 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005453 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005454
5455 /* Count the number of siblings. */
5456 for (np = node; np != NULL; np = np->wn_sibling)
5457 ++siblingcount;
5458
5459 /* Write the sibling count. */
5460 if (fd != NULL)
5461 putc(siblingcount, fd); /* <siblingcount> */
5462
5463 /* Write each sibling byte and optionally extra info. */
5464 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005465 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005466 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005467 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005468 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005469 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005470 /* For a NUL byte (end of word) write the flags etc. */
5471 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005472 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005473 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005474 * associated condition nr (stored in wn_region). The
5475 * byte value is misused to store the "rare" and "not
5476 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005477 if (np->wn_flags == (short_u)PFX_FLAGS)
5478 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005479 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005480 {
5481 putc(BY_FLAGS, fd); /* <byte> */
5482 putc(np->wn_flags, fd); /* <pflags> */
5483 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005484 putc(np->wn_prefixID, fd); /* <prefixID> */
5485 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005486 }
5487 else
5488 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005489 /* For word trees we write the flag/region items. */
5490 flags = np->wn_flags;
5491 if (regionmask != 0 && np->wn_region != regionmask)
5492 flags |= WF_REGION;
5493 if (np->wn_prefixID != 0)
5494 flags |= WF_PFX;
5495 if (flags == 0)
5496 {
5497 /* word without flags or region */
5498 putc(BY_NOFLAGS, fd); /* <byte> */
5499 }
5500 else
5501 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005502 if (np->wn_flags >= 0x100)
5503 {
5504 putc(BY_FLAGS2, fd); /* <byte> */
5505 putc(flags, fd); /* <flags> */
5506 putc((unsigned)flags >> 8, fd); /* <flags2> */
5507 }
5508 else
5509 {
5510 putc(BY_FLAGS, fd); /* <byte> */
5511 putc(flags, fd); /* <flags> */
5512 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005513 if (flags & WF_REGION)
5514 putc(np->wn_region, fd); /* <region> */
5515 if (flags & WF_PFX)
5516 putc(np->wn_prefixID, fd); /* <prefixID> */
5517 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005518 }
5519 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005520 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005521 else
5522 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005523 if (np->wn_child->wn_u1.index != 0
5524 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005525 {
5526 /* The child is written elsewhere, write the reference. */
5527 if (fd != NULL)
5528 {
5529 putc(BY_INDEX, fd); /* <byte> */
5530 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005531 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005532 }
5533 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005534 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005535 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005536 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005537
Bram Moolenaar51485f02005-06-04 21:55:20 +00005538 if (fd != NULL)
5539 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5540 {
5541 EMSG(_(e_write));
5542 return 0;
5543 }
5544 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005545 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005546
5547 /* Space used in the array when reading: one for each sibling and one for
5548 * the count. */
5549 newindex += siblingcount + 1;
5550
5551 /* Recursively dump the children of each sibling. */
5552 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005553 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5554 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005555 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005556
5557 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005558}
5559
5560
5561/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005562 * ":mkspell [-ascii] outfile infile ..."
5563 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005564 */
5565 void
5566ex_mkspell(eap)
5567 exarg_T *eap;
5568{
5569 int fcount;
5570 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005571 char_u *arg = eap->arg;
5572 int ascii = FALSE;
5573
5574 if (STRNCMP(arg, "-ascii", 6) == 0)
5575 {
5576 ascii = TRUE;
5577 arg = skipwhite(arg + 6);
5578 }
5579
5580 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5581 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005583 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005584 FreeWild(fcount, fnames);
5585 }
5586}
5587
5588/*
5589 * Create a Vim spell file from one or more word lists.
5590 * "fnames[0]" is the output file name.
5591 * "fnames[fcount - 1]" is the last input file name.
5592 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5593 * and ".spl" is appended to make the output file name.
5594 */
5595 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005596mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005597 int fcount;
5598 char_u **fnames;
5599 int ascii; /* -ascii argument given */
5600 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005601 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005602{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005603 char_u fname[MAXPATHL];
5604 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005605 char_u **innames;
5606 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005607 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005608 int i;
5609 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005610 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005611 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005612 spellinfo_T spin;
5613
5614 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005615 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005616 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005617 spin.si_followup = TRUE;
5618 spin.si_rem_accents = TRUE;
5619 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5620 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5621 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005622 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005623
Bram Moolenaarb765d632005-06-07 21:00:02 +00005624 /* default: fnames[0] is output file, following are input files */
5625 innames = &fnames[1];
5626 incount = fcount - 1;
5627
5628 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005629 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005630 len = STRLEN(fnames[0]);
5631 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5632 {
5633 /* For ":mkspell path/en.latin1.add" output file is
5634 * "path/en.latin1.add.spl". */
5635 innames = &fnames[0];
5636 incount = 1;
5637 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5638 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005639 else if (fcount == 1)
5640 {
5641 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5642 innames = &fnames[0];
5643 incount = 1;
5644 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5645 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5646 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005647 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5648 {
5649 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005650 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005651 }
5652 else
5653 /* Name should be language, make the file name from it. */
5654 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5655 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5656
5657 /* Check for .ascii.spl. */
5658 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5659 spin.si_ascii = TRUE;
5660
5661 /* Check for .add.spl. */
5662 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5663 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005664 }
5665
Bram Moolenaarb765d632005-06-07 21:00:02 +00005666 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005667 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005668 else if (vim_strchr(gettail(wfname), '_') != NULL)
5669 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005670 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005671 EMSG(_("E754: Only up to 8 regions supported"));
5672 else
5673 {
5674 /* Check for overwriting before doing things that may take a lot of
5675 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005676 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005677 {
5678 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005679 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005680 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005681 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005682 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005683 EMSG2(_(e_isadir2), wfname);
5684 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005685 }
5686
5687 /*
5688 * Init the aff and dic pointers.
5689 * Get the region names if there are more than 2 arguments.
5690 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005691 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005692 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005693 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005694
Bram Moolenaar3982c542005-06-08 21:56:31 +00005695 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005697 len = STRLEN(innames[i]);
5698 if (STRLEN(gettail(innames[i])) < 5
5699 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005700 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005701 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5702 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005704 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5705 spin.si_region_name[i * 2 + 1] =
5706 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005707 }
5708 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005709 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005710
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005711 spin.si_foldroot = wordtree_alloc(&spin);
5712 spin.si_keeproot = wordtree_alloc(&spin);
5713 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005714 if (spin.si_foldroot == NULL
5715 || spin.si_keeproot == NULL
5716 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005717 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005718 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005719 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005720 }
5721
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005722 /* When not producing a .add.spl file clear the character table when
5723 * we encounter one in the .aff file. This means we dump the current
5724 * one in the .spl file if the .aff file doesn't define one. That's
5725 * better than guessing the contents, the table will match a
5726 * previously loaded spell file. */
5727 if (!spin.si_add)
5728 spin.si_clear_chartab = TRUE;
5729
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005730 /*
5731 * Read all the .aff and .dic files.
5732 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005733 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005734 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005735 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005736 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005737 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005738 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005739
Bram Moolenaarb765d632005-06-07 21:00:02 +00005740 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005741 if (mch_stat((char *)fname, &st) >= 0)
5742 {
5743 /* Read the .aff file. Will init "spin->si_conv" based on the
5744 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005745 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005746 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005747 error = TRUE;
5748 else
5749 {
5750 /* Read the .dic file and store the words in the trees. */
5751 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005752 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005753 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005754 error = TRUE;
5755 }
5756 }
5757 else
5758 {
5759 /* No .aff file, try reading the file as a word list. Store
5760 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005761 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005762 error = TRUE;
5763 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005764
Bram Moolenaarb765d632005-06-07 21:00:02 +00005765#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005766 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005767 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005768#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005769 }
5770
Bram Moolenaar51485f02005-06-04 21:55:20 +00005771 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005772 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005773 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005774 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005775 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005776 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005777 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005778 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005779 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005780 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005781 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005782 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005783 verbose_leave();
5784 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005785 wordtree_compress(&spin, spin.si_foldroot);
5786 wordtree_compress(&spin, spin.si_keeproot);
5787 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005788 }
5789
Bram Moolenaar51485f02005-06-04 21:55:20 +00005790 if (!error)
5791 {
5792 /*
5793 * Write the info in the spell file.
5794 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005795 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005796 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005797 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005798 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005799 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005800 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005801 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005802 verbose_leave();
5803 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005804
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005805 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005806
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005807 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005808 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005809 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005810 verbose_enter();
5811 MSG(_("Done!"));
5812 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005813 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005814 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005815 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005816 verbose_leave();
5817 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005818
Bram Moolenaarb765d632005-06-07 21:00:02 +00005819 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005820 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005821 }
5822
5823 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005824 ga_clear(&spin.si_rep);
5825 ga_clear(&spin.si_sal);
5826 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005827 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005828 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005829 vim_free(spin.si_sofofr);
5830 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005831
5832 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005833 for (i = 0; i < incount; ++i)
5834 if (afile[i] != NULL)
5835 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005836
5837 /* Free all the bits and pieces at once. */
5838 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005839 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005840}
5841
Bram Moolenaarb765d632005-06-07 21:00:02 +00005842
5843/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005844 * ":[count]spellgood {word}"
5845 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005846 */
5847 void
5848ex_spell(eap)
5849 exarg_T *eap;
5850{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005851 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005852 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005853}
5854
5855/*
5856 * Add "word[len]" to 'spellfile' as a good or bad word.
5857 */
5858 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005859spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005860 char_u *word;
5861 int len;
5862 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005863 int index; /* "zG" and "zW": zero, otherwise index in
5864 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005865{
5866 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005867 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005868 int new_spf = FALSE;
5869 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005870 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005871 char_u fnamebuf[MAXPATHL];
5872 char_u line[MAXWLEN * 2];
5873 long fpos, fpos_next = 0;
5874 int i;
5875 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005876
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005877 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005878 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005879 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005880 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005881 int_wordlist = vim_tempname('s');
5882 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005883 return;
5884 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005885 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005886 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005887 else
5888 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005889 /* If 'spellfile' isn't set figure out a good default value. */
5890 if (*curbuf->b_p_spf == NUL)
5891 {
5892 init_spellfile();
5893 new_spf = TRUE;
5894 }
5895
5896 if (*curbuf->b_p_spf == NUL)
5897 {
5898 EMSG(_("E764: 'spellfile' is not set"));
5899 return;
5900 }
5901
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005902 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5903 {
5904 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5905 if (i == index)
5906 break;
5907 if (*spf == NUL)
5908 {
5909 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5910 return;
5911 }
5912 }
5913
Bram Moolenaarb765d632005-06-07 21:00:02 +00005914 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005915 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005916 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5917 buf = NULL;
5918 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005919 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005920 EMSG(_(e_bufloaded));
5921 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005922 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005923
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005924 fname = fnamebuf;
5925 }
5926
5927 if (bad)
5928 {
5929 /* When the word also appears as good word we need to remove that one,
5930 * since its flags sort before the one with WF_BANNED. */
5931 fd = mch_fopen((char *)fname, "r");
5932 if (fd != NULL)
5933 {
5934 while (!vim_fgets(line, MAXWLEN * 2, fd))
5935 {
5936 fpos = fpos_next;
5937 fpos_next = ftell(fd);
5938 if (STRNCMP(word, line, len) == 0
5939 && (line[len] == '/' || line[len] < ' '))
5940 {
5941 /* Found duplicate word. Remove it by writing a '#' at
5942 * the start of the line. Mixing reading and writing
5943 * doesn't work for all systems, close the file first. */
5944 fclose(fd);
5945 fd = mch_fopen((char *)fname, "r+");
5946 if (fd == NULL)
5947 break;
5948 if (fseek(fd, fpos, SEEK_SET) == 0)
5949 fputc('#', fd);
5950 fseek(fd, fpos_next, SEEK_SET);
5951 }
5952 }
5953 fclose(fd);
5954 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005955 }
5956
5957 fd = mch_fopen((char *)fname, "a");
5958 if (fd == NULL && new_spf)
5959 {
5960 /* We just initialized the 'spellfile' option and can't open the file.
5961 * We may need to create the "spell" directory first. We already
5962 * checked the runtime directory is writable in init_spellfile(). */
5963 STRCPY(NameBuff, fname);
5964 *gettail_sep(NameBuff) = NUL;
5965 if (mch_stat((char *)NameBuff, &st) < 0)
5966 {
5967 /* The directory doesn't exist. Try creating it and opening the
5968 * file again. */
5969 vim_mkdir(NameBuff, 0755);
5970 fd = mch_fopen((char *)fname, "a");
5971 }
5972 }
5973
5974 if (fd == NULL)
5975 EMSG2(_(e_notopen), fname);
5976 else
5977 {
5978 if (bad)
5979 fprintf(fd, "%.*s/!\n", len, word);
5980 else
5981 fprintf(fd, "%.*s\n", len, word);
5982 fclose(fd);
5983
5984 /* Update the .add.spl file. */
5985 mkspell(1, &fname, FALSE, TRUE, TRUE);
5986
5987 /* If the .add file is edited somewhere, reload it. */
5988 if (buf != NULL)
5989 buf_reload(buf);
5990
5991 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005992 }
5993}
5994
5995/*
5996 * Initialize 'spellfile' for the current buffer.
5997 */
5998 static void
5999init_spellfile()
6000{
6001 char_u buf[MAXPATHL];
6002 int l;
6003 slang_T *sl;
6004 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006005 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006006
6007 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
6008 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006009 /* Find the end of the language name. Exclude the region. */
6010 for (lend = curbuf->b_p_spl; *lend != NUL
6011 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6012 ;
6013
6014 /* Loop over all entries in 'runtimepath'. Use the first one where we
6015 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006016 rtp = p_rtp;
6017 while (*rtp != NUL)
6018 {
6019 /* Copy the path from 'runtimepath' to buf[]. */
6020 copy_option_part(&rtp, buf, MAXPATHL, ",");
6021 if (filewritable(buf) == 2)
6022 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00006023 /* Use the first language name from 'spelllang' and the
6024 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006025 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
6026 l = STRLEN(buf);
6027 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00006028 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006029 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00006030 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
6031 ? (char_u *)"ascii" : spell_enc());
6032 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6033 break;
6034 }
6035 }
6036 }
6037}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006038
Bram Moolenaar51485f02005-06-04 21:55:20 +00006039
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006040/*
6041 * Init the chartab used for spelling for ASCII.
6042 * EBCDIC is not supported!
6043 */
6044 static void
6045clear_spell_chartab(sp)
6046 spelltab_T *sp;
6047{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006048 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006049
6050 /* Init everything to FALSE. */
6051 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
6052 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
6053 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006054 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006055 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006056 sp->st_upper[i] = i;
6057 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006058
6059 /* We include digits. A word shouldn't start with a digit, but handling
6060 * that is done separately. */
6061 for (i = '0'; i <= '9'; ++i)
6062 sp->st_isw[i] = TRUE;
6063 for (i = 'A'; i <= 'Z'; ++i)
6064 {
6065 sp->st_isw[i] = TRUE;
6066 sp->st_isu[i] = TRUE;
6067 sp->st_fold[i] = i + 0x20;
6068 }
6069 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006070 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006071 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006072 sp->st_upper[i] = i - 0x20;
6073 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006074}
6075
6076/*
6077 * Init the chartab used for spelling. Only depends on 'encoding'.
6078 * Called once while starting up and when 'encoding' changes.
6079 * The default is to use isalpha(), but the spell file should define the word
6080 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006081 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006082 */
6083 void
6084init_spell_chartab()
6085{
6086 int i;
6087
6088 did_set_spelltab = FALSE;
6089 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006090#ifdef FEAT_MBYTE
6091 if (enc_dbcs)
6092 {
6093 /* DBCS: assume double-wide characters are word characters. */
6094 for (i = 128; i <= 255; ++i)
6095 if (MB_BYTE2LEN(i) == 2)
6096 spelltab.st_isw[i] = TRUE;
6097 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006098 else if (enc_utf8)
6099 {
6100 for (i = 128; i < 256; ++i)
6101 {
6102 spelltab.st_isu[i] = utf_isupper(i);
6103 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
6104 spelltab.st_fold[i] = utf_fold(i);
6105 spelltab.st_upper[i] = utf_toupper(i);
6106 }
6107 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006108 else
6109#endif
6110 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006111 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006112 for (i = 128; i < 256; ++i)
6113 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006114 if (MB_ISUPPER(i))
6115 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006116 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006117 spelltab.st_isu[i] = TRUE;
6118 spelltab.st_fold[i] = MB_TOLOWER(i);
6119 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006120 else if (MB_ISLOWER(i))
6121 {
6122 spelltab.st_isw[i] = TRUE;
6123 spelltab.st_upper[i] = MB_TOUPPER(i);
6124 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006125 }
6126 }
6127}
6128
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006129static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
6130static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
6131
6132/*
6133 * Set the spell character tables from strings in the affix file.
6134 */
6135 static int
6136set_spell_chartab(fol, low, upp)
6137 char_u *fol;
6138 char_u *low;
6139 char_u *upp;
6140{
6141 /* We build the new tables here first, so that we can compare with the
6142 * previous one. */
6143 spelltab_T new_st;
6144 char_u *pf = fol, *pl = low, *pu = upp;
6145 int f, l, u;
6146
6147 clear_spell_chartab(&new_st);
6148
6149 while (*pf != NUL)
6150 {
6151 if (*pl == NUL || *pu == NUL)
6152 {
6153 EMSG(_(e_affform));
6154 return FAIL;
6155 }
6156#ifdef FEAT_MBYTE
6157 f = mb_ptr2char_adv(&pf);
6158 l = mb_ptr2char_adv(&pl);
6159 u = mb_ptr2char_adv(&pu);
6160#else
6161 f = *pf++;
6162 l = *pl++;
6163 u = *pu++;
6164#endif
6165 /* Every character that appears is a word character. */
6166 if (f < 256)
6167 new_st.st_isw[f] = TRUE;
6168 if (l < 256)
6169 new_st.st_isw[l] = TRUE;
6170 if (u < 256)
6171 new_st.st_isw[u] = TRUE;
6172
6173 /* if "LOW" and "FOL" are not the same the "LOW" char needs
6174 * case-folding */
6175 if (l < 256 && l != f)
6176 {
6177 if (f >= 256)
6178 {
6179 EMSG(_(e_affrange));
6180 return FAIL;
6181 }
6182 new_st.st_fold[l] = f;
6183 }
6184
6185 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006186 * case-folding, it's upper case and the "UPP" is the upper case of
6187 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006188 if (u < 256 && u != f)
6189 {
6190 if (f >= 256)
6191 {
6192 EMSG(_(e_affrange));
6193 return FAIL;
6194 }
6195 new_st.st_fold[u] = f;
6196 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006197 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006198 }
6199 }
6200
6201 if (*pl != NUL || *pu != NUL)
6202 {
6203 EMSG(_(e_affform));
6204 return FAIL;
6205 }
6206
6207 return set_spell_finish(&new_st);
6208}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006209
6210/*
6211 * Set the spell character tables from strings in the .spl file.
6212 */
6213 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006214set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006215 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006216 int cnt; /* length of "flags" */
6217 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006218{
6219 /* We build the new tables here first, so that we can compare with the
6220 * previous one. */
6221 spelltab_T new_st;
6222 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006223 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006224 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006225
6226 clear_spell_chartab(&new_st);
6227
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006228 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006229 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006230 if (i < cnt)
6231 {
6232 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6233 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6234 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006235
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006236 if (*p != NUL)
6237 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006238#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006239 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006240#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006241 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006242#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006243 new_st.st_fold[i + 128] = c;
6244 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6245 new_st.st_upper[c] = i + 128;
6246 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006247 }
6248
6249 return set_spell_finish(&new_st);
6250}
6251
6252 static int
6253set_spell_finish(new_st)
6254 spelltab_T *new_st;
6255{
6256 int i;
6257
6258 if (did_set_spelltab)
6259 {
6260 /* check that it's the same table */
6261 for (i = 0; i < 256; ++i)
6262 {
6263 if (spelltab.st_isw[i] != new_st->st_isw[i]
6264 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006265 || spelltab.st_fold[i] != new_st->st_fold[i]
6266 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006267 {
6268 EMSG(_("E763: Word characters differ between spell files"));
6269 return FAIL;
6270 }
6271 }
6272 }
6273 else
6274 {
6275 /* copy the new spelltab into the one being used */
6276 spelltab = *new_st;
6277 did_set_spelltab = TRUE;
6278 }
6279
6280 return OK;
6281}
6282
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006283/*
Bram Moolenaarea408852005-06-25 22:49:46 +00006284 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006285 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00006286 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006287 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00006288 */
6289 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006290spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00006291 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006292 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00006293{
Bram Moolenaarea408852005-06-25 22:49:46 +00006294#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006295 char_u *s;
6296 int l;
6297 int c;
6298
6299 if (has_mbyte)
6300 {
6301 l = MB_BYTE2LEN(*p);
6302 s = p;
6303 if (l == 1)
6304 {
6305 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006306 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006307 {
6308 s = p + 1; /* skip a mid-word character */
6309 l = MB_BYTE2LEN(*s);
6310 }
6311 }
6312 else
6313 {
6314 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006315 if (c < 256 ? buf->b_spell_ismw[c]
6316 : (buf->b_spell_ismw_mb != NULL
6317 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006318 {
6319 s = p + l;
6320 l = MB_BYTE2LEN(*s);
6321 }
6322 }
6323
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006324 c = mb_ptr2char(s);
6325 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006326 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006327 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006328 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006329#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006330
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006331 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
6332}
6333
6334/*
6335 * Return TRUE if "p" points to a word character.
6336 * Unlike spell_iswordp() this doesn't check for "midword" characters.
6337 */
6338 static int
6339spell_iswordp_nmw(p)
6340 char_u *p;
6341{
6342#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006343 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006344
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006345 if (has_mbyte)
6346 {
6347 c = mb_ptr2char(p);
6348 if (c > 255)
6349 return mb_get_class(p) >= 2;
6350 return spelltab.st_isw[c];
6351 }
6352#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006353 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006354}
6355
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006356#ifdef FEAT_MBYTE
6357/*
6358 * Return TRUE if "p" points to a word character.
6359 * Wide version of spell_iswordp().
6360 */
6361 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006362spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006363 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006364 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006365{
6366 int *s;
6367
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006368 if (*p < 256 ? buf->b_spell_ismw[*p]
6369 : (buf->b_spell_ismw_mb != NULL
6370 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006371 s = p + 1;
6372 else
6373 s = p;
6374
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006375 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006376 {
6377 if (enc_utf8)
6378 return utf_class(*s) >= 2;
6379 if (enc_dbcs)
6380 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6381 return 0;
6382 }
6383 return spelltab.st_isw[*s];
6384}
6385#endif
6386
Bram Moolenaarea408852005-06-25 22:49:46 +00006387/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006388 * Write the table with prefix conditions to the .spl file.
6389 */
6390 static void
6391write_spell_prefcond(fd, gap)
6392 FILE *fd;
6393 garray_T *gap;
6394{
6395 int i;
6396 char_u *p;
6397 int len;
6398
6399 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6400
6401 for (i = 0; i < gap->ga_len; ++i)
6402 {
6403 /* <prefcond> : <condlen> <condstr> */
6404 p = ((char_u **)gap->ga_data)[i];
6405 if (p == NULL)
6406 fputc(0, fd);
6407 else
6408 {
6409 len = STRLEN(p);
6410 fputc(len, fd);
6411 fwrite(p, (size_t)len, (size_t)1, fd);
6412 }
6413 }
6414}
6415
6416/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006417 * Write the current tables into the .spl file.
6418 * This makes sure the same characters are recognized as word characters when
6419 * generating an when using a spell file.
6420 */
6421 static void
6422write_spell_chartab(fd)
6423 FILE *fd;
6424{
6425 char_u charbuf[256 * 4];
6426 int len = 0;
6427 int flags;
6428 int i;
6429
6430 fputc(128, fd); /* <charflagslen> */
6431 for (i = 128; i < 256; ++i)
6432 {
6433 flags = 0;
6434 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006435 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006436 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006437 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006438 fputc(flags, fd); /* <charflags> */
6439
Bram Moolenaarb765d632005-06-07 21:00:02 +00006440#ifdef FEAT_MBYTE
6441 if (has_mbyte)
6442 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6443 else
6444#endif
6445 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006446 }
6447
6448 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6449 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6450}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006451
6452/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006453 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6454 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006455 * When using a multi-byte 'encoding' the length may change!
6456 * Returns FAIL when something wrong.
6457 */
6458 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006459spell_casefold(str, len, buf, buflen)
6460 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006461 int len;
6462 char_u *buf;
6463 int buflen;
6464{
6465 int i;
6466
6467 if (len >= buflen)
6468 {
6469 buf[0] = NUL;
6470 return FAIL; /* result will not fit */
6471 }
6472
6473#ifdef FEAT_MBYTE
6474 if (has_mbyte)
6475 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006476 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006477 char_u *p;
6478 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006479
6480 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006481 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006482 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006483 if (outi + MB_MAXBYTES > buflen)
6484 {
6485 buf[outi] = NUL;
6486 return FAIL;
6487 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006488 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006489 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006490 }
6491 buf[outi] = NUL;
6492 }
6493 else
6494#endif
6495 {
6496 /* Be quick for non-multibyte encodings. */
6497 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006498 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006499 buf[i] = NUL;
6500 }
6501
6502 return OK;
6503}
6504
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006505#define SPS_BEST 1
6506#define SPS_FAST 2
6507#define SPS_DOUBLE 4
6508
6509static int sps_flags = SPS_BEST;
6510
6511/*
6512 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6513 * Sets "sps_flags".
6514 */
6515 int
6516spell_check_sps()
6517{
6518 char_u *p;
6519 char_u buf[MAXPATHL];
6520 int f;
6521
6522 sps_flags = 0;
6523
6524 for (p = p_sps; *p != NUL; )
6525 {
6526 copy_option_part(&p, buf, MAXPATHL, ",");
6527
6528 f = 0;
6529 if (STRCMP(buf, "best") == 0)
6530 f = SPS_BEST;
6531 else if (STRCMP(buf, "fast") == 0)
6532 f = SPS_FAST;
6533 else if (STRCMP(buf, "double") == 0)
6534 f = SPS_DOUBLE;
6535 else if (STRNCMP(buf, "expr:", 5) != 0
6536 && STRNCMP(buf, "file:", 5) != 0)
6537 f = -1;
6538
6539 if (f == -1 || (sps_flags != 0 && f != 0))
6540 {
6541 sps_flags = SPS_BEST;
6542 return FAIL;
6543 }
6544 if (f != 0)
6545 sps_flags = f;
6546 }
6547
6548 if (sps_flags == 0)
6549 sps_flags = SPS_BEST;
6550
6551 return OK;
6552}
6553
6554/* Remember what "z?" replaced. */
6555static char_u *repl_from = NULL;
6556static char_u *repl_to = NULL;
6557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006558/*
6559 * "z?": Find badly spelled word under or after the cursor.
6560 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006561 */
6562 void
6563spell_suggest()
6564{
6565 char_u *line;
6566 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006567 char_u wcopy[MAXWLEN + 2];
6568 char_u *p;
6569 int i;
6570 int c;
6571 suginfo_T sug;
6572 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006573 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006574 int need_cap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006575
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006576 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006577 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6578 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006579 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006580 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6581 return;
6582
6583 /* No bad word or it starts after the cursor: use the word under the
6584 * cursor. */
6585 curwin->w_cursor = prev_cursor;
6586 line = ml_get_curline();
6587 p = line + curwin->w_cursor.col;
6588 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006589 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006590 mb_ptr_back(line, p);
6591 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006592 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006593 mb_ptr_adv(p);
6594
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006595 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006596 {
6597 beep_flush();
6598 return;
6599 }
6600 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006601 }
6602
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006603 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006604
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006605 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006606 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006607
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006608 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006609
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006610 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006611 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6612 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006613
6614 if (sug.su_ga.ga_len == 0)
6615 MSG(_("Sorry, no suggestions"));
6616 else
6617 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006618 vim_free(repl_from);
6619 repl_from = NULL;
6620 vim_free(repl_to);
6621 repl_to = NULL;
6622
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006623#ifdef FEAT_RIGHTLEFT
6624 /* When 'rightleft' is set the list is drawn right-left. */
6625 cmdmsg_rl = curwin->w_p_rl;
6626 if (cmdmsg_rl)
6627 msg_col = Columns - 1;
6628#endif
6629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006630 /* List the suggestions. */
6631 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006632 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006633 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6634 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006635#ifdef FEAT_RIGHTLEFT
6636 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
6637 {
6638 /* And now the rabbit from the high hat: Avoid showing the
6639 * untranslated message rightleft. */
6640 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
6641 sug.su_badlen, sug.su_badptr);
6642 }
6643#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006644 msg_puts(IObuff);
6645 msg_clr_eos();
6646 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006648 msg_scroll = TRUE;
6649 for (i = 0; i < sug.su_ga.ga_len; ++i)
6650 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006651 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006652
6653 /* The suggested word may replace only part of the bad word, add
6654 * the not replaced part. */
6655 STRCPY(wcopy, stp->st_word);
6656 if (sug.su_badlen > stp->st_orglen)
6657 vim_strncpy(wcopy + STRLEN(wcopy),
6658 sug.su_badptr + stp->st_orglen,
6659 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006660 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
6661#ifdef FEAT_RIGHTLEFT
6662 if (cmdmsg_rl)
6663 rl_mirror(IObuff);
6664#endif
6665 msg_puts(IObuff);
6666
6667 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006668 msg_puts(IObuff);
6669
6670 /* The word may replace more than "su_badlen". */
6671 if (sug.su_badlen < stp->st_orglen)
6672 {
6673 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6674 stp->st_orglen, sug.su_badptr);
6675 msg_puts(IObuff);
6676 }
6677
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006678 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006679 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006680 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006681 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006682 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006683 stp->st_salscore ? "s " : "",
6684 stp->st_score, stp->st_altscore);
6685 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006686 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00006687 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006688#ifdef FEAT_RIGHTLEFT
6689 if (cmdmsg_rl)
6690 /* Mirror the numbers, but keep the leading space. */
6691 rl_mirror(IObuff + 1);
6692#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00006693 msg_advance(30);
6694 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006696 msg_putchar('\n');
6697 }
6698
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006699#ifdef FEAT_RIGHTLEFT
6700 cmdmsg_rl = FALSE;
6701 msg_col = 0;
6702#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006703 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006704 i = prompt_for_number(&mouse_used);
6705 if (mouse_used)
6706 i -= lines_left;
6707
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006708 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006709 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006710 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006711 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006712 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6713 repl_to = vim_strsave(stp->st_word);
6714
6715 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006716 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6717 if (p != NULL)
6718 {
6719 c = sug.su_badptr - line;
6720 mch_memmove(p, line, c);
6721 STRCPY(p + c, stp->st_word);
6722 STRCAT(p, sug.su_badptr + stp->st_orglen);
6723 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6724 curwin->w_cursor.col = c;
6725 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006726
6727 /* For redo we use a change-word command. */
6728 ResetRedobuff();
6729 AppendToRedobuff((char_u *)"ciw");
6730 AppendToRedobuff(stp->st_word);
6731 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006732 }
6733 }
6734 else
6735 curwin->w_cursor = prev_cursor;
6736 }
6737
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006738 spell_find_cleanup(&sug);
6739}
6740
6741/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006742 * Check if the word at line "lnum" column "col" is required to start with a
6743 * capital. This uses 'spellcapcheck' of the current buffer.
6744 */
6745 static int
6746check_need_cap(lnum, col)
6747 linenr_T lnum;
6748 colnr_T col;
6749{
6750 int need_cap = FALSE;
6751 char_u *line;
6752 char_u *line_copy = NULL;
6753 char_u *p;
6754 colnr_T endcol;
6755 regmatch_T regmatch;
6756
6757 if (curbuf->b_cap_prog == NULL)
6758 return FALSE;
6759
6760 line = ml_get_curline();
6761 endcol = 0;
6762 if ((int)(skipwhite(line) - line) >= (int)col)
6763 {
6764 /* At start of line, check if previous line is empty or sentence
6765 * ends there. */
6766 if (lnum == 1)
6767 need_cap = TRUE;
6768 else
6769 {
6770 line = ml_get(lnum - 1);
6771 if (*skipwhite(line) == NUL)
6772 need_cap = TRUE;
6773 else
6774 {
6775 /* Append a space in place of the line break. */
6776 line_copy = concat_str(line, (char_u *)" ");
6777 line = line_copy;
6778 endcol = STRLEN(line);
6779 }
6780 }
6781 }
6782 else
6783 endcol = col;
6784
6785 if (endcol > 0)
6786 {
6787 /* Check if sentence ends before the bad word. */
6788 regmatch.regprog = curbuf->b_cap_prog;
6789 regmatch.rm_ic = FALSE;
6790 p = line + endcol;
6791 for (;;)
6792 {
6793 mb_ptr_back(line, p);
6794 if (p == line || spell_iswordp_nmw(p))
6795 break;
6796 if (vim_regexec(&regmatch, p, 0)
6797 && regmatch.endp[0] == line + endcol)
6798 {
6799 need_cap = TRUE;
6800 break;
6801 }
6802 }
6803 }
6804
6805 vim_free(line_copy);
6806
6807 return need_cap;
6808}
6809
6810
6811/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006812 * ":spellrepall"
6813 */
6814/*ARGSUSED*/
6815 void
6816ex_spellrepall(eap)
6817 exarg_T *eap;
6818{
6819 pos_T pos = curwin->w_cursor;
6820 char_u *frompat;
6821 int addlen;
6822 char_u *line;
6823 char_u *p;
6824 int didone = FALSE;
6825 int save_ws = p_ws;
6826
6827 if (repl_from == NULL || repl_to == NULL)
6828 {
6829 EMSG(_("E752: No previous spell replacement"));
6830 return;
6831 }
6832 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6833
6834 frompat = alloc(STRLEN(repl_from) + 7);
6835 if (frompat == NULL)
6836 return;
6837 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6838 p_ws = FALSE;
6839
6840 curwin->w_cursor.lnum = 0;
6841 while (!got_int)
6842 {
6843 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6844 || u_save_cursor() == FAIL)
6845 break;
6846
6847 /* Only replace when the right word isn't there yet. This happens
6848 * when changing "etc" to "etc.". */
6849 line = ml_get_curline();
6850 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6851 repl_to, STRLEN(repl_to)) != 0)
6852 {
6853 p = alloc(STRLEN(line) + addlen + 1);
6854 if (p == NULL)
6855 break;
6856 mch_memmove(p, line, curwin->w_cursor.col);
6857 STRCPY(p + curwin->w_cursor.col, repl_to);
6858 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6859 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6860 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6861 didone = TRUE;
6862 }
6863 curwin->w_cursor.col += STRLEN(repl_to);
6864 }
6865
6866 p_ws = save_ws;
6867 curwin->w_cursor = pos;
6868 vim_free(frompat);
6869
6870 if (!didone)
6871 EMSG2(_("E753: Not found: %s"), repl_from);
6872}
6873
6874/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006875 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6876 * a list of allocated strings.
6877 */
6878 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006879spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006880 garray_T *gap;
6881 char_u *word;
6882 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006883 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006884{
6885 suginfo_T sug;
6886 int i;
6887 suggest_T *stp;
6888 char_u *wcopy;
6889
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006890 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006891
6892 /* Make room in "gap". */
6893 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6894 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6895 return;
6896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006897 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006898 {
6899 stp = &SUG(sug.su_ga, i);
6900
6901 /* The suggested word may replace only part of "word", add the not
6902 * replaced part. */
6903 wcopy = alloc(STRLEN(stp->st_word)
6904 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6905 if (wcopy == NULL)
6906 break;
6907 STRCPY(wcopy, stp->st_word);
6908 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6909 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6910 }
6911
6912 spell_find_cleanup(&sug);
6913}
6914
6915/*
6916 * Find spell suggestions for the word at the start of "badptr".
6917 * Return the suggestions in "su->su_ga".
6918 * The maximum number of suggestions is "maxcount".
6919 * Note: does use info for the current window.
6920 * This is based on the mechanisms of Aspell, but completely reimplemented.
6921 */
6922 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006923spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006924 char_u *badptr;
6925 suginfo_T *su;
6926 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006927 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006928 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006929{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006930 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006931 char_u buf[MAXPATHL];
6932 char_u *p;
6933 int do_combine = FALSE;
6934 char_u *sps_copy;
6935#ifdef FEAT_EVAL
6936 static int expr_busy = FALSE;
6937#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006938 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006939
6940 /*
6941 * Set the info in "*su".
6942 */
6943 vim_memset(su, 0, sizeof(suginfo_T));
6944 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6945 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006946 if (*badptr == NUL)
6947 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006948 hash_init(&su->su_banned);
6949
6950 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006951 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006952 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006953 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006954
6955 if (su->su_badlen >= MAXWLEN)
6956 su->su_badlen = MAXWLEN - 1; /* just in case */
6957 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6958 (void)spell_casefold(su->su_badptr, su->su_badlen,
6959 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006960 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006961 su->su_badflags = badword_captype(su->su_badptr,
6962 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006963 if (need_cap)
6964 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006965
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006966 /* If the word is not capitalised and spell_check() doesn't consider the
6967 * word to be bad then it might need to be capitalised. Add a suggestion
6968 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006969 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006970 if (!SPELL_ISUPPER(c) && attr == 0)
6971 {
6972 make_case_word(su->su_badword, buf, WF_ONECAP);
6973 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6974 0, TRUE);
6975 }
6976
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006977 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006978 if (banbadword)
6979 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006980
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006981 /* Make a copy of 'spellsuggest', because the expression may change it. */
6982 sps_copy = vim_strsave(p_sps);
6983 if (sps_copy == NULL)
6984 return;
6985
6986 /* Loop over the items in 'spellsuggest'. */
6987 for (p = sps_copy; *p != NUL; )
6988 {
6989 copy_option_part(&p, buf, MAXPATHL, ",");
6990
6991 if (STRNCMP(buf, "expr:", 5) == 0)
6992 {
6993#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006994 /* Evaluate an expression. Skip this when called recursively,
6995 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006996 if (!expr_busy)
6997 {
6998 expr_busy = TRUE;
6999 spell_suggest_expr(su, buf + 5);
7000 expr_busy = FALSE;
7001 }
7002#endif
7003 }
7004 else if (STRNCMP(buf, "file:", 5) == 0)
7005 /* Use list of suggestions in a file. */
7006 spell_suggest_file(su, buf + 5);
7007 else
7008 {
7009 /* Use internal method. */
7010 spell_suggest_intern(su);
7011 if (sps_flags & SPS_DOUBLE)
7012 do_combine = TRUE;
7013 }
7014 }
7015
7016 vim_free(sps_copy);
7017
7018 if (do_combine)
7019 /* Combine the two list of suggestions. This must be done last,
7020 * because sorting changes the order again. */
7021 score_combine(su);
7022}
7023
7024#ifdef FEAT_EVAL
7025/*
7026 * Find suggestions by evaluating expression "expr".
7027 */
7028 static void
7029spell_suggest_expr(su, expr)
7030 suginfo_T *su;
7031 char_u *expr;
7032{
7033 list_T *list;
7034 listitem_T *li;
7035 int score;
7036 char_u *p;
7037
7038 /* The work is split up in a few parts to avoid having to export
7039 * suginfo_T.
7040 * First evaluate the expression and get the resulting list. */
7041 list = eval_spell_expr(su->su_badword, expr);
7042 if (list != NULL)
7043 {
7044 /* Loop over the items in the list. */
7045 for (li = list->lv_first; li != NULL; li = li->li_next)
7046 if (li->li_tv.v_type == VAR_LIST)
7047 {
7048 /* Get the word and the score from the items. */
7049 score = get_spellword(li->li_tv.vval.v_list, &p);
7050 if (score >= 0)
7051 add_suggestion(su, &su->su_ga, p,
7052 su->su_badlen, score, 0, TRUE);
7053 }
7054 list_unref(list);
7055 }
7056
7057 /* Sort the suggestions and truncate at "maxcount". */
7058 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7059}
7060#endif
7061
7062/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007063 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007064 */
7065 static void
7066spell_suggest_file(su, fname)
7067 suginfo_T *su;
7068 char_u *fname;
7069{
7070 FILE *fd;
7071 char_u line[MAXWLEN * 2];
7072 char_u *p;
7073 int len;
7074 char_u cword[MAXWLEN];
7075
7076 /* Open the file. */
7077 fd = mch_fopen((char *)fname, "r");
7078 if (fd == NULL)
7079 {
7080 EMSG2(_(e_notopen), fname);
7081 return;
7082 }
7083
7084 /* Read it line by line. */
7085 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
7086 {
7087 line_breakcheck();
7088
7089 p = vim_strchr(line, '/');
7090 if (p == NULL)
7091 continue; /* No Tab found, just skip the line. */
7092 *p++ = NUL;
7093 if (STRICMP(su->su_badword, line) == 0)
7094 {
7095 /* Match! Isolate the good word, until CR or NL. */
7096 for (len = 0; p[len] >= ' '; ++len)
7097 ;
7098 p[len] = NUL;
7099
7100 /* If the suggestion doesn't have specific case duplicate the case
7101 * of the bad word. */
7102 if (captype(p, NULL) == 0)
7103 {
7104 make_case_word(p, cword, su->su_badflags);
7105 p = cword;
7106 }
7107
7108 add_suggestion(su, &su->su_ga, p, su->su_badlen,
7109 SCORE_FILE, 0, TRUE);
7110 }
7111 }
7112
7113 fclose(fd);
7114
7115 /* Sort the suggestions and truncate at "maxcount". */
7116 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7117}
7118
7119/*
7120 * Find suggestions for the internal method indicated by "sps_flags".
7121 */
7122 static void
7123spell_suggest_intern(su)
7124 suginfo_T *su;
7125{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007126 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007127 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007128 *
7129 * Set a maximum score to limit the combination of operations that is
7130 * tried.
7131 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007132 suggest_try_special(su);
7133
7134 /*
7135 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
7136 * from the .aff file and inserting a space (split the word).
7137 */
7138 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007139
7140 /* For the resulting top-scorers compute the sound-a-like score. */
7141 if (sps_flags & SPS_DOUBLE)
7142 score_comp_sal(su);
7143
7144 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007145 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007146 *
7147 * Only do this when we don't have a lot of suggestions yet, because it's
7148 * very slow and often doesn't find new suggestions.
7149 */
7150 if ((sps_flags & SPS_DOUBLE)
7151 || (!(sps_flags & SPS_FAST)
7152 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
7153 {
7154 /* Allow a higher score now. */
7155 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007156 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007157 }
7158
7159 /* When CTRL-C was hit while searching do show the results. */
7160 ui_breakcheck();
7161 if (got_int)
7162 {
7163 (void)vgetc();
7164 got_int = FALSE;
7165 }
7166
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007167 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007168 {
7169 if (sps_flags & SPS_BEST)
7170 /* Adjust the word score for how it sounds like. */
7171 rescore_suggestions(su);
7172
7173 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007174 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007175 }
7176}
7177
7178/*
7179 * Free the info put in "*su" by spell_find_suggest().
7180 */
7181 static void
7182spell_find_cleanup(su)
7183 suginfo_T *su;
7184{
7185 int i;
7186
7187 /* Free the suggestions. */
7188 for (i = 0; i < su->su_ga.ga_len; ++i)
7189 vim_free(SUG(su->su_ga, i).st_word);
7190 ga_clear(&su->su_ga);
7191 for (i = 0; i < su->su_sga.ga_len; ++i)
7192 vim_free(SUG(su->su_sga, i).st_word);
7193 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007194
7195 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007196 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007197}
7198
7199/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007200 * Make a copy of "word", with the first letter upper or lower cased, to
7201 * "wcopy[MAXWLEN]". "word" must not be empty.
7202 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007203 */
7204 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007205onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007206 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007207 char_u *wcopy;
7208 int upper; /* TRUE: first letter made upper case */
7209{
7210 char_u *p;
7211 int c;
7212 int l;
7213
7214 p = word;
7215#ifdef FEAT_MBYTE
7216 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007217 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007218 else
7219#endif
7220 c = *p++;
7221 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007222 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007223 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007224 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225#ifdef FEAT_MBYTE
7226 if (has_mbyte)
7227 l = mb_char2bytes(c, wcopy);
7228 else
7229#endif
7230 {
7231 l = 1;
7232 wcopy[0] = c;
7233 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007234 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007235}
7236
7237/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007238 * Make a copy of "word" with all the letters upper cased into
7239 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007240 */
7241 static void
7242allcap_copy(word, wcopy)
7243 char_u *word;
7244 char_u *wcopy;
7245{
7246 char_u *s;
7247 char_u *d;
7248 int c;
7249
7250 d = wcopy;
7251 for (s = word; *s != NUL; )
7252 {
7253#ifdef FEAT_MBYTE
7254 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007255 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256 else
7257#endif
7258 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007259 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007260
7261#ifdef FEAT_MBYTE
7262 if (has_mbyte)
7263 {
7264 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
7265 break;
7266 d += mb_char2bytes(c, d);
7267 }
7268 else
7269#endif
7270 {
7271 if (d - wcopy >= MAXWLEN - 1)
7272 break;
7273 *d++ = c;
7274 }
7275 }
7276 *d = NUL;
7277}
7278
7279/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007280 * Try finding suggestions by recognizing specific situations.
7281 */
7282 static void
7283suggest_try_special(su)
7284 suginfo_T *su;
7285{
7286 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007287 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007288 int c;
7289 char_u word[MAXWLEN];
7290
7291 /*
7292 * Recognize a word that is repeated: "the the".
7293 */
7294 p = skiptowhite(su->su_fbadword);
7295 len = p - su->su_fbadword;
7296 p = skipwhite(p);
7297 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
7298 {
7299 /* Include badflags: if the badword is onecap or allcap
7300 * use that for the goodword too: "The the" -> "The". */
7301 c = su->su_fbadword[len];
7302 su->su_fbadword[len] = NUL;
7303 make_case_word(su->su_fbadword, word, su->su_badflags);
7304 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007305 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007306 }
7307}
7308
7309/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007310 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00007311 *
7312 * This uses a state machine. At each node in the tree we try various
7313 * operations. When trying if an operation work "depth" is increased and the
7314 * stack[] is used to store info. This allows combinations, thus insert one
7315 * character, replace one and delete another. The number of changes is
7316 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 */
7318 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007319suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 suginfo_T *su;
7321{
7322 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
7323 char_u tword[MAXWLEN]; /* good word collected so far */
7324 trystate_T stack[MAXWLEN];
7325 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
7326 * to for word split) */
7327 char_u prewordlen = 0; /* length of word in "preword" */
7328 int splitoff = 0; /* index in tword after last split */
7329 trystate_T *sp;
7330 int newscore;
7331 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007332 char_u *byts, *fbyts, *pbyts;
7333 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007334 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007335 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007336 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007337 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007338 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007339 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 int len;
7341 char_u *p;
7342 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00007343 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007344 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345
7346 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00007347 * to find matches (esp. REP items). Append some more text, changing
7348 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007350 n = STRLEN(fword);
7351 p = su->su_badptr + su->su_badlen;
7352 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007353
7354 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7355 lp->lp_slang != NULL; ++lp)
7356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007357 /*
7358 * Go through the whole case-fold tree, try changes at each node.
7359 * "tword[]" contains the word collected from nodes in the tree.
7360 * "fword[]" the word we are trying to match with (initially the bad
7361 * word).
7362 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007363 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007364 sp = &stack[0];
7365 sp->ts_state = STATE_START;
7366 sp->ts_score = 0;
7367 sp->ts_curi = 1;
7368 sp->ts_fidx = 0;
7369 sp->ts_fidxtry = 0;
7370 sp->ts_twordlen = 0;
7371 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007372#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007373 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007374#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375
Bram Moolenaarea424162005-06-16 21:51:00 +00007376 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007377 * When there are postponed prefixes we need to use these first. At
7378 * the end of the prefix we continue in the case-fold tree.
7379 */
7380 fbyts = lp->lp_slang->sl_fbyts;
7381 fidxs = lp->lp_slang->sl_fidxs;
7382 pbyts = lp->lp_slang->sl_pbyts;
7383 pidxs = lp->lp_slang->sl_pidxs;
7384 if (pbyts != NULL)
7385 {
7386 byts = pbyts;
7387 idxs = pidxs;
7388 sp->ts_prefixdepth = PREFIXTREE;
7389 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
7390 }
7391 else
7392 {
7393 byts = fbyts;
7394 idxs = fidxs;
7395 sp->ts_prefixdepth = NOPREFIX;
7396 }
7397
7398 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007399 * Loop to find all suggestions. At each round we either:
7400 * - For the current state try one operation, advance "ts_curi",
7401 * increase "depth".
7402 * - When a state is done go to the next, set "ts_state".
7403 * - When all states are tried decrease "depth".
7404 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405 while (depth >= 0 && !got_int)
7406 {
7407 sp = &stack[depth];
7408 switch (sp->ts_state)
7409 {
7410 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007411 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412 /*
7413 * Start of node: Deal with NUL bytes, which means
7414 * tword[] may end here.
7415 */
7416 arridx = sp->ts_arridx; /* current node in the tree */
7417 len = byts[arridx]; /* bytes in this node */
7418 arridx += sp->ts_curi; /* index of current byte */
7419
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007420 if (sp->ts_prefixdepth == PREFIXTREE)
7421 {
7422 /* Skip over the NUL bytes, we use them later. */
7423 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7424 ;
7425 sp->ts_curi += n;
7426
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007427 /* Always past NUL bytes now. */
7428 n = (int)sp->ts_state;
7429 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007430 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007431
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007432 /* At end of a prefix or at start of prefixtree: check for
7433 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007434 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007435 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007436 /* Set su->su_badflags to the caps type at this
7437 * position. Use the caps type until here for the
7438 * prefix itself. */
7439#ifdef FEAT_MBYTE
7440 if (has_mbyte)
7441 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7442 else
7443#endif
7444 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007445 flags = badword_captype(su->su_badptr,
7446 su->su_badptr + n);
7447 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007448 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007449 ++depth;
7450 stack[depth] = stack[depth - 1];
7451 sp = &stack[depth];
7452 sp->ts_prefixdepth = depth - 1;
7453 byts = fbyts;
7454 idxs = fidxs;
7455 sp->ts_state = STATE_START;
7456 sp->ts_curi = 1; /* start just after length byte */
7457 sp->ts_arridx = 0;
7458
Bram Moolenaar53805d12005-08-01 07:08:33 +00007459 /* Move the prefix to preword[] with the right case
7460 * and make find_keepcap_word() works. */
7461 splitoff = sp->ts_twordlen;
7462 tword[splitoff] = NUL;
7463 make_case_word(tword, preword, flags);
7464 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007465 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007466 break;
7467 }
7468
Bram Moolenaar0c405862005-06-22 22:26:26 +00007469 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 {
7471 /* Past bytes in node and/or past NUL bytes. */
7472 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007473 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007474 break;
7475 }
7476
7477 /*
7478 * End of word in tree.
7479 */
7480 ++sp->ts_curi; /* eat one NUL byte */
7481
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007482 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007483
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007484 if (sp->ts_prefixdepth < MAXWLEN)
7485 {
7486 /* There was a prefix before the word. Check that the
7487 * prefix can be used with this word. */
7488 /* Count the length of the NULs in the prefix. If there
7489 * are none this must be the first try without a prefix.
7490 */
7491 n = stack[sp->ts_prefixdepth].ts_arridx;
7492 len = pbyts[n++];
7493 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7494 ;
7495 if (c > 0)
7496 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007497 /* The prefix ID is stored three bytes above the
7498 * flags. */
7499 c = valid_word_prefix(c, n, flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007500 tword + splitoff, lp->lp_slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007501 if (c == 0)
7502 break;
7503
7504 /* Use the WF_RARE flag for a rare prefix. */
7505 if (c & WF_RAREPFX)
7506 flags |= WF_RARE;
7507 }
7508 }
7509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007510 /*
7511 * Form the word with proper case in preword.
7512 * If there is a word from a previous split, append.
7513 */
7514 tword[sp->ts_twordlen] = NUL;
7515 if (flags & WF_KEEPCAP)
7516 /* Must find the word in the keep-case tree. */
7517 find_keepcap_word(lp->lp_slang, tword + splitoff,
7518 preword + prewordlen);
7519 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007521 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007522 * use that for the goodword too. But if the badword is
7523 * allcap and it's only one char long use onecap. */
7524 c = su->su_badflags;
7525 if ((c & WF_ALLCAP)
7526#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007527 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007528#else
7529 && su->su_badlen == 1
7530#endif
7531 )
7532 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007533 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007534 preword + prewordlen, flags | c);
7535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536
7537 /* Don't use a banned word. It may appear again as a good
7538 * word, thus remember it. */
7539 if (flags & WF_BANNED)
7540 {
7541 add_banned(su, preword + prewordlen);
7542 break;
7543 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007544 if (was_banned(su, preword + prewordlen)
7545 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007546 break;
7547
7548 newscore = 0;
7549 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007550 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007551 newscore += SCORE_REGION;
7552 if (flags & WF_RARE)
7553 newscore += SCORE_RARE;
7554
Bram Moolenaar0c405862005-06-22 22:26:26 +00007555 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007556 captype(preword + prewordlen, NULL)))
7557 newscore += SCORE_ICASE;
7558
Bram Moolenaar0c405862005-06-22 22:26:26 +00007559 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007560 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007561 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007562 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007563 /* The badword also ends: add suggestions. Give a penalty
7564 * when changing non-word char to word char, e.g., "thes,"
7565 * -> "these". */
7566 p = fword + sp->ts_fidx;
7567#ifdef FEAT_MBYTE
7568 if (has_mbyte)
7569 mb_ptr_back(fword, p);
7570 else
7571#endif
7572 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007573 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007574 {
7575 p = preword + STRLEN(preword);
7576#ifdef FEAT_MBYTE
7577 if (has_mbyte)
7578 mb_ptr_back(preword, p);
7579 else
7580#endif
7581 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007582 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007583 newscore += SCORE_NONWORD;
7584 }
7585
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007586 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007587 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007588 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007589 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007590 else if (sp->ts_fidx >= sp->ts_fidxtry
7591#ifdef FEAT_MBYTE
7592 /* Don't split halfway a character. */
7593 && (!has_mbyte || sp->ts_tcharlen == 0)
7594#endif
7595 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007596 {
7597 /* The word in the tree ends but the badword
7598 * continues: try inserting a space and check that a valid
7599 * words starts at fword[sp->ts_fidx]. */
7600 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7601 {
7602 /* Save things to be restored at STATE_SPLITUNDO. */
7603 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007604 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007605 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007606 sp->ts_state = STATE_SPLITUNDO;
7607
7608 ++depth;
7609 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007610
7611 /* Append a space to preword. */
7612 STRCAT(preword, " ");
7613 prewordlen = STRLEN(preword);
7614 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007615
7616 /* If the badword has a non-word character at this
7617 * position skip it. That means replacing the
7618 * non-word character with a space. */
7619 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7620 {
7621 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7622#ifdef FEAT_MBYTE
7623 if (has_mbyte)
7624 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7625 else
7626#endif
7627 ++sp->ts_fidx;
7628 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00007629
7630 /* set su->su_badflags to the caps type at this
7631 * position */
7632
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007633#ifdef FEAT_MBYTE
7634 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00007635 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007636 else
7637#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00007638 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007639 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007640 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007642 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007643 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007644 }
7645 }
7646 break;
7647
7648 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007649 /* Undo the changes done for word split. */
7650 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007651 splitoff = sp->ts_save_splitoff;
7652 prewordlen = sp->ts_save_prewordlen;
7653
7654 /* Continue looking for NUL bytes. */
7655 sp->ts_state = STATE_START;
7656 break;
7657
7658 case STATE_ENDNUL:
7659 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007660 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007661 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007662 {
7663 /* The badword ends, can't use the bytes in this node. */
7664 sp->ts_state = STATE_DEL;
7665 break;
7666 }
7667 sp->ts_state = STATE_PLAIN;
7668 /*FALLTHROUGH*/
7669
7670 case STATE_PLAIN:
7671 /*
7672 * Go over all possible bytes at this node, add each to
7673 * tword[] and use child node. "ts_curi" is the index.
7674 */
7675 arridx = sp->ts_arridx;
7676 if (sp->ts_curi > byts[arridx])
7677 {
7678 /* Done all bytes at this node, do next state. When still
7679 * at already changed bytes skip the other tricks. */
7680 if (sp->ts_fidx >= sp->ts_fidxtry)
7681 sp->ts_state = STATE_DEL;
7682 else
7683 sp->ts_state = STATE_FINAL;
7684 }
7685 else
7686 {
7687 arridx += sp->ts_curi++;
7688 c = byts[arridx];
7689
7690 /* Normal byte, go one level deeper. If it's not equal to
7691 * the byte in the bad word adjust the score. But don't
7692 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007693 if (c == fword[sp->ts_fidx]
7694#ifdef FEAT_MBYTE
7695 || (sp->ts_tcharlen > 0
7696 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007697#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007698 )
7699 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007700 else
7701 newscore = SCORE_SUBST;
7702 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7703 && try_deeper(su, stack, depth, newscore))
7704 {
7705 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007706 sp = &stack[depth];
7707 ++sp->ts_fidx;
7708 tword[sp->ts_twordlen++] = c;
7709 sp->ts_arridx = idxs[arridx];
7710#ifdef FEAT_MBYTE
7711 if (newscore == SCORE_SUBST)
7712 sp->ts_isdiff = DIFF_YES;
7713 if (has_mbyte)
7714 {
7715 /* Multi-byte characters are a bit complicated to
7716 * handle: They differ when any of the bytes
7717 * differ and then their length may also differ. */
7718 if (sp->ts_tcharlen == 0)
7719 {
7720 /* First byte. */
7721 sp->ts_tcharidx = 0;
7722 sp->ts_tcharlen = MB_BYTE2LEN(c);
7723 sp->ts_fcharstart = sp->ts_fidx - 1;
7724 sp->ts_isdiff = (newscore != 0)
7725 ? DIFF_YES : DIFF_NONE;
7726 }
7727 else if (sp->ts_isdiff == DIFF_INSERT)
7728 /* When inserting trail bytes don't advance in
7729 * the bad word. */
7730 --sp->ts_fidx;
7731 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7732 {
7733 /* Last byte of character. */
7734 if (sp->ts_isdiff == DIFF_YES)
7735 {
7736 /* Correct ts_fidx for the byte length of
7737 * the character (we didn't check that
7738 * before). */
7739 sp->ts_fidx = sp->ts_fcharstart
7740 + MB_BYTE2LEN(
7741 fword[sp->ts_fcharstart]);
7742
7743 /* For a similar character adjust score
7744 * from SCORE_SUBST to SCORE_SIMILAR. */
7745 if (lp->lp_slang->sl_has_map
7746 && similar_chars(lp->lp_slang,
7747 mb_ptr2char(tword
7748 + sp->ts_twordlen
7749 - sp->ts_tcharlen),
7750 mb_ptr2char(fword
7751 + sp->ts_fcharstart)))
7752 sp->ts_score -=
7753 SCORE_SUBST - SCORE_SIMILAR;
7754 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007755 else if (sp->ts_isdiff == DIFF_INSERT
7756 && sp->ts_twordlen > sp->ts_tcharlen)
7757 {
Bram Moolenaarea408852005-06-25 22:49:46 +00007758 p = tword + sp->ts_twordlen
7759 - sp->ts_tcharlen;
7760 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007761 if (enc_utf8 && utf_iscomposing(c))
7762 {
7763 /* Inserting a composing char doesn't
7764 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00007765 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007766 - SCORE_INSCOMP;
7767 }
7768 else
7769 {
7770 /* If the previous character was the
7771 * same, thus doubling a character,
7772 * give a bonus to the score. */
7773 mb_ptr_back(tword, p);
7774 if (c == mb_ptr2char(p))
7775 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00007776 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007777 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007778 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007779
7780 /* Starting a new char, reset the length. */
7781 sp->ts_tcharlen = 0;
7782 }
7783 }
7784 else
7785#endif
7786 {
7787 /* If we found a similar char adjust the score.
7788 * We do this after calling try_deeper() because
7789 * it's slow. */
7790 if (newscore != 0
7791 && lp->lp_slang->sl_has_map
7792 && similar_chars(lp->lp_slang,
7793 c, fword[sp->ts_fidx - 1]))
7794 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7795 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007796 }
7797 }
7798 break;
7799
7800 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007801#ifdef FEAT_MBYTE
7802 /* When past the first byte of a multi-byte char don't try
7803 * delete/insert/swap a character. */
7804 if (has_mbyte && sp->ts_tcharlen > 0)
7805 {
7806 sp->ts_state = STATE_FINAL;
7807 break;
7808 }
7809#endif
7810 /*
7811 * Try skipping one character in the bad word (delete it).
7812 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007813 sp->ts_state = STATE_INS;
7814 sp->ts_curi = 1;
7815 if (fword[sp->ts_fidx] != NUL
7816 && try_deeper(su, stack, depth, SCORE_DEL))
7817 {
7818 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007819
7820 /* Advance over the character in fword[]. Give a bonus to
7821 * the score if the same character is following "nn" ->
7822 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007823#ifdef FEAT_MBYTE
7824 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007825 {
7826 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007827 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007828 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7829 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7830 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007831 else
7832#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007833 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007834 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007835 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7836 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7837 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838 break;
7839 }
7840 /*FALLTHROUGH*/
7841
7842 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007843 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007844 * node. */
7845 n = sp->ts_arridx;
7846 if (sp->ts_curi > byts[n])
7847 {
7848 /* Done all bytes at this node, do next state. */
7849 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 }
7851 else
7852 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007853 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 n += sp->ts_curi++;
7855 c = byts[n];
7856 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7857 {
7858 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007859 sp = &stack[depth];
7860 tword[sp->ts_twordlen++] = c;
7861 sp->ts_arridx = idxs[n];
7862#ifdef FEAT_MBYTE
7863 if (has_mbyte)
7864 {
7865 fl = MB_BYTE2LEN(c);
7866 if (fl > 1)
7867 {
7868 /* There are following bytes for the same
7869 * character. We must find all bytes before
7870 * trying delete/insert/swap/etc. */
7871 sp->ts_tcharlen = fl;
7872 sp->ts_tcharidx = 1;
7873 sp->ts_isdiff = DIFF_INSERT;
7874 }
7875 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007876 else
7877 fl = 1;
7878 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007879#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007880 {
7881 /* If the previous character was the same, thus
7882 * doubling a character, give a bonus to the
7883 * score. */
7884 if (sp->ts_twordlen >= 2
7885 && tword[sp->ts_twordlen - 2] == c)
7886 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 }
7889 }
7890 break;
7891
7892 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007893 /*
7894 * Swap two bytes in the bad word: "12" -> "21".
7895 * We change "fword" here, it's changed back afterwards.
7896 */
7897 p = fword + sp->ts_fidx;
7898 c = *p;
7899 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007900 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007901 /* End of word, can't swap or replace. */
7902 sp->ts_state = STATE_FINAL;
7903 break;
7904 }
7905#ifdef FEAT_MBYTE
7906 if (has_mbyte)
7907 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007908 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00007909 c = mb_ptr2char(p);
7910 c2 = mb_ptr2char(p + n);
7911 }
7912 else
7913#endif
7914 c2 = p[1];
7915 if (c == c2)
7916 {
7917 /* Characters are identical, swap won't do anything. */
7918 sp->ts_state = STATE_SWAP3;
7919 break;
7920 }
7921 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7922 {
7923 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007924 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007925#ifdef FEAT_MBYTE
7926 if (has_mbyte)
7927 {
7928 fl = mb_char2len(c2);
7929 mch_memmove(p, p + n, fl);
7930 mb_char2bytes(c, p + fl);
7931 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7932 }
7933 else
7934#endif
7935 {
7936 p[0] = c2;
7937 p[1] = c;
7938 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7939 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007940 }
7941 else
7942 /* If this swap doesn't work then SWAP3 won't either. */
7943 sp->ts_state = STATE_REP_INI;
7944 break;
7945
Bram Moolenaarea424162005-06-16 21:51:00 +00007946 case STATE_UNSWAP:
7947 /* Undo the STATE_SWAP swap: "21" -> "12". */
7948 p = fword + sp->ts_fidx;
7949#ifdef FEAT_MBYTE
7950 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007951 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007952 n = MB_BYTE2LEN(*p);
7953 c = mb_ptr2char(p + n);
7954 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7955 mb_char2bytes(c, p);
7956 }
7957 else
7958#endif
7959 {
7960 c = *p;
7961 *p = p[1];
7962 p[1] = c;
7963 }
7964 /*FALLTHROUGH*/
7965
7966 case STATE_SWAP3:
7967 /* Swap two bytes, skipping one: "123" -> "321". We change
7968 * "fword" here, it's changed back afterwards. */
7969 p = fword + sp->ts_fidx;
7970#ifdef FEAT_MBYTE
7971 if (has_mbyte)
7972 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007973 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00007974 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007975 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00007976 c2 = mb_ptr2char(p + n);
7977 c3 = mb_ptr2char(p + n + fl);
7978 }
7979 else
7980#endif
7981 {
7982 c = *p;
7983 c2 = p[1];
7984 c3 = p[2];
7985 }
7986
7987 /* When characters are identical: "121" then SWAP3 result is
7988 * identical, ROT3L result is same as SWAP: "211", ROT3L
7989 * result is same as SWAP on next char: "112". Thus skip all
7990 * swapping. Also skip when c3 is NUL. */
7991 if (c == c3 || c3 == NUL)
7992 {
7993 sp->ts_state = STATE_REP_INI;
7994 break;
7995 }
7996 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7997 {
7998 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007999 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008000#ifdef FEAT_MBYTE
8001 if (has_mbyte)
8002 {
8003 tl = mb_char2len(c3);
8004 mch_memmove(p, p + n + fl, tl);
8005 mb_char2bytes(c2, p + tl);
8006 mb_char2bytes(c, p + fl + tl);
8007 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
8008 }
8009 else
8010#endif
8011 {
8012 p[0] = p[2];
8013 p[2] = c;
8014 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8015 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008016 }
8017 else
8018 sp->ts_state = STATE_REP_INI;
8019 break;
8020
Bram Moolenaarea424162005-06-16 21:51:00 +00008021 case STATE_UNSWAP3:
8022 /* Undo STATE_SWAP3: "321" -> "123" */
8023 p = fword + sp->ts_fidx;
8024#ifdef FEAT_MBYTE
8025 if (has_mbyte)
8026 {
8027 n = MB_BYTE2LEN(*p);
8028 c2 = mb_ptr2char(p + n);
8029 fl = MB_BYTE2LEN(p[n]);
8030 c = mb_ptr2char(p + n + fl);
8031 tl = MB_BYTE2LEN(p[n + fl]);
8032 mch_memmove(p + fl + tl, p, n);
8033 mb_char2bytes(c, p);
8034 mb_char2bytes(c2, p + tl);
8035 }
8036 else
8037#endif
8038 {
8039 c = *p;
8040 *p = p[2];
8041 p[2] = c;
8042 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008043
Bram Moolenaarea424162005-06-16 21:51:00 +00008044 /* Rotate three characters left: "123" -> "231". We change
8045 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8047 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008048 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008050 p = fword + sp->ts_fidx;
8051#ifdef FEAT_MBYTE
8052 if (has_mbyte)
8053 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008054 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008055 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008056 fl = mb_cptr2len(p + n);
8057 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00008058 mch_memmove(p, p + n, fl);
8059 mb_char2bytes(c, p + fl);
8060 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8061 }
8062 else
8063#endif
8064 {
8065 c = *p;
8066 *p = p[1];
8067 p[1] = p[2];
8068 p[2] = c;
8069 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008071 }
8072 else
8073 sp->ts_state = STATE_REP_INI;
8074 break;
8075
Bram Moolenaarea424162005-06-16 21:51:00 +00008076 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008077 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008078 p = fword + sp->ts_fidx;
8079#ifdef FEAT_MBYTE
8080 if (has_mbyte)
8081 {
8082 n = MB_BYTE2LEN(*p);
8083 n += MB_BYTE2LEN(p[n]);
8084 c = mb_ptr2char(p + n);
8085 tl = MB_BYTE2LEN(p[n]);
8086 mch_memmove(p + tl, p, n);
8087 mb_char2bytes(c, p);
8088 }
8089 else
8090#endif
8091 {
8092 c = p[2];
8093 p[2] = p[1];
8094 p[1] = *p;
8095 *p = c;
8096 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008098 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00008099 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008100 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8101 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008102 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008104 p = fword + sp->ts_fidx;
8105#ifdef FEAT_MBYTE
8106 if (has_mbyte)
8107 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008108 n = mb_cptr2len(p);
8109 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008110 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008111 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008112 mch_memmove(p + tl, p, n);
8113 mb_char2bytes(c, p);
8114 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
8115 }
8116 else
8117#endif
8118 {
8119 c = p[2];
8120 p[2] = p[1];
8121 p[1] = *p;
8122 *p = c;
8123 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8124 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125 }
8126 else
8127 sp->ts_state = STATE_REP_INI;
8128 break;
8129
Bram Moolenaarea424162005-06-16 21:51:00 +00008130 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008131 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008132 p = fword + sp->ts_fidx;
8133#ifdef FEAT_MBYTE
8134 if (has_mbyte)
8135 {
8136 c = mb_ptr2char(p);
8137 tl = MB_BYTE2LEN(*p);
8138 n = MB_BYTE2LEN(p[tl]);
8139 n += MB_BYTE2LEN(p[tl + n]);
8140 mch_memmove(p, p + tl, n);
8141 mb_char2bytes(c, p + n);
8142 }
8143 else
8144#endif
8145 {
8146 c = *p;
8147 *p = p[1];
8148 p[1] = p[2];
8149 p[2] = c;
8150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 /*FALLTHROUGH*/
8152
8153 case STATE_REP_INI:
8154 /* Check if matching with REP items from the .aff file would
8155 * work. Quickly skip if there are no REP items or the score
8156 * is going to be too high anyway. */
8157 gap = &lp->lp_slang->sl_rep;
8158 if (gap->ga_len == 0
8159 || sp->ts_score + SCORE_REP >= su->su_maxscore)
8160 {
8161 sp->ts_state = STATE_FINAL;
8162 break;
8163 }
8164
8165 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00008166 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008167 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
8168 if (sp->ts_curi < 0)
8169 {
8170 sp->ts_state = STATE_FINAL;
8171 break;
8172 }
8173
8174 sp->ts_state = STATE_REP;
8175 /*FALLTHROUGH*/
8176
8177 case STATE_REP:
8178 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00008179 * match replace the characters and check if the resulting
8180 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008181 p = fword + sp->ts_fidx;
8182
8183 gap = &lp->lp_slang->sl_rep;
8184 while (sp->ts_curi < gap->ga_len)
8185 {
8186 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
8187 if (*ftp->ft_from != *p)
8188 {
8189 /* past possible matching entries */
8190 sp->ts_curi = gap->ga_len;
8191 break;
8192 }
8193 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
8194 && try_deeper(su, stack, depth, SCORE_REP))
8195 {
8196 /* Need to undo this afterwards. */
8197 sp->ts_state = STATE_REP_UNDO;
8198
8199 /* Change the "from" to the "to" string. */
8200 ++depth;
8201 fl = STRLEN(ftp->ft_from);
8202 tl = STRLEN(ftp->ft_to);
8203 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008205 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008206 repextra += tl - fl;
8207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008208 mch_memmove(p, ftp->ft_to, tl);
8209 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00008210#ifdef FEAT_MBYTE
8211 stack[depth].ts_tcharlen = 0;
8212#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008213 break;
8214 }
8215 }
8216
8217 if (sp->ts_curi >= gap->ga_len)
8218 /* No (more) matches. */
8219 sp->ts_state = STATE_FINAL;
8220
8221 break;
8222
8223 case STATE_REP_UNDO:
8224 /* Undo a REP replacement and continue with the next one. */
8225 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
8226 + sp->ts_curi - 1;
8227 fl = STRLEN(ftp->ft_from);
8228 tl = STRLEN(ftp->ft_to);
8229 p = fword + sp->ts_fidx;
8230 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008232 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008233 repextra -= tl - fl;
8234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008235 mch_memmove(p, ftp->ft_from, fl);
8236 sp->ts_state = STATE_REP;
8237 break;
8238
8239 default:
8240 /* Did all possible states at this level, go up one level. */
8241 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008242
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008243 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
8244 {
8245 /* Continue in or go back to the prefix tree. */
8246 byts = pbyts;
8247 idxs = pidxs;
8248 splitoff = 0;
8249 }
8250
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008251 /* Don't check for CTRL-C too often, it takes time. */
8252 line_breakcheck();
8253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 }
8255 }
8256}
8257
8258/*
8259 * Try going one level deeper in the tree.
8260 */
8261 static int
8262try_deeper(su, stack, depth, score_add)
8263 suginfo_T *su;
8264 trystate_T *stack;
8265 int depth;
8266 int score_add;
8267{
8268 int newscore;
8269
8270 /* Refuse to go deeper if the scrore is getting too big. */
8271 newscore = stack[depth].ts_score + score_add;
8272 if (newscore >= su->su_maxscore)
8273 return FALSE;
8274
Bram Moolenaarea424162005-06-16 21:51:00 +00008275 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008276 stack[depth + 1].ts_state = STATE_START;
8277 stack[depth + 1].ts_score = newscore;
8278 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 return TRUE;
8280}
8281
Bram Moolenaar53805d12005-08-01 07:08:33 +00008282#ifdef FEAT_MBYTE
8283/*
8284 * Case-folding may change the number of bytes: Count nr of chars in
8285 * fword[flen] and return the byte length of that many chars in "word".
8286 */
8287 static int
8288nofold_len(fword, flen, word)
8289 char_u *fword;
8290 int flen;
8291 char_u *word;
8292{
8293 char_u *p;
8294 int i = 0;
8295
8296 for (p = fword; p < fword + flen; mb_ptr_adv(p))
8297 ++i;
8298 for (p = word; i > 0; mb_ptr_adv(p))
8299 --i;
8300 return (int)(p - word);
8301}
8302#endif
8303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008304/*
8305 * "fword" is a good word with case folded. Find the matching keep-case
8306 * words and put it in "kword".
8307 * Theoretically there could be several keep-case words that result in the
8308 * same case-folded word, but we only find one...
8309 */
8310 static void
8311find_keepcap_word(slang, fword, kword)
8312 slang_T *slang;
8313 char_u *fword;
8314 char_u *kword;
8315{
8316 char_u uword[MAXWLEN]; /* "fword" in upper-case */
8317 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008318 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008319
8320 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008321 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 int round[MAXWLEN];
8323 int fwordidx[MAXWLEN];
8324 int uwordidx[MAXWLEN];
8325 int kwordlen[MAXWLEN];
8326
8327 int flen, ulen;
8328 int l;
8329 int len;
8330 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008331 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008332 char_u *p;
8333 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008334 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335
8336 if (byts == NULL)
8337 {
8338 /* array is empty: "cannot happen" */
8339 *kword = NUL;
8340 return;
8341 }
8342
8343 /* Make an all-cap version of "fword". */
8344 allcap_copy(fword, uword);
8345
8346 /*
8347 * Each character needs to be tried both case-folded and upper-case.
8348 * All this gets very complicated if we keep in mind that changing case
8349 * may change the byte length of a multi-byte character...
8350 */
8351 depth = 0;
8352 arridx[0] = 0;
8353 round[0] = 0;
8354 fwordidx[0] = 0;
8355 uwordidx[0] = 0;
8356 kwordlen[0] = 0;
8357 while (depth >= 0)
8358 {
8359 if (fword[fwordidx[depth]] == NUL)
8360 {
8361 /* We are at the end of "fword". If the tree allows a word to end
8362 * here we have found a match. */
8363 if (byts[arridx[depth] + 1] == 0)
8364 {
8365 kword[kwordlen[depth]] = NUL;
8366 return;
8367 }
8368
8369 /* kword is getting too long, continue one level up */
8370 --depth;
8371 }
8372 else if (++round[depth] > 2)
8373 {
8374 /* tried both fold-case and upper-case character, continue one
8375 * level up */
8376 --depth;
8377 }
8378 else
8379 {
8380 /*
8381 * round[depth] == 1: Try using the folded-case character.
8382 * round[depth] == 2: Try using the upper-case character.
8383 */
8384#ifdef FEAT_MBYTE
8385 if (has_mbyte)
8386 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008387 flen = mb_cptr2len(fword + fwordidx[depth]);
8388 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008389 }
8390 else
8391#endif
8392 ulen = flen = 1;
8393 if (round[depth] == 1)
8394 {
8395 p = fword + fwordidx[depth];
8396 l = flen;
8397 }
8398 else
8399 {
8400 p = uword + uwordidx[depth];
8401 l = ulen;
8402 }
8403
8404 for (tryidx = arridx[depth]; l > 0; --l)
8405 {
8406 /* Perform a binary search in the list of accepted bytes. */
8407 len = byts[tryidx++];
8408 c = *p++;
8409 lo = tryidx;
8410 hi = tryidx + len - 1;
8411 while (lo < hi)
8412 {
8413 m = (lo + hi) / 2;
8414 if (byts[m] > c)
8415 hi = m - 1;
8416 else if (byts[m] < c)
8417 lo = m + 1;
8418 else
8419 {
8420 lo = hi = m;
8421 break;
8422 }
8423 }
8424
8425 /* Stop if there is no matching byte. */
8426 if (hi < lo || byts[lo] != c)
8427 break;
8428
8429 /* Continue at the child (if there is one). */
8430 tryidx = idxs[lo];
8431 }
8432
8433 if (l == 0)
8434 {
8435 /*
8436 * Found the matching char. Copy it to "kword" and go a
8437 * level deeper.
8438 */
8439 if (round[depth] == 1)
8440 {
8441 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8442 flen);
8443 kwordlen[depth + 1] = kwordlen[depth] + flen;
8444 }
8445 else
8446 {
8447 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8448 ulen);
8449 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8450 }
8451 fwordidx[depth + 1] = fwordidx[depth] + flen;
8452 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8453
8454 ++depth;
8455 arridx[depth] = tryidx;
8456 round[depth] = 0;
8457 }
8458 }
8459 }
8460
8461 /* Didn't find it: "cannot happen". */
8462 *kword = NUL;
8463}
8464
8465/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008466 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8467 * su->su_sga.
8468 */
8469 static void
8470score_comp_sal(su)
8471 suginfo_T *su;
8472{
8473 langp_T *lp;
8474 char_u badsound[MAXWLEN];
8475 int i;
8476 suggest_T *stp;
8477 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008478 int score;
8479
8480 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8481 return;
8482
8483 /* Use the sound-folding of the first language that supports it. */
8484 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8485 lp->lp_slang != NULL; ++lp)
8486 if (lp->lp_slang->sl_sal.ga_len > 0)
8487 {
8488 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008489 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008490
8491 for (i = 0; i < su->su_ga.ga_len; ++i)
8492 {
8493 stp = &SUG(su->su_ga, i);
8494
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008495 /* Case-fold the suggested word, sound-fold it and compute the
8496 * sound-a-like score. */
8497 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008498 if (score < SCORE_MAXMAX)
8499 {
8500 /* Add the suggestion. */
8501 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8502 sstp->st_word = vim_strsave(stp->st_word);
8503 if (sstp->st_word != NULL)
8504 {
8505 sstp->st_score = score;
8506 sstp->st_altscore = 0;
8507 sstp->st_orglen = stp->st_orglen;
8508 ++su->su_sga.ga_len;
8509 }
8510 }
8511 }
8512 break;
8513 }
8514}
8515
8516/*
8517 * Combine the list of suggestions in su->su_ga and su->su_sga.
8518 * They are intwined.
8519 */
8520 static void
8521score_combine(su)
8522 suginfo_T *su;
8523{
8524 int i;
8525 int j;
8526 garray_T ga;
8527 garray_T *gap;
8528 langp_T *lp;
8529 suggest_T *stp;
8530 char_u *p;
8531 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008532 int round;
8533
8534 /* Add the alternate score to su_ga. */
8535 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8536 lp->lp_slang != NULL; ++lp)
8537 {
8538 if (lp->lp_slang->sl_sal.ga_len > 0)
8539 {
8540 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008541 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008542
8543 for (i = 0; i < su->su_ga.ga_len; ++i)
8544 {
8545 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008546 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8547 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008548 if (stp->st_altscore == SCORE_MAXMAX)
8549 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8550 else
8551 stp->st_score = (stp->st_score * 3
8552 + stp->st_altscore) / 4;
8553 stp->st_salscore = FALSE;
8554 }
8555 break;
8556 }
8557 }
8558
8559 /* Add the alternate score to su_sga. */
8560 for (i = 0; i < su->su_sga.ga_len; ++i)
8561 {
8562 stp = &SUG(su->su_sga, i);
8563 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8564 if (stp->st_score == SCORE_MAXMAX)
8565 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8566 else
8567 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8568 stp->st_salscore = TRUE;
8569 }
8570
8571 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8572 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8573 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8574
8575 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8576 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8577 return;
8578
8579 stp = &SUG(ga, 0);
8580 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8581 {
8582 /* round 1: get a suggestion from su_ga
8583 * round 2: get a suggestion from su_sga */
8584 for (round = 1; round <= 2; ++round)
8585 {
8586 gap = round == 1 ? &su->su_ga : &su->su_sga;
8587 if (i < gap->ga_len)
8588 {
8589 /* Don't add a word if it's already there. */
8590 p = SUG(*gap, i).st_word;
8591 for (j = 0; j < ga.ga_len; ++j)
8592 if (STRCMP(stp[j].st_word, p) == 0)
8593 break;
8594 if (j == ga.ga_len)
8595 stp[ga.ga_len++] = SUG(*gap, i);
8596 else
8597 vim_free(p);
8598 }
8599 }
8600 }
8601
8602 ga_clear(&su->su_ga);
8603 ga_clear(&su->su_sga);
8604
8605 /* Truncate the list to the number of suggestions that will be displayed. */
8606 if (ga.ga_len > su->su_maxcount)
8607 {
8608 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8609 vim_free(stp[i].st_word);
8610 ga.ga_len = su->su_maxcount;
8611 }
8612
8613 su->su_ga = ga;
8614}
8615
8616/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008617 * For the goodword in "stp" compute the soundalike score compared to the
8618 * badword.
8619 */
8620 static int
8621stp_sal_score(stp, su, slang, badsound)
8622 suggest_T *stp;
8623 suginfo_T *su;
8624 slang_T *slang;
8625 char_u *badsound; /* sound-folded badword */
8626{
8627 char_u *p;
8628 char_u badsound2[MAXWLEN];
8629 char_u fword[MAXWLEN];
8630 char_u goodsound[MAXWLEN];
8631
8632 if (stp->st_orglen <= su->su_badlen)
8633 p = badsound;
8634 else
8635 {
8636 /* soundfold the bad word with more characters following */
8637 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8638
8639 /* When joining two words the sound often changes a lot. E.g., "t he"
8640 * sounds like "t h" while "the" sounds like "@". Avoid that by
8641 * removing the space. Don't do it when the good word also contains a
8642 * space. */
8643 if (vim_iswhite(su->su_badptr[su->su_badlen])
8644 && *skiptowhite(stp->st_word) == NUL)
8645 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8646 mch_memmove(p, p + 1, STRLEN(p));
8647
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008648 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008649 p = badsound2;
8650 }
8651
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008652 /* Sound-fold the word and compute the score for the difference. */
8653 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008654
8655 return soundalike_score(goodsound, p);
8656}
8657
8658/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 * Find suggestions by comparing the word in a sound-a-like form.
8660 */
8661 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008662suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 suginfo_T *su;
8664{
8665 char_u salword[MAXWLEN];
8666 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008668 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008669 int curi[MAXWLEN];
8670 langp_T *lp;
8671 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008672 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673 int depth;
8674 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008675 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008676 int round;
8677 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008678 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008679
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008680 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8682 lp->lp_slang != NULL; ++lp)
8683 {
8684 if (lp->lp_slang->sl_sal.ga_len > 0)
8685 {
8686 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008687 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008688
8689 /*
8690 * Go through the whole tree, soundfold each word and compare.
8691 * round 1: use the case-folded tree.
8692 * round 2: use the keep-case tree.
8693 */
8694 for (round = 1; round <= 2; ++round)
8695 {
8696 if (round == 1)
8697 {
8698 byts = lp->lp_slang->sl_fbyts;
8699 idxs = lp->lp_slang->sl_fidxs;
8700 }
8701 else
8702 {
8703 byts = lp->lp_slang->sl_kbyts;
8704 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008705 if (byts == NULL) /* no keep-case words */
8706 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 }
8708
8709 depth = 0;
8710 arridx[0] = 0;
8711 curi[0] = 1;
8712 while (depth >= 0 && !got_int)
8713 {
8714 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 /* Done all bytes at this node, go up one level. */
8717 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008718 line_breakcheck();
8719 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 else
8721 {
8722 /* Do one more byte at this node. */
8723 n = arridx[depth] + curi[depth];
8724 ++curi[depth];
8725 c = byts[n];
8726 if (c == 0)
8727 {
8728 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008729 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8731 {
8732 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008733 /* Sound-fold. Only in keep-case tree need to
8734 * case-fold the word. */
8735 spell_soundfold(lp->lp_slang, tword,
8736 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008738 /* Compute the edit distance between the
8739 * sound-a-like words. */
8740 sound_score = soundalike_score(salword,
8741 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008742 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008744 char_u cword[MAXWLEN];
8745 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008746 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008747
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008748 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008749 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008750 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008751 /* Need to fix case according to
8752 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008753 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008754 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008755 }
8756 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008757 p = tword;
8758
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008759 if (sps_flags & SPS_DOUBLE)
8760 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008761 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008762 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008763 else
8764 {
8765 /* Compute the score. */
8766 score = spell_edit_score(
8767 su->su_badword, p);
8768 if (sps_flags & SPS_BEST)
8769 /* give a bonus for the good word
8770 * sounding the same as the bad
8771 * word */
8772 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008773 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008774 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008775 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008776 else
8777 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008778 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008779 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 }
8782 }
8783
8784 /* Skip over other NUL bytes. */
8785 while (byts[n + 1] == 0)
8786 {
8787 ++n;
8788 ++curi[depth];
8789 }
8790 }
8791 else
8792 {
8793 /* Normal char, go one level deeper. */
8794 tword[depth++] = c;
8795 arridx[depth] = idxs[n];
8796 curi[depth] = 1;
8797 }
8798 }
8799 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 }
8801 }
8802 }
8803}
8804
8805/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008806 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 */
8808 static void
8809make_case_word(fword, cword, flags)
8810 char_u *fword;
8811 char_u *cword;
8812 int flags;
8813{
8814 if (flags & WF_ALLCAP)
8815 /* Make it all upper-case */
8816 allcap_copy(fword, cword);
8817 else if (flags & WF_ONECAP)
8818 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008819 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 else
8821 /* Use goodword as-is. */
8822 STRCPY(cword, fword);
8823}
8824
Bram Moolenaarea424162005-06-16 21:51:00 +00008825/*
8826 * Use map string "map" for languages "lp".
8827 */
8828 static void
8829set_map_str(lp, map)
8830 slang_T *lp;
8831 char_u *map;
8832{
8833 char_u *p;
8834 int headc = 0;
8835 int c;
8836 int i;
8837
8838 if (*map == NUL)
8839 {
8840 lp->sl_has_map = FALSE;
8841 return;
8842 }
8843 lp->sl_has_map = TRUE;
8844
8845 /* Init the array and hash table empty. */
8846 for (i = 0; i < 256; ++i)
8847 lp->sl_map_array[i] = 0;
8848#ifdef FEAT_MBYTE
8849 hash_init(&lp->sl_map_hash);
8850#endif
8851
8852 /*
8853 * The similar characters are stored separated with slashes:
8854 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8855 * before the same slash. For characters above 255 sl_map_hash is used.
8856 */
8857 for (p = map; *p != NUL; )
8858 {
8859#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008860 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008861#else
8862 c = *p++;
8863#endif
8864 if (c == '/')
8865 headc = 0;
8866 else
8867 {
8868 if (headc == 0)
8869 headc = c;
8870
8871#ifdef FEAT_MBYTE
8872 /* Characters above 255 don't fit in sl_map_array[], put them in
8873 * the hash table. Each entry is the char, a NUL the headchar and
8874 * a NUL. */
8875 if (c >= 256)
8876 {
8877 int cl = mb_char2len(c);
8878 int headcl = mb_char2len(headc);
8879 char_u *b;
8880 hash_T hash;
8881 hashitem_T *hi;
8882
8883 b = alloc((unsigned)(cl + headcl + 2));
8884 if (b == NULL)
8885 return;
8886 mb_char2bytes(c, b);
8887 b[cl] = NUL;
8888 mb_char2bytes(headc, b + cl + 1);
8889 b[cl + 1 + headcl] = NUL;
8890 hash = hash_hash(b);
8891 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8892 if (HASHITEM_EMPTY(hi))
8893 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8894 else
8895 {
8896 /* This should have been checked when generating the .spl
8897 * file. */
8898 EMSG(_("E999: duplicate char in MAP entry"));
8899 vim_free(b);
8900 }
8901 }
8902 else
8903#endif
8904 lp->sl_map_array[c] = headc;
8905 }
8906 }
8907}
8908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909/*
8910 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8911 * lines in the .aff file.
8912 */
8913 static int
8914similar_chars(slang, c1, c2)
8915 slang_T *slang;
8916 int c1;
8917 int c2;
8918{
Bram Moolenaarea424162005-06-16 21:51:00 +00008919 int m1, m2;
8920#ifdef FEAT_MBYTE
8921 char_u buf[MB_MAXBYTES];
8922 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923
Bram Moolenaarea424162005-06-16 21:51:00 +00008924 if (c1 >= 256)
8925 {
8926 buf[mb_char2bytes(c1, buf)] = 0;
8927 hi = hash_find(&slang->sl_map_hash, buf);
8928 if (HASHITEM_EMPTY(hi))
8929 m1 = 0;
8930 else
8931 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8932 }
8933 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008934#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008935 m1 = slang->sl_map_array[c1];
8936 if (m1 == 0)
8937 return FALSE;
8938
8939
8940#ifdef FEAT_MBYTE
8941 if (c2 >= 256)
8942 {
8943 buf[mb_char2bytes(c2, buf)] = 0;
8944 hi = hash_find(&slang->sl_map_hash, buf);
8945 if (HASHITEM_EMPTY(hi))
8946 m2 = 0;
8947 else
8948 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8949 }
8950 else
8951#endif
8952 m2 = slang->sl_map_array[c2];
8953
8954 return m1 == m2;
8955}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008956
8957/*
8958 * Add a suggestion to the list of suggestions.
8959 * Do not add a duplicate suggestion or suggestions with a bad score.
8960 * When "use_score" is not zero it's used, otherwise the score is computed
8961 * with spell_edit_score().
8962 */
8963 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008964add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008966 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008968 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008969 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008970 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008971 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972{
8973 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008975 char_u *p = NULL;
8976 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977
8978 /* Check that the word wasn't banned. */
8979 if (was_banned(su, goodword))
8980 return;
8981
Bram Moolenaar0c405862005-06-22 22:26:26 +00008982 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8983 * Remove the common part from "goodword". */
8984 i = badlen - su->su_badlen;
8985 if (i > 0)
8986 {
8987 /* This assumes there was no case folding or it didn't change the
8988 * length... */
8989 p = goodword + STRLEN(goodword) - i;
8990 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8991 {
8992 badlen = su->su_badlen;
8993 c = *p;
8994 *p = NUL;
8995 }
8996 else
8997 p = NULL;
8998 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008999 else if (i < 0)
9000 {
9001 /* When replacing part of the word check that we actually change
9002 * something. For "the the" a suggestion can be replacing the first
9003 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009004 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009005 && STRNCMP(su->su_badword, goodword, badlen) == 0)
9006 return;
9007 }
9008
Bram Moolenaar0c405862005-06-22 22:26:26 +00009009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 if (score <= su->su_maxscore)
9011 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009012 /* Check if the word is already there. Also check the length that is
9013 * being replaced "thes," -> "these" is a different suggestion from
9014 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009015 stp = &SUG(*gap, 0);
9016 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009017 if (STRCMP(stp[i].st_word, goodword) == 0
9018 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 {
9020 /* Found it. Remember the lowest score. */
9021 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009024 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009025 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009026 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 break;
9028 }
9029
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009030 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 {
9032 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009033 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 stp->st_word = vim_strsave(goodword);
9035 if (stp->st_word != NULL)
9036 {
9037 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009038 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009039 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009040 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009041 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009042
9043 /* If we have too many suggestions now, sort the list and keep
9044 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009045 if (gap->ga_len > SUG_MAX_COUNT(su))
9046 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
9047 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 }
9049 }
9050 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00009051
9052 if (p != NULL)
9053 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009054}
9055
9056/*
9057 * Add a word to be banned.
9058 */
9059 static void
9060add_banned(su, word)
9061 suginfo_T *su;
9062 char_u *word;
9063{
9064 char_u *s = vim_strsave(word);
9065 hash_T hash;
9066 hashitem_T *hi;
9067
9068 if (s != NULL)
9069 {
9070 hash = hash_hash(s);
9071 hi = hash_lookup(&su->su_banned, s, hash);
9072 if (HASHITEM_EMPTY(hi))
9073 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009074 else
9075 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 }
9077}
9078
9079/*
9080 * Return TRUE if a word appears in the list of banned words.
9081 */
9082 static int
9083was_banned(su, word)
9084 suginfo_T *su;
9085 char_u *word;
9086{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009087 hashitem_T *hi = hash_find(&su->su_banned, word);
9088
9089 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009090}
9091
9092/*
9093 * Free the banned words in "su".
9094 */
9095 static void
9096free_banned(su)
9097 suginfo_T *su;
9098{
9099 int todo;
9100 hashitem_T *hi;
9101
9102 todo = su->su_banned.ht_used;
9103 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
9104 {
9105 if (!HASHITEM_EMPTY(hi))
9106 {
9107 vim_free(hi->hi_key);
9108 --todo;
9109 }
9110 }
9111 hash_clear(&su->su_banned);
9112}
9113
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009114/*
9115 * Recompute the score if sound-folding is possible. This is slow,
9116 * thus only done for the final results.
9117 */
9118 static void
9119rescore_suggestions(su)
9120 suginfo_T *su;
9121{
9122 langp_T *lp;
9123 suggest_T *stp;
9124 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009125 int i;
9126
9127 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9128 lp->lp_slang != NULL; ++lp)
9129 {
9130 if (lp->lp_slang->sl_sal.ga_len > 0)
9131 {
9132 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009133 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009134
9135 for (i = 0; i < su->su_ga.ga_len; ++i)
9136 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009137 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009138 if (!stp->st_had_bonus)
9139 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009140 stp->st_altscore = stp_sal_score(stp, su,
9141 lp->lp_slang, sal_badword);
9142 if (stp->st_altscore == SCORE_MAXMAX)
9143 stp->st_altscore = SCORE_BIG;
9144 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009145 }
9146 }
9147 break;
9148 }
9149 }
9150}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152static int
9153#ifdef __BORLANDC__
9154_RTLENTRYF
9155#endif
9156sug_compare __ARGS((const void *s1, const void *s2));
9157
9158/*
9159 * Function given to qsort() to sort the suggestions on st_score.
9160 */
9161 static int
9162#ifdef __BORLANDC__
9163_RTLENTRYF
9164#endif
9165sug_compare(s1, s2)
9166 const void *s1;
9167 const void *s2;
9168{
9169 suggest_T *p1 = (suggest_T *)s1;
9170 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009171 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009173 if (n == 0)
9174 return p1->st_altscore - p2->st_altscore;
9175 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176}
9177
9178/*
9179 * Cleanup the suggestions:
9180 * - Sort on score.
9181 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009182 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009184 static int
9185cleanup_suggestions(gap, maxscore, keep)
9186 garray_T *gap;
9187 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009188 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009190 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 int i;
9192
9193 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009194 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195
9196 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009197 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009199 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009201 gap->ga_len = keep;
9202 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009204 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009205}
9206
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009207#if defined(FEAT_EVAL) || defined(PROTO)
9208/*
9209 * Soundfold a string, for soundfold().
9210 * Result is in allocated memory, NULL for an error.
9211 */
9212 char_u *
9213eval_soundfold(word)
9214 char_u *word;
9215{
9216 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009217 char_u sound[MAXWLEN];
9218
9219 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
9220 /* Use the sound-folding of the first language that supports it. */
9221 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9222 lp->lp_slang != NULL; ++lp)
9223 if (lp->lp_slang->sl_sal.ga_len > 0)
9224 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009225 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009226 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009227 return vim_strsave(sound);
9228 }
9229
9230 /* No language with sound folding, return word as-is. */
9231 return vim_strsave(word);
9232}
9233#endif
9234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235/*
9236 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9237 */
9238 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009239spell_soundfold(slang, inword, folded, res)
9240 slang_T *slang;
9241 char_u *inword;
9242 int folded; /* "inword" is already case-folded */
9243 char_u *res;
9244{
9245 char_u fword[MAXWLEN];
9246 char_u *word;
9247
9248 if (slang->sl_sofo)
9249 /* SOFOFROM and SOFOTO used */
9250 spell_soundfold_sofo(slang, inword, res);
9251 else
9252 {
9253 /* SAL items used. Requires the word to be case-folded. */
9254 if (folded)
9255 word = inword;
9256 else
9257 {
9258 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
9259 word = fword;
9260 }
9261
9262#ifdef FEAT_MBYTE
9263 if (has_mbyte)
9264 spell_soundfold_wsal(slang, word, res);
9265 else
9266#endif
9267 spell_soundfold_sal(slang, word, res);
9268 }
9269}
9270
9271/*
9272 * Perform sound folding of "inword" into "res" according to SOFOFROM and
9273 * SOFOTO lines.
9274 */
9275 static void
9276spell_soundfold_sofo(slang, inword, res)
9277 slang_T *slang;
9278 char_u *inword;
9279 char_u *res;
9280{
9281 char_u *s;
9282 int ri = 0;
9283 int c;
9284
9285#ifdef FEAT_MBYTE
9286 if (has_mbyte)
9287 {
9288 int prevc = 0;
9289 int *ip;
9290
9291 /* The sl_sal_first[] table contains the translation for chars up to
9292 * 255, sl_sal the rest. */
9293 for (s = inword; *s != NUL; )
9294 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009295 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009296 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9297 c = ' ';
9298 else if (c < 256)
9299 c = slang->sl_sal_first[c];
9300 else
9301 {
9302 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
9303 if (ip == NULL) /* empty list, can't match */
9304 c = NUL;
9305 else
9306 for (;;) /* find "c" in the list */
9307 {
9308 if (*ip == 0) /* not found */
9309 {
9310 c = NUL;
9311 break;
9312 }
9313 if (*ip == c) /* match! */
9314 {
9315 c = ip[1];
9316 break;
9317 }
9318 ip += 2;
9319 }
9320 }
9321
9322 if (c != NUL && c != prevc)
9323 {
9324 ri += mb_char2bytes(c, res + ri);
9325 if (ri + MB_MAXBYTES > MAXWLEN)
9326 break;
9327 prevc = c;
9328 }
9329 }
9330 }
9331 else
9332#endif
9333 {
9334 /* The sl_sal_first[] table contains the translation. */
9335 for (s = inword; (c = *s) != NUL; ++s)
9336 {
9337 if (vim_iswhite(c))
9338 c = ' ';
9339 else
9340 c = slang->sl_sal_first[c];
9341 if (c != NUL && (ri == 0 || res[ri - 1] != c))
9342 res[ri++] = c;
9343 }
9344 }
9345
9346 res[ri] = NUL;
9347}
9348
9349 static void
9350spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 slang_T *slang;
9352 char_u *inword;
9353 char_u *res;
9354{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009355 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009357 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009359 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009361 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009362 int n, k = 0;
9363 int z0;
9364 int k0;
9365 int n0;
9366 int c;
9367 int pri;
9368 int p0 = -333;
9369 int c0;
9370
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009371 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009372 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 if (slang->sl_rem_accents)
9374 {
9375 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009376 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009378 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009379 {
9380 *t++ = ' ';
9381 s = skipwhite(s);
9382 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009383 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009385 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 *t++ = *s;
9387 ++s;
9388 }
9389 }
9390 *t = NUL;
9391 }
9392 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009393 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009394
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009395 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009396
9397 /*
9398 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009399 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009400 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009401 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009402 while ((c = word[i]) != NUL)
9403 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009404 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 n = slang->sl_sal_first[c];
9406 z0 = 0;
9407
9408 if (n >= 0)
9409 {
9410 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009411 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009413 /* Quickly skip entries that don't match the word. Most
9414 * entries are less then three chars, optimize for that. */
9415 k = smp[n].sm_leadlen;
9416 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009417 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009418 if (word[i + 1] != s[1])
9419 continue;
9420 if (k > 2)
9421 {
9422 for (j = 2; j < k; ++j)
9423 if (word[i + j] != s[j])
9424 break;
9425 if (j < k)
9426 continue;
9427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009428 }
9429
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009430 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009432 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009433 while (*pf != NUL && *pf != word[i + k])
9434 ++pf;
9435 if (*pf == NUL)
9436 continue;
9437 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009438 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009439 s = smp[n].sm_rules;
9440 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441
9442 p0 = *s;
9443 k0 = k;
9444 while (*s == '-' && k > 1)
9445 {
9446 k--;
9447 s++;
9448 }
9449 if (*s == '<')
9450 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009451 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 {
9453 /* determine priority */
9454 pri = *s - '0';
9455 s++;
9456 }
9457 if (*s == '^' && *(s + 1) == '^')
9458 s++;
9459
9460 if (*s == NUL
9461 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009462 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009463 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009464 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009465 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009467 && spell_iswordp(word + i - 1, curbuf)
9468 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009469 {
9470 /* search for followup rules, if: */
9471 /* followup and k > 1 and NO '-' in searchstring */
9472 c0 = word[i + k - 1];
9473 n0 = slang->sl_sal_first[c0];
9474
9475 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009476 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 {
9478 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009479 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009481 /* Quickly skip entries that don't match the word.
9482 * */
9483 k0 = smp[n0].sm_leadlen;
9484 if (k0 > 1)
9485 {
9486 if (word[i + k] != s[1])
9487 continue;
9488 if (k0 > 2)
9489 {
9490 pf = word + i + k + 1;
9491 for (j = 2; j < k0; ++j)
9492 if (*pf++ != s[j])
9493 break;
9494 if (j < k0)
9495 continue;
9496 }
9497 }
9498 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009500 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009501 {
9502 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009503 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009504 while (*pf != NUL && *pf != word[i + k0])
9505 ++pf;
9506 if (*pf == NUL)
9507 continue;
9508 ++k0;
9509 }
9510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009512 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 while (*s == '-')
9514 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009515 /* "k0" gets NOT reduced because
9516 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 s++;
9518 }
9519 if (*s == '<')
9520 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009521 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 {
9523 p0 = *s - '0';
9524 s++;
9525 }
9526
9527 if (*s == NUL
9528 /* *s == '^' cuts */
9529 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009530 && !spell_iswordp(word + i + k0,
9531 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009532 {
9533 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536
9537 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 /* rule fits; stop search */
9541 break;
9542 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 }
9544
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009545 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 }
9548
9549 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009550 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009551 if (s == NULL)
9552 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009553 pf = smp[n].sm_rules;
9554 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 if (p0 == 1 && z == 0)
9556 {
9557 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009558 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9559 || res[reslen - 1] == *s))
9560 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 z0 = 1;
9562 z = 1;
9563 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009564 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 {
9566 word[i + k0] = *s;
9567 k0++;
9568 s++;
9569 }
9570 if (k > k0)
9571 mch_memmove(word + i + k0, word + i + k,
9572 STRLEN(word + i + k) + 1);
9573
9574 /* new "actual letter" */
9575 c = word[i];
9576 }
9577 else
9578 {
9579 /* no '<' rule used */
9580 i += k - 1;
9581 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009582 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009584 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009585 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009586 s++;
9587 }
9588 /* new "actual letter" */
9589 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009590 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 {
9592 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009593 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 mch_memmove(word, word + i + 1,
9595 STRLEN(word + i + 1) + 1);
9596 i = 0;
9597 z0 = 1;
9598 }
9599 }
9600 break;
9601 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 }
9603 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009604 else if (vim_iswhite(c))
9605 {
9606 c = ' ';
9607 k = 1;
9608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609
9610 if (z0 == 0)
9611 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009612 if (k && !p0 && reslen < MAXWLEN && c != NUL
9613 && (!slang->sl_collapse || reslen == 0
9614 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009616 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009617
9618 i++;
9619 z = 0;
9620 k = 0;
9621 }
9622 }
9623
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009624 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625}
9626
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009627#ifdef FEAT_MBYTE
9628/*
9629 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9630 * Multi-byte version of spell_soundfold().
9631 */
9632 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009633spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009634 slang_T *slang;
9635 char_u *inword;
9636 char_u *res;
9637{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009638 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009639 int word[MAXWLEN];
9640 int wres[MAXWLEN];
9641 int l;
9642 char_u *s;
9643 int *ws;
9644 char_u *t;
9645 int *pf;
9646 int i, j, z;
9647 int reslen;
9648 int n, k = 0;
9649 int z0;
9650 int k0;
9651 int n0;
9652 int c;
9653 int pri;
9654 int p0 = -333;
9655 int c0;
9656 int did_white = FALSE;
9657
9658 /*
9659 * Convert the multi-byte string to a wide-character string.
9660 * Remove accents, if wanted. We actually remove all non-word characters.
9661 * But keep white space.
9662 */
9663 n = 0;
9664 for (s = inword; *s != NUL; )
9665 {
9666 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009667 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009668 if (slang->sl_rem_accents)
9669 {
9670 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9671 {
9672 if (did_white)
9673 continue;
9674 c = ' ';
9675 did_white = TRUE;
9676 }
9677 else
9678 {
9679 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009680 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009681 continue;
9682 }
9683 }
9684 word[n++] = c;
9685 }
9686 word[n] = NUL;
9687
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009688 /*
9689 * This comes from Aspell phonet.cpp.
9690 * Converted from C++ to C. Added support for multi-byte chars.
9691 * Changed to keep spaces.
9692 */
9693 i = reslen = z = 0;
9694 while ((c = word[i]) != NUL)
9695 {
9696 /* Start with the first rule that has the character in the word. */
9697 n = slang->sl_sal_first[c & 0xff];
9698 z0 = 0;
9699
9700 if (n >= 0)
9701 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009702 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009703 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9704 {
9705 /* Quickly skip entries that don't match the word. Most
9706 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009707 if (c != ws[0])
9708 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009709 k = smp[n].sm_leadlen;
9710 if (k > 1)
9711 {
9712 if (word[i + 1] != ws[1])
9713 continue;
9714 if (k > 2)
9715 {
9716 for (j = 2; j < k; ++j)
9717 if (word[i + j] != ws[j])
9718 break;
9719 if (j < k)
9720 continue;
9721 }
9722 }
9723
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009724 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009725 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009726 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009727 while (*pf != NUL && *pf != word[i + k])
9728 ++pf;
9729 if (*pf == NUL)
9730 continue;
9731 ++k;
9732 }
9733 s = smp[n].sm_rules;
9734 pri = 5; /* default priority */
9735
9736 p0 = *s;
9737 k0 = k;
9738 while (*s == '-' && k > 1)
9739 {
9740 k--;
9741 s++;
9742 }
9743 if (*s == '<')
9744 s++;
9745 if (VIM_ISDIGIT(*s))
9746 {
9747 /* determine priority */
9748 pri = *s - '0';
9749 s++;
9750 }
9751 if (*s == '^' && *(s + 1) == '^')
9752 s++;
9753
9754 if (*s == NUL
9755 || (*s == '^'
9756 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009757 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009758 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009759 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009760 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009761 && spell_iswordp_w(word + i - 1, curbuf)
9762 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009763 {
9764 /* search for followup rules, if: */
9765 /* followup and k > 1 and NO '-' in searchstring */
9766 c0 = word[i + k - 1];
9767 n0 = slang->sl_sal_first[c0 & 0xff];
9768
9769 if (slang->sl_followup && k > 1 && n0 >= 0
9770 && p0 != '-' && word[i + k] != NUL)
9771 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009772 /* Test follow-up rule for "word[i + k]"; loop over
9773 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009774 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9775 == (c0 & 0xff); ++n0)
9776 {
9777 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009778 */
9779 if (c0 != ws[0])
9780 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009781 k0 = smp[n0].sm_leadlen;
9782 if (k0 > 1)
9783 {
9784 if (word[i + k] != ws[1])
9785 continue;
9786 if (k0 > 2)
9787 {
9788 pf = word + i + k + 1;
9789 for (j = 2; j < k0; ++j)
9790 if (*pf++ != ws[j])
9791 break;
9792 if (j < k0)
9793 continue;
9794 }
9795 }
9796 k0 += k - 1;
9797
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009798 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009799 {
9800 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009801 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009802 while (*pf != NUL && *pf != word[i + k0])
9803 ++pf;
9804 if (*pf == NUL)
9805 continue;
9806 ++k0;
9807 }
9808
9809 p0 = 5;
9810 s = smp[n0].sm_rules;
9811 while (*s == '-')
9812 {
9813 /* "k0" gets NOT reduced because
9814 * "if (k0 == k)" */
9815 s++;
9816 }
9817 if (*s == '<')
9818 s++;
9819 if (VIM_ISDIGIT(*s))
9820 {
9821 p0 = *s - '0';
9822 s++;
9823 }
9824
9825 if (*s == NUL
9826 /* *s == '^' cuts */
9827 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009828 && !spell_iswordp_w(word + i + k0,
9829 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009830 {
9831 if (k0 == k)
9832 /* this is just a piece of the string */
9833 continue;
9834
9835 if (p0 < pri)
9836 /* priority too low */
9837 continue;
9838 /* rule fits; stop search */
9839 break;
9840 }
9841 }
9842
9843 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9844 == (c0 & 0xff))
9845 continue;
9846 }
9847
9848 /* replace string */
9849 ws = smp[n].sm_to_w;
9850 s = smp[n].sm_rules;
9851 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9852 if (p0 == 1 && z == 0)
9853 {
9854 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009855 if (reslen > 0 && ws != NULL && *ws != NUL
9856 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009857 || wres[reslen - 1] == *ws))
9858 reslen--;
9859 z0 = 1;
9860 z = 1;
9861 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009862 if (ws != NULL)
9863 while (*ws != NUL && word[i + k0] != NUL)
9864 {
9865 word[i + k0] = *ws;
9866 k0++;
9867 ws++;
9868 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009869 if (k > k0)
9870 mch_memmove(word + i + k0, word + i + k,
9871 sizeof(int) * (STRLEN(word + i + k) + 1));
9872
9873 /* new "actual letter" */
9874 c = word[i];
9875 }
9876 else
9877 {
9878 /* no '<' rule used */
9879 i += k - 1;
9880 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009881 if (ws != NULL)
9882 while (*ws != NUL && ws[1] != NUL
9883 && reslen < MAXWLEN)
9884 {
9885 if (reslen == 0 || wres[reslen - 1] != *ws)
9886 wres[reslen++] = *ws;
9887 ws++;
9888 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009889 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009890 if (ws == NULL)
9891 c = NUL;
9892 else
9893 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009894 if (strstr((char *)s, "^^") != NULL)
9895 {
9896 if (c != NUL)
9897 wres[reslen++] = c;
9898 mch_memmove(word, word + i + 1,
9899 sizeof(int) * (STRLEN(word + i + 1) + 1));
9900 i = 0;
9901 z0 = 1;
9902 }
9903 }
9904 break;
9905 }
9906 }
9907 }
9908 else if (vim_iswhite(c))
9909 {
9910 c = ' ';
9911 k = 1;
9912 }
9913
9914 if (z0 == 0)
9915 {
9916 if (k && !p0 && reslen < MAXWLEN && c != NUL
9917 && (!slang->sl_collapse || reslen == 0
9918 || wres[reslen - 1] != c))
9919 /* condense only double letters */
9920 wres[reslen++] = c;
9921
9922 i++;
9923 z = 0;
9924 k = 0;
9925 }
9926 }
9927
9928 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9929 l = 0;
9930 for (n = 0; n < reslen; ++n)
9931 {
9932 l += mb_char2bytes(wres[n], res + l);
9933 if (l + MB_MAXBYTES > MAXWLEN)
9934 break;
9935 }
9936 res[l] = NUL;
9937}
9938#endif
9939
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009940/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009941 * Compute a score for two sound-a-like words.
9942 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9943 * Instead of a generic loop we write out the code. That keeps it fast by
9944 * avoiding checks that will not be possible.
9945 */
9946 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009947soundalike_score(goodstart, badstart)
9948 char_u *goodstart; /* sound-folded good word */
9949 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009950{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009951 char_u *goodsound = goodstart;
9952 char_u *badsound = badstart;
9953 int goodlen;
9954 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009955 int n;
9956 char_u *pl, *ps;
9957 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009958 int score = 0;
9959
9960 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9961 * counted so much, vowels halfway the word aren't counted at all. */
9962 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9963 {
9964 score = SCORE_DEL / 2;
9965 if (*badsound == '*')
9966 ++badsound;
9967 else
9968 ++goodsound;
9969 }
9970
9971 goodlen = STRLEN(goodsound);
9972 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009973
9974 /* Return quickly if the lenghts are too different to be fixed by two
9975 * changes. */
9976 n = goodlen - badlen;
9977 if (n < -2 || n > 2)
9978 return SCORE_MAXMAX;
9979
9980 if (n > 0)
9981 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009982 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009983 ps = badsound;
9984 }
9985 else
9986 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009987 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009988 ps = goodsound;
9989 }
9990
9991 /* Skip over the identical part. */
9992 while (*pl == *ps && *pl != NUL)
9993 {
9994 ++pl;
9995 ++ps;
9996 }
9997
9998 switch (n)
9999 {
10000 case -2:
10001 case 2:
10002 /*
10003 * Must delete two characters from "pl".
10004 */
10005 ++pl; /* first delete */
10006 while (*pl == *ps)
10007 {
10008 ++pl;
10009 ++ps;
10010 }
10011 /* strings must be equal after second delete */
10012 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010013 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010014
10015 /* Failed to compare. */
10016 break;
10017
10018 case -1:
10019 case 1:
10020 /*
10021 * Minimal one delete from "pl" required.
10022 */
10023
10024 /* 1: delete */
10025 pl2 = pl + 1;
10026 ps2 = ps;
10027 while (*pl2 == *ps2)
10028 {
10029 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010030 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010031 ++pl2;
10032 ++ps2;
10033 }
10034
10035 /* 2: delete then swap, then rest must be equal */
10036 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10037 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010038 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010039
10040 /* 3: delete then substitute, then the rest must be equal */
10041 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010042 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010043
10044 /* 4: first swap then delete */
10045 if (pl[0] == ps[1] && pl[1] == ps[0])
10046 {
10047 pl2 = pl + 2; /* swap, skip two chars */
10048 ps2 = ps + 2;
10049 while (*pl2 == *ps2)
10050 {
10051 ++pl2;
10052 ++ps2;
10053 }
10054 /* delete a char and then strings must be equal */
10055 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010056 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010057 }
10058
10059 /* 5: first substitute then delete */
10060 pl2 = pl + 1; /* substitute, skip one char */
10061 ps2 = ps + 1;
10062 while (*pl2 == *ps2)
10063 {
10064 ++pl2;
10065 ++ps2;
10066 }
10067 /* delete a char and then strings must be equal */
10068 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010069 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010070
10071 /* Failed to compare. */
10072 break;
10073
10074 case 0:
10075 /*
10076 * Lenghts are equal, thus changes must result in same length: An
10077 * insert is only possible in combination with a delete.
10078 * 1: check if for identical strings
10079 */
10080 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010081 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010082
10083 /* 2: swap */
10084 if (pl[0] == ps[1] && pl[1] == ps[0])
10085 {
10086 pl2 = pl + 2; /* swap, skip two chars */
10087 ps2 = ps + 2;
10088 while (*pl2 == *ps2)
10089 {
10090 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010091 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010092 ++pl2;
10093 ++ps2;
10094 }
10095 /* 3: swap and swap again */
10096 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10097 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010098 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010099
10100 /* 4: swap and substitute */
10101 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010102 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010103 }
10104
10105 /* 5: substitute */
10106 pl2 = pl + 1;
10107 ps2 = ps + 1;
10108 while (*pl2 == *ps2)
10109 {
10110 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010111 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010112 ++pl2;
10113 ++ps2;
10114 }
10115
10116 /* 6: substitute and swap */
10117 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10118 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010119 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010120
10121 /* 7: substitute and substitute */
10122 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010123 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010124
10125 /* 8: insert then delete */
10126 pl2 = pl;
10127 ps2 = ps + 1;
10128 while (*pl2 == *ps2)
10129 {
10130 ++pl2;
10131 ++ps2;
10132 }
10133 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010134 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010135
10136 /* 9: delete then insert */
10137 pl2 = pl + 1;
10138 ps2 = ps;
10139 while (*pl2 == *ps2)
10140 {
10141 ++pl2;
10142 ++ps2;
10143 }
10144 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010145 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010146
10147 /* Failed to compare. */
10148 break;
10149 }
10150
10151 return SCORE_MAXMAX;
10152}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154/*
10155 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010156 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010157 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010159 * It has been converted from C++ to C and modified to support multi-byte
10160 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010161 */
10162 static int
10163spell_edit_score(badword, goodword)
10164 char_u *badword;
10165 char_u *goodword;
10166{
10167 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010168 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169 int j, i;
10170 int t;
10171 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010172 int pbc, pgc;
10173#ifdef FEAT_MBYTE
10174 char_u *p;
10175 int wbadword[MAXWLEN];
10176 int wgoodword[MAXWLEN];
10177
10178 if (has_mbyte)
10179 {
10180 /* Get the characters from the multi-byte strings and put them in an
10181 * int array for easy access. */
10182 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010183 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010184 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010185 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010186 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010187 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010188 }
10189 else
10190#endif
10191 {
10192 badlen = STRLEN(badword) + 1;
10193 goodlen = STRLEN(goodword) + 1;
10194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195
10196 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
10197#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
10199 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010200 if (cnt == NULL)
10201 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202
10203 CNT(0, 0) = 0;
10204 for (j = 1; j <= goodlen; ++j)
10205 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
10206
10207 for (i = 1; i <= badlen; ++i)
10208 {
10209 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
10210 for (j = 1; j <= goodlen; ++j)
10211 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010212#ifdef FEAT_MBYTE
10213 if (has_mbyte)
10214 {
10215 bc = wbadword[i - 1];
10216 gc = wgoodword[j - 1];
10217 }
10218 else
10219#endif
10220 {
10221 bc = badword[i - 1];
10222 gc = goodword[j - 1];
10223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 if (bc == gc)
10225 CNT(i, j) = CNT(i - 1, j - 1);
10226 else
10227 {
10228 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010229 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
10231 else
10232 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
10233
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010234 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010236#ifdef FEAT_MBYTE
10237 if (has_mbyte)
10238 {
10239 pbc = wbadword[i - 2];
10240 pgc = wgoodword[j - 2];
10241 }
10242 else
10243#endif
10244 {
10245 pbc = badword[i - 2];
10246 pgc = goodword[j - 2];
10247 }
10248 if (bc == pgc && pbc == gc)
10249 {
10250 t = SCORE_SWAP + CNT(i - 2, j - 2);
10251 if (t < CNT(i, j))
10252 CNT(i, j) = t;
10253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010254 }
10255 t = SCORE_DEL + CNT(i - 1, j);
10256 if (t < CNT(i, j))
10257 CNT(i, j) = t;
10258 t = SCORE_INS + CNT(i, j - 1);
10259 if (t < CNT(i, j))
10260 CNT(i, j) = t;
10261 }
10262 }
10263 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010264
10265 i = CNT(badlen - 1, goodlen - 1);
10266 vim_free(cnt);
10267 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010269
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010270/*
10271 * ":spelldump"
10272 */
10273/*ARGSUSED*/
10274 void
10275ex_spelldump(eap)
10276 exarg_T *eap;
10277{
10278 buf_T *buf = curbuf;
10279 langp_T *lp;
10280 slang_T *slang;
10281 idx_T arridx[MAXWLEN];
10282 int curi[MAXWLEN];
10283 char_u word[MAXWLEN];
10284 int c;
10285 char_u *byts;
10286 idx_T *idxs;
10287 linenr_T lnum = 0;
10288 int round;
10289 int depth;
10290 int n;
10291 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010292 char_u *region_names = NULL; /* region names being used */
10293 int do_region = TRUE; /* dump region names and numbers */
10294 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010295
10296 if (no_spell_checking())
10297 return;
10298
10299 /* Create a new empty buffer by splitting the window. */
10300 do_cmdline_cmd((char_u *)"new");
10301 if (!bufempty() || !buf_valid(buf))
10302 return;
10303
Bram Moolenaar7887d882005-07-01 22:33:52 +000010304 /* Find out if we can support regions: All languages must support the same
10305 * regions or none at all. */
10306 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10307 {
10308 p = lp->lp_slang->sl_regions;
10309 if (p[0] != 0)
10310 {
10311 if (region_names == NULL) /* first language with regions */
10312 region_names = p;
10313 else if (STRCMP(region_names, p) != 0)
10314 {
10315 do_region = FALSE; /* region names are different */
10316 break;
10317 }
10318 }
10319 }
10320
10321 if (do_region && region_names != NULL)
10322 {
10323 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
10324 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10325 }
10326 else
10327 do_region = FALSE;
10328
10329 /*
10330 * Loop over all files loaded for the entries in 'spelllang'.
10331 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010332 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10333 {
10334 slang = lp->lp_slang;
10335
10336 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
10337 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10338
10339 /* round 1: case-folded tree
10340 * round 2: keep-case tree */
10341 for (round = 1; round <= 2; ++round)
10342 {
10343 if (round == 1)
10344 {
10345 byts = slang->sl_fbyts;
10346 idxs = slang->sl_fidxs;
10347 }
10348 else
10349 {
10350 byts = slang->sl_kbyts;
10351 idxs = slang->sl_kidxs;
10352 }
10353 if (byts == NULL)
10354 continue; /* array is empty */
10355
10356 depth = 0;
10357 arridx[0] = 0;
10358 curi[0] = 1;
10359 while (depth >= 0 && !got_int)
10360 {
10361 if (curi[depth] > byts[arridx[depth]])
10362 {
10363 /* Done all bytes at this node, go up one level. */
10364 --depth;
10365 line_breakcheck();
10366 }
10367 else
10368 {
10369 /* Do one more byte at this node. */
10370 n = arridx[depth] + curi[depth];
10371 ++curi[depth];
10372 c = byts[n];
10373 if (c == 0)
10374 {
10375 /* End of word, deal with the word.
10376 * Don't use keep-case words in the fold-case tree,
10377 * they will appear in the keep-case tree.
10378 * Only use the word when the region matches. */
10379 flags = (int)idxs[n];
10380 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000010381 && (do_region
10382 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010383 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010384 & lp->lp_region) != 0))
10385 {
10386 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010387 if (!do_region)
10388 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010389
10390 /* Dump the basic word if there is no prefix or
10391 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010392 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010393 if (c == 0 || curi[depth] == 2)
10394 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010395
10396 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010397 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010398 lnum = apply_prefixes(slang, word, round,
10399 flags, lnum);
10400 }
10401 }
10402 else
10403 {
10404 /* Normal char, go one level deeper. */
10405 word[depth++] = c;
10406 arridx[depth] = idxs[n];
10407 curi[depth] = 1;
10408 }
10409 }
10410 }
10411 }
10412 }
10413
10414 /* Delete the empty line that we started with. */
10415 if (curbuf->b_ml.ml_line_count > 1)
10416 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10417
10418 redraw_later(NOT_VALID);
10419}
10420
10421/*
10422 * Dump one word: apply case modifications and append a line to the buffer.
10423 */
10424 static void
10425dump_word(word, round, flags, lnum)
10426 char_u *word;
10427 int round;
10428 int flags;
10429 linenr_T lnum;
10430{
10431 int keepcap = FALSE;
10432 char_u *p;
10433 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010434 char_u badword[MAXWLEN + 10];
10435 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010436
10437 if (round == 1 && (flags & WF_CAPMASK) != 0)
10438 {
10439 /* Need to fix case according to "flags". */
10440 make_case_word(word, cword, flags);
10441 p = cword;
10442 }
10443 else
10444 {
10445 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010446 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10447 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010448 keepcap = TRUE;
10449 }
10450
Bram Moolenaar7887d882005-07-01 22:33:52 +000010451 /* Add flags and regions after a slash. */
10452 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010453 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010454 STRCPY(badword, p);
10455 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010456 if (keepcap)
10457 STRCAT(badword, "=");
10458 if (flags & WF_BANNED)
10459 STRCAT(badword, "!");
10460 else if (flags & WF_RARE)
10461 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010462 if (flags & WF_REGION)
10463 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010464 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010465 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010466 p = badword;
10467 }
10468
10469 ml_append(lnum, p, (colnr_T)0, FALSE);
10470}
10471
10472/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010473 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10474 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010475 * Return the updated line number.
10476 */
10477 static linenr_T
10478apply_prefixes(slang, word, round, flags, startlnum)
10479 slang_T *slang;
10480 char_u *word; /* case-folded word */
10481 int round;
10482 int flags; /* flags with prefix ID */
10483 linenr_T startlnum;
10484{
10485 idx_T arridx[MAXWLEN];
10486 int curi[MAXWLEN];
10487 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010488 char_u word_up[MAXWLEN];
10489 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010490 int c;
10491 char_u *byts;
10492 idx_T *idxs;
10493 linenr_T lnum = startlnum;
10494 int depth;
10495 int n;
10496 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010497 int i;
10498
Bram Moolenaar53805d12005-08-01 07:08:33 +000010499 /* if the word starts with a lower-case letter make the word with an
10500 * upper-case letter in word_up[]. */
10501 c = PTR2CHAR(word);
10502 if (SPELL_TOUPPER(c) != c)
10503 {
10504 onecap_copy(word, word_up, TRUE);
10505 has_word_up = TRUE;
10506 }
10507
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010508 byts = slang->sl_pbyts;
10509 idxs = slang->sl_pidxs;
10510 if (byts != NULL) /* array not is empty */
10511 {
10512 /*
10513 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010514 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010515 */
10516 depth = 0;
10517 arridx[0] = 0;
10518 curi[0] = 1;
10519 while (depth >= 0 && !got_int)
10520 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010521 n = arridx[depth];
10522 len = byts[n];
10523 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010524 {
10525 /* Done all bytes at this node, go up one level. */
10526 --depth;
10527 line_breakcheck();
10528 }
10529 else
10530 {
10531 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010532 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010533 ++curi[depth];
10534 c = byts[n];
10535 if (c == 0)
10536 {
10537 /* End of prefix, find out how many IDs there are. */
10538 for (i = 1; i < len; ++i)
10539 if (byts[n + i] != 0)
10540 break;
10541 curi[depth] += i - 1;
10542
Bram Moolenaar53805d12005-08-01 07:08:33 +000010543 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10544 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010545 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010546 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010547 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010548 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010549 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010550 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010551
10552 /* Check for prefix that matches the word when the
10553 * first letter is upper-case, but only if the prefix has
10554 * a condition. */
10555 if (has_word_up)
10556 {
10557 c = valid_word_prefix(i, n, flags, word_up, slang,
10558 TRUE);
10559 if (c != 0)
10560 {
10561 vim_strncpy(prefix + depth, word_up,
10562 MAXWLEN - depth - 1);
10563 dump_word(prefix, round,
10564 (c & WF_RAREPFX) ? (flags | WF_RARE)
10565 : flags, lnum++);
10566 }
10567 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010568 }
10569 else
10570 {
10571 /* Normal char, go one level deeper. */
10572 prefix[depth++] = c;
10573 arridx[depth] = idxs[n];
10574 curi[depth] = 1;
10575 }
10576 }
10577 }
10578 }
10579
10580 return lnum;
10581}
10582
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010583#if defined(FEAT_INS_EXPAND) || defined(PROTO)
10584static int spell_expand_need_cap;
10585
10586/*
10587 * Find start of the word in front of the cursor. We don't check if it is
10588 * badly spelled, with completion we can only change the word in front of the
10589 * cursor.
10590 * Used for Insert mode completion CTRL-X ?.
10591 * Returns the column number of the word.
10592 */
10593 int
10594spell_word_start(startcol)
10595 int startcol;
10596{
10597 char_u *line;
10598 char_u *p;
10599 int col = 0;
10600
10601 if (no_spell_checking())
10602 return startcol;
10603
10604 /* Find a word character before "startcol". */
10605 line = ml_get_curline();
10606 for (p = line + startcol; p > line; )
10607 {
10608 mb_ptr_back(line, p);
10609 if (spell_iswordp_nmw(p))
10610 break;
10611 }
10612
10613 /* Go back to start of the word. */
10614 while (p > line)
10615 {
10616 col = p - line;
10617 mb_ptr_back(line, p);
10618 if (!spell_iswordp(p, curbuf))
10619 break;
10620 col = 0;
10621 }
10622
10623 /* Need to check for 'spellcapcheck' now, the word is removed before
10624 * expand_spelling() is called. Therefore the ugly global variable. */
10625 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
10626
10627 return col;
10628}
10629
10630/*
10631 * Get list of spelling suggestions.
10632 * Used for Insert mode completion CTRL-X ?.
10633 * Returns the number of matches. The matches are in "matchp[]", array of
10634 * allocated strings.
10635 */
10636/*ARGSUSED*/
10637 int
10638expand_spelling(lnum, col, pat, matchp)
10639 linenr_T lnum;
10640 int col;
10641 char_u *pat;
10642 char_u ***matchp;
10643{
10644 garray_T ga;
10645
10646 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
10647 *matchp = ga.ga_data;
10648 return ga.ga_len;
10649}
10650#endif
10651
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010652#endif /* FEAT_SYN_HL */