blob: e4020699ab09fa4f1941bd11ea87e5384dd9e308 [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 Moolenaarcf6bf392005-06-27 22:27:46 +0000481#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000483#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000484#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
485 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000486
487#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488#define SCORE_MAXMAX 999999 /* accept any score */
489
490/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000491 * Structure to store info for word matching.
492 */
493typedef struct matchinf_S
494{
495 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000496
497 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000498 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000499 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000500 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000501 char_u *mi_cend; /* char after what was used for
502 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000503
504 /* case-folded text */
505 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000506 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000507
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000508 /* for when checking word after a prefix */
509 int mi_prefarridx; /* index in sl_pidxs with list of
510 prefixID/condition */
511 int mi_prefcnt; /* number of entries at mi_prefarridx */
512 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000513#ifdef FEAT_MBYTE
514 int mi_cprefixlen; /* byte length of prefix in original
515 case */
516#else
517# define mi_cprefixlen mi_prefixlen /* it's the same value */
518#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000519
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000520 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000521 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000522 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000523 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000524} matchinf_T;
525
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000526/*
527 * The tables used for recognizing word characters according to spelling.
528 * These are only used for the first 256 characters of 'encoding'.
529 */
530typedef struct spelltab_S
531{
532 char_u st_isw[256]; /* flags: is word char */
533 char_u st_isu[256]; /* flags: is uppercase char */
534 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000535 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000536} spelltab_T;
537
538static spelltab_T spelltab;
539static int did_set_spelltab;
540
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000541#define CF_WORD 0x01
542#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000543
544static void clear_spell_chartab __ARGS((spelltab_T *sp));
545static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000546static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
547static int spell_iswordp_nmw __ARGS((char_u *p));
548#ifdef FEAT_MBYTE
549static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
550#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000551static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000552
553/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000554 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000555 */
556typedef enum
557{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000558 STATE_START = 0, /* At start of node check for NUL bytes (goodword
559 * ends); if badword ends there is a match, otherwise
560 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000561 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000562 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000563 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
564 STATE_PLAIN, /* Use each byte of the node. */
565 STATE_DEL, /* Delete a byte from the bad word. */
566 STATE_INS, /* Insert a byte in the bad word. */
567 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000568 STATE_UNSWAP, /* Undo swap two characters. */
569 STATE_SWAP3, /* Swap two characters over three. */
570 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000571 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000572 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000573 STATE_REP_INI, /* Prepare for using REP items. */
574 STATE_REP, /* Use matching REP items from the .aff file. */
575 STATE_REP_UNDO, /* Undo a REP item replacement. */
576 STATE_FINAL /* End of this node. */
577} state_T;
578
579/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000580 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000581 */
582typedef struct trystate_S
583{
Bram Moolenaarea424162005-06-16 21:51:00 +0000584 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000585 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000586 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000587 short ts_curi; /* index in list of child nodes */
588 char_u ts_fidx; /* index in fword[], case-folded bad word */
589 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
590 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000591 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
592 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000593#ifdef FEAT_MBYTE
594 char_u ts_tcharlen; /* number of bytes in tword character */
595 char_u ts_tcharidx; /* current byte index in tword character */
596 char_u ts_isdiff; /* DIFF_ values */
597 char_u ts_fcharstart; /* index in fword where badword char started */
598#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000599 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000600 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000601 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000602} trystate_T;
603
Bram Moolenaarea424162005-06-16 21:51:00 +0000604/* values for ts_isdiff */
605#define DIFF_NONE 0 /* no different byte (yet) */
606#define DIFF_YES 1 /* different byte found */
607#define DIFF_INSERT 2 /* inserting character */
608
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000609/* special values ts_prefixdepth */
610#define PREFIXTREE 0xfe /* walking through the prefix tree */
611#define NOPREFIX 0xff /* not using prefixes */
612
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000613/* mode values for find_word */
614#define FIND_FOLDWORD 0 /* find word case-folded */
615#define FIND_KEEPWORD 1 /* find keep-case word */
616#define FIND_PREFIX 2 /* find word after prefix */
617
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000618static slang_T *slang_alloc __ARGS((char_u *lang));
619static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000620static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000621static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000622static 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 +0000623static void find_prefix __ARGS((matchinf_T *mip));
624static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000625static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000626static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000627static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000628static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000629static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000630static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000631static 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 +0000632static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000633static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
634static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000635#ifdef FEAT_MBYTE
636static int *mb_str2wide __ARGS((char_u *s));
637#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000638static 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 +0000639static void clear_midword __ARGS((buf_T *buf));
640static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000641static int find_region __ARGS((char_u *rp, char_u *region));
642static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000643static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000645static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000646static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
647static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000648static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000649static 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 +0000650#ifdef FEAT_EVAL
651static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
652#endif
653static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
654static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000655static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000657static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000658static void suggest_try_special __ARGS((suginfo_T *su));
659static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000661#ifdef FEAT_MBYTE
662static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
663#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000665static void score_comp_sal __ARGS((suginfo_T *su));
666static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000667static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000668static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000669static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000670static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000671static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000672static 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 +0000673static void add_banned __ARGS((suginfo_T *su, char_u *word));
674static int was_banned __ARGS((suginfo_T *su, char_u *word));
675static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000676static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000677static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000678static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
679static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
680static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000681#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000682static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000683#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000684static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000685static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000686static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
687static 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 +0000688
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000689/*
690 * Use our own character-case definitions, because the current locale may
691 * differ from what the .spl file uses.
692 * These must not be called with negative number!
693 */
694#ifndef FEAT_MBYTE
695/* Non-multi-byte implementation. */
696# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
697# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
698# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
699#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000700# if defined(HAVE_WCHAR_H)
701# include <wchar.h> /* for towupper() and towlower() */
702# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000703/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
704 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
705 * the "w" library function for characters above 255 if available. */
706# ifdef HAVE_TOWLOWER
707# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
708 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
709# else
710# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
711 : (c) < 256 ? spelltab.st_fold[c] : (c))
712# endif
713
714# ifdef HAVE_TOWUPPER
715# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
716 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
717# else
718# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
719 : (c) < 256 ? spelltab.st_upper[c] : (c))
720# endif
721
722# ifdef HAVE_ISWUPPER
723# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
724 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
725# else
726# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000727 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000728# endif
729#endif
730
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000731
732static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000733static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000734static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000735
736/*
737 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000738 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000739 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
740 * or when it's OK it remains unchanged.
741 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000742 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000743 * "capcol" is used to check for a Capitalised word after the end of a
744 * sentence. If it's zero then perform the check. Return the column where to
745 * check next, or -1 when no sentence end was found. If it's NULL then don't
746 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000747 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000748 * Returns the length of the word in bytes, also when it's OK, so that the
749 * caller can skip over the word.
750 */
751 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000752spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000753 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000754 char_u *ptr;
755 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000756 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000757{
758 matchinf_T mi; /* Most things are put in "mi" so that it can
759 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000760 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000761 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000762
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000763 /* A word never starts at a space or a control character. Return quickly
764 * then, skipping over the character. */
765 if (*ptr <= ' ')
766 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000767
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000768 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000769 * 0X99FF. But when a word character follows do check spelling to find
770 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000771 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000772 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000773 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
774 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000775 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000777 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000778 nrlen = mi.mi_end - ptr;
779 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000780 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000781 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000782
783 /* Try including the digits in the word. */
784 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000785 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000786 else
787 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000788
Bram Moolenaar0c405862005-06-22 22:26:26 +0000789 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000790 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000791 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000792 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000793 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000794 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000795 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000796 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000797
798 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
799 {
800 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000801 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000802 if (!SPELL_ISUPPER(c))
803 {
804 *attrp = highlight_attr[HLF_SPC];
805 return (int)(mi.mi_fend - ptr);
806 }
807 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000808 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000809 if (capcol != NULL)
810 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000811
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000812 /* We always use the characters up to the next non-word character,
813 * also for bad words. */
814 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000815
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000816 /* Check caps type later. */
817 mi.mi_capflags = 0;
818 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000819 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000820
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000821 /* Include one non-word character so that we can check for the
822 * word end. */
823 if (*mi.mi_fend != NUL)
824 mb_ptr_adv(mi.mi_fend);
825
826 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
827 MAXWLEN + 1);
828 mi.mi_fwordlen = STRLEN(mi.mi_fword);
829
830 /* The word is bad unless we recognize it. */
831 mi.mi_result = SP_BAD;
832
833 /*
834 * Loop over the languages specified in 'spelllang'.
835 * We check them all, because a matching word may be longer than an
836 * already found matching word.
837 */
838 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
839 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
840 {
841 /* Check for a matching word in case-folded words. */
842 find_word(&mi, FIND_FOLDWORD);
843
844 /* Check for a matching word in keep-case words. */
845 find_word(&mi, FIND_KEEPWORD);
846
847 /* Check for matching prefixes. */
848 find_prefix(&mi);
849 }
850
851 if (mi.mi_result != SP_OK)
852 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000853 /* If we found a number skip over it. Allows for "42nd". Do flag
854 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000855 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000856 {
857 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
858 return nrlen;
859 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000860
861 /* When we are at a non-word character there is no error, just
862 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000863 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000864 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000865 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
866 {
867 regmatch_T regmatch;
868
869 /* Check for end of sentence. */
870 regmatch.regprog = wp->w_buffer->b_cap_prog;
871 regmatch.rm_ic = FALSE;
872 if (vim_regexec(&regmatch, ptr, 0))
873 *capcol = (int)(regmatch.endp[0] - ptr);
874 }
875
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000876#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000877 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000878 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000879#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000880 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000881 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000882
883 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
884 *attrp = highlight_attr[HLF_SPB];
885 else if (mi.mi_result == SP_RARE)
886 *attrp = highlight_attr[HLF_SPR];
887 else
888 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000889 }
890
Bram Moolenaar51485f02005-06-04 21:55:20 +0000891 return (int)(mi.mi_end - ptr);
892}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000893
Bram Moolenaar51485f02005-06-04 21:55:20 +0000894/*
895 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000896 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
897 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
898 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
899 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000900 *
901 * For a match mip->mi_result is updated.
902 */
903 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000904find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000906 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000907{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000908 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000910 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000911 int endidxcnt = 0;
912 int len;
913 int wlen = 0;
914 int flen;
915 int c;
916 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000917 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000918#ifdef FEAT_MBYTE
919 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000920 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000921#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000922 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000923 slang_T *slang = mip->mi_lp->lp_slang;
924 unsigned flags;
925 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000926 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000928 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000929 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000930 /* Check for word with matching case in keep-case tree. */
931 ptr = mip->mi_word;
932 flen = 9999; /* no case folding, always enough bytes */
933 byts = slang->sl_kbyts;
934 idxs = slang->sl_kidxs;
935 }
936 else
937 {
938 /* Check for case-folded in case-folded tree. */
939 ptr = mip->mi_fword;
940 flen = mip->mi_fwordlen; /* available case-folded bytes */
941 byts = slang->sl_fbyts;
942 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000943
944 if (mode == FIND_PREFIX)
945 {
946 /* Skip over the prefix. */
947 wlen = mip->mi_prefixlen;
948 flen -= mip->mi_prefixlen;
949 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000950 }
951
Bram Moolenaar51485f02005-06-04 21:55:20 +0000952 if (byts == NULL)
953 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000954
Bram Moolenaar51485f02005-06-04 21:55:20 +0000955 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000956 * Repeat advancing in the tree until:
957 * - there is a byte that doesn't match,
958 * - we reach the end of the tree,
959 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 */
961 for (;;)
962 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000963 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000964 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000965
966 len = byts[arridx++];
967
968 /* If the first possible byte is a zero the word could end here.
969 * Remember this index, we first check for the longest word. */
970 if (byts[arridx] == 0)
971 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000972 if (endidxcnt == MAXWLEN)
973 {
974 /* Must be a corrupted spell file. */
975 EMSG(_(e_format));
976 return;
977 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000978 endlen[endidxcnt] = wlen;
979 endidx[endidxcnt++] = arridx++;
980 --len;
981
982 /* Skip over the zeros, there can be several flag/region
983 * combinations. */
984 while (len > 0 && byts[arridx] == 0)
985 {
986 ++arridx;
987 --len;
988 }
989 if (len == 0)
990 break; /* no children, word must end here */
991 }
992
993 /* Stop looking at end of the line. */
994 if (ptr[wlen] == NUL)
995 break;
996
997 /* Perform a binary search in the list of accepted bytes. */
998 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000999 if (c == TAB) /* <Tab> is handled like <Space> */
1000 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001001 lo = arridx;
1002 hi = arridx + len - 1;
1003 while (lo < hi)
1004 {
1005 m = (lo + hi) / 2;
1006 if (byts[m] > c)
1007 hi = m - 1;
1008 else if (byts[m] < c)
1009 lo = m + 1;
1010 else
1011 {
1012 lo = hi = m;
1013 break;
1014 }
1015 }
1016
1017 /* Stop if there is no matching byte. */
1018 if (hi < lo || byts[lo] != c)
1019 break;
1020
1021 /* Continue at the child (if there is one). */
1022 arridx = idxs[lo];
1023 ++wlen;
1024 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001025
1026 /* One space in the good word may stand for several spaces in the
1027 * checked word. */
1028 if (c == ' ')
1029 {
1030 for (;;)
1031 {
1032 if (flen <= 0 && *mip->mi_fend != NUL)
1033 flen = fold_more(mip);
1034 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1035 break;
1036 ++wlen;
1037 --flen;
1038 }
1039 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040 }
1041
1042 /*
1043 * Verify that one of the possible endings is valid. Try the longest
1044 * first.
1045 */
1046 while (endidxcnt > 0)
1047 {
1048 --endidxcnt;
1049 arridx = endidx[endidxcnt];
1050 wlen = endlen[endidxcnt];
1051
1052#ifdef FEAT_MBYTE
1053 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1054 continue; /* not at first byte of character */
1055#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001056 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 continue; /* next char is a word character */
1058
1059#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001060 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 {
1062 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001063 * when folding case. This can be slow, take a shortcut when the
1064 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001065 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001066 if (STRNCMP(ptr, p, wlen) != 0)
1067 {
1068 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1069 mb_ptr_adv(p);
1070 wlen = p - mip->mi_word;
1071 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001072 }
1073#endif
1074
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001075 /* Check flags and region. For FIND_PREFIX check the condition and
1076 * prefix ID.
1077 * Repeat this if there are more flags/region alternatives until there
1078 * is a match. */
1079 res = SP_BAD;
1080 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1081 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001082 {
1083 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001084
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001085 /* For the fold-case tree check that the case of the checked word
1086 * matches with what the word in the tree requires.
1087 * For keep-case tree the case is always right. For prefixes we
1088 * don't bother to check. */
1089 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001090 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001091 if (mip->mi_cend != mip->mi_word + wlen)
1092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001093 /* mi_capflags was set for a different word length, need
1094 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001095 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001096 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001097 }
1098
Bram Moolenaar0c405862005-06-22 22:26:26 +00001099 if (mip->mi_capflags == WF_KEEPCAP
1100 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001101 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001102 }
1103
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001104 /* When mode is FIND_PREFIX the word must support the prefix:
1105 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001106 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001108 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001109 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001110 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001111 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001112 mip->mi_word + mip->mi_cprefixlen, slang,
1113 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001114 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001115 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001116
1117 /* Use the WF_RARE flag for a rare prefix. */
1118 if (c & WF_RAREPFX)
1119 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001120 }
1121
1122 if (flags & WF_BANNED)
1123 res = SP_BANNED;
1124 else if (flags & WF_REGION)
1125 {
1126 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001127 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001128 res = SP_OK;
1129 else
1130 res = SP_LOCAL;
1131 }
1132 else if (flags & WF_RARE)
1133 res = SP_RARE;
1134 else
1135 res = SP_OK;
1136
1137 /* Always use the longest match and the best result. */
1138 if (mip->mi_result > res)
1139 {
1140 mip->mi_result = res;
1141 mip->mi_end = mip->mi_word + wlen;
1142 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001143 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001144 mip->mi_end = mip->mi_word + wlen;
1145
1146 if (res == SP_OK)
1147 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001148 }
1149
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001150 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001151 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001152 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001153}
1154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001155/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001156 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1157 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001158 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001159 */
1160 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001161valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001162 int totprefcnt; /* nr of prefix IDs */
1163 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001164 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001165 char_u *word;
1166 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001167 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001168{
1169 int prefcnt;
1170 int pidx;
1171 regprog_T *rp;
1172 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001173 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001174
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001175 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001176 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1177 {
1178 pidx = slang->sl_pidxs[arridx + prefcnt];
1179
1180 /* Check the prefix ID. */
1181 if (prefid != (pidx & 0xff))
1182 continue;
1183
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001184 /* Check if the prefix doesn't combine and the word already has a
1185 * suffix. */
1186 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1187 continue;
1188
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001189 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001190 * stored in the two bytes above the prefix ID byte. */
1191 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001192 if (rp != NULL)
1193 {
1194 regmatch.regprog = rp;
1195 regmatch.rm_ic = FALSE;
1196 if (!vim_regexec(&regmatch, word, 0))
1197 continue;
1198 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001199 else if (cond_req)
1200 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001201
Bram Moolenaar53805d12005-08-01 07:08:33 +00001202 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001203 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001204 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001205 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001206}
1207
1208/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001209 * Check if the word at "mip->mi_word" has a matching prefix.
1210 * If it does, then check the following word.
1211 *
1212 * For a match mip->mi_result is updated.
1213 */
1214 static void
1215find_prefix(mip)
1216 matchinf_T *mip;
1217{
1218 idx_T arridx = 0;
1219 int len;
1220 int wlen = 0;
1221 int flen;
1222 int c;
1223 char_u *ptr;
1224 idx_T lo, hi, m;
1225 slang_T *slang = mip->mi_lp->lp_slang;
1226 char_u *byts;
1227 idx_T *idxs;
1228
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001229 byts = slang->sl_pbyts;
1230 if (byts == NULL)
1231 return; /* array is empty */
1232
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001233 /* We use the case-folded word here, since prefixes are always
1234 * case-folded. */
1235 ptr = mip->mi_fword;
1236 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001237 idxs = slang->sl_pidxs;
1238
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001239 /*
1240 * Repeat advancing in the tree until:
1241 * - there is a byte that doesn't match,
1242 * - we reach the end of the tree,
1243 * - or we reach the end of the line.
1244 */
1245 for (;;)
1246 {
1247 if (flen == 0 && *mip->mi_fend != NUL)
1248 flen = fold_more(mip);
1249
1250 len = byts[arridx++];
1251
1252 /* If the first possible byte is a zero the prefix could end here.
1253 * Check if the following word matches and supports the prefix. */
1254 if (byts[arridx] == 0)
1255 {
1256 /* There can be several prefixes with different conditions. We
1257 * try them all, since we don't know which one will give the
1258 * longest match. The word is the same each time, pass the list
1259 * of possible prefixes to find_word(). */
1260 mip->mi_prefarridx = arridx;
1261 mip->mi_prefcnt = len;
1262 while (len > 0 && byts[arridx] == 0)
1263 {
1264 ++arridx;
1265 --len;
1266 }
1267 mip->mi_prefcnt -= len;
1268
1269 /* Find the word that comes after the prefix. */
1270 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001271#ifdef FEAT_MBYTE
1272 if (has_mbyte)
1273 {
1274 /* Case-folded length may differ from original length. */
1275 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1276 mip->mi_word);
1277 }
1278 else
1279 mip->mi_cprefixlen = wlen;
1280#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001281 find_word(mip, FIND_PREFIX);
1282
1283
1284 if (len == 0)
1285 break; /* no children, word must end here */
1286 }
1287
1288 /* Stop looking at end of the line. */
1289 if (ptr[wlen] == NUL)
1290 break;
1291
1292 /* Perform a binary search in the list of accepted bytes. */
1293 c = ptr[wlen];
1294 lo = arridx;
1295 hi = arridx + len - 1;
1296 while (lo < hi)
1297 {
1298 m = (lo + hi) / 2;
1299 if (byts[m] > c)
1300 hi = m - 1;
1301 else if (byts[m] < c)
1302 lo = m + 1;
1303 else
1304 {
1305 lo = hi = m;
1306 break;
1307 }
1308 }
1309
1310 /* Stop if there is no matching byte. */
1311 if (hi < lo || byts[lo] != c)
1312 break;
1313
1314 /* Continue at the child (if there is one). */
1315 arridx = idxs[lo];
1316 ++wlen;
1317 --flen;
1318 }
1319}
1320
1321/*
1322 * Need to fold at least one more character. Do until next non-word character
1323 * for efficiency.
1324 * Return the length of the folded chars in bytes.
1325 */
1326 static int
1327fold_more(mip)
1328 matchinf_T *mip;
1329{
1330 int flen;
1331 char_u *p;
1332
1333 p = mip->mi_fend;
1334 do
1335 {
1336 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001337 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001338
1339 /* Include the non-word character so that we can check for the
1340 * word end. */
1341 if (*mip->mi_fend != NUL)
1342 mb_ptr_adv(mip->mi_fend);
1343
1344 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1345 mip->mi_fword + mip->mi_fwordlen,
1346 MAXWLEN - mip->mi_fwordlen);
1347 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1348 mip->mi_fwordlen += flen;
1349 return flen;
1350}
1351
1352/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001353 * Check case flags for a word. Return TRUE if the word has the requested
1354 * case.
1355 */
1356 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001357spell_valid_case(wordflags, treeflags)
1358 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001359 int treeflags; /* flags for the word in the spell tree */
1360{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001361 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001362 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001363 && ((treeflags & WF_ONECAP) == 0
1364 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001365}
1366
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001367/*
1368 * Return TRUE if spell checking is not enabled.
1369 */
1370 static int
1371no_spell_checking()
1372{
1373 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1374 {
1375 EMSG(_("E756: Spell checking is not enabled"));
1376 return TRUE;
1377 }
1378 return FALSE;
1379}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001381/*
1382 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001383 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001384 * Return OK if found, FAIL otherwise.
1385 */
1386 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001387spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001388 int dir; /* FORWARD or BACKWARD */
1389 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001390 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001391{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001392 linenr_T lnum;
1393 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001394 char_u *line;
1395 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001396 char_u *endp;
1397 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001398 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001399 int has_syntax = syntax_present(curbuf);
1400 int col;
1401 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001402 char_u *buf = NULL;
1403 int buflen = 0;
1404 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001405 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001406
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001407 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001408 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001409
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001410 /*
1411 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001412 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001413 *
1414 * When searching backwards, we continue in the line to find the last
1415 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001416 *
1417 * We concatenate the start of the next line, so that wrapped words work
1418 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1419 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001420 */
1421 lnum = curwin->w_cursor.lnum;
1422 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001423
1424 while (!got_int)
1425 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001426 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001427
Bram Moolenaar0c405862005-06-22 22:26:26 +00001428 len = STRLEN(line);
1429 if (buflen < len + MAXWLEN + 2)
1430 {
1431 vim_free(buf);
1432 buflen = len + MAXWLEN + 2;
1433 buf = alloc(buflen);
1434 if (buf == NULL)
1435 break;
1436 }
1437
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001438 /* In first line check first word for Capital. */
1439 if (lnum == 1)
1440 capcol = 0;
1441
1442 /* For checking first word with a capital skip white space. */
1443 if (capcol == 0)
1444 capcol = skipwhite(line) - line;
1445
Bram Moolenaar0c405862005-06-22 22:26:26 +00001446 /* Copy the line into "buf" and append the start of the next line if
1447 * possible. */
1448 STRCPY(buf, line);
1449 if (lnum < curbuf->b_ml.ml_line_count)
1450 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1451
1452 p = buf + skip;
1453 endp = buf + len;
1454 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001455 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001456 /* When searching backward don't search after the cursor. */
1457 if (dir == BACKWARD
1458 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001459 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001460 break;
1461
1462 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001463 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001464 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001465
1466 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001467 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001470 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001471 /* When searching forward only accept a bad word after
1472 * the cursor. */
1473 if (dir == BACKWARD
1474 || lnum > curwin->w_cursor.lnum
1475 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001476 && (colnr_T)(curline ? p - buf + len
1477 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001478 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001479 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001480 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001481 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001482 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001483 (void)syn_get_id(lnum, (colnr_T)col,
1484 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001485 }
1486 else
1487 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488
Bram Moolenaar51485f02005-06-04 21:55:20 +00001489 if (can_spell)
1490 {
1491 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001492 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001493#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001494 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001495#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001496 if (dir == FORWARD)
1497 {
1498 /* No need to search further. */
1499 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001500 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001501 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001502 }
1503 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001504 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001505 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001506 }
1507
Bram Moolenaar51485f02005-06-04 21:55:20 +00001508 /* advance to character after the word */
1509 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001510 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001511 }
1512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001513 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001514 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001515
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001516 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001517 if (dir == BACKWARD)
1518 {
1519 if (found_pos.lnum != 0)
1520 {
1521 /* Use the last match in the line. */
1522 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001523 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001524 return OK;
1525 }
1526 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001527 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001528 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001529 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001530 }
1531 else
1532 {
1533 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001534 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001535 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001536
1537 /* Skip the characters at the start of the next line that were
1538 * included in a match crossing line boundaries. */
1539 if (attr == 0)
1540 skip = p - endp;
1541 else
1542 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001543
1544 /* Capscol skips over the inserted space. */
1545 --capcol;
1546
1547 /* But after empty line check first word in next line */
1548 if (*skipwhite(line) == NUL)
1549 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001550 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001551
1552 line_breakcheck();
1553 }
1554
Bram Moolenaar0c405862005-06-22 22:26:26 +00001555 vim_free(buf);
1556 return FAIL;
1557}
1558
1559/*
1560 * For spell checking: concatenate the start of the following line "line" into
1561 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1562 */
1563 void
1564spell_cat_line(buf, line, maxlen)
1565 char_u *buf;
1566 char_u *line;
1567 int maxlen;
1568{
1569 char_u *p;
1570 int n;
1571
1572 p = skipwhite(line);
1573 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1574 p = skipwhite(p + 1);
1575
1576 if (*p != NUL)
1577 {
1578 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001579 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001580 n = p - line;
1581 if (n >= maxlen)
1582 n = maxlen - 1;
1583 vim_memset(buf + 1, ' ', n);
1584 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001585}
1586
1587/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001588 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001589 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001590 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001591 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001592spell_load_lang(lang)
1593 char_u *lang;
1594{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001595 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001596 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001597 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001598
Bram Moolenaarb765d632005-06-07 21:00:02 +00001599 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001600 * It's truncated when an error is detected. */
1601 STRCPY(langcp, lang);
1602
Bram Moolenaarb765d632005-06-07 21:00:02 +00001603 /*
1604 * Find the first spell file for "lang" in 'runtimepath' and load it.
1605 */
1606 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1607 "spell/%s.%s.spl", lang, spell_enc());
1608 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001609
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001610 if (r == FAIL && *langcp != NUL)
1611 {
1612 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001613 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001614 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001615 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001616 }
1617
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001618 if (r == FAIL)
1619 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1620 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001621 else if (*langcp != NUL)
1622 {
1623 /* Load all the additions. */
1624 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1625 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1626 }
1627}
1628
1629/*
1630 * Return the encoding used for spell checking: Use 'encoding', except that we
1631 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1632 */
1633 static char_u *
1634spell_enc()
1635{
1636
1637#ifdef FEAT_MBYTE
1638 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1639 return p_enc;
1640#endif
1641 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001642}
1643
1644/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001645 * Get the name of the .spl file for the internal wordlist into
1646 * "fname[MAXPATHL]".
1647 */
1648 static void
1649int_wordlist_spl(fname)
1650 char_u *fname;
1651{
1652 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1653 int_wordlist, spell_enc());
1654}
1655
1656/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001657 * Allocate a new slang_T.
1658 * Caller must fill "sl_next".
1659 */
1660 static slang_T *
1661slang_alloc(lang)
1662 char_u *lang;
1663{
1664 slang_T *lp;
1665
Bram Moolenaar51485f02005-06-04 21:55:20 +00001666 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001667 if (lp != NULL)
1668 {
1669 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001670 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 }
1672 return lp;
1673}
1674
1675/*
1676 * Free the contents of an slang_T and the structure itself.
1677 */
1678 static void
1679slang_free(lp)
1680 slang_T *lp;
1681{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001682 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001683 vim_free(lp->sl_fname);
1684 slang_clear(lp);
1685 vim_free(lp);
1686}
1687
1688/*
1689 * Clear an slang_T so that the file can be reloaded.
1690 */
1691 static void
1692slang_clear(lp)
1693 slang_T *lp;
1694{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001695 garray_T *gap;
1696 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001697 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001698 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001699
Bram Moolenaar51485f02005-06-04 21:55:20 +00001700 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001701 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001702 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001703 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001704 vim_free(lp->sl_pbyts);
1705 lp->sl_pbyts = NULL;
1706
Bram Moolenaar51485f02005-06-04 21:55:20 +00001707 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001708 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001709 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001710 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001711 vim_free(lp->sl_pidxs);
1712 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001713
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001714 gap = &lp->sl_rep;
1715 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001716 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001717 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1718 vim_free(ftp->ft_from);
1719 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001720 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001721 ga_clear(gap);
1722
1723 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001724 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001725 {
1726 /* "ga_len" is set to 1 without adding an item for latin1 */
1727 if (gap->ga_data != NULL)
1728 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1729 for (i = 0; i < gap->ga_len; ++i)
1730 vim_free(((int **)gap->ga_data)[i]);
1731 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001732 else
1733 /* SAL items: free salitem_T items */
1734 while (gap->ga_len > 0)
1735 {
1736 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1737 vim_free(smp->sm_lead);
1738 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1739 vim_free(smp->sm_to);
1740#ifdef FEAT_MBYTE
1741 vim_free(smp->sm_lead_w);
1742 vim_free(smp->sm_oneof_w);
1743 vim_free(smp->sm_to_w);
1744#endif
1745 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001746 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001747
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001748 for (i = 0; i < lp->sl_prefixcnt; ++i)
1749 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001750 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001751 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001752 lp->sl_prefprog = NULL;
1753
1754 vim_free(lp->sl_midword);
1755 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001756
Bram Moolenaarea424162005-06-16 21:51:00 +00001757#ifdef FEAT_MBYTE
1758 {
1759 int todo = lp->sl_map_hash.ht_used;
1760 hashitem_T *hi;
1761
1762 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1763 if (!HASHITEM_EMPTY(hi))
1764 {
1765 --todo;
1766 vim_free(hi->hi_key);
1767 }
1768 }
1769 hash_clear(&lp->sl_map_hash);
1770#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001771}
1772
1773/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001774 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001775 * Invoked through do_in_runtimepath().
1776 */
1777 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001778spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001779 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001780 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001783}
1784
1785/*
1786 * Load one spell file and store the info into a slang_T.
1787 *
1788 * This is invoked in two ways:
1789 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1790 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1791 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1792 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001793 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001794 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001795 static slang_T *
1796spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001797 char_u *fname;
1798 char_u *lang;
1799 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001800 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001801{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802 FILE *fd;
1803 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001805 char_u *bp;
1806 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001807 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001808 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001809 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001810 int round;
1811 char_u *save_sourcing_name = sourcing_name;
1812 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001813 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001814 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001815 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001816 garray_T *gap;
1817 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001818 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001819 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001820 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001821 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822
Bram Moolenaarb765d632005-06-07 21:00:02 +00001823 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001824 if (fd == NULL)
1825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001826 if (!silent)
1827 EMSG2(_(e_notopen), fname);
1828 else if (p_verbose > 2)
1829 {
1830 verbose_enter();
1831 smsg((char_u *)e_notopen, fname);
1832 verbose_leave();
1833 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001834 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001835 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001836 if (p_verbose > 2)
1837 {
1838 verbose_enter();
1839 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1840 verbose_leave();
1841 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001842
Bram Moolenaarb765d632005-06-07 21:00:02 +00001843 if (old_lp == NULL)
1844 {
1845 lp = slang_alloc(lang);
1846 if (lp == NULL)
1847 goto endFAIL;
1848
1849 /* Remember the file name, used to reload the file when it's updated. */
1850 lp->sl_fname = vim_strsave(fname);
1851 if (lp->sl_fname == NULL)
1852 goto endFAIL;
1853
1854 /* Check for .add.spl. */
1855 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1856 }
1857 else
1858 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001859
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001860 /* Set sourcing_name, so that error messages mention the file name. */
1861 sourcing_name = fname;
1862 sourcing_lnum = 0;
1863
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001864 /* <HEADER>: <fileID>
1865 * <regioncnt> <regionname> ...
1866 * <charflagslen> <charflags>
1867 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001868 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001869 * <prefcondcnt> <prefcond> ...
1870 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001871 for (i = 0; i < VIMSPELLMAGICL; ++i)
1872 buf[i] = getc(fd); /* <fileID> */
1873 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1874 {
1875 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001876 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001877 }
1878
1879 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001880 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881 {
1882truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001883 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001884 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001885 }
1886 if (cnt > 8)
1887 {
1888formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001889 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001890 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001891 }
1892 for (i = 0; i < cnt; ++i)
1893 {
1894 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1895 lp->sl_regions[i * 2 + 1] = getc(fd);
1896 }
1897 lp->sl_regions[cnt * 2] = NUL;
1898
Bram Moolenaar7887d882005-07-01 22:33:52 +00001899 /* <charflagslen> <charflags> */
1900 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001901 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001902 goto endFAIL;
1903
1904 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001905 fol = read_cnt_string(fd, 2, &ccnt);
1906 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001907 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001908 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001909 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001910 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001911
1912 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1913 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001914 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001915
1916 vim_free(p);
1917 vim_free(fol);
1918
1919 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1920 if ((p == NULL) != (fol == NULL))
1921 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001922
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001923 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001924 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001925 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001926 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001927
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001928 /* <prefcondcnt> <prefcond> ... */
1929 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1930 if (cnt > 0)
1931 {
1932 lp->sl_prefprog = (regprog_T **)alloc_clear(
1933 (unsigned)sizeof(regprog_T *) * cnt);
1934 if (lp->sl_prefprog == NULL)
1935 goto endFAIL;
1936 lp->sl_prefixcnt = cnt;
1937
1938 for (i = 0; i < cnt; ++i)
1939 {
1940 /* <prefcond> : <condlen> <condstr> */
1941 n = getc(fd); /* <condlen> */
1942 if (n < 0)
1943 goto formerr;
1944 /* When <condlen> is zero we have an empty condition. Otherwise
1945 * compile the regexp program used to check for the condition. */
1946 if (n > 0)
1947 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001948 buf[0] = '^'; /* always match at one position only */
1949 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001950 while (n-- > 0)
1951 *p++ = getc(fd); /* <condstr> */
1952 *p = NUL;
1953 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1954 }
1955 }
1956 }
1957
1958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959 /* <SUGGEST> : <repcount> <rep> ...
1960 * <salflags> <salcount> <sal> ...
1961 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001962
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001963 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1964 if (cnt < 0)
1965 goto formerr;
1966
1967 gap = &lp->sl_rep;
1968 if (ga_grow(gap, cnt) == FAIL)
1969 goto endFAIL;
1970
1971 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1972 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001973 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001974 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001975 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001976 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001977 goto endFAIL;
1978 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001979 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001980 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001981 vim_free(ftp->ft_from);
1982 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001983 }
1984 }
1985
1986 /* Fill the first-index table. */
1987 first = lp->sl_rep_first;
1988 for (i = 0; i < 256; ++i)
1989 first[i] = -1;
1990 for (i = 0; i < gap->ga_len; ++i)
1991 {
1992 ftp = &((fromto_T *)gap->ga_data)[i];
1993 if (first[*ftp->ft_from] == -1)
1994 first[*ftp->ft_from] = i;
1995 }
1996
1997 i = getc(fd); /* <salflags> */
1998 if (i & SAL_F0LLOWUP)
1999 lp->sl_followup = TRUE;
2000 if (i & SAL_COLLAPSE)
2001 lp->sl_collapse = TRUE;
2002 if (i & SAL_REM_ACCENTS)
2003 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002004 if (i & SAL_SOFO)
2005 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002006 else
2007 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002008
2009 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2010 if (cnt < 0)
2011 goto formerr;
2012
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002013 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002014 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002015 /*
2016 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2017 */
2018 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002019 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002020
Bram Moolenaar7887d882005-07-01 22:33:52 +00002021 /* <salfromlen> <salfrom> */
2022 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002023 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002024 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002025
Bram Moolenaar7887d882005-07-01 22:33:52 +00002026 /* <saltolen> <salto> */
2027 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002028 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002029 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002030 vim_free(bp);
2031 goto endFAIL;
2032 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002033
Bram Moolenaar7887d882005-07-01 22:33:52 +00002034 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002035 if (bp != NULL && fol != NULL)
2036 i = set_sofo(lp, bp, fol);
2037 else if (bp != NULL || fol != NULL)
2038 i = FAIL; /* only one of two strings is an error */
2039 else
2040 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002041
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002042 vim_free(bp);
2043 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002044 if (i == FAIL)
2045 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002046 }
2047 else
2048 {
2049 /*
2050 * SAL items
2051 */
2052 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002053 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002054 if (ga_grow(gap, cnt) == FAIL)
2055 goto endFAIL;
2056
2057 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2058 for (; gap->ga_len < cnt; ++gap->ga_len)
2059 {
2060 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2061 ccnt = getc(fd); /* <salfromlen> */
2062 if (ccnt < 0)
2063 goto formerr;
2064 if ((p = alloc(ccnt + 2)) == NULL)
2065 goto endFAIL;
2066 smp->sm_lead = p;
2067
2068 /* Read up to the first special char into sm_lead. */
2069 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002070 {
2071 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002072 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002073 break;
2074 *p++ = c;
2075 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002076 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002077 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002078
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002079 /* Put (abc) chars in sm_oneof, if any. */
2080 if (c == '(')
2081 {
2082 smp->sm_oneof = p;
2083 for (++i; i < ccnt; ++i)
2084 {
2085 c = getc(fd); /* <salfrom> */
2086 if (c == ')')
2087 break;
2088 *p++ = c;
2089 }
2090 *p++ = NUL;
2091 if (++i < ccnt)
2092 c = getc(fd);
2093 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002094 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002095 smp->sm_oneof = NULL;
2096
2097 /* Any following chars go in sm_rules. */
2098 smp->sm_rules = p;
2099 if (i < ccnt)
2100 /* store the char we got while checking for end of sm_lead */
2101 *p++ = c;
2102 for (++i; i < ccnt; ++i)
2103 *p++ = getc(fd); /* <salfrom> */
2104 *p++ = NUL;
2105
Bram Moolenaar7887d882005-07-01 22:33:52 +00002106 /* <saltolen> <salto> */
2107 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002108 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002109 {
2110 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002111 goto formerr;
2112 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002113
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002114#ifdef FEAT_MBYTE
2115 if (has_mbyte)
2116 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002117 /* convert the multi-byte strings to wide char strings */
2118 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2119 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2120 if (smp->sm_oneof == NULL)
2121 smp->sm_oneof_w = NULL;
2122 else
2123 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002124 if (smp->sm_to == NULL)
2125 smp->sm_to_w = NULL;
2126 else
2127 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002128 if (smp->sm_lead_w == NULL
2129 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002130 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002131 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002132 vim_free(smp->sm_lead);
2133 vim_free(smp->sm_to);
2134 vim_free(smp->sm_lead_w);
2135 vim_free(smp->sm_oneof_w);
2136 vim_free(smp->sm_to_w);
2137 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002138 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002139 }
2140#endif
2141 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002142
2143 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002144 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002145 }
2146
Bram Moolenaar7887d882005-07-01 22:33:52 +00002147 /* <maplen> <mapstr> */
2148 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002149 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002150 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002151 if (p != NULL)
2152 {
2153 set_map_str(lp, p);
2154 vim_free(p);
2155 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002156
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002158 * round 2: <KWORDTREE>
2159 * round 3: <PREFIXTREE> */
2160 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002161 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002162 /* The tree size was computed when writing the file, so that we can
2163 * allocate it as one long block. <nodecount> */
2164 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2165 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002166 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002167 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002169 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002170 bp = lalloc((long_u)len, TRUE);
2171 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002172 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002173 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002174 lp->sl_fbyts = bp;
2175 else if (round == 2)
2176 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002177 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002178 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002179
2180 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002181 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2182 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002183 goto endFAIL;
2184 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002185 lp->sl_fidxs = ip;
2186 else if (round == 2)
2187 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002188 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002189 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002190
2191 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002192 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002193 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002194 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002195 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002196 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002197 }
2198 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002199
Bram Moolenaarb765d632005-06-07 21:00:02 +00002200 /* For a new file link it in the list of spell files. */
2201 if (old_lp == NULL)
2202 {
2203 lp->sl_next = first_lang;
2204 first_lang = lp;
2205 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002206
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002207 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002208
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002209endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002210 if (lang != NULL)
2211 /* truncating the name signals the error to spell_load_lang() */
2212 *lang = NUL;
2213 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002214 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002215 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 lp = NULL;
2217 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002218
2219endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002220 if (fd != NULL)
2221 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002222 sourcing_name = save_sourcing_name;
2223 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002224
2225 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002226}
2227
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002228/*
2229 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002230 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002231 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002232 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002233 */
2234 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002235read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002236 FILE *fd;
2237 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002238 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002239{
2240 int cnt = 0;
2241 int i;
2242 char_u *str;
2243
2244 /* read the length bytes, MSB first */
2245 for (i = 0; i < cnt_bytes; ++i)
2246 cnt = (cnt << 8) + getc(fd);
2247 if (cnt < 0)
2248 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002249 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002250 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002251 return NULL;
2252 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002253 *cntp = cnt;
2254 if (cnt == 0)
2255 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002256
2257 /* allocate memory */
2258 str = alloc((unsigned)cnt + 1);
2259 if (str == NULL)
2260 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002261 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002262 return NULL;
2263 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002264
2265 /* Read the string. Doesn't check for truncated file. */
2266 for (i = 0; i < cnt; ++i)
2267 str[i] = getc(fd);
2268 str[i] = NUL;
2269
2270 return str;
2271}
2272
Bram Moolenaar7887d882005-07-01 22:33:52 +00002273/*
2274 * Set the SOFOFROM and SOFOTO items in language "lp".
2275 * Returns FAIL when there is something wrong.
2276 */
2277 static int
2278set_sofo(lp, from, to)
2279 slang_T *lp;
2280 char_u *from;
2281 char_u *to;
2282{
2283 int i;
2284
2285#ifdef FEAT_MBYTE
2286 garray_T *gap;
2287 char_u *s;
2288 char_u *p;
2289 int c;
2290 int *inp;
2291
2292 if (has_mbyte)
2293 {
2294 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2295 * characters. The index is the low byte of the character.
2296 * The list contains from-to pairs with a terminating NUL.
2297 * sl_sal_first[] is used for latin1 "from" characters. */
2298 gap = &lp->sl_sal;
2299 ga_init2(gap, sizeof(int *), 1);
2300 if (ga_grow(gap, 256) == FAIL)
2301 return FAIL;
2302 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2303 gap->ga_len = 256;
2304
2305 /* First count the number of items for each list. Temporarily use
2306 * sl_sal_first[] for this. */
2307 for (p = from, s = to; *p != NUL && *s != NUL; )
2308 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002309 c = mb_cptr2char_adv(&p);
2310 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002311 if (c >= 256)
2312 ++lp->sl_sal_first[c & 0xff];
2313 }
2314 if (*p != NUL || *s != NUL) /* lengths differ */
2315 return FAIL;
2316
2317 /* Allocate the lists. */
2318 for (i = 0; i < 256; ++i)
2319 if (lp->sl_sal_first[i] > 0)
2320 {
2321 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2322 if (p == NULL)
2323 return FAIL;
2324 ((int **)gap->ga_data)[i] = (int *)p;
2325 *(int *)p = 0;
2326 }
2327
2328 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2329 * list. */
2330 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2331 for (p = from, s = to; *p != NUL && *s != NUL; )
2332 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002333 c = mb_cptr2char_adv(&p);
2334 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002335 if (c >= 256)
2336 {
2337 /* Append the from-to chars at the end of the list with
2338 * the low byte. */
2339 inp = ((int **)gap->ga_data)[c & 0xff];
2340 while (*inp != 0)
2341 ++inp;
2342 *inp++ = c; /* from char */
2343 *inp++ = i; /* to char */
2344 *inp++ = NUL; /* NUL at the end */
2345 }
2346 else
2347 /* mapping byte to char is done in sl_sal_first[] */
2348 lp->sl_sal_first[c] = i;
2349 }
2350 }
2351 else
2352#endif
2353 {
2354 /* mapping bytes to bytes is done in sl_sal_first[] */
2355 if (STRLEN(from) != STRLEN(to))
2356 return FAIL;
2357
2358 for (i = 0; to[i] != NUL; ++i)
2359 lp->sl_sal_first[from[i]] = to[i];
2360 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2361 }
2362
2363 return OK;
2364}
2365
2366/*
2367 * Fill the first-index table for "lp".
2368 */
2369 static void
2370set_sal_first(lp)
2371 slang_T *lp;
2372{
2373 salfirst_T *sfirst;
2374 int i;
2375 salitem_T *smp;
2376 int c;
2377 garray_T *gap = &lp->sl_sal;
2378
2379 sfirst = lp->sl_sal_first;
2380 for (i = 0; i < 256; ++i)
2381 sfirst[i] = -1;
2382 smp = (salitem_T *)gap->ga_data;
2383 for (i = 0; i < gap->ga_len; ++i)
2384 {
2385#ifdef FEAT_MBYTE
2386 if (has_mbyte)
2387 /* Use the lowest byte of the first character. For latin1 it's
2388 * the character, for other encodings it should differ for most
2389 * characters. */
2390 c = *smp[i].sm_lead_w & 0xff;
2391 else
2392#endif
2393 c = *smp[i].sm_lead;
2394 if (sfirst[c] == -1)
2395 {
2396 sfirst[c] = i;
2397#ifdef FEAT_MBYTE
2398 if (has_mbyte)
2399 {
2400 int n;
2401
2402 /* Make sure all entries with this byte are following each
2403 * other. Move the ones that are in the wrong position. Do
2404 * keep the same ordering! */
2405 while (i + 1 < gap->ga_len
2406 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2407 /* Skip over entry with same index byte. */
2408 ++i;
2409
2410 for (n = 1; i + n < gap->ga_len; ++n)
2411 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2412 {
2413 salitem_T tsal;
2414
2415 /* Move entry with same index byte after the entries
2416 * we already found. */
2417 ++i;
2418 --n;
2419 tsal = smp[i + n];
2420 mch_memmove(smp + i + 1, smp + i,
2421 sizeof(salitem_T) * n);
2422 smp[i] = tsal;
2423 }
2424 }
2425#endif
2426 }
2427 }
2428}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002429
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002430#ifdef FEAT_MBYTE
2431/*
2432 * Turn a multi-byte string into a wide character string.
2433 * Return it in allocated memory (NULL for out-of-memory)
2434 */
2435 static int *
2436mb_str2wide(s)
2437 char_u *s;
2438{
2439 int *res;
2440 char_u *p;
2441 int i = 0;
2442
2443 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2444 if (res != NULL)
2445 {
2446 for (p = s; *p != NUL; )
2447 res[i++] = mb_ptr2char_adv(&p);
2448 res[i] = NUL;
2449 }
2450 return res;
2451}
2452#endif
2453
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002455 * Read one row of siblings from the spell file and store it in the byte array
2456 * "byts" and index array "idxs". Recursively read the children.
2457 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002458 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 *
2460 * Returns the index follosing the siblings.
2461 * Returns -1 if the file is shorter than expected.
2462 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002464 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002465read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002466 FILE *fd;
2467 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002468 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002469 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002470 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002471 int prefixtree; /* TRUE for reading PREFIXTREE */
2472 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002474 int len;
2475 int i;
2476 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002477 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002478 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002479 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002480#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002481
Bram Moolenaar51485f02005-06-04 21:55:20 +00002482 len = getc(fd); /* <siblingcount> */
2483 if (len <= 0)
2484 return -1;
2485
2486 if (startidx + len >= maxidx)
2487 return -2;
2488 byts[idx++] = len;
2489
2490 /* Read the byte values, flag/region bytes and shared indexes. */
2491 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002493 c = getc(fd); /* <byte> */
2494 if (c < 0)
2495 return -1;
2496 if (c <= BY_SPECIAL)
2497 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002498 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002499 {
2500 /* No flags, all regions. */
2501 idxs[idx] = 0;
2502 c = 0;
2503 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002504 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002505 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002506 if (prefixtree)
2507 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002508 /* Read the optional pflags byte, the prefix ID and the
2509 * condition nr. In idxs[] store the prefix ID in the low
2510 * byte, the condition index shifted up 8 bits, the flags
2511 * shifted up 24 bits. */
2512 if (c == BY_FLAGS)
2513 c = getc(fd) << 24; /* <pflags> */
2514 else
2515 c = 0;
2516
2517 c |= getc(fd); /* <prefixID> */
2518
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002519 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2520 if (n >= maxprefcondnr)
2521 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002522 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002523 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002524 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002525 {
2526 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002527 * idxs[] the flags go in the low two bytes, region above
2528 * that and prefix ID above the region. */
2529 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002530 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002531 if (c2 == BY_FLAGS2)
2532 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002533 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002534 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002535 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002536 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002537 }
2538
Bram Moolenaar51485f02005-06-04 21:55:20 +00002539 idxs[idx] = c;
2540 c = 0;
2541 }
2542 else /* c == BY_INDEX */
2543 {
2544 /* <nodeidx> */
2545 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2546 if (n < 0 || n >= maxidx)
2547 return -2;
2548 idxs[idx] = n + SHARED_MASK;
2549 c = getc(fd); /* <xbyte> */
2550 }
2551 }
2552 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002553 }
2554
Bram Moolenaar51485f02005-06-04 21:55:20 +00002555 /* Recursively read the children for non-shared siblings.
2556 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2557 * remove SHARED_MASK) */
2558 for (i = 1; i <= len; ++i)
2559 if (byts[startidx + i] != 0)
2560 {
2561 if (idxs[startidx + i] & SHARED_MASK)
2562 idxs[startidx + i] &= ~SHARED_MASK;
2563 else
2564 {
2565 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002566 idx = read_tree(fd, byts, idxs, maxidx, idx,
2567 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002568 if (idx < 0)
2569 break;
2570 }
2571 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572
Bram Moolenaar51485f02005-06-04 21:55:20 +00002573 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002574}
2575
2576/*
2577 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002578 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002579 */
2580 char_u *
2581did_set_spelllang(buf)
2582 buf_T *buf;
2583{
2584 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002585 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002586 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002587 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002588 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002589 int region_mask;
2590 slang_T *lp;
2591 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002592 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002593 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002594 int len;
2595 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002596 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002597 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002598 char_u *use_region = NULL;
2599 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600
2601 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002602 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002603
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002604 /* loop over comma separated language names. */
2605 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002606 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002607 /* Get one language name. */
2608 copy_option_part(&splp, lang, MAXWLEN, ",");
2609
Bram Moolenaar5482f332005-04-17 20:18:43 +00002610 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002611 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002612
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002613 /* If the name ends in ".spl" use it as the name of the spell file.
2614 * If there is a region name let "region" point to it and remove it
2615 * from the name. */
2616 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2617 {
2618 filename = TRUE;
2619
Bram Moolenaarb6356332005-07-18 21:40:44 +00002620 /* Locate a region and remove it from the file name. */
2621 p = vim_strchr(gettail(lang), '_');
2622 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2623 && !ASCII_ISALPHA(p[3]))
2624 {
2625 vim_strncpy(region_cp, p + 1, 2);
2626 mch_memmove(p, p + 3, len - (p - lang) - 2);
2627 len -= 3;
2628 region = region_cp;
2629 }
2630 else
2631 dont_use_region = TRUE;
2632
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002633 /* Check if we loaded this language before. */
2634 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2635 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2636 break;
2637 }
2638 else
2639 {
2640 filename = FALSE;
2641 if (len > 3 && lang[len - 3] == '_')
2642 {
2643 region = lang + len - 2;
2644 len -= 3;
2645 lang[len] = NUL;
2646 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002647 else
2648 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002649
2650 /* Check if we loaded this language before. */
2651 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2652 if (STRICMP(lang, lp->sl_name) == 0)
2653 break;
2654 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002655
Bram Moolenaarb6356332005-07-18 21:40:44 +00002656 if (region != NULL)
2657 {
2658 /* If the region differs from what was used before then don't
2659 * use it for 'spellfile'. */
2660 if (use_region != NULL && STRCMP(region, use_region) != 0)
2661 dont_use_region = TRUE;
2662 use_region = region;
2663 }
2664
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002665 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002666 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002667 {
2668 if (filename)
2669 (void)spell_load_file(lang, lang, NULL, FALSE);
2670 else
2671 spell_load_lang(lang);
2672 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002673
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002674 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002675 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002676 */
2677 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002678 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2679 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002681 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002682 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002683 {
2684 /* find region in sl_regions */
2685 c = find_region(lp->sl_regions, region);
2686 if (c == REGION_ALL)
2687 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002688 if (lp->sl_add)
2689 {
2690 if (*lp->sl_regions != NUL)
2691 /* This addition file is for other regions. */
2692 region_mask = 0;
2693 }
2694 else
2695 /* This is probably an error. Give a warning and
2696 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002697 smsg((char_u *)
2698 _("Warning: region %s not supported"),
2699 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002700 }
2701 else
2702 region_mask = 1 << c;
2703 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002704
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002705 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002706 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002707 if (ga_grow(&ga, 1) == FAIL)
2708 {
2709 ga_clear(&ga);
2710 return e_outofmem;
2711 }
2712 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2713 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2714 ++ga.ga_len;
2715 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002716 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002718 }
2719
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002720 /* round 0: load int_wordlist, if possible.
2721 * round 1: load first name in 'spellfile'.
2722 * round 2: load second name in 'spellfile.
2723 * etc. */
2724 spf = curbuf->b_p_spf;
2725 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002727 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002728 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002729 /* Internal wordlist, if there is one. */
2730 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002731 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002732 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002733 }
2734 else
2735 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002736 /* One entry in 'spellfile'. */
2737 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2738 STRCAT(spf_name, ".spl");
2739
2740 /* If it was already found above then skip it. */
2741 for (c = 0; c < ga.ga_len; ++c)
2742 if (fullpathcmp(spf_name,
2743 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2744 FALSE) == FPC_SAME)
2745 break;
2746 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002747 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002748 }
2749
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002750 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2752 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2753 break;
2754 if (lp == NULL)
2755 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002756 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002757 * region name, the region is ignored otherwise. for int_wordlist
2758 * use an arbitrary name. */
2759 if (round == 0)
2760 STRCPY(lang, "internal wordlist");
2761 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002762 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002763 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002764 p = vim_strchr(lang, '.');
2765 if (p != NULL)
2766 *p = NUL; /* truncate at ".encoding.add" */
2767 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002768 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002769 }
2770 if (lp != NULL && ga_grow(&ga, 1) == OK)
2771 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002772 region_mask = REGION_ALL;
2773 if (use_region != NULL && !dont_use_region)
2774 {
2775 /* find region in sl_regions */
2776 c = find_region(lp->sl_regions, use_region);
2777 if (c != REGION_ALL)
2778 region_mask = 1 << c;
2779 else if (*lp->sl_regions != NUL)
2780 /* This spell file is for other regions. */
2781 region_mask = 0;
2782 }
2783
2784 if (region_mask != 0)
2785 {
2786 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2787 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2788 ++ga.ga_len;
2789 use_midword(lp, buf);
2790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002791 }
2792 }
2793
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 /* Add a NULL entry to mark the end of the list. */
2795 if (ga_grow(&ga, 1) == FAIL)
2796 {
2797 ga_clear(&ga);
2798 return e_outofmem;
2799 }
2800 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2801 ++ga.ga_len;
2802
2803 /* Everything is fine, store the new b_langp value. */
2804 ga_clear(&buf->b_langp);
2805 buf->b_langp = ga;
2806
2807 return NULL;
2808}
2809
2810/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002811 * Clear the midword characters for buffer "buf".
2812 */
2813 static void
2814clear_midword(buf)
2815 buf_T *buf;
2816{
2817 vim_memset(buf->b_spell_ismw, 0, 256);
2818#ifdef FEAT_MBYTE
2819 vim_free(buf->b_spell_ismw_mb);
2820 buf->b_spell_ismw_mb = NULL;
2821#endif
2822}
2823
2824/*
2825 * Use the "sl_midword" field of language "lp" for buffer "buf".
2826 * They add up to any currently used midword characters.
2827 */
2828 static void
2829use_midword(lp, buf)
2830 slang_T *lp;
2831 buf_T *buf;
2832{
2833 char_u *p;
2834
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002835 if (lp->sl_midword == NULL) /* there aren't any */
2836 return;
2837
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002838 for (p = lp->sl_midword; *p != NUL; )
2839#ifdef FEAT_MBYTE
2840 if (has_mbyte)
2841 {
2842 int c, l, n;
2843 char_u *bp;
2844
2845 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002846 l = (*mb_ptr2len)(p);
2847 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002848 buf->b_spell_ismw[c] = TRUE;
2849 else if (buf->b_spell_ismw_mb == NULL)
2850 /* First multi-byte char in "b_spell_ismw_mb". */
2851 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2852 else
2853 {
2854 /* Append multi-byte chars to "b_spell_ismw_mb". */
2855 n = STRLEN(buf->b_spell_ismw_mb);
2856 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2857 if (bp != NULL)
2858 {
2859 vim_free(buf->b_spell_ismw_mb);
2860 buf->b_spell_ismw_mb = bp;
2861 vim_strncpy(bp + n, p, l);
2862 }
2863 }
2864 p += l;
2865 }
2866 else
2867#endif
2868 buf->b_spell_ismw[*p++] = TRUE;
2869}
2870
2871/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002872 * Find the region "region[2]" in "rp" (points to "sl_regions").
2873 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002874 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002875 */
2876 static int
2877find_region(rp, region)
2878 char_u *rp;
2879 char_u *region;
2880{
2881 int i;
2882
2883 for (i = 0; ; i += 2)
2884 {
2885 if (rp[i] == NUL)
2886 return REGION_ALL;
2887 if (rp[i] == region[0] && rp[i + 1] == region[1])
2888 break;
2889 }
2890 return i / 2;
2891}
2892
2893/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002894 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002895 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002896 * Word WF_ONECAP
2897 * W WORD WF_ALLCAP
2898 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002899 */
2900 static int
2901captype(word, end)
2902 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002903 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002904{
2905 char_u *p;
2906 int c;
2907 int firstcap;
2908 int allcap;
2909 int past_second = FALSE; /* past second word char */
2910
2911 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002912 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002914 return 0; /* only non-word characters, illegal word */
2915#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002916 if (has_mbyte)
2917 c = mb_ptr2char_adv(&p);
2918 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002919#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002920 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002921 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002922
2923 /*
2924 * Need to check all letters to find a word with mixed upper/lower.
2925 * But a word with an upper char only at start is a ONECAP.
2926 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002927 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002928 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002929 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002930 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002931 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002932 {
2933 /* UUl -> KEEPCAP */
2934 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002935 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002936 allcap = FALSE;
2937 }
2938 else if (!allcap)
2939 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002940 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002941 past_second = TRUE;
2942 }
2943
2944 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002945 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002946 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002947 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002948 return 0;
2949}
2950
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002951/*
2952 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2953 * capital. So that make_case_word() can turn WOrd into Word.
2954 * Add ALLCAP for "WOrD".
2955 */
2956 static int
2957badword_captype(word, end)
2958 char_u *word;
2959 char_u *end;
2960{
2961 int flags = captype(word, end);
2962 int l, u;
2963 int first;
2964 char_u *p;
2965
2966 if (flags & WF_KEEPCAP)
2967 {
2968 /* Count the number of UPPER and lower case letters. */
2969 l = u = 0;
2970 first = FALSE;
2971 for (p = word; p < end; mb_ptr_adv(p))
2972 {
2973 if (SPELL_ISUPPER(PTR2CHAR(p)))
2974 {
2975 ++u;
2976 if (p == word)
2977 first = TRUE;
2978 }
2979 else
2980 ++l;
2981 }
2982
2983 /* If there are more UPPER than lower case letters suggest an
2984 * ALLCAP word. Otherwise, if the first letter is UPPER then
2985 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2986 * require three upper case letters. */
2987 if (u > l && u > 2)
2988 flags |= WF_ALLCAP;
2989 else if (first)
2990 flags |= WF_ONECAP;
2991 }
2992 return flags;
2993}
2994
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002995# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2996/*
2997 * Free all languages.
2998 */
2999 void
3000spell_free_all()
3001{
3002 slang_T *lp;
3003 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003004 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003005
3006 /* Go through all buffers and handle 'spelllang'. */
3007 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3008 ga_clear(&buf->b_langp);
3009
3010 while (first_lang != NULL)
3011 {
3012 lp = first_lang;
3013 first_lang = lp->sl_next;
3014 slang_free(lp);
3015 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003016
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003017 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003018 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003019 /* Delete the internal wordlist and its .spl file */
3020 mch_remove(int_wordlist);
3021 int_wordlist_spl(fname);
3022 mch_remove(fname);
3023 vim_free(int_wordlist);
3024 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003025 }
3026
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003027 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003028}
3029# endif
3030
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003031# if defined(FEAT_MBYTE) || defined(PROTO)
3032/*
3033 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003034 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003035 */
3036 void
3037spell_reload()
3038{
3039 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003040 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003041
Bram Moolenaarea408852005-06-25 22:49:46 +00003042 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003043 init_spell_chartab();
3044
3045 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003046 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003047
3048 /* Go through all buffers and handle 'spelllang'. */
3049 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3050 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003051 /* Only load the wordlists when 'spelllang' is set and there is a
3052 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003053 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003054 {
3055 FOR_ALL_WINDOWS(wp)
3056 if (wp->w_buffer == buf && wp->w_p_spell)
3057 {
3058 (void)did_set_spelllang(buf);
3059# ifdef FEAT_WINDOWS
3060 break;
3061# endif
3062 }
3063 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003064 }
3065}
3066# endif
3067
Bram Moolenaarb765d632005-06-07 21:00:02 +00003068/*
3069 * Reload the spell file "fname" if it's loaded.
3070 */
3071 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003072spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003073 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003074 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003075{
3076 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003077 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003078
Bram Moolenaarb765d632005-06-07 21:00:02 +00003079 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3080 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3081 {
3082 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003083 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003084 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003085 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003087
3088 /* When "zg" was used and the file wasn't loaded yet, should redo
3089 * 'spelllang' to get it loaded. */
3090 if (added_word && !didit)
3091 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003092}
3093
3094
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003095/*
3096 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003097 */
3098
Bram Moolenaar51485f02005-06-04 21:55:20 +00003099#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003100 and .dic file. */
3101/*
3102 * Main structure to store the contents of a ".aff" file.
3103 */
3104typedef struct afffile_S
3105{
3106 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003107 int af_rar; /* RAR ID for rare word */
3108 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003109 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003110 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003111 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3112 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003113} afffile_T;
3114
3115typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003116/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3117struct affentry_S
3118{
3119 affentry_T *ae_next; /* next affix with same name/number */
3120 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3121 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122 char_u *ae_cond; /* condition (NULL for ".") */
3123 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003124 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003125};
3126
Bram Moolenaar53805d12005-08-01 07:08:33 +00003127#define AH_KEY_LEN 10
3128
Bram Moolenaar51485f02005-06-04 21:55:20 +00003129/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3130typedef struct affheader_S
3131{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003132 /* key for hashtable == name of affix entry */
3133#ifdef FEAT_MBYTE
3134 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3135#else
3136 char_u ah_key[2]; /* one byte char plus NUL */
3137#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003138 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003139 int ah_combine; /* suffix may combine with prefix */
3140 affentry_T *ah_first; /* first affix entry */
3141} affheader_T;
3142
3143#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3144
3145/*
3146 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003147 * the need to keep track of each allocated thing, everything is freed all at
3148 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003149 */
3150#define SBLOCKSIZE 16000 /* size of sb_data */
3151typedef struct sblock_S sblock_T;
3152struct sblock_S
3153{
3154 sblock_T *sb_next; /* next block in list */
3155 int sb_used; /* nr of bytes already in use */
3156 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003157};
3158
3159/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003160 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003161 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003162typedef struct wordnode_S wordnode_T;
3163struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003164{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003165 union /* shared to save space */
3166 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003167 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003168 int index; /* index in written nodes (valid after first
3169 round) */
3170 } wn_u1;
3171 union /* shared to save space */
3172 {
3173 wordnode_T *next; /* next node with same hash key */
3174 wordnode_T *wnode; /* parent node that will write this node */
3175 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003176 wordnode_T *wn_child; /* child (next byte in word) */
3177 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3178 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003179 int wn_refs; /* Nr. of references to this node. Only
3180 relevant for first node in a list of
3181 siblings, in following siblings it is
3182 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003183 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003184 char_u wn_prefixID; /* when "wn_byte" is NUL: supported/required
3185 prefix ID or 0 */
3186 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3187 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003188 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003189#ifdef SPELL_PRINTTREE
3190 int wn_nr; /* sequence nr for printing */
3191#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003192};
3193
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003194#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3195
Bram Moolenaar51485f02005-06-04 21:55:20 +00003196#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003197
Bram Moolenaar51485f02005-06-04 21:55:20 +00003198/*
3199 * Info used while reading the spell files.
3200 */
3201typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003202{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003203 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003204 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003205 int si_fold_added; /* nr of words added since compressing */
3206
Bram Moolenaar51485f02005-06-04 21:55:20 +00003207 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003208 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003209 int si_keep_added; /* nr of words added since compressing */
3210
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003211 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003212
Bram Moolenaar51485f02005-06-04 21:55:20 +00003213 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003214 wordnode_T *si_first_free; /* List of nodes that have been freed during
3215 compression, linked by "wn_child" field. */
3216#ifdef SPELL_PRINTTREE
3217 int si_wordnode_nr; /* sequence nr for nodes */
3218#endif
3219
3220
Bram Moolenaar51485f02005-06-04 21:55:20 +00003221 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003222 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003223 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003224 int si_region; /* region mask */
3225 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003226 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003227 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003228 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003229 int si_region_count; /* number of regions supported (1 when there
3230 are no regions) */
3231 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003232
3233 garray_T si_rep; /* list of fromto_T entries from REP lines */
3234 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003235 char_u *si_sofofr; /* SOFOFROM text */
3236 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003237 int si_followup; /* soundsalike: ? */
3238 int si_collapse; /* soundsalike: ? */
3239 int si_rem_accents; /* soundsalike: remove accents */
3240 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003241 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003242 garray_T si_prefcond; /* table with conditions for postponed
3243 * prefixes, each stored as a string */
3244 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003245} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003246
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003247static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003248static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003249static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3250static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003251static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003252static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003253static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
3254static char_u *get_pfxlist __ARGS((spellinfo_T *spin, afffile_T *affile, char_u *afflist));
3255static 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));
3256static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3257static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3258static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003259static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003260static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
3261static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist));
3262static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int prefixID));
3263static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3264static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3265static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3266static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3267static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003268static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003269static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003270static void clear_node __ARGS((wordnode_T *node));
3271static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003273static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003274
Bram Moolenaar53805d12005-08-01 07:08:33 +00003275/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3276 * but it must be negative to indicate the prefix tree to tree_add_word().
3277 * Use a negative number with the lower 8 bits zero. */
3278#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003279
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003280#ifdef SPELL_PRINTTREE
3281/*
3282 * For debugging the tree code: print the current tree in a (more or less)
3283 * readable format, so that we can see what happens when adding a word and/or
3284 * compressing the tree.
3285 * Based on code from Olaf Seibert.
3286 */
3287#define PRINTLINESIZE 1000
3288#define PRINTWIDTH 6
3289
3290#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3291 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3292
3293static char line1[PRINTLINESIZE];
3294static char line2[PRINTLINESIZE];
3295static char line3[PRINTLINESIZE];
3296
3297 static void
3298spell_clear_flags(wordnode_T *node)
3299{
3300 wordnode_T *np;
3301
3302 for (np = node; np != NULL; np = np->wn_sibling)
3303 {
3304 np->wn_u1.index = FALSE;
3305 spell_clear_flags(np->wn_child);
3306 }
3307}
3308
3309 static void
3310spell_print_node(wordnode_T *node, int depth)
3311{
3312 if (node->wn_u1.index)
3313 {
3314 /* Done this node before, print the reference. */
3315 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3316 PRINTSOME(line2, depth, " ", 0, 0);
3317 PRINTSOME(line3, depth, " ", 0, 0);
3318 msg(line1);
3319 msg(line2);
3320 msg(line3);
3321 }
3322 else
3323 {
3324 node->wn_u1.index = TRUE;
3325
3326 if (node->wn_byte != NUL)
3327 {
3328 if (node->wn_child != NULL)
3329 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3330 else
3331 /* Cannot happen? */
3332 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3333 }
3334 else
3335 PRINTSOME(line1, depth, " $ ", 0, 0);
3336
3337 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3338
3339 if (node->wn_sibling != NULL)
3340 PRINTSOME(line3, depth, " | ", 0, 0);
3341 else
3342 PRINTSOME(line3, depth, " ", 0, 0);
3343
3344 if (node->wn_byte == NUL)
3345 {
3346 msg(line1);
3347 msg(line2);
3348 msg(line3);
3349 }
3350
3351 /* do the children */
3352 if (node->wn_byte != NUL && node->wn_child != NULL)
3353 spell_print_node(node->wn_child, depth + 1);
3354
3355 /* do the siblings */
3356 if (node->wn_sibling != NULL)
3357 {
3358 /* get rid of all parent details except | */
3359 STRCPY(line1, line3);
3360 STRCPY(line2, line3);
3361 spell_print_node(node->wn_sibling, depth);
3362 }
3363 }
3364}
3365
3366 static void
3367spell_print_tree(wordnode_T *root)
3368{
3369 if (root != NULL)
3370 {
3371 /* Clear the "wn_u1.index" fields, used to remember what has been
3372 * done. */
3373 spell_clear_flags(root);
3374
3375 /* Recursively print the tree. */
3376 spell_print_node(root, 0);
3377 }
3378}
3379#endif /* SPELL_PRINTTREE */
3380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003381/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003382 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003383 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003384 */
3385 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003386spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003387 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003388 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003389{
3390 FILE *fd;
3391 afffile_T *aff;
3392 char_u rline[MAXLINELEN];
3393 char_u *line;
3394 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003395#define MAXITEMCNT 7
3396 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003397 int itemcnt;
3398 char_u *p;
3399 int lnum = 0;
3400 affheader_T *cur_aff = NULL;
3401 int aff_todo = 0;
3402 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003403 char_u *low = NULL;
3404 char_u *fol = NULL;
3405 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003406 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 int do_rep;
3408 int do_sal;
3409 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003410 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003411 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003412 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003413 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003414 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003415
Bram Moolenaar51485f02005-06-04 21:55:20 +00003416 /*
3417 * Open the file.
3418 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003419 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003420 if (fd == NULL)
3421 {
3422 EMSG2(_(e_notopen), fname);
3423 return NULL;
3424 }
3425
Bram Moolenaarb765d632005-06-07 21:00:02 +00003426 if (spin->si_verbose || p_verbose > 2)
3427 {
3428 if (!spin->si_verbose)
3429 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003430 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003431 out_flush();
3432 if (!spin->si_verbose)
3433 verbose_leave();
3434 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 /* Only do REP lines when not done in another .aff file already. */
3437 do_rep = spin->si_rep.ga_len == 0;
3438
3439 /* Only do SAL lines when not done in another .aff file already. */
3440 do_sal = spin->si_sal.ga_len == 0;
3441
3442 /* Only do MAP lines when not done in another .aff file already. */
3443 do_map = spin->si_map.ga_len == 0;
3444
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003445 /* Only do MIDWORD line when not done in another .aff file already */
3446 do_midword = spin->si_midword == NULL;
3447
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003448 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3449 do_sofo = spin->si_sofofr == NULL;
3450
Bram Moolenaar51485f02005-06-04 21:55:20 +00003451 /*
3452 * Allocate and init the afffile_T structure.
3453 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003454 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003455 if (aff == NULL)
3456 return NULL;
3457 hash_init(&aff->af_pref);
3458 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003459
3460 /*
3461 * Read all the lines in the file one by one.
3462 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003463 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003464 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003465 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003466 ++lnum;
3467
3468 /* Skip comment lines. */
3469 if (*rline == '#')
3470 continue;
3471
3472 /* Convert from "SET" to 'encoding' when needed. */
3473 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003474#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003475 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003476 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003477 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003478 if (pc == NULL)
3479 {
3480 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3481 fname, lnum, rline);
3482 continue;
3483 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003484 line = pc;
3485 }
3486 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003487#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003488 {
3489 pc = NULL;
3490 line = rline;
3491 }
3492
3493 /* Split the line up in white separated items. Put a NUL after each
3494 * item. */
3495 itemcnt = 0;
3496 for (p = line; ; )
3497 {
3498 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3499 ++p;
3500 if (*p == NUL)
3501 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003502 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003503 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003504 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003505 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003506 ++p;
3507 if (*p == NUL)
3508 break;
3509 *p++ = NUL;
3510 }
3511
3512 /* Handle non-empty lines. */
3513 if (itemcnt > 0)
3514 {
3515 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3516 && aff->af_enc == NULL)
3517 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003518#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003519 /* Setup for conversion from "ENC" to 'encoding'. */
3520 aff->af_enc = enc_canonize(items[1]);
3521 if (aff->af_enc != NULL && !spin->si_ascii
3522 && convert_setup(&spin->si_conv, aff->af_enc,
3523 p_enc) == FAIL)
3524 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3525 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003526 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003527#else
3528 smsg((char_u *)_("Conversion in %s not supported"), fname);
3529#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003530 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003531 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3532 {
3533 if (do_midword)
3534 spin->si_midword = vim_strsave(items[1]);
3535 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003536 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003538 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003540 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003542 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003543 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003544 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3545 && aff->af_rar == 0)
3546 {
3547 aff->af_rar = items[1][0];
3548 if (items[1][1] != NUL)
3549 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3550 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003551 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3552 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003553 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003554 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003555 if (items[1][1] != NUL)
3556 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3557 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003558 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3559 && aff->af_bad == 0)
3560 {
3561 aff->af_bad = items[1][0];
3562 if (items[1][1] != NUL)
3563 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3564 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003565 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3566 {
3567 aff->af_pfxpostpone = TRUE;
3568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003569 else if ((STRCMP(items[0], "PFX") == 0
3570 || STRCMP(items[0], "SFX") == 0)
3571 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003572 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003574 /* Myspell allows extra text after the item, but that might
3575 * mean mistakes go unnoticed. Require a comment-starter. */
3576 if (itemcnt > 4 && *items[4] != '#')
3577 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3578 fname, lnum, items[4]);
3579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003580 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003581 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003582 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003583 if (cur_aff == NULL)
3584 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003585#ifdef FEAT_MBYTE
3586 if (has_mbyte)
3587 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003588 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003589 if (l >= AH_KEY_LEN)
3590 l = 1; /* too long, must be an overlong sequence */
3591 else
3592 mch_memmove(cur_aff->ah_key, items[1], l);
3593 }
3594 else
3595#endif
3596 {
3597 *cur_aff->ah_key = *items[1];
3598 l = 1;
3599 }
3600 cur_aff->ah_key[l] = NUL;
3601 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003602 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003603 if (*items[2] == 'Y')
3604 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003605 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003606 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3607 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003608
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003610 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003611 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003612 /* Use a new number in the .spl file later, to be able to
3613 * handle multiple .aff files. */
3614 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003615 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003616 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003617 else
3618 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003619 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003620 hi = hash_find(tp, cur_aff->ah_key);
3621 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003622 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003623 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3624 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003625 aff_todo = 0;
3626 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003627 else
3628 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003629 }
3630 else if ((STRCMP(items[0], "PFX") == 0
3631 || STRCMP(items[0], "SFX") == 0)
3632 && aff_todo > 0
3633 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003634 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003635 {
3636 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003637 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003638 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003639 int lasti = 5;
3640
3641 /* Check for "rare" after the other info. */
3642 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3643 {
3644 rare = TRUE;
3645 lasti = 6;
3646 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003647
Bram Moolenaar8db73182005-06-17 21:51:16 +00003648 /* Myspell allows extra text after the item, but that might
3649 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003650 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003651 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003652 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003653
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003654 /* New item for an affix letter. */
3655 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003656 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003657 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003658 if (aff_entry == NULL)
3659 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003660 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003661
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003662 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003663 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003664 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003665 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003666
Bram Moolenaar51485f02005-06-04 21:55:20 +00003667 /* Don't use an affix entry with non-ASCII characters when
3668 * "spin->si_ascii" is TRUE. */
3669 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003670 || has_non_ascii(aff_entry->ae_add)))
3671 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003672 aff_entry->ae_next = cur_aff->ah_first;
3673 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003674
3675 if (STRCMP(items[4], ".") != 0)
3676 {
3677 char_u buf[MAXLINELEN];
3678
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003679 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003680 if (*items[0] == 'P')
3681 sprintf((char *)buf, "^%s", items[4]);
3682 else
3683 sprintf((char *)buf, "%s$", items[4]);
3684 aff_entry->ae_prog = vim_regcomp(buf,
3685 RE_MAGIC + RE_STRING);
3686 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003687
3688 /* For postponed prefixes we need an entry in si_prefcond
3689 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003690 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003691 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003692 /* When the chop string is one lower-case letter and
3693 * the add string ends in the upper-case letter we set
3694 * the "upper" flag, clear "ae_chop" and remove the
3695 * letters from "ae_add". The condition must either
3696 * be empty or start with the same letter. */
3697 if (aff_entry->ae_chop != NULL
3698 && aff_entry->ae_add != NULL
3699#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003700 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00003701 aff_entry->ae_chop)] == NUL
3702#else
3703 && aff_entry->ae_chop[1] == NUL
3704#endif
3705 )
3706 {
3707 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003708
Bram Moolenaar53805d12005-08-01 07:08:33 +00003709 c = PTR2CHAR(aff_entry->ae_chop);
3710 c_up = SPELL_TOUPPER(c);
3711 if (c_up != c
3712 && (aff_entry->ae_cond == NULL
3713 || PTR2CHAR(aff_entry->ae_cond) == c))
3714 {
3715 p = aff_entry->ae_add
3716 + STRLEN(aff_entry->ae_add);
3717 mb_ptr_back(aff_entry->ae_add, p);
3718 if (PTR2CHAR(p) == c_up)
3719 {
3720 upper = TRUE;
3721 aff_entry->ae_chop = NULL;
3722 *p = NUL;
3723
3724 /* The condition is matched with the
3725 * actual word, thus must check for the
3726 * upper-case letter. */
3727 if (aff_entry->ae_cond != NULL)
3728 {
3729 char_u buf[MAXLINELEN];
3730#ifdef FEAT_MBYTE
3731 if (has_mbyte)
3732 {
3733 onecap_copy(items[4], buf, TRUE);
3734 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003735 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003736 }
3737 else
3738#endif
3739 *aff_entry->ae_cond = c_up;
3740 if (aff_entry->ae_cond != NULL)
3741 {
3742 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003743 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003744 vim_free(aff_entry->ae_prog);
3745 aff_entry->ae_prog = vim_regcomp(
3746 buf, RE_MAGIC + RE_STRING);
3747 }
3748 }
3749 }
3750 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003751 }
3752
Bram Moolenaar53805d12005-08-01 07:08:33 +00003753 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003754 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003755 int idx;
3756 char_u **pp;
3757 int n;
3758
3759 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3760 --idx)
3761 {
3762 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3763 if (str_equal(p, aff_entry->ae_cond))
3764 break;
3765 }
3766 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3767 {
3768 /* Not found, add a new condition. */
3769 idx = spin->si_prefcond.ga_len++;
3770 pp = ((char_u **)spin->si_prefcond.ga_data)
3771 + idx;
3772 if (aff_entry->ae_cond == NULL)
3773 *pp = NULL;
3774 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003775 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00003776 aff_entry->ae_cond);
3777 }
3778
3779 /* Add the prefix to the prefix tree. */
3780 if (aff_entry->ae_add == NULL)
3781 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003782 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003783 p = aff_entry->ae_add;
3784 /* PFX_FLAGS is a negative number, so that
3785 * tree_add_word() knows this is the prefix tree. */
3786 n = PFX_FLAGS;
3787 if (rare)
3788 n |= WFP_RARE;
3789 if (!cur_aff->ah_combine)
3790 n |= WFP_NC;
3791 if (upper)
3792 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003793 tree_add_word(spin, p, spin->si_prefroot, n,
3794 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003795 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003796 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003797 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003798 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003799 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3800 {
3801 if (fol != NULL)
3802 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3803 fname, lnum);
3804 else
3805 fol = vim_strsave(items[1]);
3806 }
3807 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3808 {
3809 if (low != NULL)
3810 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3811 fname, lnum);
3812 else
3813 low = vim_strsave(items[1]);
3814 }
3815 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3816 {
3817 if (upp != NULL)
3818 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3819 fname, lnum);
3820 else
3821 upp = vim_strsave(items[1]);
3822 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003823 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003825 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003826 if (!isdigit(*items[1]))
3827 smsg((char_u *)_("Expected REP count in %s line %d"),
3828 fname, lnum);
3829 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003830 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3831 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003832 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 if (do_rep)
3834 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3835 }
3836 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3837 {
3838 /* MAP item or count */
3839 if (!found_map)
3840 {
3841 /* First line contains the count. */
3842 found_map = TRUE;
3843 if (!isdigit(*items[1]))
3844 smsg((char_u *)_("Expected MAP count in %s line %d"),
3845 fname, lnum);
3846 }
3847 else if (do_map)
3848 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003849 int c;
3850
3851 /* Check that every character appears only once. */
3852 for (p = items[1]; *p != NUL; )
3853 {
3854#ifdef FEAT_MBYTE
3855 c = mb_ptr2char_adv(&p);
3856#else
3857 c = *p++;
3858#endif
3859 if ((spin->si_map.ga_len > 0
3860 && vim_strchr(spin->si_map.ga_data, c)
3861 != NULL)
3862 || vim_strchr(p, c) != NULL)
3863 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3864 fname, lnum);
3865 }
3866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 /* We simply concatenate all the MAP strings, separated by
3868 * slashes. */
3869 ga_concat(&spin->si_map, items[1]);
3870 ga_append(&spin->si_map, '/');
3871 }
3872 }
3873 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3874 {
3875 if (do_sal)
3876 {
3877 /* SAL item (sounds-a-like)
3878 * Either one of the known keys or a from-to pair. */
3879 if (STRCMP(items[1], "followup") == 0)
3880 spin->si_followup = sal_to_bool(items[2]);
3881 else if (STRCMP(items[1], "collapse_result") == 0)
3882 spin->si_collapse = sal_to_bool(items[2]);
3883 else if (STRCMP(items[1], "remove_accents") == 0)
3884 spin->si_rem_accents = sal_to_bool(items[2]);
3885 else
3886 /* when "to" is "_" it means empty */
3887 add_fromto(spin, &spin->si_sal, items[1],
3888 STRCMP(items[2], "_") == 0 ? (char_u *)""
3889 : items[2]);
3890 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003891 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003892 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3893 && (!do_sofo || spin->si_sofofr == NULL))
3894 {
3895 if (do_sofo)
3896 spin->si_sofofr = vim_strsave(items[1]);
3897 }
3898 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3899 && (!do_sofo || spin->si_sofoto == NULL))
3900 {
3901 if (do_sofo)
3902 spin->si_sofoto = vim_strsave(items[1]);
3903 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003904 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003905 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3906 fname, lnum, items[0]);
3907 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003908 }
3909
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003910 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3911 smsg((char_u *)_("Missing SOFO%s line in %s"),
3912 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3913 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3914 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3915
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003916 if (fol != NULL || low != NULL || upp != NULL)
3917 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003918 if (spin->si_clear_chartab)
3919 {
3920 /* Clear the char type tables, don't want to use any of the
3921 * currently used spell properties. */
3922 init_spell_chartab();
3923 spin->si_clear_chartab = FALSE;
3924 }
3925
Bram Moolenaar3982c542005-06-08 21:56:31 +00003926 /*
3927 * Don't write a word table for an ASCII file, so that we don't check
3928 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003929 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003930 * mb_get_class(), the list of chars in the file will be incomplete.
3931 */
3932 if (!spin->si_ascii
3933#ifdef FEAT_MBYTE
3934 && !enc_utf8
3935#endif
3936 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003937 {
3938 if (fol == NULL || low == NULL || upp == NULL)
3939 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3940 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003941 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003942 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003943
3944 vim_free(fol);
3945 vim_free(low);
3946 vim_free(upp);
3947 }
3948
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003949 vim_free(pc);
3950 fclose(fd);
3951 return aff;
3952}
3953
3954/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003955 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3956 * NULL as equal.
3957 */
3958 static int
3959str_equal(s1, s2)
3960 char_u *s1;
3961 char_u *s2;
3962{
3963 if (s1 == NULL || s2 == NULL)
3964 return s1 == s2;
3965 return STRCMP(s1, s2) == 0;
3966}
3967
3968/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003969 * Add a from-to item to "gap". Used for REP and SAL items.
3970 * They are stored case-folded.
3971 */
3972 static void
3973add_fromto(spin, gap, from, to)
3974 spellinfo_T *spin;
3975 garray_T *gap;
3976 char_u *from;
3977 char_u *to;
3978{
3979 fromto_T *ftp;
3980 char_u word[MAXWLEN];
3981
3982 if (ga_grow(gap, 1) == OK)
3983 {
3984 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3985 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003986 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003988 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989 ++gap->ga_len;
3990 }
3991}
3992
3993/*
3994 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3995 */
3996 static int
3997sal_to_bool(s)
3998 char_u *s;
3999{
4000 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4001}
4002
4003/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004004 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4005 * When "s" is NULL FALSE is returned.
4006 */
4007 static int
4008has_non_ascii(s)
4009 char_u *s;
4010{
4011 char_u *p;
4012
4013 if (s != NULL)
4014 for (p = s; *p != NUL; ++p)
4015 if (*p >= 128)
4016 return TRUE;
4017 return FALSE;
4018}
4019
4020/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004021 * Free the structure filled by spell_read_aff().
4022 */
4023 static void
4024spell_free_aff(aff)
4025 afffile_T *aff;
4026{
4027 hashtab_T *ht;
4028 hashitem_T *hi;
4029 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004030 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004031 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004032
4033 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004034
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004036 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4037 {
4038 todo = ht->ht_used;
4039 for (hi = ht->ht_array; todo > 0; ++hi)
4040 {
4041 if (!HASHITEM_EMPTY(hi))
4042 {
4043 --todo;
4044 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4046 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004047 }
4048 }
4049 if (ht == &aff->af_suff)
4050 break;
4051 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004053 hash_clear(&aff->af_pref);
4054 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004055}
4056
4057/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004060 */
4061 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004062spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004063 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004064 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004065 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004066{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004067 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004068 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004070 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004072 char_u *pc;
4073 char_u *w;
4074 int l;
4075 hash_T hash;
4076 hashitem_T *hi;
4077 FILE *fd;
4078 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004079 int non_ascii = 0;
4080 int retval = OK;
4081 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004082 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004083
Bram Moolenaar51485f02005-06-04 21:55:20 +00004084 /*
4085 * Open the file.
4086 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004087 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088 if (fd == NULL)
4089 {
4090 EMSG2(_(e_notopen), fname);
4091 return FAIL;
4092 }
4093
Bram Moolenaar51485f02005-06-04 21:55:20 +00004094 /* The hashtable is only used to detect duplicated words. */
4095 hash_init(&ht);
4096
Bram Moolenaarb765d632005-06-07 21:00:02 +00004097 if (spin->si_verbose || p_verbose > 2)
4098 {
4099 if (!spin->si_verbose)
4100 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004101 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004102 out_flush();
4103 if (!spin->si_verbose)
4104 verbose_leave();
4105 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004106
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004107 /* start with a message for the first line */
4108 spin->si_msg_count = 999999;
4109
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110 /* Read and ignore the first line: word count. */
4111 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004112 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004113 EMSG2(_("E760: No word count in %s"), fname);
4114
4115 /*
4116 * Read all the lines in the file one by one.
4117 * The words are converted to 'encoding' here, before being added to
4118 * the hashtable.
4119 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004120 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004121 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004122 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004123 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004124 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004125 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004126
Bram Moolenaar51485f02005-06-04 21:55:20 +00004127 /* Remove CR, LF and white space from the end. White space halfway
4128 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004129 l = STRLEN(line);
4130 while (l > 0 && line[l - 1] <= ' ')
4131 --l;
4132 if (l == 0)
4133 continue; /* empty line */
4134 line[l] = NUL;
4135
Bram Moolenaar51485f02005-06-04 21:55:20 +00004136 /* Find the optional affix names. */
4137 afflist = vim_strchr(line, '/');
4138 if (afflist != NULL)
4139 *afflist++ = NUL;
4140
4141 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4142 if (spin->si_ascii && has_non_ascii(line))
4143 {
4144 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004145 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004146 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004147
Bram Moolenaarb765d632005-06-07 21:00:02 +00004148#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004149 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004150 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004151 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004152 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004153 if (pc == NULL)
4154 {
4155 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4156 fname, lnum, line);
4157 continue;
4158 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004159 w = pc;
4160 }
4161 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004162#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004163 {
4164 pc = NULL;
4165 w = line;
4166 }
4167
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004168 /* This takes time, print a message every 10000 words. */
4169 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004170 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004171 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004172 vim_snprintf((char *)message, sizeof(message),
4173 _("line %6d, word %6d - %s"),
4174 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4175 msg_start();
4176 msg_puts_long_attr(message, 0);
4177 msg_clr_eos();
4178 msg_didout = FALSE;
4179 msg_col = 0;
4180 out_flush();
4181 }
4182
Bram Moolenaar51485f02005-06-04 21:55:20 +00004183 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004184 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004185 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004187 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004188 if (retval == FAIL)
4189 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004190
Bram Moolenaar51485f02005-06-04 21:55:20 +00004191 hash = hash_hash(dw);
4192 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193 if (!HASHITEM_EMPTY(hi))
4194 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004195 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004196 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004197 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004198
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004199 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004200 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004201 if (afflist != NULL)
4202 {
4203 /* Check for affix name that stands for keep-case word and stands
4204 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004205 if (affile->af_kep != NUL
4206 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004207 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004208 if (affile->af_rar != NUL
4209 && vim_strchr(afflist, affile->af_rar) != NULL)
4210 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004211 if (affile->af_bad != NUL
4212 && vim_strchr(afflist, affile->af_bad) != NULL)
4213 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004214
4215 if (affile->af_pfxpostpone)
4216 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004217 pfxlist = get_pfxlist(spin, affile, afflist);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004218 }
4219
Bram Moolenaar51485f02005-06-04 21:55:20 +00004220 /* Add the word to the word tree(s). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004221 if (store_word(spin, dw, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004222 retval = FAIL;
4223
4224 if (afflist != NULL)
4225 {
4226 /* Find all matching suffixes and add the resulting words.
4227 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004228 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004229 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004230 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004231 retval = FAIL;
4232
4233 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004234 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004235 &affile->af_pref, NULL,
4236 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004237 retval = FAIL;
4238 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004239 }
4240
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 if (spin->si_ascii && non_ascii > 0)
4242 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4243 non_ascii);
4244 hash_clear(&ht);
4245
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004246 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004247 return retval;
4248}
4249
4250/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004251 * Get the list of prefix IDs from the affix list "afflist".
4252 * Used for PFXPOSTPONE.
4253 * Returns a string allocated with getroom(). NULL when there are no prefixes
4254 * or when out of memory.
4255 */
4256 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004257get_pfxlist(spin, affile, afflist)
4258 spellinfo_T *spin;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004259 afffile_T *affile;
4260 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004261{
4262 char_u *p;
4263 int cnt;
4264 int round;
4265 char_u *res = NULL;
4266 char_u key[2];
4267 hashitem_T *hi;
4268
4269 key[1] = NUL;
4270
4271 /* round 1: count the number of prefix IDs.
4272 * round 2: move prefix IDs to "res" */
4273 for (round = 1; round <= 2; ++round)
4274 {
4275 cnt = 0;
4276 for (p = afflist; *p != NUL; ++p)
4277 {
4278 key[0] = *p;
4279 hi = hash_find(&affile->af_pref, key);
4280 if (!HASHITEM_EMPTY(hi))
4281 {
4282 /* This is a prefix ID, use the new number. */
4283 if (round == 2)
4284 res[cnt] = HI2AH(hi)->ah_newID;
4285 ++cnt;
4286 }
4287 }
4288 if (round == 1 && cnt > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004289 res = getroom(spin, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004290 if (res == NULL)
4291 break;
4292 }
4293
4294 if (res != NULL)
4295 res[cnt] = NUL;
4296 return res;
4297}
4298
4299/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004300 * Apply affixes to a word and store the resulting words.
4301 * "ht" is the hashtable with affentry_T that need to be applied, either
4302 * prefixes or suffixes.
4303 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4304 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004305 *
4306 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004307 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004308 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004309store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004310 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004311 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004312 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004313 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004314 hashtab_T *ht;
4315 hashtab_T *xht;
4316 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004317 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004318 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004319{
4320 int todo;
4321 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004322 affheader_T *ah;
4323 affentry_T *ae;
4324 regmatch_T regmatch;
4325 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004326 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004327 int i;
4328 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004329 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004330 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004331 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004332
Bram Moolenaar51485f02005-06-04 21:55:20 +00004333 todo = ht->ht_used;
4334 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004335 {
4336 if (!HASHITEM_EMPTY(hi))
4337 {
4338 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004339 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004340
Bram Moolenaar51485f02005-06-04 21:55:20 +00004341 /* Check that the affix combines, if required, and that the word
4342 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004343 c = PTR2CHAR(ah->ah_key);
4344 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004345 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004346 /* Loop over all affix entries with this name. */
4347 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004348 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004349 /* Check the condition. It's not logical to match case
4350 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004351 * Myspell.
4352 * For prefixes, when "PFXPOSTPONE" was used, only do
4353 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004354 regmatch.regprog = ae->ae_prog;
4355 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004356 if ((xht != NULL || !affile->af_pfxpostpone
4357 || ae->ae_chop != NULL)
4358 && (ae->ae_prog == NULL
4359 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004360 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004361 /* Match. Remove the chop and add the affix. */
4362 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004364 /* prefix: chop/add at the start of the word */
4365 if (ae->ae_add == NULL)
4366 *newword = NUL;
4367 else
4368 STRCPY(newword, ae->ae_add);
4369 p = word;
4370 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004371 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004372 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004373#ifdef FEAT_MBYTE
4374 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004375 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004376 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004377 for ( ; i > 0; --i)
4378 mb_ptr_adv(p);
4379 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004380 else
4381#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004382 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004383 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004384 STRCAT(newword, p);
4385 }
4386 else
4387 {
4388 /* suffix: chop/add at the end of the word */
4389 STRCPY(newword, word);
4390 if (ae->ae_chop != NULL)
4391 {
4392 /* Remove chop string. */
4393 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004394 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004395 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004396 mb_ptr_back(newword, p);
4397 *p = NUL;
4398 }
4399 if (ae->ae_add != NULL)
4400 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004401 }
4402
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004403 /* Obey the "rare" flag of the affix. */
4404 if (ae->ae_rare)
4405 use_flags = flags | WF_RARE;
4406 else
4407 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004408 use_pfxlist = pfxlist;
4409
4410 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004411 if (spin->si_prefroot != NULL
4412 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004413 {
4414 /* ... add a flag to indicate an affix was used. */
4415 use_flags |= WF_HAS_AFF;
4416
4417 /* ... don't use a prefix list if combining
4418 * affixes is not allowed */
4419 if (!ah->ah_combine || comb)
4420 use_pfxlist = NULL;
4421 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004422
Bram Moolenaar51485f02005-06-04 21:55:20 +00004423 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004424 if (store_word(spin, newword, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004425 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004426 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004427
Bram Moolenaar51485f02005-06-04 21:55:20 +00004428 /* When added a suffix and combining is allowed also
4429 * try adding prefixes additionally. */
4430 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004431 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004432 xht, NULL, TRUE,
4433 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004434 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004435 }
4436 }
4437 }
4438 }
4439 }
4440
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004441 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004442}
4443
4444/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004445 * Read a file with a list of words.
4446 */
4447 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004448spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004449 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004450 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004451{
4452 FILE *fd;
4453 long lnum = 0;
4454 char_u rline[MAXLINELEN];
4455 char_u *line;
4456 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004457 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004458 int l;
4459 int retval = OK;
4460 int did_word = FALSE;
4461 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004462 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004463 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004464
4465 /*
4466 * Open the file.
4467 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004468 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004469 if (fd == NULL)
4470 {
4471 EMSG2(_(e_notopen), fname);
4472 return FAIL;
4473 }
4474
Bram Moolenaarb765d632005-06-07 21:00:02 +00004475 if (spin->si_verbose || p_verbose > 2)
4476 {
4477 if (!spin->si_verbose)
4478 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004479 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004480 out_flush();
4481 if (!spin->si_verbose)
4482 verbose_leave();
4483 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004484
4485 /*
4486 * Read all the lines in the file one by one.
4487 */
4488 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4489 {
4490 line_breakcheck();
4491 ++lnum;
4492
4493 /* Skip comment lines. */
4494 if (*rline == '#')
4495 continue;
4496
4497 /* Remove CR, LF and white space from the end. */
4498 l = STRLEN(rline);
4499 while (l > 0 && rline[l - 1] <= ' ')
4500 --l;
4501 if (l == 0)
4502 continue; /* empty or blank line */
4503 rline[l] = NUL;
4504
4505 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4506 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004507#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004508 if (spin->si_conv.vc_type != CONV_NONE)
4509 {
4510 pc = string_convert(&spin->si_conv, rline, NULL);
4511 if (pc == NULL)
4512 {
4513 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4514 fname, lnum, rline);
4515 continue;
4516 }
4517 line = pc;
4518 }
4519 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004520#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004521 {
4522 pc = NULL;
4523 line = rline;
4524 }
4525
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004526 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004527 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004528 ++line;
4529 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004530 {
4531 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004532 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4533 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004534 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004535 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4536 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004537 else
4538 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004539#ifdef FEAT_MBYTE
4540 char_u *enc;
4541
Bram Moolenaar51485f02005-06-04 21:55:20 +00004542 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004543 line += 10;
4544 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004545 if (enc != NULL && !spin->si_ascii
4546 && convert_setup(&spin->si_conv, enc,
4547 p_enc) == FAIL)
4548 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004549 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004550 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004551 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004552#else
4553 smsg((char_u *)_("Conversion in %s not supported"), fname);
4554#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004555 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004556 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004557 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004558
Bram Moolenaar3982c542005-06-08 21:56:31 +00004559 if (STRNCMP(line, "regions=", 8) == 0)
4560 {
4561 if (spin->si_region_count > 1)
4562 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4563 fname, lnum, line);
4564 else
4565 {
4566 line += 8;
4567 if (STRLEN(line) > 16)
4568 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4569 fname, lnum, line);
4570 else
4571 {
4572 spin->si_region_count = STRLEN(line) / 2;
4573 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004574
4575 /* Adjust the mask for a word valid in all regions. */
4576 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004577 }
4578 }
4579 continue;
4580 }
4581
Bram Moolenaar7887d882005-07-01 22:33:52 +00004582 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4583 fname, lnum, line - 1);
4584 continue;
4585 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004586
Bram Moolenaar7887d882005-07-01 22:33:52 +00004587 flags = 0;
4588 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004589
Bram Moolenaar7887d882005-07-01 22:33:52 +00004590 /* Check for flags and region after a slash. */
4591 p = vim_strchr(line, '/');
4592 if (p != NULL)
4593 {
4594 *p++ = NUL;
4595 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004596 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004597 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004598 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004599 else if (*p == '!') /* Bad, bad, wicked word. */
4600 flags |= WF_BANNED;
4601 else if (*p == '?') /* Rare word. */
4602 flags |= WF_RARE;
4603 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004604 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004605 if ((flags & WF_REGION) == 0) /* first one */
4606 regionmask = 0;
4607 flags |= WF_REGION;
4608
4609 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004610 if (l > spin->si_region_count)
4611 {
4612 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004613 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004614 break;
4615 }
4616 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004617 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004618 else
4619 {
4620 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4621 fname, lnum, p);
4622 break;
4623 }
4624 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004625 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004626 }
4627
4628 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4629 if (spin->si_ascii && has_non_ascii(line))
4630 {
4631 ++non_ascii;
4632 continue;
4633 }
4634
4635 /* Normal word: store it. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004636 if (store_word(spin, line, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004637 {
4638 retval = FAIL;
4639 break;
4640 }
4641 did_word = TRUE;
4642 }
4643
4644 vim_free(pc);
4645 fclose(fd);
4646
Bram Moolenaarb765d632005-06-07 21:00:02 +00004647 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4648 {
4649 if (p_verbose > 2)
4650 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004651 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4652 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004653 if (p_verbose > 2)
4654 verbose_leave();
4655 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004656 return retval;
4657}
4658
4659/*
4660 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004661 * This avoids calling free() for every little struct we use (and keeping
4662 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004663 * The memory is cleared to all zeros.
4664 * Returns NULL when out of memory.
4665 */
4666 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004667getroom(spin, len, align)
4668 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004669 size_t len; /* length needed */
4670 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004671{
4672 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004673 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004674
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004675 if (align && bl != NULL)
4676 /* Round size up for alignment. On some systems structures need to be
4677 * aligned to the size of a pointer (e.g., SPARC). */
4678 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4679 & ~(sizeof(char *) - 1);
4680
Bram Moolenaar51485f02005-06-04 21:55:20 +00004681 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4682 {
4683 /* Allocate a block of memory. This is not freed until much later. */
4684 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4685 if (bl == NULL)
4686 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004687 bl->sb_next = spin->si_blocks;
4688 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004689 bl->sb_used = 0;
4690 }
4691
4692 p = bl->sb_data + bl->sb_used;
4693 bl->sb_used += len;
4694
4695 return p;
4696}
4697
4698/*
4699 * Make a copy of a string into memory allocated with getroom().
4700 */
4701 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004702getroom_save(spin, s)
4703 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004704 char_u *s;
4705{
4706 char_u *sc;
4707
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004708 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004709 if (sc != NULL)
4710 STRCPY(sc, s);
4711 return sc;
4712}
4713
4714
4715/*
4716 * Free the list of allocated sblock_T.
4717 */
4718 static void
4719free_blocks(bl)
4720 sblock_T *bl;
4721{
4722 sblock_T *next;
4723
4724 while (bl != NULL)
4725 {
4726 next = bl->sb_next;
4727 vim_free(bl);
4728 bl = next;
4729 }
4730}
4731
4732/*
4733 * Allocate the root of a word tree.
4734 */
4735 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004736wordtree_alloc(spin)
4737 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004738{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004739 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004740}
4741
4742/*
4743 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004744 * Always store it in the case-folded tree. For a keep-case word this is
4745 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4746 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004747 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004748 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004749 */
4750 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004751store_word(spin, word, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004752 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004753 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004754 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004755 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004756 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004757{
4758 int len = STRLEN(word);
4759 int ct = captype(word, word + len);
4760 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004761 int res = OK;
4762 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004765 for (p = pfxlist; res == OK; ++p)
4766 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004767 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4768 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004769 if (p == NULL || *p == NUL)
4770 break;
4771 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004772 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004773
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004774 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004775 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004776 for (p = pfxlist; res == OK; ++p)
4777 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004778 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4779 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004780 if (p == NULL || *p == NUL)
4781 break;
4782 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004783 ++spin->si_keepwcount;
4784 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004785 return res;
4786}
4787
4788/*
4789 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004790 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4791 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004792 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004793 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004794 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004795tree_add_word(spin, word, root, flags, region, prefixID)
4796 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004797 char_u *word;
4798 wordnode_T *root;
4799 int flags;
4800 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004801 int prefixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803 wordnode_T *node = root;
4804 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004805 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004806 wordnode_T **prev = NULL;
4807 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004808
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 /* Add each byte of the word to the tree, including the NUL at the end. */
4810 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004811 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004812 /* When there is more than one reference to this node we need to make
4813 * a copy, so that we can modify it. Copy the whole list of siblings
4814 * (we don't optimize for a partly shared list of siblings). */
4815 if (node != NULL && node->wn_refs > 1)
4816 {
4817 --node->wn_refs;
4818 copyprev = prev;
4819 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4820 {
4821 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004822 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004823 if (np == NULL)
4824 return FAIL;
4825 np->wn_child = copyp->wn_child;
4826 if (np->wn_child != NULL)
4827 ++np->wn_child->wn_refs; /* child gets extra ref */
4828 np->wn_byte = copyp->wn_byte;
4829 if (np->wn_byte == NUL)
4830 {
4831 np->wn_flags = copyp->wn_flags;
4832 np->wn_region = copyp->wn_region;
4833 np->wn_prefixID = copyp->wn_prefixID;
4834 }
4835
4836 /* Link the new node in the list, there will be one ref. */
4837 np->wn_refs = 1;
4838 *copyprev = np;
4839 copyprev = &np->wn_sibling;
4840
4841 /* Let "node" point to the head of the copied list. */
4842 if (copyp == node)
4843 node = np;
4844 }
4845 }
4846
Bram Moolenaar51485f02005-06-04 21:55:20 +00004847 /* Look for the sibling that has the same character. They are sorted
4848 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004849 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004850 * done on flags and then on prefixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004851 while (node != NULL
4852 && (node->wn_byte < word[i]
4853 || (node->wn_byte == NUL
4854 && (flags < 0
4855 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004856 : node->wn_flags < (flags & WN_MASK)
4857 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004858 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004859 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004860 prev = &node->wn_sibling;
4861 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004862 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004863 if (node == NULL
4864 || node->wn_byte != word[i]
4865 || (word[i] == NUL
4866 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004867 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004868 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004869 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004870 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004871 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004872 if (np == NULL)
4873 return FAIL;
4874 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004875
4876 /* If "node" is NULL this is a new child or the end of the sibling
4877 * list: ref count is one. Otherwise use ref count of sibling and
4878 * make ref count of sibling one (matters when inserting in front
4879 * of the list of siblings). */
4880 if (node == NULL)
4881 np->wn_refs = 1;
4882 else
4883 {
4884 np->wn_refs = node->wn_refs;
4885 node->wn_refs = 1;
4886 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004887 *prev = np;
4888 np->wn_sibling = node;
4889 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004890 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004891
Bram Moolenaar51485f02005-06-04 21:55:20 +00004892 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004893 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004894 node->wn_flags = flags;
4895 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004896 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004898 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004899 prev = &node->wn_child;
4900 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004901 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004902#ifdef SPELL_PRINTTREE
4903 smsg("Added \"%s\"", word);
4904 spell_print_tree(root->wn_sibling);
4905#endif
4906
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004907 /* count nr of words added since last message */
4908 ++spin->si_msg_count;
4909
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004910 /*
4911 * Every so many words compress the tree, so that we don't use too much
4912 * memory.
4913 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004914 i = FALSE;
4915 if (root == spin->si_foldroot)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004916 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004917 if (++spin->si_fold_added >= SPELL_COMPRESS_CNT)
4918 {
4919 i = TRUE;
4920 spin->si_fold_added = 0;
4921 }
4922 }
4923 else if (root == spin->si_keeproot)
4924 {
4925 if (++spin->si_keep_added >= SPELL_COMPRESS_CNT)
4926 {
4927 i = TRUE;
4928 spin->si_keep_added = 0;
4929 }
4930 }
4931 if (i)
4932 {
4933 if (spin->si_verbose)
4934 {
4935 msg_start();
4936 msg_puts((char_u *)_(msg_compressing));
4937 msg_clr_eos();
4938 msg_didout = FALSE;
4939 msg_col = 0;
4940 out_flush();
4941 }
4942 wordtree_compress(spin, root);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004943 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004944
4945 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004946}
4947
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004948/*
4949 * Get a wordnode_T, either from the list of previously freed nodes or
4950 * allocate a new one.
4951 */
4952 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004953get_wordnode(spin)
4954 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004955{
4956 wordnode_T *n;
4957
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004958 if (spin->si_first_free == NULL)
4959 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004960 else
4961 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004962 n = spin->si_first_free;
4963 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004964 vim_memset(n, 0, sizeof(wordnode_T));
4965 }
4966#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004967 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004968#endif
4969 return n;
4970}
4971
4972/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004973 * Decrement the reference count on a node (which is the head of a list of
4974 * siblings). If the reference count becomes zero free the node and its
4975 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004976 */
4977 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004978deref_wordnode(spin, node)
4979 spellinfo_T *spin;
4980 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004981{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004982 wordnode_T *np;
4983
4984 if (--node->wn_refs == 0)
4985 for (np = node; np != NULL; np = np->wn_sibling)
4986 {
4987 if (np->wn_child != NULL)
4988 deref_wordnode(spin, np->wn_child);
4989 free_wordnode(spin, np);
4990 }
4991}
4992
4993/*
4994 * Free a wordnode_T for re-use later.
4995 * Only the "wn_child" field becomes invalid.
4996 */
4997 static void
4998free_wordnode(spin, n)
4999 spellinfo_T *spin;
5000 wordnode_T *n;
5001{
5002 n->wn_child = spin->si_first_free;
5003 spin->si_first_free = n;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005004}
5005
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005007 * Compress a tree: find tails that are identical and can be shared.
5008 */
5009 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005010wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005011 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005012 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005013{
5014 hashtab_T ht;
5015 int n;
5016 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005017 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005018
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005019 /* Skip the root itself, it's not actually used. The first sibling is the
5020 * start of the tree. */
5021 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005022 {
5023 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005024 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005025
5026#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005027 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005028#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005029 {
5030 if (!spin->si_verbose)
5031 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005032 if (tot > 1000000)
5033 perc = (tot - n) / (tot / 100);
5034 else
5035 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005036 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005037 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005038 if (p_verbose > 2)
5039 verbose_leave();
5040 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005041#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005042 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005043#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044 hash_clear(&ht);
5045 }
5046}
5047
5048/*
5049 * Compress a node, its siblings and its children, depth first.
5050 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005051 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005052 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005053node_compress(spin, node, ht, tot)
5054 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005055 wordnode_T *node;
5056 hashtab_T *ht;
5057 int *tot; /* total count of nodes before compressing,
5058 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005059{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005060 wordnode_T *np;
5061 wordnode_T *tp;
5062 wordnode_T *child;
5063 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005064 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005065 int len = 0;
5066 unsigned nr, n;
5067 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068
Bram Moolenaar51485f02005-06-04 21:55:20 +00005069 /*
5070 * Go through the list of siblings. Compress each child and then try
5071 * finding an identical child to replace it.
5072 * Note that with "child" we mean not just the node that is pointed to,
5073 * but the whole list of siblings, of which the node is the first.
5074 */
5075 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005076 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005077 ++len;
5078 if ((child = np->wn_child) != NULL)
5079 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005080 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005081 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005082
5083 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005084 hash = hash_hash(child->wn_u1.hashkey);
5085 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005086 tp = NULL;
5087 if (!HASHITEM_EMPTY(hi))
5088 {
5089 /* There are children with an identical hash value. Now check
5090 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005091 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005092 if (node_equal(child, tp))
5093 {
5094 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005095 * current one. This means the current child and all
5096 * its siblings is unlinked from the tree. */
5097 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005098 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005099 np->wn_child = tp;
5100 ++compressed;
5101 break;
5102 }
5103 if (tp == NULL)
5104 {
5105 /* No other child with this hash value equals the child of
5106 * the node, add it to the linked list after the first
5107 * item. */
5108 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005109 child->wn_u2.next = tp->wn_u2.next;
5110 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005111 }
5112 }
5113 else
5114 /* No other child has this hash value, add it to the
5115 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005116 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005117 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005118 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005119 *tot += len;
5120
5121 /*
5122 * Make a hash key for the node and its siblings, so that we can quickly
5123 * find a lookalike node. This must be done after compressing the sibling
5124 * list, otherwise the hash key would become invalid by the compression.
5125 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005126 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005127 nr = 0;
5128 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005129 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005130 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005131 /* end node: use wn_flags, wn_region and wn_prefixID */
5132 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005133 else
5134 /* byte node: use the byte value and the child pointer */
5135 n = np->wn_byte + ((long_u)np->wn_child << 8);
5136 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005137 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005138
5139 /* Avoid NUL bytes, it terminates the hash key. */
5140 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005141 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005142 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005143 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005144 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005145 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005146 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005147 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
5148 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005149
5150 return compressed;
5151}
5152
5153/*
5154 * Return TRUE when two nodes have identical siblings and children.
5155 */
5156 static int
5157node_equal(n1, n2)
5158 wordnode_T *n1;
5159 wordnode_T *n2;
5160{
5161 wordnode_T *p1;
5162 wordnode_T *p2;
5163
5164 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
5165 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
5166 if (p1->wn_byte != p2->wn_byte
5167 || (p1->wn_byte == NUL
5168 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005169 || p1->wn_region != p2->wn_region
5170 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005171 : (p1->wn_child != p2->wn_child)))
5172 break;
5173
5174 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005175}
5176
5177/*
5178 * Write a number to file "fd", MSB first, in "len" bytes.
5179 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005180 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005181put_bytes(fd, nr, len)
5182 FILE *fd;
5183 long_u nr;
5184 int len;
5185{
5186 int i;
5187
5188 for (i = len - 1; i >= 0; --i)
5189 putc((int)(nr >> (i * 8)), fd);
5190}
5191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005192static int
5193#ifdef __BORLANDC__
5194_RTLENTRYF
5195#endif
5196rep_compare __ARGS((const void *s1, const void *s2));
5197
5198/*
5199 * Function given to qsort() to sort the REP items on "from" string.
5200 */
5201 static int
5202#ifdef __BORLANDC__
5203_RTLENTRYF
5204#endif
5205rep_compare(s1, s2)
5206 const void *s1;
5207 const void *s2;
5208{
5209 fromto_T *p1 = (fromto_T *)s1;
5210 fromto_T *p2 = (fromto_T *)s2;
5211
5212 return STRCMP(p1->ft_from, p2->ft_from);
5213}
5214
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005215/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005216 * Write the Vim spell file "fname".
5217 */
5218 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005219write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005220 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005221 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005222{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005223 FILE *fd;
5224 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005225 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005226 wordnode_T *tree;
5227 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 int i;
5229 int l;
5230 garray_T *gap;
5231 fromto_T *ftp;
5232 char_u *p;
5233 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005236 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005237 {
5238 EMSG2(_(e_notopen), fname);
5239 return;
5240 }
5241
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005242 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005243 * <charflagslen> <charflags>
5244 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005245 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005246 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005247
5248 /* <fileID> */
5249 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
5250 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251
5252 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005253 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005255 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
5256 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
5257 (size_t)1, fd);
5258 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 }
5260 else
5261 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005262 putc(0, fd);
5263 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005264 }
5265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 /*
5267 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005268 * <charflagslen> <charflags> <fcharlen> <fchars>
5269 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 * 'encoding'.
5271 * Also skip this for an .add.spl file, the main spell file must contain
5272 * the table (avoids that it conflicts). File is shorter too.
5273 */
5274 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005275 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005276 putc(0, fd);
5277 putc(0, fd);
5278 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005279 }
5280 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005281 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005282
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005283
5284 if (spin->si_midword == NULL)
5285 put_bytes(fd, 0L, 2); /* <midwordlen> */
5286 else
5287 {
5288 i = STRLEN(spin->si_midword);
5289 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
5290 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
5291 }
5292
5293
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005294 /* Write the prefix conditions. */
5295 write_spell_prefcond(fd, &spin->si_prefcond);
5296
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005297 /* <SUGGEST> : <repcount> <rep> ...
5298 * <salflags> <salcount> <sal> ...
5299 * <maplen> <mapstr> */
5300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 /* Sort the REP items. */
5302 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
5303 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005304
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005305 /* round 1: REP items
5306 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 for (round = 1; round <= 2; ++round)
5308 {
5309 if (round == 1)
5310 gap = &spin->si_rep;
5311 else
5312 {
5313 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 i = 0;
5316 if (spin->si_followup)
5317 i |= SAL_F0LLOWUP;
5318 if (spin->si_collapse)
5319 i |= SAL_COLLAPSE;
5320 if (spin->si_rem_accents)
5321 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005322 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5323 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005325 if (i & SAL_SOFO)
5326 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 }
5328
5329 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
5330 for (i = 0; i < gap->ga_len; ++i)
5331 {
5332 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
5333 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
5334 ftp = &((fromto_T *)gap->ga_data)[i];
5335 for (rr = 1; rr <= 2; ++rr)
5336 {
5337 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5338 l = STRLEN(p);
5339 putc(l, fd);
5340 fwrite(p, l, (size_t)1, fd);
5341 }
5342 }
5343 }
5344
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005345 /* SOFOFROM and SOFOTO */
5346 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5347 {
5348 put_bytes(fd, 1L, 2); /* <salcount> */
5349
5350 l = STRLEN(spin->si_sofofr);
5351 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5352 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5353
5354 l = STRLEN(spin->si_sofoto);
5355 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5356 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5357 }
5358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5360 if (spin->si_map.ga_len > 0) /* <mapstr> */
5361 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5362 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005363
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005364 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005365 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005366 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005367 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005368 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005369 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005370 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005371 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005372 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005373 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005374 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005375 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005376
Bram Moolenaar0c405862005-06-22 22:26:26 +00005377 /* Clear the index and wnode fields in the tree. */
5378 clear_node(tree);
5379
Bram Moolenaar51485f02005-06-04 21:55:20 +00005380 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005381 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005382 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005383 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005384
Bram Moolenaar51485f02005-06-04 21:55:20 +00005385 /* number of nodes in 4 bytes */
5386 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005387 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005388
Bram Moolenaar51485f02005-06-04 21:55:20 +00005389 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005390 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005391 }
5392
Bram Moolenaar51485f02005-06-04 21:55:20 +00005393 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005394}
5395
5396/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005397 * Clear the index and wnode fields of "node", it siblings and its
5398 * children. This is needed because they are a union with other items to save
5399 * space.
5400 */
5401 static void
5402clear_node(node)
5403 wordnode_T *node;
5404{
5405 wordnode_T *np;
5406
5407 if (node != NULL)
5408 for (np = node; np != NULL; np = np->wn_sibling)
5409 {
5410 np->wn_u1.index = 0;
5411 np->wn_u2.wnode = NULL;
5412
5413 if (np->wn_byte != NUL)
5414 clear_node(np->wn_child);
5415 }
5416}
5417
5418
5419/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005420 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005421 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005422 * This first writes the list of possible bytes (siblings). Then for each
5423 * byte recursively write the children.
5424 *
5425 * NOTE: The code here must match the code in read_tree(), since assumptions
5426 * are made about the indexes (so that we don't have to write them in the
5427 * file).
5428 *
5429 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005430 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005431 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005432put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005433 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005434 wordnode_T *node;
5435 int index;
5436 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005437 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005438{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005439 int newindex = index;
5440 int siblingcount = 0;
5441 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005442 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005443
Bram Moolenaar51485f02005-06-04 21:55:20 +00005444 /* If "node" is zero the tree is empty. */
5445 if (node == NULL)
5446 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005447
Bram Moolenaar51485f02005-06-04 21:55:20 +00005448 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005449 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005450
5451 /* Count the number of siblings. */
5452 for (np = node; np != NULL; np = np->wn_sibling)
5453 ++siblingcount;
5454
5455 /* Write the sibling count. */
5456 if (fd != NULL)
5457 putc(siblingcount, fd); /* <siblingcount> */
5458
5459 /* Write each sibling byte and optionally extra info. */
5460 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005461 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005462 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005463 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005464 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005465 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005466 /* For a NUL byte (end of word) write the flags etc. */
5467 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005468 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005469 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005470 * associated condition nr (stored in wn_region). The
5471 * byte value is misused to store the "rare" and "not
5472 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005473 if (np->wn_flags == (short_u)PFX_FLAGS)
5474 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005475 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005476 {
5477 putc(BY_FLAGS, fd); /* <byte> */
5478 putc(np->wn_flags, fd); /* <pflags> */
5479 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005480 putc(np->wn_prefixID, fd); /* <prefixID> */
5481 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005482 }
5483 else
5484 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005485 /* For word trees we write the flag/region items. */
5486 flags = np->wn_flags;
5487 if (regionmask != 0 && np->wn_region != regionmask)
5488 flags |= WF_REGION;
5489 if (np->wn_prefixID != 0)
5490 flags |= WF_PFX;
5491 if (flags == 0)
5492 {
5493 /* word without flags or region */
5494 putc(BY_NOFLAGS, fd); /* <byte> */
5495 }
5496 else
5497 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005498 if (np->wn_flags >= 0x100)
5499 {
5500 putc(BY_FLAGS2, fd); /* <byte> */
5501 putc(flags, fd); /* <flags> */
5502 putc((unsigned)flags >> 8, fd); /* <flags2> */
5503 }
5504 else
5505 {
5506 putc(BY_FLAGS, fd); /* <byte> */
5507 putc(flags, fd); /* <flags> */
5508 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005509 if (flags & WF_REGION)
5510 putc(np->wn_region, fd); /* <region> */
5511 if (flags & WF_PFX)
5512 putc(np->wn_prefixID, fd); /* <prefixID> */
5513 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005514 }
5515 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005516 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005517 else
5518 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005519 if (np->wn_child->wn_u1.index != 0
5520 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005521 {
5522 /* The child is written elsewhere, write the reference. */
5523 if (fd != NULL)
5524 {
5525 putc(BY_INDEX, fd); /* <byte> */
5526 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005527 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005528 }
5529 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005530 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005531 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005532 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005533
Bram Moolenaar51485f02005-06-04 21:55:20 +00005534 if (fd != NULL)
5535 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5536 {
5537 EMSG(_(e_write));
5538 return 0;
5539 }
5540 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005542
5543 /* Space used in the array when reading: one for each sibling and one for
5544 * the count. */
5545 newindex += siblingcount + 1;
5546
5547 /* Recursively dump the children of each sibling. */
5548 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005549 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5550 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005551 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005552
5553 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005554}
5555
5556
5557/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005558 * ":mkspell [-ascii] outfile infile ..."
5559 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005560 */
5561 void
5562ex_mkspell(eap)
5563 exarg_T *eap;
5564{
5565 int fcount;
5566 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005567 char_u *arg = eap->arg;
5568 int ascii = FALSE;
5569
5570 if (STRNCMP(arg, "-ascii", 6) == 0)
5571 {
5572 ascii = TRUE;
5573 arg = skipwhite(arg + 6);
5574 }
5575
5576 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5577 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005579 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005580 FreeWild(fcount, fnames);
5581 }
5582}
5583
5584/*
5585 * Create a Vim spell file from one or more word lists.
5586 * "fnames[0]" is the output file name.
5587 * "fnames[fcount - 1]" is the last input file name.
5588 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5589 * and ".spl" is appended to make the output file name.
5590 */
5591 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005592mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005593 int fcount;
5594 char_u **fnames;
5595 int ascii; /* -ascii argument given */
5596 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005597 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005598{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005599 char_u fname[MAXPATHL];
5600 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005601 char_u **innames;
5602 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005603 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005604 int i;
5605 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005606 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005607 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005608 spellinfo_T spin;
5609
5610 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005611 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005612 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005613 spin.si_followup = TRUE;
5614 spin.si_rem_accents = TRUE;
5615 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5616 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5617 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005618 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005619
Bram Moolenaarb765d632005-06-07 21:00:02 +00005620 /* default: fnames[0] is output file, following are input files */
5621 innames = &fnames[1];
5622 incount = fcount - 1;
5623
5624 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005625 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005626 len = STRLEN(fnames[0]);
5627 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5628 {
5629 /* For ":mkspell path/en.latin1.add" output file is
5630 * "path/en.latin1.add.spl". */
5631 innames = &fnames[0];
5632 incount = 1;
5633 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5634 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005635 else if (fcount == 1)
5636 {
5637 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5638 innames = &fnames[0];
5639 incount = 1;
5640 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5641 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5642 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005643 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5644 {
5645 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005646 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005647 }
5648 else
5649 /* Name should be language, make the file name from it. */
5650 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5651 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5652
5653 /* Check for .ascii.spl. */
5654 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5655 spin.si_ascii = TRUE;
5656
5657 /* Check for .add.spl. */
5658 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5659 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005660 }
5661
Bram Moolenaarb765d632005-06-07 21:00:02 +00005662 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005663 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005664 else if (vim_strchr(gettail(wfname), '_') != NULL)
5665 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005666 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005667 EMSG(_("E754: Only up to 8 regions supported"));
5668 else
5669 {
5670 /* Check for overwriting before doing things that may take a lot of
5671 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005672 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005673 {
5674 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005675 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005676 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005677 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005678 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005679 EMSG2(_(e_isadir2), wfname);
5680 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005681 }
5682
5683 /*
5684 * Init the aff and dic pointers.
5685 * Get the region names if there are more than 2 arguments.
5686 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005687 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005689 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005690
Bram Moolenaar3982c542005-06-08 21:56:31 +00005691 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005692 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005693 len = STRLEN(innames[i]);
5694 if (STRLEN(gettail(innames[i])) < 5
5695 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005697 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5698 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005699 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005700 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5701 spin.si_region_name[i * 2 + 1] =
5702 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703 }
5704 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005705 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005706
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005707 spin.si_foldroot = wordtree_alloc(&spin);
5708 spin.si_keeproot = wordtree_alloc(&spin);
5709 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005710 if (spin.si_foldroot == NULL
5711 || spin.si_keeproot == NULL
5712 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005713 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005714 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005715 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005716 }
5717
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005718 /* When not producing a .add.spl file clear the character table when
5719 * we encounter one in the .aff file. This means we dump the current
5720 * one in the .spl file if the .aff file doesn't define one. That's
5721 * better than guessing the contents, the table will match a
5722 * previously loaded spell file. */
5723 if (!spin.si_add)
5724 spin.si_clear_chartab = TRUE;
5725
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005726 /*
5727 * Read all the .aff and .dic files.
5728 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005729 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005730 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005731 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005732 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005733 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005734 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005735
Bram Moolenaarb765d632005-06-07 21:00:02 +00005736 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005737 if (mch_stat((char *)fname, &st) >= 0)
5738 {
5739 /* Read the .aff file. Will init "spin->si_conv" based on the
5740 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005741 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005742 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005743 error = TRUE;
5744 else
5745 {
5746 /* Read the .dic file and store the words in the trees. */
5747 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005748 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005749 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005750 error = TRUE;
5751 }
5752 }
5753 else
5754 {
5755 /* No .aff file, try reading the file as a word list. Store
5756 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005757 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005758 error = TRUE;
5759 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005760
Bram Moolenaarb765d632005-06-07 21:00:02 +00005761#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005762 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005763 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005764#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005765 }
5766
Bram Moolenaar51485f02005-06-04 21:55:20 +00005767 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005768 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005769 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005770 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005771 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005772 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005773 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005774 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005775 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005776 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005777 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005778 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005779 verbose_leave();
5780 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005781 wordtree_compress(&spin, spin.si_foldroot);
5782 wordtree_compress(&spin, spin.si_keeproot);
5783 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005784 }
5785
Bram Moolenaar51485f02005-06-04 21:55:20 +00005786 if (!error)
5787 {
5788 /*
5789 * Write the info in the spell file.
5790 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005791 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005792 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005793 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005794 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005795 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005796 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005797 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005798 verbose_leave();
5799 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005800
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005801 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005802
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005803 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005804 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005805 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005806 verbose_enter();
5807 MSG(_("Done!"));
5808 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005809 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005810 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005811 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005812 verbose_leave();
5813 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005814
Bram Moolenaarb765d632005-06-07 21:00:02 +00005815 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005816 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005817 }
5818
5819 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005820 ga_clear(&spin.si_rep);
5821 ga_clear(&spin.si_sal);
5822 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005823 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005824 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005825 vim_free(spin.si_sofofr);
5826 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005827
5828 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005829 for (i = 0; i < incount; ++i)
5830 if (afile[i] != NULL)
5831 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005832
5833 /* Free all the bits and pieces at once. */
5834 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005835 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005836}
5837
Bram Moolenaarb765d632005-06-07 21:00:02 +00005838
5839/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005840 * ":[count]spellgood {word}"
5841 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005842 */
5843 void
5844ex_spell(eap)
5845 exarg_T *eap;
5846{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005847 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005848 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005849}
5850
5851/*
5852 * Add "word[len]" to 'spellfile' as a good or bad word.
5853 */
5854 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005855spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005856 char_u *word;
5857 int len;
5858 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005859 int index; /* "zG" and "zW": zero, otherwise index in
5860 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005861{
5862 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005863 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005864 int new_spf = FALSE;
5865 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005866 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005867 char_u fnamebuf[MAXPATHL];
5868 char_u line[MAXWLEN * 2];
5869 long fpos, fpos_next = 0;
5870 int i;
5871 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005872
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005873 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005874 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005875 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005876 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005877 int_wordlist = vim_tempname('s');
5878 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005879 return;
5880 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005881 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005882 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005883 else
5884 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005885 /* If 'spellfile' isn't set figure out a good default value. */
5886 if (*curbuf->b_p_spf == NUL)
5887 {
5888 init_spellfile();
5889 new_spf = TRUE;
5890 }
5891
5892 if (*curbuf->b_p_spf == NUL)
5893 {
5894 EMSG(_("E764: 'spellfile' is not set"));
5895 return;
5896 }
5897
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005898 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5899 {
5900 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5901 if (i == index)
5902 break;
5903 if (*spf == NUL)
5904 {
5905 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5906 return;
5907 }
5908 }
5909
Bram Moolenaarb765d632005-06-07 21:00:02 +00005910 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005911 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005912 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5913 buf = NULL;
5914 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005915 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005916 EMSG(_(e_bufloaded));
5917 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005918 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005919
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005920 fname = fnamebuf;
5921 }
5922
5923 if (bad)
5924 {
5925 /* When the word also appears as good word we need to remove that one,
5926 * since its flags sort before the one with WF_BANNED. */
5927 fd = mch_fopen((char *)fname, "r");
5928 if (fd != NULL)
5929 {
5930 while (!vim_fgets(line, MAXWLEN * 2, fd))
5931 {
5932 fpos = fpos_next;
5933 fpos_next = ftell(fd);
5934 if (STRNCMP(word, line, len) == 0
5935 && (line[len] == '/' || line[len] < ' '))
5936 {
5937 /* Found duplicate word. Remove it by writing a '#' at
5938 * the start of the line. Mixing reading and writing
5939 * doesn't work for all systems, close the file first. */
5940 fclose(fd);
5941 fd = mch_fopen((char *)fname, "r+");
5942 if (fd == NULL)
5943 break;
5944 if (fseek(fd, fpos, SEEK_SET) == 0)
5945 fputc('#', fd);
5946 fseek(fd, fpos_next, SEEK_SET);
5947 }
5948 }
5949 fclose(fd);
5950 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005951 }
5952
5953 fd = mch_fopen((char *)fname, "a");
5954 if (fd == NULL && new_spf)
5955 {
5956 /* We just initialized the 'spellfile' option and can't open the file.
5957 * We may need to create the "spell" directory first. We already
5958 * checked the runtime directory is writable in init_spellfile(). */
5959 STRCPY(NameBuff, fname);
5960 *gettail_sep(NameBuff) = NUL;
5961 if (mch_stat((char *)NameBuff, &st) < 0)
5962 {
5963 /* The directory doesn't exist. Try creating it and opening the
5964 * file again. */
5965 vim_mkdir(NameBuff, 0755);
5966 fd = mch_fopen((char *)fname, "a");
5967 }
5968 }
5969
5970 if (fd == NULL)
5971 EMSG2(_(e_notopen), fname);
5972 else
5973 {
5974 if (bad)
5975 fprintf(fd, "%.*s/!\n", len, word);
5976 else
5977 fprintf(fd, "%.*s\n", len, word);
5978 fclose(fd);
5979
5980 /* Update the .add.spl file. */
5981 mkspell(1, &fname, FALSE, TRUE, TRUE);
5982
5983 /* If the .add file is edited somewhere, reload it. */
5984 if (buf != NULL)
5985 buf_reload(buf);
5986
5987 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005988 }
5989}
5990
5991/*
5992 * Initialize 'spellfile' for the current buffer.
5993 */
5994 static void
5995init_spellfile()
5996{
5997 char_u buf[MAXPATHL];
5998 int l;
5999 slang_T *sl;
6000 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006001 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006002
6003 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
6004 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006005 /* Find the end of the language name. Exclude the region. */
6006 for (lend = curbuf->b_p_spl; *lend != NUL
6007 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6008 ;
6009
6010 /* Loop over all entries in 'runtimepath'. Use the first one where we
6011 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006012 rtp = p_rtp;
6013 while (*rtp != NUL)
6014 {
6015 /* Copy the path from 'runtimepath' to buf[]. */
6016 copy_option_part(&rtp, buf, MAXPATHL, ",");
6017 if (filewritable(buf) == 2)
6018 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00006019 /* Use the first language name from 'spelllang' and the
6020 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006021 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
6022 l = STRLEN(buf);
6023 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00006024 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006025 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00006026 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
6027 ? (char_u *)"ascii" : spell_enc());
6028 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6029 break;
6030 }
6031 }
6032 }
6033}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006034
Bram Moolenaar51485f02005-06-04 21:55:20 +00006035
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006036/*
6037 * Init the chartab used for spelling for ASCII.
6038 * EBCDIC is not supported!
6039 */
6040 static void
6041clear_spell_chartab(sp)
6042 spelltab_T *sp;
6043{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006044 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006045
6046 /* Init everything to FALSE. */
6047 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
6048 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
6049 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006050 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006051 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006052 sp->st_upper[i] = i;
6053 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006054
6055 /* We include digits. A word shouldn't start with a digit, but handling
6056 * that is done separately. */
6057 for (i = '0'; i <= '9'; ++i)
6058 sp->st_isw[i] = TRUE;
6059 for (i = 'A'; i <= 'Z'; ++i)
6060 {
6061 sp->st_isw[i] = TRUE;
6062 sp->st_isu[i] = TRUE;
6063 sp->st_fold[i] = i + 0x20;
6064 }
6065 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006066 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006067 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006068 sp->st_upper[i] = i - 0x20;
6069 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006070}
6071
6072/*
6073 * Init the chartab used for spelling. Only depends on 'encoding'.
6074 * Called once while starting up and when 'encoding' changes.
6075 * The default is to use isalpha(), but the spell file should define the word
6076 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006077 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006078 */
6079 void
6080init_spell_chartab()
6081{
6082 int i;
6083
6084 did_set_spelltab = FALSE;
6085 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006086#ifdef FEAT_MBYTE
6087 if (enc_dbcs)
6088 {
6089 /* DBCS: assume double-wide characters are word characters. */
6090 for (i = 128; i <= 255; ++i)
6091 if (MB_BYTE2LEN(i) == 2)
6092 spelltab.st_isw[i] = TRUE;
6093 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006094 else if (enc_utf8)
6095 {
6096 for (i = 128; i < 256; ++i)
6097 {
6098 spelltab.st_isu[i] = utf_isupper(i);
6099 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
6100 spelltab.st_fold[i] = utf_fold(i);
6101 spelltab.st_upper[i] = utf_toupper(i);
6102 }
6103 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006104 else
6105#endif
6106 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006107 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006108 for (i = 128; i < 256; ++i)
6109 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006110 if (MB_ISUPPER(i))
6111 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006112 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006113 spelltab.st_isu[i] = TRUE;
6114 spelltab.st_fold[i] = MB_TOLOWER(i);
6115 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006116 else if (MB_ISLOWER(i))
6117 {
6118 spelltab.st_isw[i] = TRUE;
6119 spelltab.st_upper[i] = MB_TOUPPER(i);
6120 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006121 }
6122 }
6123}
6124
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006125static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
6126static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
6127
6128/*
6129 * Set the spell character tables from strings in the affix file.
6130 */
6131 static int
6132set_spell_chartab(fol, low, upp)
6133 char_u *fol;
6134 char_u *low;
6135 char_u *upp;
6136{
6137 /* We build the new tables here first, so that we can compare with the
6138 * previous one. */
6139 spelltab_T new_st;
6140 char_u *pf = fol, *pl = low, *pu = upp;
6141 int f, l, u;
6142
6143 clear_spell_chartab(&new_st);
6144
6145 while (*pf != NUL)
6146 {
6147 if (*pl == NUL || *pu == NUL)
6148 {
6149 EMSG(_(e_affform));
6150 return FAIL;
6151 }
6152#ifdef FEAT_MBYTE
6153 f = mb_ptr2char_adv(&pf);
6154 l = mb_ptr2char_adv(&pl);
6155 u = mb_ptr2char_adv(&pu);
6156#else
6157 f = *pf++;
6158 l = *pl++;
6159 u = *pu++;
6160#endif
6161 /* Every character that appears is a word character. */
6162 if (f < 256)
6163 new_st.st_isw[f] = TRUE;
6164 if (l < 256)
6165 new_st.st_isw[l] = TRUE;
6166 if (u < 256)
6167 new_st.st_isw[u] = TRUE;
6168
6169 /* if "LOW" and "FOL" are not the same the "LOW" char needs
6170 * case-folding */
6171 if (l < 256 && l != f)
6172 {
6173 if (f >= 256)
6174 {
6175 EMSG(_(e_affrange));
6176 return FAIL;
6177 }
6178 new_st.st_fold[l] = f;
6179 }
6180
6181 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006182 * case-folding, it's upper case and the "UPP" is the upper case of
6183 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006184 if (u < 256 && u != f)
6185 {
6186 if (f >= 256)
6187 {
6188 EMSG(_(e_affrange));
6189 return FAIL;
6190 }
6191 new_st.st_fold[u] = f;
6192 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006193 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006194 }
6195 }
6196
6197 if (*pl != NUL || *pu != NUL)
6198 {
6199 EMSG(_(e_affform));
6200 return FAIL;
6201 }
6202
6203 return set_spell_finish(&new_st);
6204}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006205
6206/*
6207 * Set the spell character tables from strings in the .spl file.
6208 */
6209 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006210set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006211 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006212 int cnt; /* length of "flags" */
6213 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006214{
6215 /* We build the new tables here first, so that we can compare with the
6216 * previous one. */
6217 spelltab_T new_st;
6218 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006219 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006220 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006221
6222 clear_spell_chartab(&new_st);
6223
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006224 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006225 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006226 if (i < cnt)
6227 {
6228 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6229 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6230 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006231
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006232 if (*p != NUL)
6233 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006234#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006235 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006236#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006237 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006238#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006239 new_st.st_fold[i + 128] = c;
6240 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6241 new_st.st_upper[c] = i + 128;
6242 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006243 }
6244
6245 return set_spell_finish(&new_st);
6246}
6247
6248 static int
6249set_spell_finish(new_st)
6250 spelltab_T *new_st;
6251{
6252 int i;
6253
6254 if (did_set_spelltab)
6255 {
6256 /* check that it's the same table */
6257 for (i = 0; i < 256; ++i)
6258 {
6259 if (spelltab.st_isw[i] != new_st->st_isw[i]
6260 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006261 || spelltab.st_fold[i] != new_st->st_fold[i]
6262 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006263 {
6264 EMSG(_("E763: Word characters differ between spell files"));
6265 return FAIL;
6266 }
6267 }
6268 }
6269 else
6270 {
6271 /* copy the new spelltab into the one being used */
6272 spelltab = *new_st;
6273 did_set_spelltab = TRUE;
6274 }
6275
6276 return OK;
6277}
6278
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006279/*
Bram Moolenaarea408852005-06-25 22:49:46 +00006280 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006281 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00006282 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006283 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00006284 */
6285 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006286spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00006287 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006288 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00006289{
Bram Moolenaarea408852005-06-25 22:49:46 +00006290#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006291 char_u *s;
6292 int l;
6293 int c;
6294
6295 if (has_mbyte)
6296 {
6297 l = MB_BYTE2LEN(*p);
6298 s = p;
6299 if (l == 1)
6300 {
6301 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006302 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006303 {
6304 s = p + 1; /* skip a mid-word character */
6305 l = MB_BYTE2LEN(*s);
6306 }
6307 }
6308 else
6309 {
6310 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006311 if (c < 256 ? buf->b_spell_ismw[c]
6312 : (buf->b_spell_ismw_mb != NULL
6313 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006314 {
6315 s = p + l;
6316 l = MB_BYTE2LEN(*s);
6317 }
6318 }
6319
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006320 c = mb_ptr2char(s);
6321 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006322 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006323 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006324 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006325#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006326
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006327 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
6328}
6329
6330/*
6331 * Return TRUE if "p" points to a word character.
6332 * Unlike spell_iswordp() this doesn't check for "midword" characters.
6333 */
6334 static int
6335spell_iswordp_nmw(p)
6336 char_u *p;
6337{
6338#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006339 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006340
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006341 if (has_mbyte)
6342 {
6343 c = mb_ptr2char(p);
6344 if (c > 255)
6345 return mb_get_class(p) >= 2;
6346 return spelltab.st_isw[c];
6347 }
6348#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006349 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006350}
6351
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006352#ifdef FEAT_MBYTE
6353/*
6354 * Return TRUE if "p" points to a word character.
6355 * Wide version of spell_iswordp().
6356 */
6357 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006358spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006359 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006360 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006361{
6362 int *s;
6363
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006364 if (*p < 256 ? buf->b_spell_ismw[*p]
6365 : (buf->b_spell_ismw_mb != NULL
6366 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006367 s = p + 1;
6368 else
6369 s = p;
6370
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006371 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006372 {
6373 if (enc_utf8)
6374 return utf_class(*s) >= 2;
6375 if (enc_dbcs)
6376 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6377 return 0;
6378 }
6379 return spelltab.st_isw[*s];
6380}
6381#endif
6382
Bram Moolenaarea408852005-06-25 22:49:46 +00006383/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006384 * Write the table with prefix conditions to the .spl file.
6385 */
6386 static void
6387write_spell_prefcond(fd, gap)
6388 FILE *fd;
6389 garray_T *gap;
6390{
6391 int i;
6392 char_u *p;
6393 int len;
6394
6395 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6396
6397 for (i = 0; i < gap->ga_len; ++i)
6398 {
6399 /* <prefcond> : <condlen> <condstr> */
6400 p = ((char_u **)gap->ga_data)[i];
6401 if (p == NULL)
6402 fputc(0, fd);
6403 else
6404 {
6405 len = STRLEN(p);
6406 fputc(len, fd);
6407 fwrite(p, (size_t)len, (size_t)1, fd);
6408 }
6409 }
6410}
6411
6412/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006413 * Write the current tables into the .spl file.
6414 * This makes sure the same characters are recognized as word characters when
6415 * generating an when using a spell file.
6416 */
6417 static void
6418write_spell_chartab(fd)
6419 FILE *fd;
6420{
6421 char_u charbuf[256 * 4];
6422 int len = 0;
6423 int flags;
6424 int i;
6425
6426 fputc(128, fd); /* <charflagslen> */
6427 for (i = 128; i < 256; ++i)
6428 {
6429 flags = 0;
6430 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006431 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006432 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006433 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006434 fputc(flags, fd); /* <charflags> */
6435
Bram Moolenaarb765d632005-06-07 21:00:02 +00006436#ifdef FEAT_MBYTE
6437 if (has_mbyte)
6438 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6439 else
6440#endif
6441 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006442 }
6443
6444 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6445 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6446}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006447
6448/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006449 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6450 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006451 * When using a multi-byte 'encoding' the length may change!
6452 * Returns FAIL when something wrong.
6453 */
6454 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006455spell_casefold(str, len, buf, buflen)
6456 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006457 int len;
6458 char_u *buf;
6459 int buflen;
6460{
6461 int i;
6462
6463 if (len >= buflen)
6464 {
6465 buf[0] = NUL;
6466 return FAIL; /* result will not fit */
6467 }
6468
6469#ifdef FEAT_MBYTE
6470 if (has_mbyte)
6471 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006472 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006473 char_u *p;
6474 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006475
6476 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006477 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006478 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006479 if (outi + MB_MAXBYTES > buflen)
6480 {
6481 buf[outi] = NUL;
6482 return FAIL;
6483 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006484 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006485 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006486 }
6487 buf[outi] = NUL;
6488 }
6489 else
6490#endif
6491 {
6492 /* Be quick for non-multibyte encodings. */
6493 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006494 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006495 buf[i] = NUL;
6496 }
6497
6498 return OK;
6499}
6500
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006501#define SPS_BEST 1
6502#define SPS_FAST 2
6503#define SPS_DOUBLE 4
6504
6505static int sps_flags = SPS_BEST;
6506
6507/*
6508 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6509 * Sets "sps_flags".
6510 */
6511 int
6512spell_check_sps()
6513{
6514 char_u *p;
6515 char_u buf[MAXPATHL];
6516 int f;
6517
6518 sps_flags = 0;
6519
6520 for (p = p_sps; *p != NUL; )
6521 {
6522 copy_option_part(&p, buf, MAXPATHL, ",");
6523
6524 f = 0;
6525 if (STRCMP(buf, "best") == 0)
6526 f = SPS_BEST;
6527 else if (STRCMP(buf, "fast") == 0)
6528 f = SPS_FAST;
6529 else if (STRCMP(buf, "double") == 0)
6530 f = SPS_DOUBLE;
6531 else if (STRNCMP(buf, "expr:", 5) != 0
6532 && STRNCMP(buf, "file:", 5) != 0)
6533 f = -1;
6534
6535 if (f == -1 || (sps_flags != 0 && f != 0))
6536 {
6537 sps_flags = SPS_BEST;
6538 return FAIL;
6539 }
6540 if (f != 0)
6541 sps_flags = f;
6542 }
6543
6544 if (sps_flags == 0)
6545 sps_flags = SPS_BEST;
6546
6547 return OK;
6548}
6549
6550/* Remember what "z?" replaced. */
6551static char_u *repl_from = NULL;
6552static char_u *repl_to = NULL;
6553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006554/*
6555 * "z?": Find badly spelled word under or after the cursor.
6556 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006557 */
6558 void
6559spell_suggest()
6560{
6561 char_u *line;
6562 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006563 char_u wcopy[MAXWLEN + 2];
6564 char_u *p;
6565 int i;
6566 int c;
6567 suginfo_T sug;
6568 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006569 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006570 int need_cap;
6571 regmatch_T regmatch;
6572 int endcol;
6573 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006574
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006575 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006576 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6577 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006578 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006579 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6580 return;
6581
6582 /* No bad word or it starts after the cursor: use the word under the
6583 * cursor. */
6584 curwin->w_cursor = prev_cursor;
6585 line = ml_get_curline();
6586 p = line + curwin->w_cursor.col;
6587 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006588 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006589 mb_ptr_back(line, p);
6590 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006591 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006592 mb_ptr_adv(p);
6593
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006594 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006595 {
6596 beep_flush();
6597 return;
6598 }
6599 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006600 }
6601
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006602 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006603 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006604
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006605 /* Figure out if the word should be capitalised. */
6606 need_cap = FALSE;
6607 if (curbuf->b_cap_prog != NULL)
6608 {
6609 endcol = 0;
Bram Moolenaar97409f12005-07-08 22:17:29 +00006610 if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006611 {
6612 /* At start of line, check if previous line is empty or sentence
6613 * ends there. */
6614 if (curwin->w_cursor.lnum == 1)
6615 need_cap = TRUE;
6616 else
6617 {
6618 line = ml_get(curwin->w_cursor.lnum - 1);
6619 if (*skipwhite(line) == NUL)
6620 need_cap = TRUE;
6621 else
6622 {
6623 /* Append a space in place of the line break. */
6624 line_copy = concat_str(line, (char_u *)" ");
6625 line = line_copy;
6626 endcol = STRLEN(line);
6627 }
6628 }
6629 }
6630 else
6631 endcol = curwin->w_cursor.col;
6632
6633 if (endcol > 0)
6634 {
6635 /* Check if sentence ends before the bad word. */
6636 regmatch.regprog = curbuf->b_cap_prog;
6637 regmatch.rm_ic = FALSE;
6638 p = line + endcol;
6639 for (;;)
6640 {
6641 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006642 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006643 break;
6644 if (vim_regexec(&regmatch, p, 0)
6645 && regmatch.endp[0] == line + endcol)
6646 {
6647 need_cap = TRUE;
6648 break;
6649 }
6650 }
6651 }
6652
6653 /* get the line again, we may have been using the previous one */
6654 line = ml_get_curline();
6655 vim_free(line_copy);
6656 }
6657
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006658 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006659 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6660 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006661
6662 if (sug.su_ga.ga_len == 0)
6663 MSG(_("Sorry, no suggestions"));
6664 else
6665 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006666 vim_free(repl_from);
6667 repl_from = NULL;
6668 vim_free(repl_to);
6669 repl_to = NULL;
6670
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006671#ifdef FEAT_RIGHTLEFT
6672 /* When 'rightleft' is set the list is drawn right-left. */
6673 cmdmsg_rl = curwin->w_p_rl;
6674 if (cmdmsg_rl)
6675 msg_col = Columns - 1;
6676#endif
6677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006678 /* List the suggestions. */
6679 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006680 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006681 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6682 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683#ifdef FEAT_RIGHTLEFT
6684 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
6685 {
6686 /* And now the rabbit from the high hat: Avoid showing the
6687 * untranslated message rightleft. */
6688 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
6689 sug.su_badlen, sug.su_badptr);
6690 }
6691#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006692 msg_puts(IObuff);
6693 msg_clr_eos();
6694 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006696 msg_scroll = TRUE;
6697 for (i = 0; i < sug.su_ga.ga_len; ++i)
6698 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006699 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006700
6701 /* The suggested word may replace only part of the bad word, add
6702 * the not replaced part. */
6703 STRCPY(wcopy, stp->st_word);
6704 if (sug.su_badlen > stp->st_orglen)
6705 vim_strncpy(wcopy + STRLEN(wcopy),
6706 sug.su_badptr + stp->st_orglen,
6707 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006708 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
6709#ifdef FEAT_RIGHTLEFT
6710 if (cmdmsg_rl)
6711 rl_mirror(IObuff);
6712#endif
6713 msg_puts(IObuff);
6714
6715 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006716 msg_puts(IObuff);
6717
6718 /* The word may replace more than "su_badlen". */
6719 if (sug.su_badlen < stp->st_orglen)
6720 {
6721 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6722 stp->st_orglen, sug.su_badptr);
6723 msg_puts(IObuff);
6724 }
6725
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006726 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006727 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006728 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006729 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006730 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006731 stp->st_salscore ? "s " : "",
6732 stp->st_score, stp->st_altscore);
6733 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006734 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00006735 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006736#ifdef FEAT_RIGHTLEFT
6737 if (cmdmsg_rl)
6738 /* Mirror the numbers, but keep the leading space. */
6739 rl_mirror(IObuff + 1);
6740#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00006741 msg_advance(30);
6742 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006744 msg_putchar('\n');
6745 }
6746
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006747#ifdef FEAT_RIGHTLEFT
6748 cmdmsg_rl = FALSE;
6749 msg_col = 0;
6750#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006751 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006752 i = prompt_for_number(&mouse_used);
6753 if (mouse_used)
6754 i -= lines_left;
6755
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006756 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006757 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006758 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006759 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006760 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6761 repl_to = vim_strsave(stp->st_word);
6762
6763 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006764 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6765 if (p != NULL)
6766 {
6767 c = sug.su_badptr - line;
6768 mch_memmove(p, line, c);
6769 STRCPY(p + c, stp->st_word);
6770 STRCAT(p, sug.su_badptr + stp->st_orglen);
6771 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6772 curwin->w_cursor.col = c;
6773 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006774
6775 /* For redo we use a change-word command. */
6776 ResetRedobuff();
6777 AppendToRedobuff((char_u *)"ciw");
6778 AppendToRedobuff(stp->st_word);
6779 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006780 }
6781 }
6782 else
6783 curwin->w_cursor = prev_cursor;
6784 }
6785
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006786 spell_find_cleanup(&sug);
6787}
6788
6789/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006790 * ":spellrepall"
6791 */
6792/*ARGSUSED*/
6793 void
6794ex_spellrepall(eap)
6795 exarg_T *eap;
6796{
6797 pos_T pos = curwin->w_cursor;
6798 char_u *frompat;
6799 int addlen;
6800 char_u *line;
6801 char_u *p;
6802 int didone = FALSE;
6803 int save_ws = p_ws;
6804
6805 if (repl_from == NULL || repl_to == NULL)
6806 {
6807 EMSG(_("E752: No previous spell replacement"));
6808 return;
6809 }
6810 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6811
6812 frompat = alloc(STRLEN(repl_from) + 7);
6813 if (frompat == NULL)
6814 return;
6815 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6816 p_ws = FALSE;
6817
6818 curwin->w_cursor.lnum = 0;
6819 while (!got_int)
6820 {
6821 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6822 || u_save_cursor() == FAIL)
6823 break;
6824
6825 /* Only replace when the right word isn't there yet. This happens
6826 * when changing "etc" to "etc.". */
6827 line = ml_get_curline();
6828 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6829 repl_to, STRLEN(repl_to)) != 0)
6830 {
6831 p = alloc(STRLEN(line) + addlen + 1);
6832 if (p == NULL)
6833 break;
6834 mch_memmove(p, line, curwin->w_cursor.col);
6835 STRCPY(p + curwin->w_cursor.col, repl_to);
6836 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6837 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6838 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6839 didone = TRUE;
6840 }
6841 curwin->w_cursor.col += STRLEN(repl_to);
6842 }
6843
6844 p_ws = save_ws;
6845 curwin->w_cursor = pos;
6846 vim_free(frompat);
6847
6848 if (!didone)
6849 EMSG2(_("E753: Not found: %s"), repl_from);
6850}
6851
6852/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006853 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6854 * a list of allocated strings.
6855 */
6856 void
6857spell_suggest_list(gap, word, maxcount)
6858 garray_T *gap;
6859 char_u *word;
6860 int maxcount; /* maximum nr of suggestions */
6861{
6862 suginfo_T sug;
6863 int i;
6864 suggest_T *stp;
6865 char_u *wcopy;
6866
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006867 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006868
6869 /* Make room in "gap". */
6870 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6871 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6872 return;
6873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006874 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006875 {
6876 stp = &SUG(sug.su_ga, i);
6877
6878 /* The suggested word may replace only part of "word", add the not
6879 * replaced part. */
6880 wcopy = alloc(STRLEN(stp->st_word)
6881 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6882 if (wcopy == NULL)
6883 break;
6884 STRCPY(wcopy, stp->st_word);
6885 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6886 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6887 }
6888
6889 spell_find_cleanup(&sug);
6890}
6891
6892/*
6893 * Find spell suggestions for the word at the start of "badptr".
6894 * Return the suggestions in "su->su_ga".
6895 * The maximum number of suggestions is "maxcount".
6896 * Note: does use info for the current window.
6897 * This is based on the mechanisms of Aspell, but completely reimplemented.
6898 */
6899 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006900spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006901 char_u *badptr;
6902 suginfo_T *su;
6903 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006904 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006905 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006906{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006907 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006908 char_u buf[MAXPATHL];
6909 char_u *p;
6910 int do_combine = FALSE;
6911 char_u *sps_copy;
6912#ifdef FEAT_EVAL
6913 static int expr_busy = FALSE;
6914#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006915 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006916
6917 /*
6918 * Set the info in "*su".
6919 */
6920 vim_memset(su, 0, sizeof(suginfo_T));
6921 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6922 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006923 if (*badptr == NUL)
6924 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006925 hash_init(&su->su_banned);
6926
6927 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006928 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006929 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006930 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006931
6932 if (su->su_badlen >= MAXWLEN)
6933 su->su_badlen = MAXWLEN - 1; /* just in case */
6934 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6935 (void)spell_casefold(su->su_badptr, su->su_badlen,
6936 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006937 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006938 su->su_badflags = badword_captype(su->su_badptr,
6939 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006940 if (need_cap)
6941 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006942
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006943 /* If the word is not capitalised and spell_check() doesn't consider the
6944 * word to be bad then it might need to be capitalised. Add a suggestion
6945 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006946 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006947 if (!SPELL_ISUPPER(c) && attr == 0)
6948 {
6949 make_case_word(su->su_badword, buf, WF_ONECAP);
6950 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6951 0, TRUE);
6952 }
6953
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006954 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006955 if (banbadword)
6956 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006957
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006958 /* Make a copy of 'spellsuggest', because the expression may change it. */
6959 sps_copy = vim_strsave(p_sps);
6960 if (sps_copy == NULL)
6961 return;
6962
6963 /* Loop over the items in 'spellsuggest'. */
6964 for (p = sps_copy; *p != NUL; )
6965 {
6966 copy_option_part(&p, buf, MAXPATHL, ",");
6967
6968 if (STRNCMP(buf, "expr:", 5) == 0)
6969 {
6970#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006971 /* Evaluate an expression. Skip this when called recursively,
6972 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006973 if (!expr_busy)
6974 {
6975 expr_busy = TRUE;
6976 spell_suggest_expr(su, buf + 5);
6977 expr_busy = FALSE;
6978 }
6979#endif
6980 }
6981 else if (STRNCMP(buf, "file:", 5) == 0)
6982 /* Use list of suggestions in a file. */
6983 spell_suggest_file(su, buf + 5);
6984 else
6985 {
6986 /* Use internal method. */
6987 spell_suggest_intern(su);
6988 if (sps_flags & SPS_DOUBLE)
6989 do_combine = TRUE;
6990 }
6991 }
6992
6993 vim_free(sps_copy);
6994
6995 if (do_combine)
6996 /* Combine the two list of suggestions. This must be done last,
6997 * because sorting changes the order again. */
6998 score_combine(su);
6999}
7000
7001#ifdef FEAT_EVAL
7002/*
7003 * Find suggestions by evaluating expression "expr".
7004 */
7005 static void
7006spell_suggest_expr(su, expr)
7007 suginfo_T *su;
7008 char_u *expr;
7009{
7010 list_T *list;
7011 listitem_T *li;
7012 int score;
7013 char_u *p;
7014
7015 /* The work is split up in a few parts to avoid having to export
7016 * suginfo_T.
7017 * First evaluate the expression and get the resulting list. */
7018 list = eval_spell_expr(su->su_badword, expr);
7019 if (list != NULL)
7020 {
7021 /* Loop over the items in the list. */
7022 for (li = list->lv_first; li != NULL; li = li->li_next)
7023 if (li->li_tv.v_type == VAR_LIST)
7024 {
7025 /* Get the word and the score from the items. */
7026 score = get_spellword(li->li_tv.vval.v_list, &p);
7027 if (score >= 0)
7028 add_suggestion(su, &su->su_ga, p,
7029 su->su_badlen, score, 0, TRUE);
7030 }
7031 list_unref(list);
7032 }
7033
7034 /* Sort the suggestions and truncate at "maxcount". */
7035 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7036}
7037#endif
7038
7039/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007040 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007041 */
7042 static void
7043spell_suggest_file(su, fname)
7044 suginfo_T *su;
7045 char_u *fname;
7046{
7047 FILE *fd;
7048 char_u line[MAXWLEN * 2];
7049 char_u *p;
7050 int len;
7051 char_u cword[MAXWLEN];
7052
7053 /* Open the file. */
7054 fd = mch_fopen((char *)fname, "r");
7055 if (fd == NULL)
7056 {
7057 EMSG2(_(e_notopen), fname);
7058 return;
7059 }
7060
7061 /* Read it line by line. */
7062 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
7063 {
7064 line_breakcheck();
7065
7066 p = vim_strchr(line, '/');
7067 if (p == NULL)
7068 continue; /* No Tab found, just skip the line. */
7069 *p++ = NUL;
7070 if (STRICMP(su->su_badword, line) == 0)
7071 {
7072 /* Match! Isolate the good word, until CR or NL. */
7073 for (len = 0; p[len] >= ' '; ++len)
7074 ;
7075 p[len] = NUL;
7076
7077 /* If the suggestion doesn't have specific case duplicate the case
7078 * of the bad word. */
7079 if (captype(p, NULL) == 0)
7080 {
7081 make_case_word(p, cword, su->su_badflags);
7082 p = cword;
7083 }
7084
7085 add_suggestion(su, &su->su_ga, p, su->su_badlen,
7086 SCORE_FILE, 0, TRUE);
7087 }
7088 }
7089
7090 fclose(fd);
7091
7092 /* Sort the suggestions and truncate at "maxcount". */
7093 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7094}
7095
7096/*
7097 * Find suggestions for the internal method indicated by "sps_flags".
7098 */
7099 static void
7100spell_suggest_intern(su)
7101 suginfo_T *su;
7102{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007103 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007104 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007105 *
7106 * Set a maximum score to limit the combination of operations that is
7107 * tried.
7108 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007109 suggest_try_special(su);
7110
7111 /*
7112 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
7113 * from the .aff file and inserting a space (split the word).
7114 */
7115 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007116
7117 /* For the resulting top-scorers compute the sound-a-like score. */
7118 if (sps_flags & SPS_DOUBLE)
7119 score_comp_sal(su);
7120
7121 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007122 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007123 *
7124 * Only do this when we don't have a lot of suggestions yet, because it's
7125 * very slow and often doesn't find new suggestions.
7126 */
7127 if ((sps_flags & SPS_DOUBLE)
7128 || (!(sps_flags & SPS_FAST)
7129 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
7130 {
7131 /* Allow a higher score now. */
7132 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007133 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007134 }
7135
7136 /* When CTRL-C was hit while searching do show the results. */
7137 ui_breakcheck();
7138 if (got_int)
7139 {
7140 (void)vgetc();
7141 got_int = FALSE;
7142 }
7143
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007144 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007145 {
7146 if (sps_flags & SPS_BEST)
7147 /* Adjust the word score for how it sounds like. */
7148 rescore_suggestions(su);
7149
7150 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007151 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007152 }
7153}
7154
7155/*
7156 * Free the info put in "*su" by spell_find_suggest().
7157 */
7158 static void
7159spell_find_cleanup(su)
7160 suginfo_T *su;
7161{
7162 int i;
7163
7164 /* Free the suggestions. */
7165 for (i = 0; i < su->su_ga.ga_len; ++i)
7166 vim_free(SUG(su->su_ga, i).st_word);
7167 ga_clear(&su->su_ga);
7168 for (i = 0; i < su->su_sga.ga_len; ++i)
7169 vim_free(SUG(su->su_sga, i).st_word);
7170 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007171
7172 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007173 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174}
7175
7176/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007177 * Make a copy of "word", with the first letter upper or lower cased, to
7178 * "wcopy[MAXWLEN]". "word" must not be empty.
7179 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007180 */
7181 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007182onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007183 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007184 char_u *wcopy;
7185 int upper; /* TRUE: first letter made upper case */
7186{
7187 char_u *p;
7188 int c;
7189 int l;
7190
7191 p = word;
7192#ifdef FEAT_MBYTE
7193 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007194 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007195 else
7196#endif
7197 c = *p++;
7198 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007199 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007200 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007201 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202#ifdef FEAT_MBYTE
7203 if (has_mbyte)
7204 l = mb_char2bytes(c, wcopy);
7205 else
7206#endif
7207 {
7208 l = 1;
7209 wcopy[0] = c;
7210 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007211 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007212}
7213
7214/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007215 * Make a copy of "word" with all the letters upper cased into
7216 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007217 */
7218 static void
7219allcap_copy(word, wcopy)
7220 char_u *word;
7221 char_u *wcopy;
7222{
7223 char_u *s;
7224 char_u *d;
7225 int c;
7226
7227 d = wcopy;
7228 for (s = word; *s != NUL; )
7229 {
7230#ifdef FEAT_MBYTE
7231 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007232 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007233 else
7234#endif
7235 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007236 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237
7238#ifdef FEAT_MBYTE
7239 if (has_mbyte)
7240 {
7241 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
7242 break;
7243 d += mb_char2bytes(c, d);
7244 }
7245 else
7246#endif
7247 {
7248 if (d - wcopy >= MAXWLEN - 1)
7249 break;
7250 *d++ = c;
7251 }
7252 }
7253 *d = NUL;
7254}
7255
7256/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007257 * Try finding suggestions by recognizing specific situations.
7258 */
7259 static void
7260suggest_try_special(su)
7261 suginfo_T *su;
7262{
7263 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007264 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007265 int c;
7266 char_u word[MAXWLEN];
7267
7268 /*
7269 * Recognize a word that is repeated: "the the".
7270 */
7271 p = skiptowhite(su->su_fbadword);
7272 len = p - su->su_fbadword;
7273 p = skipwhite(p);
7274 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
7275 {
7276 /* Include badflags: if the badword is onecap or allcap
7277 * use that for the goodword too: "The the" -> "The". */
7278 c = su->su_fbadword[len];
7279 su->su_fbadword[len] = NUL;
7280 make_case_word(su->su_fbadword, word, su->su_badflags);
7281 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007282 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007283 }
7284}
7285
7286/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007287 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00007288 *
7289 * This uses a state machine. At each node in the tree we try various
7290 * operations. When trying if an operation work "depth" is increased and the
7291 * stack[] is used to store info. This allows combinations, thus insert one
7292 * character, replace one and delete another. The number of changes is
7293 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007294 */
7295 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007296suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 suginfo_T *su;
7298{
7299 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
7300 char_u tword[MAXWLEN]; /* good word collected so far */
7301 trystate_T stack[MAXWLEN];
7302 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
7303 * to for word split) */
7304 char_u prewordlen = 0; /* length of word in "preword" */
7305 int splitoff = 0; /* index in tword after last split */
7306 trystate_T *sp;
7307 int newscore;
7308 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007309 char_u *byts, *fbyts, *pbyts;
7310 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007311 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007312 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007313 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007314 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007316 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 int len;
7318 char_u *p;
7319 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00007320 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007321 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007322
7323 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00007324 * to find matches (esp. REP items). Append some more text, changing
7325 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007326 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007327 n = STRLEN(fword);
7328 p = su->su_badptr + su->su_badlen;
7329 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330
7331 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7332 lp->lp_slang != NULL; ++lp)
7333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007334 /*
7335 * Go through the whole case-fold tree, try changes at each node.
7336 * "tword[]" contains the word collected from nodes in the tree.
7337 * "fword[]" the word we are trying to match with (initially the bad
7338 * word).
7339 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007341 sp = &stack[0];
7342 sp->ts_state = STATE_START;
7343 sp->ts_score = 0;
7344 sp->ts_curi = 1;
7345 sp->ts_fidx = 0;
7346 sp->ts_fidxtry = 0;
7347 sp->ts_twordlen = 0;
7348 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007349#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007350 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007351#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007352
Bram Moolenaarea424162005-06-16 21:51:00 +00007353 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007354 * When there are postponed prefixes we need to use these first. At
7355 * the end of the prefix we continue in the case-fold tree.
7356 */
7357 fbyts = lp->lp_slang->sl_fbyts;
7358 fidxs = lp->lp_slang->sl_fidxs;
7359 pbyts = lp->lp_slang->sl_pbyts;
7360 pidxs = lp->lp_slang->sl_pidxs;
7361 if (pbyts != NULL)
7362 {
7363 byts = pbyts;
7364 idxs = pidxs;
7365 sp->ts_prefixdepth = PREFIXTREE;
7366 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
7367 }
7368 else
7369 {
7370 byts = fbyts;
7371 idxs = fidxs;
7372 sp->ts_prefixdepth = NOPREFIX;
7373 }
7374
7375 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007376 * Loop to find all suggestions. At each round we either:
7377 * - For the current state try one operation, advance "ts_curi",
7378 * increase "depth".
7379 * - When a state is done go to the next, set "ts_state".
7380 * - When all states are tried decrease "depth".
7381 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007382 while (depth >= 0 && !got_int)
7383 {
7384 sp = &stack[depth];
7385 switch (sp->ts_state)
7386 {
7387 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007388 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007389 /*
7390 * Start of node: Deal with NUL bytes, which means
7391 * tword[] may end here.
7392 */
7393 arridx = sp->ts_arridx; /* current node in the tree */
7394 len = byts[arridx]; /* bytes in this node */
7395 arridx += sp->ts_curi; /* index of current byte */
7396
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007397 if (sp->ts_prefixdepth == PREFIXTREE)
7398 {
7399 /* Skip over the NUL bytes, we use them later. */
7400 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7401 ;
7402 sp->ts_curi += n;
7403
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007404 /* Always past NUL bytes now. */
7405 n = (int)sp->ts_state;
7406 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007407 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007408
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007409 /* At end of a prefix or at start of prefixtree: check for
7410 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007411 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007412 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007413 /* Set su->su_badflags to the caps type at this
7414 * position. Use the caps type until here for the
7415 * prefix itself. */
7416#ifdef FEAT_MBYTE
7417 if (has_mbyte)
7418 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7419 else
7420#endif
7421 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007422 flags = badword_captype(su->su_badptr,
7423 su->su_badptr + n);
7424 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007425 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007426 ++depth;
7427 stack[depth] = stack[depth - 1];
7428 sp = &stack[depth];
7429 sp->ts_prefixdepth = depth - 1;
7430 byts = fbyts;
7431 idxs = fidxs;
7432 sp->ts_state = STATE_START;
7433 sp->ts_curi = 1; /* start just after length byte */
7434 sp->ts_arridx = 0;
7435
Bram Moolenaar53805d12005-08-01 07:08:33 +00007436 /* Move the prefix to preword[] with the right case
7437 * and make find_keepcap_word() works. */
7438 splitoff = sp->ts_twordlen;
7439 tword[splitoff] = NUL;
7440 make_case_word(tword, preword, flags);
7441 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007442 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007443 break;
7444 }
7445
Bram Moolenaar0c405862005-06-22 22:26:26 +00007446 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007447 {
7448 /* Past bytes in node and/or past NUL bytes. */
7449 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007450 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007451 break;
7452 }
7453
7454 /*
7455 * End of word in tree.
7456 */
7457 ++sp->ts_curi; /* eat one NUL byte */
7458
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007459 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007460
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007461 if (sp->ts_prefixdepth < MAXWLEN)
7462 {
7463 /* There was a prefix before the word. Check that the
7464 * prefix can be used with this word. */
7465 /* Count the length of the NULs in the prefix. If there
7466 * are none this must be the first try without a prefix.
7467 */
7468 n = stack[sp->ts_prefixdepth].ts_arridx;
7469 len = pbyts[n++];
7470 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7471 ;
7472 if (c > 0)
7473 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007474 /* The prefix ID is stored three bytes above the
7475 * flags. */
7476 c = valid_word_prefix(c, n, flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007477 tword + splitoff, lp->lp_slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007478 if (c == 0)
7479 break;
7480
7481 /* Use the WF_RARE flag for a rare prefix. */
7482 if (c & WF_RAREPFX)
7483 flags |= WF_RARE;
7484 }
7485 }
7486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007487 /*
7488 * Form the word with proper case in preword.
7489 * If there is a word from a previous split, append.
7490 */
7491 tword[sp->ts_twordlen] = NUL;
7492 if (flags & WF_KEEPCAP)
7493 /* Must find the word in the keep-case tree. */
7494 find_keepcap_word(lp->lp_slang, tword + splitoff,
7495 preword + prewordlen);
7496 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007498 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007499 * use that for the goodword too. But if the badword is
7500 * allcap and it's only one char long use onecap. */
7501 c = su->su_badflags;
7502 if ((c & WF_ALLCAP)
7503#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007504 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007505#else
7506 && su->su_badlen == 1
7507#endif
7508 )
7509 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007510 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007511 preword + prewordlen, flags | c);
7512 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007513
7514 /* Don't use a banned word. It may appear again as a good
7515 * word, thus remember it. */
7516 if (flags & WF_BANNED)
7517 {
7518 add_banned(su, preword + prewordlen);
7519 break;
7520 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007521 if (was_banned(su, preword + prewordlen)
7522 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007523 break;
7524
7525 newscore = 0;
7526 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007527 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007528 newscore += SCORE_REGION;
7529 if (flags & WF_RARE)
7530 newscore += SCORE_RARE;
7531
Bram Moolenaar0c405862005-06-22 22:26:26 +00007532 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007533 captype(preword + prewordlen, NULL)))
7534 newscore += SCORE_ICASE;
7535
Bram Moolenaar0c405862005-06-22 22:26:26 +00007536 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007537 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007538 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007539 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007540 /* The badword also ends: add suggestions. Give a penalty
7541 * when changing non-word char to word char, e.g., "thes,"
7542 * -> "these". */
7543 p = fword + sp->ts_fidx;
7544#ifdef FEAT_MBYTE
7545 if (has_mbyte)
7546 mb_ptr_back(fword, p);
7547 else
7548#endif
7549 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007550 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007551 {
7552 p = preword + STRLEN(preword);
7553#ifdef FEAT_MBYTE
7554 if (has_mbyte)
7555 mb_ptr_back(preword, p);
7556 else
7557#endif
7558 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007559 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007560 newscore += SCORE_NONWORD;
7561 }
7562
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007563 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007564 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007565 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007566 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007567 else if (sp->ts_fidx >= sp->ts_fidxtry
7568#ifdef FEAT_MBYTE
7569 /* Don't split halfway a character. */
7570 && (!has_mbyte || sp->ts_tcharlen == 0)
7571#endif
7572 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007573 {
7574 /* The word in the tree ends but the badword
7575 * continues: try inserting a space and check that a valid
7576 * words starts at fword[sp->ts_fidx]. */
7577 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7578 {
7579 /* Save things to be restored at STATE_SPLITUNDO. */
7580 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007581 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007582 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007583 sp->ts_state = STATE_SPLITUNDO;
7584
7585 ++depth;
7586 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007587
7588 /* Append a space to preword. */
7589 STRCAT(preword, " ");
7590 prewordlen = STRLEN(preword);
7591 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007592
7593 /* If the badword has a non-word character at this
7594 * position skip it. That means replacing the
7595 * non-word character with a space. */
7596 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7597 {
7598 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7599#ifdef FEAT_MBYTE
7600 if (has_mbyte)
7601 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7602 else
7603#endif
7604 ++sp->ts_fidx;
7605 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00007606
7607 /* set su->su_badflags to the caps type at this
7608 * position */
7609
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007610#ifdef FEAT_MBYTE
7611 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00007612 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007613 else
7614#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00007615 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007616 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007617 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007619 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007620 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007621 }
7622 }
7623 break;
7624
7625 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007626 /* Undo the changes done for word split. */
7627 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007628 splitoff = sp->ts_save_splitoff;
7629 prewordlen = sp->ts_save_prewordlen;
7630
7631 /* Continue looking for NUL bytes. */
7632 sp->ts_state = STATE_START;
7633 break;
7634
7635 case STATE_ENDNUL:
7636 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007637 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007638 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007639 {
7640 /* The badword ends, can't use the bytes in this node. */
7641 sp->ts_state = STATE_DEL;
7642 break;
7643 }
7644 sp->ts_state = STATE_PLAIN;
7645 /*FALLTHROUGH*/
7646
7647 case STATE_PLAIN:
7648 /*
7649 * Go over all possible bytes at this node, add each to
7650 * tword[] and use child node. "ts_curi" is the index.
7651 */
7652 arridx = sp->ts_arridx;
7653 if (sp->ts_curi > byts[arridx])
7654 {
7655 /* Done all bytes at this node, do next state. When still
7656 * at already changed bytes skip the other tricks. */
7657 if (sp->ts_fidx >= sp->ts_fidxtry)
7658 sp->ts_state = STATE_DEL;
7659 else
7660 sp->ts_state = STATE_FINAL;
7661 }
7662 else
7663 {
7664 arridx += sp->ts_curi++;
7665 c = byts[arridx];
7666
7667 /* Normal byte, go one level deeper. If it's not equal to
7668 * the byte in the bad word adjust the score. But don't
7669 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007670 if (c == fword[sp->ts_fidx]
7671#ifdef FEAT_MBYTE
7672 || (sp->ts_tcharlen > 0
7673 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007674#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007675 )
7676 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007677 else
7678 newscore = SCORE_SUBST;
7679 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7680 && try_deeper(su, stack, depth, newscore))
7681 {
7682 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007683 sp = &stack[depth];
7684 ++sp->ts_fidx;
7685 tword[sp->ts_twordlen++] = c;
7686 sp->ts_arridx = idxs[arridx];
7687#ifdef FEAT_MBYTE
7688 if (newscore == SCORE_SUBST)
7689 sp->ts_isdiff = DIFF_YES;
7690 if (has_mbyte)
7691 {
7692 /* Multi-byte characters are a bit complicated to
7693 * handle: They differ when any of the bytes
7694 * differ and then their length may also differ. */
7695 if (sp->ts_tcharlen == 0)
7696 {
7697 /* First byte. */
7698 sp->ts_tcharidx = 0;
7699 sp->ts_tcharlen = MB_BYTE2LEN(c);
7700 sp->ts_fcharstart = sp->ts_fidx - 1;
7701 sp->ts_isdiff = (newscore != 0)
7702 ? DIFF_YES : DIFF_NONE;
7703 }
7704 else if (sp->ts_isdiff == DIFF_INSERT)
7705 /* When inserting trail bytes don't advance in
7706 * the bad word. */
7707 --sp->ts_fidx;
7708 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7709 {
7710 /* Last byte of character. */
7711 if (sp->ts_isdiff == DIFF_YES)
7712 {
7713 /* Correct ts_fidx for the byte length of
7714 * the character (we didn't check that
7715 * before). */
7716 sp->ts_fidx = sp->ts_fcharstart
7717 + MB_BYTE2LEN(
7718 fword[sp->ts_fcharstart]);
7719
7720 /* For a similar character adjust score
7721 * from SCORE_SUBST to SCORE_SIMILAR. */
7722 if (lp->lp_slang->sl_has_map
7723 && similar_chars(lp->lp_slang,
7724 mb_ptr2char(tword
7725 + sp->ts_twordlen
7726 - sp->ts_tcharlen),
7727 mb_ptr2char(fword
7728 + sp->ts_fcharstart)))
7729 sp->ts_score -=
7730 SCORE_SUBST - SCORE_SIMILAR;
7731 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007732 else if (sp->ts_isdiff == DIFF_INSERT
7733 && sp->ts_twordlen > sp->ts_tcharlen)
7734 {
7735 /* If the previous character was the same,
7736 * thus doubling a character, give a bonus
7737 * to the score. */
7738 p = tword + sp->ts_twordlen
7739 - sp->ts_tcharlen;
7740 c = mb_ptr2char(p);
7741 mb_ptr_back(tword, p);
7742 if (c == mb_ptr2char(p))
7743 sp->ts_score -= SCORE_INS
7744 - SCORE_INSDUP;
7745 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007746
7747 /* Starting a new char, reset the length. */
7748 sp->ts_tcharlen = 0;
7749 }
7750 }
7751 else
7752#endif
7753 {
7754 /* If we found a similar char adjust the score.
7755 * We do this after calling try_deeper() because
7756 * it's slow. */
7757 if (newscore != 0
7758 && lp->lp_slang->sl_has_map
7759 && similar_chars(lp->lp_slang,
7760 c, fword[sp->ts_fidx - 1]))
7761 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 }
7764 }
7765 break;
7766
7767 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007768#ifdef FEAT_MBYTE
7769 /* When past the first byte of a multi-byte char don't try
7770 * delete/insert/swap a character. */
7771 if (has_mbyte && sp->ts_tcharlen > 0)
7772 {
7773 sp->ts_state = STATE_FINAL;
7774 break;
7775 }
7776#endif
7777 /*
7778 * Try skipping one character in the bad word (delete it).
7779 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007780 sp->ts_state = STATE_INS;
7781 sp->ts_curi = 1;
7782 if (fword[sp->ts_fidx] != NUL
7783 && try_deeper(su, stack, depth, SCORE_DEL))
7784 {
7785 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007786
7787 /* Advance over the character in fword[]. Give a bonus to
7788 * the score if the same character is following "nn" ->
7789 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007790#ifdef FEAT_MBYTE
7791 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007792 {
7793 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007794 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007795 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7796 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7797 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007798 else
7799#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007800 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007801 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007802 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7803 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007805 break;
7806 }
7807 /*FALLTHROUGH*/
7808
7809 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007810 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007811 * node. */
7812 n = sp->ts_arridx;
7813 if (sp->ts_curi > byts[n])
7814 {
7815 /* Done all bytes at this node, do next state. */
7816 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007817 }
7818 else
7819 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007820 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007821 n += sp->ts_curi++;
7822 c = byts[n];
7823 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7824 {
7825 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007826 sp = &stack[depth];
7827 tword[sp->ts_twordlen++] = c;
7828 sp->ts_arridx = idxs[n];
7829#ifdef FEAT_MBYTE
7830 if (has_mbyte)
7831 {
7832 fl = MB_BYTE2LEN(c);
7833 if (fl > 1)
7834 {
7835 /* There are following bytes for the same
7836 * character. We must find all bytes before
7837 * trying delete/insert/swap/etc. */
7838 sp->ts_tcharlen = fl;
7839 sp->ts_tcharidx = 1;
7840 sp->ts_isdiff = DIFF_INSERT;
7841 }
7842 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007843 else
7844 fl = 1;
7845 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007846#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007847 {
7848 /* If the previous character was the same, thus
7849 * doubling a character, give a bonus to the
7850 * score. */
7851 if (sp->ts_twordlen >= 2
7852 && tword[sp->ts_twordlen - 2] == c)
7853 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 }
7856 }
7857 break;
7858
7859 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007860 /*
7861 * Swap two bytes in the bad word: "12" -> "21".
7862 * We change "fword" here, it's changed back afterwards.
7863 */
7864 p = fword + sp->ts_fidx;
7865 c = *p;
7866 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007867 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007868 /* End of word, can't swap or replace. */
7869 sp->ts_state = STATE_FINAL;
7870 break;
7871 }
7872#ifdef FEAT_MBYTE
7873 if (has_mbyte)
7874 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007875 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00007876 c = mb_ptr2char(p);
7877 c2 = mb_ptr2char(p + n);
7878 }
7879 else
7880#endif
7881 c2 = p[1];
7882 if (c == c2)
7883 {
7884 /* Characters are identical, swap won't do anything. */
7885 sp->ts_state = STATE_SWAP3;
7886 break;
7887 }
7888 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7889 {
7890 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007891 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007892#ifdef FEAT_MBYTE
7893 if (has_mbyte)
7894 {
7895 fl = mb_char2len(c2);
7896 mch_memmove(p, p + n, fl);
7897 mb_char2bytes(c, p + fl);
7898 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7899 }
7900 else
7901#endif
7902 {
7903 p[0] = c2;
7904 p[1] = c;
7905 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7906 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007907 }
7908 else
7909 /* If this swap doesn't work then SWAP3 won't either. */
7910 sp->ts_state = STATE_REP_INI;
7911 break;
7912
Bram Moolenaarea424162005-06-16 21:51:00 +00007913 case STATE_UNSWAP:
7914 /* Undo the STATE_SWAP swap: "21" -> "12". */
7915 p = fword + sp->ts_fidx;
7916#ifdef FEAT_MBYTE
7917 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007919 n = MB_BYTE2LEN(*p);
7920 c = mb_ptr2char(p + n);
7921 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7922 mb_char2bytes(c, p);
7923 }
7924 else
7925#endif
7926 {
7927 c = *p;
7928 *p = p[1];
7929 p[1] = c;
7930 }
7931 /*FALLTHROUGH*/
7932
7933 case STATE_SWAP3:
7934 /* Swap two bytes, skipping one: "123" -> "321". We change
7935 * "fword" here, it's changed back afterwards. */
7936 p = fword + sp->ts_fidx;
7937#ifdef FEAT_MBYTE
7938 if (has_mbyte)
7939 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007940 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00007941 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007942 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00007943 c2 = mb_ptr2char(p + n);
7944 c3 = mb_ptr2char(p + n + fl);
7945 }
7946 else
7947#endif
7948 {
7949 c = *p;
7950 c2 = p[1];
7951 c3 = p[2];
7952 }
7953
7954 /* When characters are identical: "121" then SWAP3 result is
7955 * identical, ROT3L result is same as SWAP: "211", ROT3L
7956 * result is same as SWAP on next char: "112". Thus skip all
7957 * swapping. Also skip when c3 is NUL. */
7958 if (c == c3 || c3 == NUL)
7959 {
7960 sp->ts_state = STATE_REP_INI;
7961 break;
7962 }
7963 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7964 {
7965 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007966 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007967#ifdef FEAT_MBYTE
7968 if (has_mbyte)
7969 {
7970 tl = mb_char2len(c3);
7971 mch_memmove(p, p + n + fl, tl);
7972 mb_char2bytes(c2, p + tl);
7973 mb_char2bytes(c, p + fl + tl);
7974 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7975 }
7976 else
7977#endif
7978 {
7979 p[0] = p[2];
7980 p[2] = c;
7981 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7982 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007983 }
7984 else
7985 sp->ts_state = STATE_REP_INI;
7986 break;
7987
Bram Moolenaarea424162005-06-16 21:51:00 +00007988 case STATE_UNSWAP3:
7989 /* Undo STATE_SWAP3: "321" -> "123" */
7990 p = fword + sp->ts_fidx;
7991#ifdef FEAT_MBYTE
7992 if (has_mbyte)
7993 {
7994 n = MB_BYTE2LEN(*p);
7995 c2 = mb_ptr2char(p + n);
7996 fl = MB_BYTE2LEN(p[n]);
7997 c = mb_ptr2char(p + n + fl);
7998 tl = MB_BYTE2LEN(p[n + fl]);
7999 mch_memmove(p + fl + tl, p, n);
8000 mb_char2bytes(c, p);
8001 mb_char2bytes(c2, p + tl);
8002 }
8003 else
8004#endif
8005 {
8006 c = *p;
8007 *p = p[2];
8008 p[2] = c;
8009 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008010
Bram Moolenaarea424162005-06-16 21:51:00 +00008011 /* Rotate three characters left: "123" -> "231". We change
8012 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8014 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008015 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008016 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008017 p = fword + sp->ts_fidx;
8018#ifdef FEAT_MBYTE
8019 if (has_mbyte)
8020 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008021 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008022 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008023 fl = mb_cptr2len(p + n);
8024 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00008025 mch_memmove(p, p + n, fl);
8026 mb_char2bytes(c, p + fl);
8027 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8028 }
8029 else
8030#endif
8031 {
8032 c = *p;
8033 *p = p[1];
8034 p[1] = p[2];
8035 p[2] = c;
8036 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008038 }
8039 else
8040 sp->ts_state = STATE_REP_INI;
8041 break;
8042
Bram Moolenaarea424162005-06-16 21:51:00 +00008043 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008044 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008045 p = fword + sp->ts_fidx;
8046#ifdef FEAT_MBYTE
8047 if (has_mbyte)
8048 {
8049 n = MB_BYTE2LEN(*p);
8050 n += MB_BYTE2LEN(p[n]);
8051 c = mb_ptr2char(p + n);
8052 tl = MB_BYTE2LEN(p[n]);
8053 mch_memmove(p + tl, p, n);
8054 mb_char2bytes(c, p);
8055 }
8056 else
8057#endif
8058 {
8059 c = p[2];
8060 p[2] = p[1];
8061 p[1] = *p;
8062 *p = c;
8063 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008065 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00008066 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8068 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008069 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008070 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008071 p = fword + sp->ts_fidx;
8072#ifdef FEAT_MBYTE
8073 if (has_mbyte)
8074 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008075 n = mb_cptr2len(p);
8076 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008077 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008078 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008079 mch_memmove(p + tl, p, n);
8080 mb_char2bytes(c, p);
8081 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
8082 }
8083 else
8084#endif
8085 {
8086 c = p[2];
8087 p[2] = p[1];
8088 p[1] = *p;
8089 *p = c;
8090 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008092 }
8093 else
8094 sp->ts_state = STATE_REP_INI;
8095 break;
8096
Bram Moolenaarea424162005-06-16 21:51:00 +00008097 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008098 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008099 p = fword + sp->ts_fidx;
8100#ifdef FEAT_MBYTE
8101 if (has_mbyte)
8102 {
8103 c = mb_ptr2char(p);
8104 tl = MB_BYTE2LEN(*p);
8105 n = MB_BYTE2LEN(p[tl]);
8106 n += MB_BYTE2LEN(p[tl + n]);
8107 mch_memmove(p, p + tl, n);
8108 mb_char2bytes(c, p + n);
8109 }
8110 else
8111#endif
8112 {
8113 c = *p;
8114 *p = p[1];
8115 p[1] = p[2];
8116 p[2] = c;
8117 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 /*FALLTHROUGH*/
8119
8120 case STATE_REP_INI:
8121 /* Check if matching with REP items from the .aff file would
8122 * work. Quickly skip if there are no REP items or the score
8123 * is going to be too high anyway. */
8124 gap = &lp->lp_slang->sl_rep;
8125 if (gap->ga_len == 0
8126 || sp->ts_score + SCORE_REP >= su->su_maxscore)
8127 {
8128 sp->ts_state = STATE_FINAL;
8129 break;
8130 }
8131
8132 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00008133 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008134 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
8135 if (sp->ts_curi < 0)
8136 {
8137 sp->ts_state = STATE_FINAL;
8138 break;
8139 }
8140
8141 sp->ts_state = STATE_REP;
8142 /*FALLTHROUGH*/
8143
8144 case STATE_REP:
8145 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00008146 * match replace the characters and check if the resulting
8147 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 p = fword + sp->ts_fidx;
8149
8150 gap = &lp->lp_slang->sl_rep;
8151 while (sp->ts_curi < gap->ga_len)
8152 {
8153 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
8154 if (*ftp->ft_from != *p)
8155 {
8156 /* past possible matching entries */
8157 sp->ts_curi = gap->ga_len;
8158 break;
8159 }
8160 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
8161 && try_deeper(su, stack, depth, SCORE_REP))
8162 {
8163 /* Need to undo this afterwards. */
8164 sp->ts_state = STATE_REP_UNDO;
8165
8166 /* Change the "from" to the "to" string. */
8167 ++depth;
8168 fl = STRLEN(ftp->ft_from);
8169 tl = STRLEN(ftp->ft_to);
8170 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008173 repextra += tl - fl;
8174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175 mch_memmove(p, ftp->ft_to, tl);
8176 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00008177#ifdef FEAT_MBYTE
8178 stack[depth].ts_tcharlen = 0;
8179#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008180 break;
8181 }
8182 }
8183
8184 if (sp->ts_curi >= gap->ga_len)
8185 /* No (more) matches. */
8186 sp->ts_state = STATE_FINAL;
8187
8188 break;
8189
8190 case STATE_REP_UNDO:
8191 /* Undo a REP replacement and continue with the next one. */
8192 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
8193 + sp->ts_curi - 1;
8194 fl = STRLEN(ftp->ft_from);
8195 tl = STRLEN(ftp->ft_to);
8196 p = fword + sp->ts_fidx;
8197 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008199 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008200 repextra -= tl - fl;
8201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008202 mch_memmove(p, ftp->ft_from, fl);
8203 sp->ts_state = STATE_REP;
8204 break;
8205
8206 default:
8207 /* Did all possible states at this level, go up one level. */
8208 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008209
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008210 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
8211 {
8212 /* Continue in or go back to the prefix tree. */
8213 byts = pbyts;
8214 idxs = pidxs;
8215 splitoff = 0;
8216 }
8217
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008218 /* Don't check for CTRL-C too often, it takes time. */
8219 line_breakcheck();
8220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 }
8222 }
8223}
8224
8225/*
8226 * Try going one level deeper in the tree.
8227 */
8228 static int
8229try_deeper(su, stack, depth, score_add)
8230 suginfo_T *su;
8231 trystate_T *stack;
8232 int depth;
8233 int score_add;
8234{
8235 int newscore;
8236
8237 /* Refuse to go deeper if the scrore is getting too big. */
8238 newscore = stack[depth].ts_score + score_add;
8239 if (newscore >= su->su_maxscore)
8240 return FALSE;
8241
Bram Moolenaarea424162005-06-16 21:51:00 +00008242 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008243 stack[depth + 1].ts_state = STATE_START;
8244 stack[depth + 1].ts_score = newscore;
8245 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008246 return TRUE;
8247}
8248
Bram Moolenaar53805d12005-08-01 07:08:33 +00008249#ifdef FEAT_MBYTE
8250/*
8251 * Case-folding may change the number of bytes: Count nr of chars in
8252 * fword[flen] and return the byte length of that many chars in "word".
8253 */
8254 static int
8255nofold_len(fword, flen, word)
8256 char_u *fword;
8257 int flen;
8258 char_u *word;
8259{
8260 char_u *p;
8261 int i = 0;
8262
8263 for (p = fword; p < fword + flen; mb_ptr_adv(p))
8264 ++i;
8265 for (p = word; i > 0; mb_ptr_adv(p))
8266 --i;
8267 return (int)(p - word);
8268}
8269#endif
8270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008271/*
8272 * "fword" is a good word with case folded. Find the matching keep-case
8273 * words and put it in "kword".
8274 * Theoretically there could be several keep-case words that result in the
8275 * same case-folded word, but we only find one...
8276 */
8277 static void
8278find_keepcap_word(slang, fword, kword)
8279 slang_T *slang;
8280 char_u *fword;
8281 char_u *kword;
8282{
8283 char_u uword[MAXWLEN]; /* "fword" in upper-case */
8284 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008285 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008286
8287 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008288 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008289 int round[MAXWLEN];
8290 int fwordidx[MAXWLEN];
8291 int uwordidx[MAXWLEN];
8292 int kwordlen[MAXWLEN];
8293
8294 int flen, ulen;
8295 int l;
8296 int len;
8297 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008298 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008299 char_u *p;
8300 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008301 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008302
8303 if (byts == NULL)
8304 {
8305 /* array is empty: "cannot happen" */
8306 *kword = NUL;
8307 return;
8308 }
8309
8310 /* Make an all-cap version of "fword". */
8311 allcap_copy(fword, uword);
8312
8313 /*
8314 * Each character needs to be tried both case-folded and upper-case.
8315 * All this gets very complicated if we keep in mind that changing case
8316 * may change the byte length of a multi-byte character...
8317 */
8318 depth = 0;
8319 arridx[0] = 0;
8320 round[0] = 0;
8321 fwordidx[0] = 0;
8322 uwordidx[0] = 0;
8323 kwordlen[0] = 0;
8324 while (depth >= 0)
8325 {
8326 if (fword[fwordidx[depth]] == NUL)
8327 {
8328 /* We are at the end of "fword". If the tree allows a word to end
8329 * here we have found a match. */
8330 if (byts[arridx[depth] + 1] == 0)
8331 {
8332 kword[kwordlen[depth]] = NUL;
8333 return;
8334 }
8335
8336 /* kword is getting too long, continue one level up */
8337 --depth;
8338 }
8339 else if (++round[depth] > 2)
8340 {
8341 /* tried both fold-case and upper-case character, continue one
8342 * level up */
8343 --depth;
8344 }
8345 else
8346 {
8347 /*
8348 * round[depth] == 1: Try using the folded-case character.
8349 * round[depth] == 2: Try using the upper-case character.
8350 */
8351#ifdef FEAT_MBYTE
8352 if (has_mbyte)
8353 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008354 flen = mb_cptr2len(fword + fwordidx[depth]);
8355 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008356 }
8357 else
8358#endif
8359 ulen = flen = 1;
8360 if (round[depth] == 1)
8361 {
8362 p = fword + fwordidx[depth];
8363 l = flen;
8364 }
8365 else
8366 {
8367 p = uword + uwordidx[depth];
8368 l = ulen;
8369 }
8370
8371 for (tryidx = arridx[depth]; l > 0; --l)
8372 {
8373 /* Perform a binary search in the list of accepted bytes. */
8374 len = byts[tryidx++];
8375 c = *p++;
8376 lo = tryidx;
8377 hi = tryidx + len - 1;
8378 while (lo < hi)
8379 {
8380 m = (lo + hi) / 2;
8381 if (byts[m] > c)
8382 hi = m - 1;
8383 else if (byts[m] < c)
8384 lo = m + 1;
8385 else
8386 {
8387 lo = hi = m;
8388 break;
8389 }
8390 }
8391
8392 /* Stop if there is no matching byte. */
8393 if (hi < lo || byts[lo] != c)
8394 break;
8395
8396 /* Continue at the child (if there is one). */
8397 tryidx = idxs[lo];
8398 }
8399
8400 if (l == 0)
8401 {
8402 /*
8403 * Found the matching char. Copy it to "kword" and go a
8404 * level deeper.
8405 */
8406 if (round[depth] == 1)
8407 {
8408 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8409 flen);
8410 kwordlen[depth + 1] = kwordlen[depth] + flen;
8411 }
8412 else
8413 {
8414 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8415 ulen);
8416 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8417 }
8418 fwordidx[depth + 1] = fwordidx[depth] + flen;
8419 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8420
8421 ++depth;
8422 arridx[depth] = tryidx;
8423 round[depth] = 0;
8424 }
8425 }
8426 }
8427
8428 /* Didn't find it: "cannot happen". */
8429 *kword = NUL;
8430}
8431
8432/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008433 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8434 * su->su_sga.
8435 */
8436 static void
8437score_comp_sal(su)
8438 suginfo_T *su;
8439{
8440 langp_T *lp;
8441 char_u badsound[MAXWLEN];
8442 int i;
8443 suggest_T *stp;
8444 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008445 int score;
8446
8447 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8448 return;
8449
8450 /* Use the sound-folding of the first language that supports it. */
8451 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8452 lp->lp_slang != NULL; ++lp)
8453 if (lp->lp_slang->sl_sal.ga_len > 0)
8454 {
8455 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008456 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008457
8458 for (i = 0; i < su->su_ga.ga_len; ++i)
8459 {
8460 stp = &SUG(su->su_ga, i);
8461
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008462 /* Case-fold the suggested word, sound-fold it and compute the
8463 * sound-a-like score. */
8464 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008465 if (score < SCORE_MAXMAX)
8466 {
8467 /* Add the suggestion. */
8468 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8469 sstp->st_word = vim_strsave(stp->st_word);
8470 if (sstp->st_word != NULL)
8471 {
8472 sstp->st_score = score;
8473 sstp->st_altscore = 0;
8474 sstp->st_orglen = stp->st_orglen;
8475 ++su->su_sga.ga_len;
8476 }
8477 }
8478 }
8479 break;
8480 }
8481}
8482
8483/*
8484 * Combine the list of suggestions in su->su_ga and su->su_sga.
8485 * They are intwined.
8486 */
8487 static void
8488score_combine(su)
8489 suginfo_T *su;
8490{
8491 int i;
8492 int j;
8493 garray_T ga;
8494 garray_T *gap;
8495 langp_T *lp;
8496 suggest_T *stp;
8497 char_u *p;
8498 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008499 int round;
8500
8501 /* Add the alternate score to su_ga. */
8502 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8503 lp->lp_slang != NULL; ++lp)
8504 {
8505 if (lp->lp_slang->sl_sal.ga_len > 0)
8506 {
8507 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008508 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008509
8510 for (i = 0; i < su->su_ga.ga_len; ++i)
8511 {
8512 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008513 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8514 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008515 if (stp->st_altscore == SCORE_MAXMAX)
8516 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8517 else
8518 stp->st_score = (stp->st_score * 3
8519 + stp->st_altscore) / 4;
8520 stp->st_salscore = FALSE;
8521 }
8522 break;
8523 }
8524 }
8525
8526 /* Add the alternate score to su_sga. */
8527 for (i = 0; i < su->su_sga.ga_len; ++i)
8528 {
8529 stp = &SUG(su->su_sga, i);
8530 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8531 if (stp->st_score == SCORE_MAXMAX)
8532 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8533 else
8534 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8535 stp->st_salscore = TRUE;
8536 }
8537
8538 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8539 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8540 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8541
8542 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8543 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8544 return;
8545
8546 stp = &SUG(ga, 0);
8547 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8548 {
8549 /* round 1: get a suggestion from su_ga
8550 * round 2: get a suggestion from su_sga */
8551 for (round = 1; round <= 2; ++round)
8552 {
8553 gap = round == 1 ? &su->su_ga : &su->su_sga;
8554 if (i < gap->ga_len)
8555 {
8556 /* Don't add a word if it's already there. */
8557 p = SUG(*gap, i).st_word;
8558 for (j = 0; j < ga.ga_len; ++j)
8559 if (STRCMP(stp[j].st_word, p) == 0)
8560 break;
8561 if (j == ga.ga_len)
8562 stp[ga.ga_len++] = SUG(*gap, i);
8563 else
8564 vim_free(p);
8565 }
8566 }
8567 }
8568
8569 ga_clear(&su->su_ga);
8570 ga_clear(&su->su_sga);
8571
8572 /* Truncate the list to the number of suggestions that will be displayed. */
8573 if (ga.ga_len > su->su_maxcount)
8574 {
8575 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8576 vim_free(stp[i].st_word);
8577 ga.ga_len = su->su_maxcount;
8578 }
8579
8580 su->su_ga = ga;
8581}
8582
8583/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008584 * For the goodword in "stp" compute the soundalike score compared to the
8585 * badword.
8586 */
8587 static int
8588stp_sal_score(stp, su, slang, badsound)
8589 suggest_T *stp;
8590 suginfo_T *su;
8591 slang_T *slang;
8592 char_u *badsound; /* sound-folded badword */
8593{
8594 char_u *p;
8595 char_u badsound2[MAXWLEN];
8596 char_u fword[MAXWLEN];
8597 char_u goodsound[MAXWLEN];
8598
8599 if (stp->st_orglen <= su->su_badlen)
8600 p = badsound;
8601 else
8602 {
8603 /* soundfold the bad word with more characters following */
8604 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8605
8606 /* When joining two words the sound often changes a lot. E.g., "t he"
8607 * sounds like "t h" while "the" sounds like "@". Avoid that by
8608 * removing the space. Don't do it when the good word also contains a
8609 * space. */
8610 if (vim_iswhite(su->su_badptr[su->su_badlen])
8611 && *skiptowhite(stp->st_word) == NUL)
8612 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8613 mch_memmove(p, p + 1, STRLEN(p));
8614
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008615 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008616 p = badsound2;
8617 }
8618
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008619 /* Sound-fold the word and compute the score for the difference. */
8620 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008621
8622 return soundalike_score(goodsound, p);
8623}
8624
8625/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 * Find suggestions by comparing the word in a sound-a-like form.
8627 */
8628 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008629suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008630 suginfo_T *su;
8631{
8632 char_u salword[MAXWLEN];
8633 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008634 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008635 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 int curi[MAXWLEN];
8637 langp_T *lp;
8638 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008639 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 int depth;
8641 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008642 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008643 int round;
8644 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008645 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008647 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008648 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8649 lp->lp_slang != NULL; ++lp)
8650 {
8651 if (lp->lp_slang->sl_sal.ga_len > 0)
8652 {
8653 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008654 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008655
8656 /*
8657 * Go through the whole tree, soundfold each word and compare.
8658 * round 1: use the case-folded tree.
8659 * round 2: use the keep-case tree.
8660 */
8661 for (round = 1; round <= 2; ++round)
8662 {
8663 if (round == 1)
8664 {
8665 byts = lp->lp_slang->sl_fbyts;
8666 idxs = lp->lp_slang->sl_fidxs;
8667 }
8668 else
8669 {
8670 byts = lp->lp_slang->sl_kbyts;
8671 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008672 if (byts == NULL) /* no keep-case words */
8673 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008674 }
8675
8676 depth = 0;
8677 arridx[0] = 0;
8678 curi[0] = 1;
8679 while (depth >= 0 && !got_int)
8680 {
8681 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008683 /* Done all bytes at this node, go up one level. */
8684 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008685 line_breakcheck();
8686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 else
8688 {
8689 /* Do one more byte at this node. */
8690 n = arridx[depth] + curi[depth];
8691 ++curi[depth];
8692 c = byts[n];
8693 if (c == 0)
8694 {
8695 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008696 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008697 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8698 {
8699 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008700 /* Sound-fold. Only in keep-case tree need to
8701 * case-fold the word. */
8702 spell_soundfold(lp->lp_slang, tword,
8703 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008704
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008705 /* Compute the edit distance between the
8706 * sound-a-like words. */
8707 sound_score = soundalike_score(salword,
8708 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008709 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008710 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008711 char_u cword[MAXWLEN];
8712 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008713 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008714
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008715 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008716 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008717 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008718 /* Need to fix case according to
8719 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008721 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 }
8723 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008724 p = tword;
8725
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008726 if (sps_flags & SPS_DOUBLE)
8727 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008728 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008729 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008730 else
8731 {
8732 /* Compute the score. */
8733 score = spell_edit_score(
8734 su->su_badword, p);
8735 if (sps_flags & SPS_BEST)
8736 /* give a bonus for the good word
8737 * sounding the same as the bad
8738 * word */
8739 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008740 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008741 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008742 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008743 else
8744 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008745 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008746 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008747 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 }
8749 }
8750
8751 /* Skip over other NUL bytes. */
8752 while (byts[n + 1] == 0)
8753 {
8754 ++n;
8755 ++curi[depth];
8756 }
8757 }
8758 else
8759 {
8760 /* Normal char, go one level deeper. */
8761 tword[depth++] = c;
8762 arridx[depth] = idxs[n];
8763 curi[depth] = 1;
8764 }
8765 }
8766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008767 }
8768 }
8769 }
8770}
8771
8772/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008773 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 */
8775 static void
8776make_case_word(fword, cword, flags)
8777 char_u *fword;
8778 char_u *cword;
8779 int flags;
8780{
8781 if (flags & WF_ALLCAP)
8782 /* Make it all upper-case */
8783 allcap_copy(fword, cword);
8784 else if (flags & WF_ONECAP)
8785 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008786 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 else
8788 /* Use goodword as-is. */
8789 STRCPY(cword, fword);
8790}
8791
Bram Moolenaarea424162005-06-16 21:51:00 +00008792/*
8793 * Use map string "map" for languages "lp".
8794 */
8795 static void
8796set_map_str(lp, map)
8797 slang_T *lp;
8798 char_u *map;
8799{
8800 char_u *p;
8801 int headc = 0;
8802 int c;
8803 int i;
8804
8805 if (*map == NUL)
8806 {
8807 lp->sl_has_map = FALSE;
8808 return;
8809 }
8810 lp->sl_has_map = TRUE;
8811
8812 /* Init the array and hash table empty. */
8813 for (i = 0; i < 256; ++i)
8814 lp->sl_map_array[i] = 0;
8815#ifdef FEAT_MBYTE
8816 hash_init(&lp->sl_map_hash);
8817#endif
8818
8819 /*
8820 * The similar characters are stored separated with slashes:
8821 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8822 * before the same slash. For characters above 255 sl_map_hash is used.
8823 */
8824 for (p = map; *p != NUL; )
8825 {
8826#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008827 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008828#else
8829 c = *p++;
8830#endif
8831 if (c == '/')
8832 headc = 0;
8833 else
8834 {
8835 if (headc == 0)
8836 headc = c;
8837
8838#ifdef FEAT_MBYTE
8839 /* Characters above 255 don't fit in sl_map_array[], put them in
8840 * the hash table. Each entry is the char, a NUL the headchar and
8841 * a NUL. */
8842 if (c >= 256)
8843 {
8844 int cl = mb_char2len(c);
8845 int headcl = mb_char2len(headc);
8846 char_u *b;
8847 hash_T hash;
8848 hashitem_T *hi;
8849
8850 b = alloc((unsigned)(cl + headcl + 2));
8851 if (b == NULL)
8852 return;
8853 mb_char2bytes(c, b);
8854 b[cl] = NUL;
8855 mb_char2bytes(headc, b + cl + 1);
8856 b[cl + 1 + headcl] = NUL;
8857 hash = hash_hash(b);
8858 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8859 if (HASHITEM_EMPTY(hi))
8860 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8861 else
8862 {
8863 /* This should have been checked when generating the .spl
8864 * file. */
8865 EMSG(_("E999: duplicate char in MAP entry"));
8866 vim_free(b);
8867 }
8868 }
8869 else
8870#endif
8871 lp->sl_map_array[c] = headc;
8872 }
8873 }
8874}
8875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876/*
8877 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8878 * lines in the .aff file.
8879 */
8880 static int
8881similar_chars(slang, c1, c2)
8882 slang_T *slang;
8883 int c1;
8884 int c2;
8885{
Bram Moolenaarea424162005-06-16 21:51:00 +00008886 int m1, m2;
8887#ifdef FEAT_MBYTE
8888 char_u buf[MB_MAXBYTES];
8889 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890
Bram Moolenaarea424162005-06-16 21:51:00 +00008891 if (c1 >= 256)
8892 {
8893 buf[mb_char2bytes(c1, buf)] = 0;
8894 hi = hash_find(&slang->sl_map_hash, buf);
8895 if (HASHITEM_EMPTY(hi))
8896 m1 = 0;
8897 else
8898 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8899 }
8900 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008901#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008902 m1 = slang->sl_map_array[c1];
8903 if (m1 == 0)
8904 return FALSE;
8905
8906
8907#ifdef FEAT_MBYTE
8908 if (c2 >= 256)
8909 {
8910 buf[mb_char2bytes(c2, buf)] = 0;
8911 hi = hash_find(&slang->sl_map_hash, buf);
8912 if (HASHITEM_EMPTY(hi))
8913 m2 = 0;
8914 else
8915 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8916 }
8917 else
8918#endif
8919 m2 = slang->sl_map_array[c2];
8920
8921 return m1 == m2;
8922}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923
8924/*
8925 * Add a suggestion to the list of suggestions.
8926 * Do not add a duplicate suggestion or suggestions with a bad score.
8927 * When "use_score" is not zero it's used, otherwise the score is computed
8928 * with spell_edit_score().
8929 */
8930 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008931add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008933 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008935 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008936 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008937 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008938 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939{
8940 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008942 char_u *p = NULL;
8943 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944
8945 /* Check that the word wasn't banned. */
8946 if (was_banned(su, goodword))
8947 return;
8948
Bram Moolenaar0c405862005-06-22 22:26:26 +00008949 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8950 * Remove the common part from "goodword". */
8951 i = badlen - su->su_badlen;
8952 if (i > 0)
8953 {
8954 /* This assumes there was no case folding or it didn't change the
8955 * length... */
8956 p = goodword + STRLEN(goodword) - i;
8957 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8958 {
8959 badlen = su->su_badlen;
8960 c = *p;
8961 *p = NUL;
8962 }
8963 else
8964 p = NULL;
8965 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008966 else if (i < 0)
8967 {
8968 /* When replacing part of the word check that we actually change
8969 * something. For "the the" a suggestion can be replacing the first
8970 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008971 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008972 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8973 return;
8974 }
8975
Bram Moolenaar0c405862005-06-22 22:26:26 +00008976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977 if (score <= su->su_maxscore)
8978 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008979 /* Check if the word is already there. Also check the length that is
8980 * being replaced "thes," -> "these" is a different suggestion from
8981 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008982 stp = &SUG(*gap, 0);
8983 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008984 if (STRCMP(stp[i].st_word, goodword) == 0
8985 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 {
8987 /* Found it. Remember the lowest score. */
8988 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008991 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008992 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008993 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 break;
8995 }
8996
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008997 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 {
8999 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009000 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 stp->st_word = vim_strsave(goodword);
9002 if (stp->st_word != NULL)
9003 {
9004 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009005 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009006 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009007 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009008 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009
9010 /* If we have too many suggestions now, sort the list and keep
9011 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009012 if (gap->ga_len > SUG_MAX_COUNT(su))
9013 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
9014 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 }
9016 }
9017 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00009018
9019 if (p != NULL)
9020 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021}
9022
9023/*
9024 * Add a word to be banned.
9025 */
9026 static void
9027add_banned(su, word)
9028 suginfo_T *su;
9029 char_u *word;
9030{
9031 char_u *s = vim_strsave(word);
9032 hash_T hash;
9033 hashitem_T *hi;
9034
9035 if (s != NULL)
9036 {
9037 hash = hash_hash(s);
9038 hi = hash_lookup(&su->su_banned, s, hash);
9039 if (HASHITEM_EMPTY(hi))
9040 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009041 else
9042 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 }
9044}
9045
9046/*
9047 * Return TRUE if a word appears in the list of banned words.
9048 */
9049 static int
9050was_banned(su, word)
9051 suginfo_T *su;
9052 char_u *word;
9053{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009054 hashitem_T *hi = hash_find(&su->su_banned, word);
9055
9056 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009057}
9058
9059/*
9060 * Free the banned words in "su".
9061 */
9062 static void
9063free_banned(su)
9064 suginfo_T *su;
9065{
9066 int todo;
9067 hashitem_T *hi;
9068
9069 todo = su->su_banned.ht_used;
9070 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
9071 {
9072 if (!HASHITEM_EMPTY(hi))
9073 {
9074 vim_free(hi->hi_key);
9075 --todo;
9076 }
9077 }
9078 hash_clear(&su->su_banned);
9079}
9080
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009081/*
9082 * Recompute the score if sound-folding is possible. This is slow,
9083 * thus only done for the final results.
9084 */
9085 static void
9086rescore_suggestions(su)
9087 suginfo_T *su;
9088{
9089 langp_T *lp;
9090 suggest_T *stp;
9091 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009092 int i;
9093
9094 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9095 lp->lp_slang != NULL; ++lp)
9096 {
9097 if (lp->lp_slang->sl_sal.ga_len > 0)
9098 {
9099 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009100 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009101
9102 for (i = 0; i < su->su_ga.ga_len; ++i)
9103 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009104 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009105 if (!stp->st_had_bonus)
9106 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009107 stp->st_altscore = stp_sal_score(stp, su,
9108 lp->lp_slang, sal_badword);
9109 if (stp->st_altscore == SCORE_MAXMAX)
9110 stp->st_altscore = SCORE_BIG;
9111 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009112 }
9113 }
9114 break;
9115 }
9116 }
9117}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119static int
9120#ifdef __BORLANDC__
9121_RTLENTRYF
9122#endif
9123sug_compare __ARGS((const void *s1, const void *s2));
9124
9125/*
9126 * Function given to qsort() to sort the suggestions on st_score.
9127 */
9128 static int
9129#ifdef __BORLANDC__
9130_RTLENTRYF
9131#endif
9132sug_compare(s1, s2)
9133 const void *s1;
9134 const void *s2;
9135{
9136 suggest_T *p1 = (suggest_T *)s1;
9137 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009138 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009140 if (n == 0)
9141 return p1->st_altscore - p2->st_altscore;
9142 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143}
9144
9145/*
9146 * Cleanup the suggestions:
9147 * - Sort on score.
9148 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009149 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009151 static int
9152cleanup_suggestions(gap, maxscore, keep)
9153 garray_T *gap;
9154 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009155 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009156{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009157 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009158 int i;
9159
9160 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009161 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162
9163 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009164 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009166 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009168 gap->ga_len = keep;
9169 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009171 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172}
9173
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009174#if defined(FEAT_EVAL) || defined(PROTO)
9175/*
9176 * Soundfold a string, for soundfold().
9177 * Result is in allocated memory, NULL for an error.
9178 */
9179 char_u *
9180eval_soundfold(word)
9181 char_u *word;
9182{
9183 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009184 char_u sound[MAXWLEN];
9185
9186 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
9187 /* Use the sound-folding of the first language that supports it. */
9188 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9189 lp->lp_slang != NULL; ++lp)
9190 if (lp->lp_slang->sl_sal.ga_len > 0)
9191 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009192 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009193 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009194 return vim_strsave(sound);
9195 }
9196
9197 /* No language with sound folding, return word as-is. */
9198 return vim_strsave(word);
9199}
9200#endif
9201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202/*
9203 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9204 */
9205 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009206spell_soundfold(slang, inword, folded, res)
9207 slang_T *slang;
9208 char_u *inword;
9209 int folded; /* "inword" is already case-folded */
9210 char_u *res;
9211{
9212 char_u fword[MAXWLEN];
9213 char_u *word;
9214
9215 if (slang->sl_sofo)
9216 /* SOFOFROM and SOFOTO used */
9217 spell_soundfold_sofo(slang, inword, res);
9218 else
9219 {
9220 /* SAL items used. Requires the word to be case-folded. */
9221 if (folded)
9222 word = inword;
9223 else
9224 {
9225 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
9226 word = fword;
9227 }
9228
9229#ifdef FEAT_MBYTE
9230 if (has_mbyte)
9231 spell_soundfold_wsal(slang, word, res);
9232 else
9233#endif
9234 spell_soundfold_sal(slang, word, res);
9235 }
9236}
9237
9238/*
9239 * Perform sound folding of "inword" into "res" according to SOFOFROM and
9240 * SOFOTO lines.
9241 */
9242 static void
9243spell_soundfold_sofo(slang, inword, res)
9244 slang_T *slang;
9245 char_u *inword;
9246 char_u *res;
9247{
9248 char_u *s;
9249 int ri = 0;
9250 int c;
9251
9252#ifdef FEAT_MBYTE
9253 if (has_mbyte)
9254 {
9255 int prevc = 0;
9256 int *ip;
9257
9258 /* The sl_sal_first[] table contains the translation for chars up to
9259 * 255, sl_sal the rest. */
9260 for (s = inword; *s != NUL; )
9261 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009262 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009263 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9264 c = ' ';
9265 else if (c < 256)
9266 c = slang->sl_sal_first[c];
9267 else
9268 {
9269 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
9270 if (ip == NULL) /* empty list, can't match */
9271 c = NUL;
9272 else
9273 for (;;) /* find "c" in the list */
9274 {
9275 if (*ip == 0) /* not found */
9276 {
9277 c = NUL;
9278 break;
9279 }
9280 if (*ip == c) /* match! */
9281 {
9282 c = ip[1];
9283 break;
9284 }
9285 ip += 2;
9286 }
9287 }
9288
9289 if (c != NUL && c != prevc)
9290 {
9291 ri += mb_char2bytes(c, res + ri);
9292 if (ri + MB_MAXBYTES > MAXWLEN)
9293 break;
9294 prevc = c;
9295 }
9296 }
9297 }
9298 else
9299#endif
9300 {
9301 /* The sl_sal_first[] table contains the translation. */
9302 for (s = inword; (c = *s) != NUL; ++s)
9303 {
9304 if (vim_iswhite(c))
9305 c = ' ';
9306 else
9307 c = slang->sl_sal_first[c];
9308 if (c != NUL && (ri == 0 || res[ri - 1] != c))
9309 res[ri++] = c;
9310 }
9311 }
9312
9313 res[ri] = NUL;
9314}
9315
9316 static void
9317spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 slang_T *slang;
9319 char_u *inword;
9320 char_u *res;
9321{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009322 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009324 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009326 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009328 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 int n, k = 0;
9330 int z0;
9331 int k0;
9332 int n0;
9333 int c;
9334 int pri;
9335 int p0 = -333;
9336 int c0;
9337
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009338 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009339 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009340 if (slang->sl_rem_accents)
9341 {
9342 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009343 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009345 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009346 {
9347 *t++ = ' ';
9348 s = skipwhite(s);
9349 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009350 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009352 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353 *t++ = *s;
9354 ++s;
9355 }
9356 }
9357 *t = NUL;
9358 }
9359 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009360 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009362 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363
9364 /*
9365 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009366 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009368 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 while ((c = word[i]) != NUL)
9370 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009371 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372 n = slang->sl_sal_first[c];
9373 z0 = 0;
9374
9375 if (n >= 0)
9376 {
9377 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009378 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009380 /* Quickly skip entries that don't match the word. Most
9381 * entries are less then three chars, optimize for that. */
9382 k = smp[n].sm_leadlen;
9383 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009385 if (word[i + 1] != s[1])
9386 continue;
9387 if (k > 2)
9388 {
9389 for (j = 2; j < k; ++j)
9390 if (word[i + j] != s[j])
9391 break;
9392 if (j < k)
9393 continue;
9394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009395 }
9396
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009397 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009399 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009400 while (*pf != NUL && *pf != word[i + k])
9401 ++pf;
9402 if (*pf == NUL)
9403 continue;
9404 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009406 s = smp[n].sm_rules;
9407 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408
9409 p0 = *s;
9410 k0 = k;
9411 while (*s == '-' && k > 1)
9412 {
9413 k--;
9414 s++;
9415 }
9416 if (*s == '<')
9417 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009418 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 {
9420 /* determine priority */
9421 pri = *s - '0';
9422 s++;
9423 }
9424 if (*s == '^' && *(s + 1) == '^')
9425 s++;
9426
9427 if (*s == NUL
9428 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009429 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009430 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009432 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009434 && spell_iswordp(word + i - 1, curbuf)
9435 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009436 {
9437 /* search for followup rules, if: */
9438 /* followup and k > 1 and NO '-' in searchstring */
9439 c0 = word[i + k - 1];
9440 n0 = slang->sl_sal_first[c0];
9441
9442 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009443 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009444 {
9445 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009446 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009448 /* Quickly skip entries that don't match the word.
9449 * */
9450 k0 = smp[n0].sm_leadlen;
9451 if (k0 > 1)
9452 {
9453 if (word[i + k] != s[1])
9454 continue;
9455 if (k0 > 2)
9456 {
9457 pf = word + i + k + 1;
9458 for (j = 2; j < k0; ++j)
9459 if (*pf++ != s[j])
9460 break;
9461 if (j < k0)
9462 continue;
9463 }
9464 }
9465 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009467 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009468 {
9469 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009470 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009471 while (*pf != NUL && *pf != word[i + k0])
9472 ++pf;
9473 if (*pf == NUL)
9474 continue;
9475 ++k0;
9476 }
9477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009479 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 while (*s == '-')
9481 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009482 /* "k0" gets NOT reduced because
9483 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009484 s++;
9485 }
9486 if (*s == '<')
9487 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009488 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489 {
9490 p0 = *s - '0';
9491 s++;
9492 }
9493
9494 if (*s == NUL
9495 /* *s == '^' cuts */
9496 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009497 && !spell_iswordp(word + i + k0,
9498 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 {
9500 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009501 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503
9504 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 /* rule fits; stop search */
9508 break;
9509 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 }
9511
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009512 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 }
9515
9516 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009517 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009518 if (s == NULL)
9519 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009520 pf = smp[n].sm_rules;
9521 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 if (p0 == 1 && z == 0)
9523 {
9524 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009525 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9526 || res[reslen - 1] == *s))
9527 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009528 z0 = 1;
9529 z = 1;
9530 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009531 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009532 {
9533 word[i + k0] = *s;
9534 k0++;
9535 s++;
9536 }
9537 if (k > k0)
9538 mch_memmove(word + i + k0, word + i + k,
9539 STRLEN(word + i + k) + 1);
9540
9541 /* new "actual letter" */
9542 c = word[i];
9543 }
9544 else
9545 {
9546 /* no '<' rule used */
9547 i += k - 1;
9548 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009549 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009551 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009552 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 s++;
9554 }
9555 /* new "actual letter" */
9556 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009557 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009558 {
9559 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009560 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 mch_memmove(word, word + i + 1,
9562 STRLEN(word + i + 1) + 1);
9563 i = 0;
9564 z0 = 1;
9565 }
9566 }
9567 break;
9568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 }
9570 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009571 else if (vim_iswhite(c))
9572 {
9573 c = ' ';
9574 k = 1;
9575 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576
9577 if (z0 == 0)
9578 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009579 if (k && !p0 && reslen < MAXWLEN && c != NUL
9580 && (!slang->sl_collapse || reslen == 0
9581 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009583 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584
9585 i++;
9586 z = 0;
9587 k = 0;
9588 }
9589 }
9590
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009591 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592}
9593
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009594#ifdef FEAT_MBYTE
9595/*
9596 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9597 * Multi-byte version of spell_soundfold().
9598 */
9599 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009600spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009601 slang_T *slang;
9602 char_u *inword;
9603 char_u *res;
9604{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009605 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009606 int word[MAXWLEN];
9607 int wres[MAXWLEN];
9608 int l;
9609 char_u *s;
9610 int *ws;
9611 char_u *t;
9612 int *pf;
9613 int i, j, z;
9614 int reslen;
9615 int n, k = 0;
9616 int z0;
9617 int k0;
9618 int n0;
9619 int c;
9620 int pri;
9621 int p0 = -333;
9622 int c0;
9623 int did_white = FALSE;
9624
9625 /*
9626 * Convert the multi-byte string to a wide-character string.
9627 * Remove accents, if wanted. We actually remove all non-word characters.
9628 * But keep white space.
9629 */
9630 n = 0;
9631 for (s = inword; *s != NUL; )
9632 {
9633 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009634 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009635 if (slang->sl_rem_accents)
9636 {
9637 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9638 {
9639 if (did_white)
9640 continue;
9641 c = ' ';
9642 did_white = TRUE;
9643 }
9644 else
9645 {
9646 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009647 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009648 continue;
9649 }
9650 }
9651 word[n++] = c;
9652 }
9653 word[n] = NUL;
9654
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009655 /*
9656 * This comes from Aspell phonet.cpp.
9657 * Converted from C++ to C. Added support for multi-byte chars.
9658 * Changed to keep spaces.
9659 */
9660 i = reslen = z = 0;
9661 while ((c = word[i]) != NUL)
9662 {
9663 /* Start with the first rule that has the character in the word. */
9664 n = slang->sl_sal_first[c & 0xff];
9665 z0 = 0;
9666
9667 if (n >= 0)
9668 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009669 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009670 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9671 {
9672 /* Quickly skip entries that don't match the word. Most
9673 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009674 if (c != ws[0])
9675 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009676 k = smp[n].sm_leadlen;
9677 if (k > 1)
9678 {
9679 if (word[i + 1] != ws[1])
9680 continue;
9681 if (k > 2)
9682 {
9683 for (j = 2; j < k; ++j)
9684 if (word[i + j] != ws[j])
9685 break;
9686 if (j < k)
9687 continue;
9688 }
9689 }
9690
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009691 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009692 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009693 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009694 while (*pf != NUL && *pf != word[i + k])
9695 ++pf;
9696 if (*pf == NUL)
9697 continue;
9698 ++k;
9699 }
9700 s = smp[n].sm_rules;
9701 pri = 5; /* default priority */
9702
9703 p0 = *s;
9704 k0 = k;
9705 while (*s == '-' && k > 1)
9706 {
9707 k--;
9708 s++;
9709 }
9710 if (*s == '<')
9711 s++;
9712 if (VIM_ISDIGIT(*s))
9713 {
9714 /* determine priority */
9715 pri = *s - '0';
9716 s++;
9717 }
9718 if (*s == '^' && *(s + 1) == '^')
9719 s++;
9720
9721 if (*s == NUL
9722 || (*s == '^'
9723 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009724 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009725 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009726 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009727 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009728 && spell_iswordp_w(word + i - 1, curbuf)
9729 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009730 {
9731 /* search for followup rules, if: */
9732 /* followup and k > 1 and NO '-' in searchstring */
9733 c0 = word[i + k - 1];
9734 n0 = slang->sl_sal_first[c0 & 0xff];
9735
9736 if (slang->sl_followup && k > 1 && n0 >= 0
9737 && p0 != '-' && word[i + k] != NUL)
9738 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009739 /* Test follow-up rule for "word[i + k]"; loop over
9740 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009741 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9742 == (c0 & 0xff); ++n0)
9743 {
9744 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009745 */
9746 if (c0 != ws[0])
9747 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009748 k0 = smp[n0].sm_leadlen;
9749 if (k0 > 1)
9750 {
9751 if (word[i + k] != ws[1])
9752 continue;
9753 if (k0 > 2)
9754 {
9755 pf = word + i + k + 1;
9756 for (j = 2; j < k0; ++j)
9757 if (*pf++ != ws[j])
9758 break;
9759 if (j < k0)
9760 continue;
9761 }
9762 }
9763 k0 += k - 1;
9764
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009765 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009766 {
9767 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009768 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009769 while (*pf != NUL && *pf != word[i + k0])
9770 ++pf;
9771 if (*pf == NUL)
9772 continue;
9773 ++k0;
9774 }
9775
9776 p0 = 5;
9777 s = smp[n0].sm_rules;
9778 while (*s == '-')
9779 {
9780 /* "k0" gets NOT reduced because
9781 * "if (k0 == k)" */
9782 s++;
9783 }
9784 if (*s == '<')
9785 s++;
9786 if (VIM_ISDIGIT(*s))
9787 {
9788 p0 = *s - '0';
9789 s++;
9790 }
9791
9792 if (*s == NUL
9793 /* *s == '^' cuts */
9794 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009795 && !spell_iswordp_w(word + i + k0,
9796 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009797 {
9798 if (k0 == k)
9799 /* this is just a piece of the string */
9800 continue;
9801
9802 if (p0 < pri)
9803 /* priority too low */
9804 continue;
9805 /* rule fits; stop search */
9806 break;
9807 }
9808 }
9809
9810 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9811 == (c0 & 0xff))
9812 continue;
9813 }
9814
9815 /* replace string */
9816 ws = smp[n].sm_to_w;
9817 s = smp[n].sm_rules;
9818 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9819 if (p0 == 1 && z == 0)
9820 {
9821 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009822 if (reslen > 0 && ws != NULL && *ws != NUL
9823 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009824 || wres[reslen - 1] == *ws))
9825 reslen--;
9826 z0 = 1;
9827 z = 1;
9828 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009829 if (ws != NULL)
9830 while (*ws != NUL && word[i + k0] != NUL)
9831 {
9832 word[i + k0] = *ws;
9833 k0++;
9834 ws++;
9835 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009836 if (k > k0)
9837 mch_memmove(word + i + k0, word + i + k,
9838 sizeof(int) * (STRLEN(word + i + k) + 1));
9839
9840 /* new "actual letter" */
9841 c = word[i];
9842 }
9843 else
9844 {
9845 /* no '<' rule used */
9846 i += k - 1;
9847 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009848 if (ws != NULL)
9849 while (*ws != NUL && ws[1] != NUL
9850 && reslen < MAXWLEN)
9851 {
9852 if (reslen == 0 || wres[reslen - 1] != *ws)
9853 wres[reslen++] = *ws;
9854 ws++;
9855 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009856 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009857 if (ws == NULL)
9858 c = NUL;
9859 else
9860 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009861 if (strstr((char *)s, "^^") != NULL)
9862 {
9863 if (c != NUL)
9864 wres[reslen++] = c;
9865 mch_memmove(word, word + i + 1,
9866 sizeof(int) * (STRLEN(word + i + 1) + 1));
9867 i = 0;
9868 z0 = 1;
9869 }
9870 }
9871 break;
9872 }
9873 }
9874 }
9875 else if (vim_iswhite(c))
9876 {
9877 c = ' ';
9878 k = 1;
9879 }
9880
9881 if (z0 == 0)
9882 {
9883 if (k && !p0 && reslen < MAXWLEN && c != NUL
9884 && (!slang->sl_collapse || reslen == 0
9885 || wres[reslen - 1] != c))
9886 /* condense only double letters */
9887 wres[reslen++] = c;
9888
9889 i++;
9890 z = 0;
9891 k = 0;
9892 }
9893 }
9894
9895 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9896 l = 0;
9897 for (n = 0; n < reslen; ++n)
9898 {
9899 l += mb_char2bytes(wres[n], res + l);
9900 if (l + MB_MAXBYTES > MAXWLEN)
9901 break;
9902 }
9903 res[l] = NUL;
9904}
9905#endif
9906
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009907/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009908 * Compute a score for two sound-a-like words.
9909 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9910 * Instead of a generic loop we write out the code. That keeps it fast by
9911 * avoiding checks that will not be possible.
9912 */
9913 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009914soundalike_score(goodstart, badstart)
9915 char_u *goodstart; /* sound-folded good word */
9916 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009917{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009918 char_u *goodsound = goodstart;
9919 char_u *badsound = badstart;
9920 int goodlen;
9921 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009922 int n;
9923 char_u *pl, *ps;
9924 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009925 int score = 0;
9926
9927 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9928 * counted so much, vowels halfway the word aren't counted at all. */
9929 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9930 {
9931 score = SCORE_DEL / 2;
9932 if (*badsound == '*')
9933 ++badsound;
9934 else
9935 ++goodsound;
9936 }
9937
9938 goodlen = STRLEN(goodsound);
9939 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009940
9941 /* Return quickly if the lenghts are too different to be fixed by two
9942 * changes. */
9943 n = goodlen - badlen;
9944 if (n < -2 || n > 2)
9945 return SCORE_MAXMAX;
9946
9947 if (n > 0)
9948 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009949 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009950 ps = badsound;
9951 }
9952 else
9953 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009954 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009955 ps = goodsound;
9956 }
9957
9958 /* Skip over the identical part. */
9959 while (*pl == *ps && *pl != NUL)
9960 {
9961 ++pl;
9962 ++ps;
9963 }
9964
9965 switch (n)
9966 {
9967 case -2:
9968 case 2:
9969 /*
9970 * Must delete two characters from "pl".
9971 */
9972 ++pl; /* first delete */
9973 while (*pl == *ps)
9974 {
9975 ++pl;
9976 ++ps;
9977 }
9978 /* strings must be equal after second delete */
9979 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009980 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009981
9982 /* Failed to compare. */
9983 break;
9984
9985 case -1:
9986 case 1:
9987 /*
9988 * Minimal one delete from "pl" required.
9989 */
9990
9991 /* 1: delete */
9992 pl2 = pl + 1;
9993 ps2 = ps;
9994 while (*pl2 == *ps2)
9995 {
9996 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009997 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009998 ++pl2;
9999 ++ps2;
10000 }
10001
10002 /* 2: delete then swap, then rest must be equal */
10003 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10004 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010005 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010006
10007 /* 3: delete then substitute, then the rest must be equal */
10008 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010009 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010010
10011 /* 4: first swap then delete */
10012 if (pl[0] == ps[1] && pl[1] == ps[0])
10013 {
10014 pl2 = pl + 2; /* swap, skip two chars */
10015 ps2 = ps + 2;
10016 while (*pl2 == *ps2)
10017 {
10018 ++pl2;
10019 ++ps2;
10020 }
10021 /* delete a char and then strings must be equal */
10022 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010023 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010024 }
10025
10026 /* 5: first substitute then delete */
10027 pl2 = pl + 1; /* substitute, skip one char */
10028 ps2 = ps + 1;
10029 while (*pl2 == *ps2)
10030 {
10031 ++pl2;
10032 ++ps2;
10033 }
10034 /* delete a char and then strings must be equal */
10035 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010036 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010037
10038 /* Failed to compare. */
10039 break;
10040
10041 case 0:
10042 /*
10043 * Lenghts are equal, thus changes must result in same length: An
10044 * insert is only possible in combination with a delete.
10045 * 1: check if for identical strings
10046 */
10047 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010048 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010049
10050 /* 2: swap */
10051 if (pl[0] == ps[1] && pl[1] == ps[0])
10052 {
10053 pl2 = pl + 2; /* swap, skip two chars */
10054 ps2 = ps + 2;
10055 while (*pl2 == *ps2)
10056 {
10057 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010058 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010059 ++pl2;
10060 ++ps2;
10061 }
10062 /* 3: swap and swap again */
10063 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10064 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010065 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010066
10067 /* 4: swap and substitute */
10068 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010069 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010070 }
10071
10072 /* 5: substitute */
10073 pl2 = pl + 1;
10074 ps2 = ps + 1;
10075 while (*pl2 == *ps2)
10076 {
10077 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010078 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010079 ++pl2;
10080 ++ps2;
10081 }
10082
10083 /* 6: substitute and swap */
10084 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10085 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010086 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010087
10088 /* 7: substitute and substitute */
10089 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010090 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010091
10092 /* 8: insert then delete */
10093 pl2 = pl;
10094 ps2 = ps + 1;
10095 while (*pl2 == *ps2)
10096 {
10097 ++pl2;
10098 ++ps2;
10099 }
10100 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010101 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010102
10103 /* 9: delete then insert */
10104 pl2 = pl + 1;
10105 ps2 = ps;
10106 while (*pl2 == *ps2)
10107 {
10108 ++pl2;
10109 ++ps2;
10110 }
10111 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010112 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010113
10114 /* Failed to compare. */
10115 break;
10116 }
10117
10118 return SCORE_MAXMAX;
10119}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010121/*
10122 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010123 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010124 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010126 * It has been converted from C++ to C and modified to support multi-byte
10127 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 */
10129 static int
10130spell_edit_score(badword, goodword)
10131 char_u *badword;
10132 char_u *goodword;
10133{
10134 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010135 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 int j, i;
10137 int t;
10138 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010139 int pbc, pgc;
10140#ifdef FEAT_MBYTE
10141 char_u *p;
10142 int wbadword[MAXWLEN];
10143 int wgoodword[MAXWLEN];
10144
10145 if (has_mbyte)
10146 {
10147 /* Get the characters from the multi-byte strings and put them in an
10148 * int array for easy access. */
10149 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010150 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010151 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010152 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010153 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010154 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010155 }
10156 else
10157#endif
10158 {
10159 badlen = STRLEN(badword) + 1;
10160 goodlen = STRLEN(goodword) + 1;
10161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162
10163 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
10164#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
10166 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010167 if (cnt == NULL)
10168 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169
10170 CNT(0, 0) = 0;
10171 for (j = 1; j <= goodlen; ++j)
10172 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
10173
10174 for (i = 1; i <= badlen; ++i)
10175 {
10176 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
10177 for (j = 1; j <= goodlen; ++j)
10178 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010179#ifdef FEAT_MBYTE
10180 if (has_mbyte)
10181 {
10182 bc = wbadword[i - 1];
10183 gc = wgoodword[j - 1];
10184 }
10185 else
10186#endif
10187 {
10188 bc = badword[i - 1];
10189 gc = goodword[j - 1];
10190 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 if (bc == gc)
10192 CNT(i, j) = CNT(i - 1, j - 1);
10193 else
10194 {
10195 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010196 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
10198 else
10199 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
10200
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010201 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010203#ifdef FEAT_MBYTE
10204 if (has_mbyte)
10205 {
10206 pbc = wbadword[i - 2];
10207 pgc = wgoodword[j - 2];
10208 }
10209 else
10210#endif
10211 {
10212 pbc = badword[i - 2];
10213 pgc = goodword[j - 2];
10214 }
10215 if (bc == pgc && pbc == gc)
10216 {
10217 t = SCORE_SWAP + CNT(i - 2, j - 2);
10218 if (t < CNT(i, j))
10219 CNT(i, j) = t;
10220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 }
10222 t = SCORE_DEL + CNT(i - 1, j);
10223 if (t < CNT(i, j))
10224 CNT(i, j) = t;
10225 t = SCORE_INS + CNT(i, j - 1);
10226 if (t < CNT(i, j))
10227 CNT(i, j) = t;
10228 }
10229 }
10230 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010231
10232 i = CNT(badlen - 1, goodlen - 1);
10233 vim_free(cnt);
10234 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010236
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010237/*
10238 * ":spelldump"
10239 */
10240/*ARGSUSED*/
10241 void
10242ex_spelldump(eap)
10243 exarg_T *eap;
10244{
10245 buf_T *buf = curbuf;
10246 langp_T *lp;
10247 slang_T *slang;
10248 idx_T arridx[MAXWLEN];
10249 int curi[MAXWLEN];
10250 char_u word[MAXWLEN];
10251 int c;
10252 char_u *byts;
10253 idx_T *idxs;
10254 linenr_T lnum = 0;
10255 int round;
10256 int depth;
10257 int n;
10258 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010259 char_u *region_names = NULL; /* region names being used */
10260 int do_region = TRUE; /* dump region names and numbers */
10261 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010262
10263 if (no_spell_checking())
10264 return;
10265
10266 /* Create a new empty buffer by splitting the window. */
10267 do_cmdline_cmd((char_u *)"new");
10268 if (!bufempty() || !buf_valid(buf))
10269 return;
10270
Bram Moolenaar7887d882005-07-01 22:33:52 +000010271 /* Find out if we can support regions: All languages must support the same
10272 * regions or none at all. */
10273 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10274 {
10275 p = lp->lp_slang->sl_regions;
10276 if (p[0] != 0)
10277 {
10278 if (region_names == NULL) /* first language with regions */
10279 region_names = p;
10280 else if (STRCMP(region_names, p) != 0)
10281 {
10282 do_region = FALSE; /* region names are different */
10283 break;
10284 }
10285 }
10286 }
10287
10288 if (do_region && region_names != NULL)
10289 {
10290 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
10291 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10292 }
10293 else
10294 do_region = FALSE;
10295
10296 /*
10297 * Loop over all files loaded for the entries in 'spelllang'.
10298 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010299 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10300 {
10301 slang = lp->lp_slang;
10302
10303 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
10304 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10305
10306 /* round 1: case-folded tree
10307 * round 2: keep-case tree */
10308 for (round = 1; round <= 2; ++round)
10309 {
10310 if (round == 1)
10311 {
10312 byts = slang->sl_fbyts;
10313 idxs = slang->sl_fidxs;
10314 }
10315 else
10316 {
10317 byts = slang->sl_kbyts;
10318 idxs = slang->sl_kidxs;
10319 }
10320 if (byts == NULL)
10321 continue; /* array is empty */
10322
10323 depth = 0;
10324 arridx[0] = 0;
10325 curi[0] = 1;
10326 while (depth >= 0 && !got_int)
10327 {
10328 if (curi[depth] > byts[arridx[depth]])
10329 {
10330 /* Done all bytes at this node, go up one level. */
10331 --depth;
10332 line_breakcheck();
10333 }
10334 else
10335 {
10336 /* Do one more byte at this node. */
10337 n = arridx[depth] + curi[depth];
10338 ++curi[depth];
10339 c = byts[n];
10340 if (c == 0)
10341 {
10342 /* End of word, deal with the word.
10343 * Don't use keep-case words in the fold-case tree,
10344 * they will appear in the keep-case tree.
10345 * Only use the word when the region matches. */
10346 flags = (int)idxs[n];
10347 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000010348 && (do_region
10349 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010350 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010351 & lp->lp_region) != 0))
10352 {
10353 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010354 if (!do_region)
10355 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010356
10357 /* Dump the basic word if there is no prefix or
10358 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010359 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010360 if (c == 0 || curi[depth] == 2)
10361 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010362
10363 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010364 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010365 lnum = apply_prefixes(slang, word, round,
10366 flags, lnum);
10367 }
10368 }
10369 else
10370 {
10371 /* Normal char, go one level deeper. */
10372 word[depth++] = c;
10373 arridx[depth] = idxs[n];
10374 curi[depth] = 1;
10375 }
10376 }
10377 }
10378 }
10379 }
10380
10381 /* Delete the empty line that we started with. */
10382 if (curbuf->b_ml.ml_line_count > 1)
10383 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10384
10385 redraw_later(NOT_VALID);
10386}
10387
10388/*
10389 * Dump one word: apply case modifications and append a line to the buffer.
10390 */
10391 static void
10392dump_word(word, round, flags, lnum)
10393 char_u *word;
10394 int round;
10395 int flags;
10396 linenr_T lnum;
10397{
10398 int keepcap = FALSE;
10399 char_u *p;
10400 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010401 char_u badword[MAXWLEN + 10];
10402 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010403
10404 if (round == 1 && (flags & WF_CAPMASK) != 0)
10405 {
10406 /* Need to fix case according to "flags". */
10407 make_case_word(word, cword, flags);
10408 p = cword;
10409 }
10410 else
10411 {
10412 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010413 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10414 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010415 keepcap = TRUE;
10416 }
10417
Bram Moolenaar7887d882005-07-01 22:33:52 +000010418 /* Add flags and regions after a slash. */
10419 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010420 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010421 STRCPY(badword, p);
10422 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010423 if (keepcap)
10424 STRCAT(badword, "=");
10425 if (flags & WF_BANNED)
10426 STRCAT(badword, "!");
10427 else if (flags & WF_RARE)
10428 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010429 if (flags & WF_REGION)
10430 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010431 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010432 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010433 p = badword;
10434 }
10435
10436 ml_append(lnum, p, (colnr_T)0, FALSE);
10437}
10438
10439/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010440 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10441 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010442 * Return the updated line number.
10443 */
10444 static linenr_T
10445apply_prefixes(slang, word, round, flags, startlnum)
10446 slang_T *slang;
10447 char_u *word; /* case-folded word */
10448 int round;
10449 int flags; /* flags with prefix ID */
10450 linenr_T startlnum;
10451{
10452 idx_T arridx[MAXWLEN];
10453 int curi[MAXWLEN];
10454 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010455 char_u word_up[MAXWLEN];
10456 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010457 int c;
10458 char_u *byts;
10459 idx_T *idxs;
10460 linenr_T lnum = startlnum;
10461 int depth;
10462 int n;
10463 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010464 int i;
10465
Bram Moolenaar53805d12005-08-01 07:08:33 +000010466 /* if the word starts with a lower-case letter make the word with an
10467 * upper-case letter in word_up[]. */
10468 c = PTR2CHAR(word);
10469 if (SPELL_TOUPPER(c) != c)
10470 {
10471 onecap_copy(word, word_up, TRUE);
10472 has_word_up = TRUE;
10473 }
10474
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010475 byts = slang->sl_pbyts;
10476 idxs = slang->sl_pidxs;
10477 if (byts != NULL) /* array not is empty */
10478 {
10479 /*
10480 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010481 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010482 */
10483 depth = 0;
10484 arridx[0] = 0;
10485 curi[0] = 1;
10486 while (depth >= 0 && !got_int)
10487 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010488 n = arridx[depth];
10489 len = byts[n];
10490 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010491 {
10492 /* Done all bytes at this node, go up one level. */
10493 --depth;
10494 line_breakcheck();
10495 }
10496 else
10497 {
10498 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010499 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010500 ++curi[depth];
10501 c = byts[n];
10502 if (c == 0)
10503 {
10504 /* End of prefix, find out how many IDs there are. */
10505 for (i = 1; i < len; ++i)
10506 if (byts[n + i] != 0)
10507 break;
10508 curi[depth] += i - 1;
10509
Bram Moolenaar53805d12005-08-01 07:08:33 +000010510 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10511 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010512 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010513 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010514 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010515 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010516 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010517 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010518
10519 /* Check for prefix that matches the word when the
10520 * first letter is upper-case, but only if the prefix has
10521 * a condition. */
10522 if (has_word_up)
10523 {
10524 c = valid_word_prefix(i, n, flags, word_up, slang,
10525 TRUE);
10526 if (c != 0)
10527 {
10528 vim_strncpy(prefix + depth, word_up,
10529 MAXWLEN - depth - 1);
10530 dump_word(prefix, round,
10531 (c & WF_RAREPFX) ? (flags | WF_RARE)
10532 : flags, lnum++);
10533 }
10534 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010535 }
10536 else
10537 {
10538 /* Normal char, go one level deeper. */
10539 prefix[depth++] = c;
10540 arridx[depth] = idxs[n];
10541 curi[depth] = 1;
10542 }
10543 }
10544 }
10545 }
10546
10547 return lnum;
10548}
10549
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010550#endif /* FEAT_SYN_HL */