blob: 10390ece6d4c940bad9d36299749a75aa9fed592 [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 Moolenaare5b8e3d2005-08-12 19:48:49 +0000477#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000478#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000479#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000480#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000481#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000482#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000483#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000484#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000486#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
488 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489
490#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000491#define SCORE_MAXMAX 999999 /* accept any score */
492
493/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000494 * Structure to store info for word matching.
495 */
496typedef struct matchinf_S
497{
498 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000499
500 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000501 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000502 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000503 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000504 char_u *mi_cend; /* char after what was used for
505 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000506
507 /* case-folded text */
508 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000509 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000510
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000511 /* for when checking word after a prefix */
512 int mi_prefarridx; /* index in sl_pidxs with list of
513 prefixID/condition */
514 int mi_prefcnt; /* number of entries at mi_prefarridx */
515 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000516#ifdef FEAT_MBYTE
517 int mi_cprefixlen; /* byte length of prefix in original
518 case */
519#else
520# define mi_cprefixlen mi_prefixlen /* it's the same value */
521#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000522
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000523 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000524 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000525 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000526 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000527} matchinf_T;
528
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000529/*
530 * The tables used for recognizing word characters according to spelling.
531 * These are only used for the first 256 characters of 'encoding'.
532 */
533typedef struct spelltab_S
534{
535 char_u st_isw[256]; /* flags: is word char */
536 char_u st_isu[256]; /* flags: is uppercase char */
537 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000538 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000539} spelltab_T;
540
541static spelltab_T spelltab;
542static int did_set_spelltab;
543
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000544#define CF_WORD 0x01
545#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000546
547static void clear_spell_chartab __ARGS((spelltab_T *sp));
548static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000549static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
550static int spell_iswordp_nmw __ARGS((char_u *p));
551#ifdef FEAT_MBYTE
552static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
553#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000554static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000555
556/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000557 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000558 */
559typedef enum
560{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000561 STATE_START = 0, /* At start of node check for NUL bytes (goodword
562 * ends); if badword ends there is a match, otherwise
563 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000564 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000565 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000566 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
567 STATE_PLAIN, /* Use each byte of the node. */
568 STATE_DEL, /* Delete a byte from the bad word. */
569 STATE_INS, /* Insert a byte in the bad word. */
570 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000571 STATE_UNSWAP, /* Undo swap two characters. */
572 STATE_SWAP3, /* Swap two characters over three. */
573 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000574 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000575 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000576 STATE_REP_INI, /* Prepare for using REP items. */
577 STATE_REP, /* Use matching REP items from the .aff file. */
578 STATE_REP_UNDO, /* Undo a REP item replacement. */
579 STATE_FINAL /* End of this node. */
580} state_T;
581
582/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000583 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000584 */
585typedef struct trystate_S
586{
Bram Moolenaarea424162005-06-16 21:51:00 +0000587 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000588 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000590 short ts_curi; /* index in list of child nodes */
591 char_u ts_fidx; /* index in fword[], case-folded bad word */
592 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
593 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000594 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
595 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000596#ifdef FEAT_MBYTE
597 char_u ts_tcharlen; /* number of bytes in tword character */
598 char_u ts_tcharidx; /* current byte index in tword character */
599 char_u ts_isdiff; /* DIFF_ values */
600 char_u ts_fcharstart; /* index in fword where badword char started */
601#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000602 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000603 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000604 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605} trystate_T;
606
Bram Moolenaarea424162005-06-16 21:51:00 +0000607/* values for ts_isdiff */
608#define DIFF_NONE 0 /* no different byte (yet) */
609#define DIFF_YES 1 /* different byte found */
610#define DIFF_INSERT 2 /* inserting character */
611
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000612/* special values ts_prefixdepth */
613#define PREFIXTREE 0xfe /* walking through the prefix tree */
614#define NOPREFIX 0xff /* not using prefixes */
615
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000616/* mode values for find_word */
617#define FIND_FOLDWORD 0 /* find word case-folded */
618#define FIND_KEEPWORD 1 /* find keep-case word */
619#define FIND_PREFIX 2 /* find word after prefix */
620
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000621static slang_T *slang_alloc __ARGS((char_u *lang));
622static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000623static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000624static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000625static 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 +0000626static void find_prefix __ARGS((matchinf_T *mip));
627static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000628static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000629static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000630static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000631static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000632static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000633static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000634static 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 +0000635static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000636static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
637static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000638#ifdef FEAT_MBYTE
639static int *mb_str2wide __ARGS((char_u *s));
640#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000641static 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 +0000642static void clear_midword __ARGS((buf_T *buf));
643static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000644static int find_region __ARGS((char_u *rp, char_u *region));
645static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000646static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000648static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000649static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
650static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000651static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000652static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000653static 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 +0000654#ifdef FEAT_EVAL
655static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
656#endif
657static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
658static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000659static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000660static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000661static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000662static void suggest_try_special __ARGS((suginfo_T *su));
663static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000665#ifdef FEAT_MBYTE
666static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
667#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000668static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000669static void score_comp_sal __ARGS((suginfo_T *su));
670static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000671static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000672static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000673static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000674static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000675static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000676static 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 +0000677static void add_banned __ARGS((suginfo_T *su, char_u *word));
678static int was_banned __ARGS((suginfo_T *su, char_u *word));
679static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000680static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000681static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000682static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
683static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
684static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000685#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000686static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000687#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000688static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000690static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
691static 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 +0000692
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000693/*
694 * Use our own character-case definitions, because the current locale may
695 * differ from what the .spl file uses.
696 * These must not be called with negative number!
697 */
698#ifndef FEAT_MBYTE
699/* Non-multi-byte implementation. */
700# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
701# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
702# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
703#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000704# if defined(HAVE_WCHAR_H)
705# include <wchar.h> /* for towupper() and towlower() */
706# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000707/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
708 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
709 * the "w" library function for characters above 255 if available. */
710# ifdef HAVE_TOWLOWER
711# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
712 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
713# else
714# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
715 : (c) < 256 ? spelltab.st_fold[c] : (c))
716# endif
717
718# ifdef HAVE_TOWUPPER
719# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
720 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
721# else
722# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
723 : (c) < 256 ? spelltab.st_upper[c] : (c))
724# endif
725
726# ifdef HAVE_ISWUPPER
727# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
728 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
729# else
730# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000731 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000732# endif
733#endif
734
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000735
736static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000737static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000738static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000739
740/*
741 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000742 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000743 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
744 * or when it's OK it remains unchanged.
745 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000746 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000747 * "capcol" is used to check for a Capitalised word after the end of a
748 * sentence. If it's zero then perform the check. Return the column where to
749 * check next, or -1 when no sentence end was found. If it's NULL then don't
750 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000751 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000752 * Returns the length of the word in bytes, also when it's OK, so that the
753 * caller can skip over the word.
754 */
755 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000756spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000757 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000758 char_u *ptr;
759 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000760 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000761{
762 matchinf_T mi; /* Most things are put in "mi" so that it can
763 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000764 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000765 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000767 /* A word never starts at a space or a control character. Return quickly
768 * then, skipping over the character. */
769 if (*ptr <= ' ')
770 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000771
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000772 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000773 * 0X99FF. But when a word character follows do check spelling to find
774 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000775 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000776 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000777 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
778 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000779 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000781 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000782 nrlen = mi.mi_end - ptr;
783 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000784 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000785 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000786
787 /* Try including the digits in the word. */
788 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000789 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000790 else
791 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000792
Bram Moolenaar0c405862005-06-22 22:26:26 +0000793 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000795 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000796 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000797 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000798 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000799 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000800 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000801
802 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
803 {
804 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000805 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000806 if (!SPELL_ISUPPER(c))
807 {
808 *attrp = highlight_attr[HLF_SPC];
809 return (int)(mi.mi_fend - ptr);
810 }
811 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000812 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000813 if (capcol != NULL)
814 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000815
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000816 /* We always use the characters up to the next non-word character,
817 * also for bad words. */
818 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000819
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000820 /* Check caps type later. */
821 mi.mi_capflags = 0;
822 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000823 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000824
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000825 /* Include one non-word character so that we can check for the
826 * word end. */
827 if (*mi.mi_fend != NUL)
828 mb_ptr_adv(mi.mi_fend);
829
830 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
831 MAXWLEN + 1);
832 mi.mi_fwordlen = STRLEN(mi.mi_fword);
833
834 /* The word is bad unless we recognize it. */
835 mi.mi_result = SP_BAD;
836
837 /*
838 * Loop over the languages specified in 'spelllang'.
839 * We check them all, because a matching word may be longer than an
840 * already found matching word.
841 */
842 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
843 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
844 {
845 /* Check for a matching word in case-folded words. */
846 find_word(&mi, FIND_FOLDWORD);
847
848 /* Check for a matching word in keep-case words. */
849 find_word(&mi, FIND_KEEPWORD);
850
851 /* Check for matching prefixes. */
852 find_prefix(&mi);
853 }
854
855 if (mi.mi_result != SP_OK)
856 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000857 /* If we found a number skip over it. Allows for "42nd". Do flag
858 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000859 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000860 {
861 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
862 return nrlen;
863 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000864
865 /* When we are at a non-word character there is no error, just
866 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000867 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000868 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000869 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
870 {
871 regmatch_T regmatch;
872
873 /* Check for end of sentence. */
874 regmatch.regprog = wp->w_buffer->b_cap_prog;
875 regmatch.rm_ic = FALSE;
876 if (vim_regexec(&regmatch, ptr, 0))
877 *capcol = (int)(regmatch.endp[0] - ptr);
878 }
879
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000881 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000883#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000884 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000885 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000886
887 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
888 *attrp = highlight_attr[HLF_SPB];
889 else if (mi.mi_result == SP_RARE)
890 *attrp = highlight_attr[HLF_SPR];
891 else
892 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000893 }
894
Bram Moolenaar51485f02005-06-04 21:55:20 +0000895 return (int)(mi.mi_end - ptr);
896}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000897
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898/*
899 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000900 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
901 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
902 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
903 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000904 *
905 * For a match mip->mi_result is updated.
906 */
907 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000908find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000910 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000911{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000912 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000913 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000914 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000915 int endidxcnt = 0;
916 int len;
917 int wlen = 0;
918 int flen;
919 int c;
920 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000921 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000922#ifdef FEAT_MBYTE
923 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000924 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000925#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000926 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927 slang_T *slang = mip->mi_lp->lp_slang;
928 unsigned flags;
929 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000930 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000931
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000932 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000933 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000934 /* Check for word with matching case in keep-case tree. */
935 ptr = mip->mi_word;
936 flen = 9999; /* no case folding, always enough bytes */
937 byts = slang->sl_kbyts;
938 idxs = slang->sl_kidxs;
939 }
940 else
941 {
942 /* Check for case-folded in case-folded tree. */
943 ptr = mip->mi_fword;
944 flen = mip->mi_fwordlen; /* available case-folded bytes */
945 byts = slang->sl_fbyts;
946 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000947
948 if (mode == FIND_PREFIX)
949 {
950 /* Skip over the prefix. */
951 wlen = mip->mi_prefixlen;
952 flen -= mip->mi_prefixlen;
953 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000954 }
955
Bram Moolenaar51485f02005-06-04 21:55:20 +0000956 if (byts == NULL)
957 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000958
Bram Moolenaar51485f02005-06-04 21:55:20 +0000959 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000960 * Repeat advancing in the tree until:
961 * - there is a byte that doesn't match,
962 * - we reach the end of the tree,
963 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000964 */
965 for (;;)
966 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000967 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000968 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000969
970 len = byts[arridx++];
971
972 /* If the first possible byte is a zero the word could end here.
973 * Remember this index, we first check for the longest word. */
974 if (byts[arridx] == 0)
975 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000976 if (endidxcnt == MAXWLEN)
977 {
978 /* Must be a corrupted spell file. */
979 EMSG(_(e_format));
980 return;
981 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000982 endlen[endidxcnt] = wlen;
983 endidx[endidxcnt++] = arridx++;
984 --len;
985
986 /* Skip over the zeros, there can be several flag/region
987 * combinations. */
988 while (len > 0 && byts[arridx] == 0)
989 {
990 ++arridx;
991 --len;
992 }
993 if (len == 0)
994 break; /* no children, word must end here */
995 }
996
997 /* Stop looking at end of the line. */
998 if (ptr[wlen] == NUL)
999 break;
1000
1001 /* Perform a binary search in the list of accepted bytes. */
1002 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001003 if (c == TAB) /* <Tab> is handled like <Space> */
1004 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001005 lo = arridx;
1006 hi = arridx + len - 1;
1007 while (lo < hi)
1008 {
1009 m = (lo + hi) / 2;
1010 if (byts[m] > c)
1011 hi = m - 1;
1012 else if (byts[m] < c)
1013 lo = m + 1;
1014 else
1015 {
1016 lo = hi = m;
1017 break;
1018 }
1019 }
1020
1021 /* Stop if there is no matching byte. */
1022 if (hi < lo || byts[lo] != c)
1023 break;
1024
1025 /* Continue at the child (if there is one). */
1026 arridx = idxs[lo];
1027 ++wlen;
1028 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001029
1030 /* One space in the good word may stand for several spaces in the
1031 * checked word. */
1032 if (c == ' ')
1033 {
1034 for (;;)
1035 {
1036 if (flen <= 0 && *mip->mi_fend != NUL)
1037 flen = fold_more(mip);
1038 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1039 break;
1040 ++wlen;
1041 --flen;
1042 }
1043 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001044 }
1045
1046 /*
1047 * Verify that one of the possible endings is valid. Try the longest
1048 * first.
1049 */
1050 while (endidxcnt > 0)
1051 {
1052 --endidxcnt;
1053 arridx = endidx[endidxcnt];
1054 wlen = endlen[endidxcnt];
1055
1056#ifdef FEAT_MBYTE
1057 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1058 continue; /* not at first byte of character */
1059#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001060 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 continue; /* next char is a word character */
1062
1063#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001064 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001065 {
1066 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001067 * when folding case. This can be slow, take a shortcut when the
1068 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001069 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001070 if (STRNCMP(ptr, p, wlen) != 0)
1071 {
1072 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1073 mb_ptr_adv(p);
1074 wlen = p - mip->mi_word;
1075 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001076 }
1077#endif
1078
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079 /* Check flags and region. For FIND_PREFIX check the condition and
1080 * prefix ID.
1081 * Repeat this if there are more flags/region alternatives until there
1082 * is a match. */
1083 res = SP_BAD;
1084 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1085 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001086 {
1087 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001088
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001089 /* For the fold-case tree check that the case of the checked word
1090 * matches with what the word in the tree requires.
1091 * For keep-case tree the case is always right. For prefixes we
1092 * don't bother to check. */
1093 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001094 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001095 if (mip->mi_cend != mip->mi_word + wlen)
1096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001097 /* mi_capflags was set for a different word length, need
1098 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001099 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001100 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001101 }
1102
Bram Moolenaar0c405862005-06-22 22:26:26 +00001103 if (mip->mi_capflags == WF_KEEPCAP
1104 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001105 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001106 }
1107
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001108 /* When mode is FIND_PREFIX the word must support the prefix:
1109 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001110 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001111 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001112 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001114 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001115 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001116 mip->mi_word + mip->mi_cprefixlen, slang,
1117 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001118 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001119 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001120
1121 /* Use the WF_RARE flag for a rare prefix. */
1122 if (c & WF_RAREPFX)
1123 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001124 }
1125
1126 if (flags & WF_BANNED)
1127 res = SP_BANNED;
1128 else if (flags & WF_REGION)
1129 {
1130 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001131 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001132 res = SP_OK;
1133 else
1134 res = SP_LOCAL;
1135 }
1136 else if (flags & WF_RARE)
1137 res = SP_RARE;
1138 else
1139 res = SP_OK;
1140
1141 /* Always use the longest match and the best result. */
1142 if (mip->mi_result > res)
1143 {
1144 mip->mi_result = res;
1145 mip->mi_end = mip->mi_word + wlen;
1146 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001147 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001148 mip->mi_end = mip->mi_word + wlen;
1149
1150 if (res == SP_OK)
1151 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001152 }
1153
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001154 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001155 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001156 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001157}
1158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001159/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001160 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1161 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001162 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001163 */
1164 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001165valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001166 int totprefcnt; /* nr of prefix IDs */
1167 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001168 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001169 char_u *word;
1170 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001171 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001172{
1173 int prefcnt;
1174 int pidx;
1175 regprog_T *rp;
1176 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001177 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001178
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001179 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001180 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1181 {
1182 pidx = slang->sl_pidxs[arridx + prefcnt];
1183
1184 /* Check the prefix ID. */
1185 if (prefid != (pidx & 0xff))
1186 continue;
1187
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001188 /* Check if the prefix doesn't combine and the word already has a
1189 * suffix. */
1190 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1191 continue;
1192
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001193 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001194 * stored in the two bytes above the prefix ID byte. */
1195 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001196 if (rp != NULL)
1197 {
1198 regmatch.regprog = rp;
1199 regmatch.rm_ic = FALSE;
1200 if (!vim_regexec(&regmatch, word, 0))
1201 continue;
1202 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001203 else if (cond_req)
1204 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001205
Bram Moolenaar53805d12005-08-01 07:08:33 +00001206 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001207 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001208 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001209 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001210}
1211
1212/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213 * Check if the word at "mip->mi_word" has a matching prefix.
1214 * If it does, then check the following word.
1215 *
1216 * For a match mip->mi_result is updated.
1217 */
1218 static void
1219find_prefix(mip)
1220 matchinf_T *mip;
1221{
1222 idx_T arridx = 0;
1223 int len;
1224 int wlen = 0;
1225 int flen;
1226 int c;
1227 char_u *ptr;
1228 idx_T lo, hi, m;
1229 slang_T *slang = mip->mi_lp->lp_slang;
1230 char_u *byts;
1231 idx_T *idxs;
1232
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001233 byts = slang->sl_pbyts;
1234 if (byts == NULL)
1235 return; /* array is empty */
1236
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001237 /* We use the case-folded word here, since prefixes are always
1238 * case-folded. */
1239 ptr = mip->mi_fword;
1240 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001241 idxs = slang->sl_pidxs;
1242
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001243 /*
1244 * Repeat advancing in the tree until:
1245 * - there is a byte that doesn't match,
1246 * - we reach the end of the tree,
1247 * - or we reach the end of the line.
1248 */
1249 for (;;)
1250 {
1251 if (flen == 0 && *mip->mi_fend != NUL)
1252 flen = fold_more(mip);
1253
1254 len = byts[arridx++];
1255
1256 /* If the first possible byte is a zero the prefix could end here.
1257 * Check if the following word matches and supports the prefix. */
1258 if (byts[arridx] == 0)
1259 {
1260 /* There can be several prefixes with different conditions. We
1261 * try them all, since we don't know which one will give the
1262 * longest match. The word is the same each time, pass the list
1263 * of possible prefixes to find_word(). */
1264 mip->mi_prefarridx = arridx;
1265 mip->mi_prefcnt = len;
1266 while (len > 0 && byts[arridx] == 0)
1267 {
1268 ++arridx;
1269 --len;
1270 }
1271 mip->mi_prefcnt -= len;
1272
1273 /* Find the word that comes after the prefix. */
1274 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001275#ifdef FEAT_MBYTE
1276 if (has_mbyte)
1277 {
1278 /* Case-folded length may differ from original length. */
1279 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1280 mip->mi_word);
1281 }
1282 else
1283 mip->mi_cprefixlen = wlen;
1284#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001285 find_word(mip, FIND_PREFIX);
1286
1287
1288 if (len == 0)
1289 break; /* no children, word must end here */
1290 }
1291
1292 /* Stop looking at end of the line. */
1293 if (ptr[wlen] == NUL)
1294 break;
1295
1296 /* Perform a binary search in the list of accepted bytes. */
1297 c = ptr[wlen];
1298 lo = arridx;
1299 hi = arridx + len - 1;
1300 while (lo < hi)
1301 {
1302 m = (lo + hi) / 2;
1303 if (byts[m] > c)
1304 hi = m - 1;
1305 else if (byts[m] < c)
1306 lo = m + 1;
1307 else
1308 {
1309 lo = hi = m;
1310 break;
1311 }
1312 }
1313
1314 /* Stop if there is no matching byte. */
1315 if (hi < lo || byts[lo] != c)
1316 break;
1317
1318 /* Continue at the child (if there is one). */
1319 arridx = idxs[lo];
1320 ++wlen;
1321 --flen;
1322 }
1323}
1324
1325/*
1326 * Need to fold at least one more character. Do until next non-word character
1327 * for efficiency.
1328 * Return the length of the folded chars in bytes.
1329 */
1330 static int
1331fold_more(mip)
1332 matchinf_T *mip;
1333{
1334 int flen;
1335 char_u *p;
1336
1337 p = mip->mi_fend;
1338 do
1339 {
1340 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001341 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001342
1343 /* Include the non-word character so that we can check for the
1344 * word end. */
1345 if (*mip->mi_fend != NUL)
1346 mb_ptr_adv(mip->mi_fend);
1347
1348 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1349 mip->mi_fword + mip->mi_fwordlen,
1350 MAXWLEN - mip->mi_fwordlen);
1351 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1352 mip->mi_fwordlen += flen;
1353 return flen;
1354}
1355
1356/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001357 * Check case flags for a word. Return TRUE if the word has the requested
1358 * case.
1359 */
1360 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001361spell_valid_case(wordflags, treeflags)
1362 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001363 int treeflags; /* flags for the word in the spell tree */
1364{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001365 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001366 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001367 && ((treeflags & WF_ONECAP) == 0
1368 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001369}
1370
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001371/*
1372 * Return TRUE if spell checking is not enabled.
1373 */
1374 static int
1375no_spell_checking()
1376{
1377 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1378 {
1379 EMSG(_("E756: Spell checking is not enabled"));
1380 return TRUE;
1381 }
1382 return FALSE;
1383}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001384
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001385/*
1386 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001387 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001388 * Return OK if found, FAIL otherwise.
1389 */
1390 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001391spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001392 int dir; /* FORWARD or BACKWARD */
1393 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001394 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001395{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001396 linenr_T lnum;
1397 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001398 char_u *line;
1399 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001400 char_u *endp;
1401 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001402 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001403 int has_syntax = syntax_present(curbuf);
1404 int col;
1405 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001406 char_u *buf = NULL;
1407 int buflen = 0;
1408 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001409 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001410
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001411 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001412 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001413
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001414 /*
1415 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001416 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001417 *
1418 * When searching backwards, we continue in the line to find the last
1419 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001420 *
1421 * We concatenate the start of the next line, so that wrapped words work
1422 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1423 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001424 */
1425 lnum = curwin->w_cursor.lnum;
1426 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001427
1428 while (!got_int)
1429 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001430 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001431
Bram Moolenaar0c405862005-06-22 22:26:26 +00001432 len = STRLEN(line);
1433 if (buflen < len + MAXWLEN + 2)
1434 {
1435 vim_free(buf);
1436 buflen = len + MAXWLEN + 2;
1437 buf = alloc(buflen);
1438 if (buf == NULL)
1439 break;
1440 }
1441
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001442 /* In first line check first word for Capital. */
1443 if (lnum == 1)
1444 capcol = 0;
1445
1446 /* For checking first word with a capital skip white space. */
1447 if (capcol == 0)
1448 capcol = skipwhite(line) - line;
1449
Bram Moolenaar0c405862005-06-22 22:26:26 +00001450 /* Copy the line into "buf" and append the start of the next line if
1451 * possible. */
1452 STRCPY(buf, line);
1453 if (lnum < curbuf->b_ml.ml_line_count)
1454 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1455
1456 p = buf + skip;
1457 endp = buf + len;
1458 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001459 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001460 /* When searching backward don't search after the cursor. */
1461 if (dir == BACKWARD
1462 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001463 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001464 break;
1465
1466 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001467 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001468 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469
1470 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001471 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001472 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001473 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001474 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001475 /* When searching forward only accept a bad word after
1476 * the cursor. */
1477 if (dir == BACKWARD
1478 || lnum > curwin->w_cursor.lnum
1479 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001480 && (colnr_T)(curline ? p - buf + len
1481 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001482 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001483 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001484 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001485 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001486 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001487 (void)syn_get_id(lnum, (colnr_T)col,
1488 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001489 }
1490 else
1491 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001492
Bram Moolenaar51485f02005-06-04 21:55:20 +00001493 if (can_spell)
1494 {
1495 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001496 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001497#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001498 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001499#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001500 if (dir == FORWARD)
1501 {
1502 /* No need to search further. */
1503 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001504 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001505 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001506 }
1507 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001509 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001510 }
1511
Bram Moolenaar51485f02005-06-04 21:55:20 +00001512 /* advance to character after the word */
1513 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001514 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001515 }
1516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001517 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001518 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001519
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001520 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001521 if (dir == BACKWARD)
1522 {
1523 if (found_pos.lnum != 0)
1524 {
1525 /* Use the last match in the line. */
1526 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001527 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001528 return OK;
1529 }
1530 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001531 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001532 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001533 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001534 }
1535 else
1536 {
1537 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001538 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001539 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001540
1541 /* Skip the characters at the start of the next line that were
1542 * included in a match crossing line boundaries. */
1543 if (attr == 0)
1544 skip = p - endp;
1545 else
1546 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001547
1548 /* Capscol skips over the inserted space. */
1549 --capcol;
1550
1551 /* But after empty line check first word in next line */
1552 if (*skipwhite(line) == NUL)
1553 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001554 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001555
1556 line_breakcheck();
1557 }
1558
Bram Moolenaar0c405862005-06-22 22:26:26 +00001559 vim_free(buf);
1560 return FAIL;
1561}
1562
1563/*
1564 * For spell checking: concatenate the start of the following line "line" into
1565 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1566 */
1567 void
1568spell_cat_line(buf, line, maxlen)
1569 char_u *buf;
1570 char_u *line;
1571 int maxlen;
1572{
1573 char_u *p;
1574 int n;
1575
1576 p = skipwhite(line);
1577 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1578 p = skipwhite(p + 1);
1579
1580 if (*p != NUL)
1581 {
1582 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001583 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001584 n = p - line;
1585 if (n >= maxlen)
1586 n = maxlen - 1;
1587 vim_memset(buf + 1, ' ', n);
1588 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001589}
1590
1591/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001592 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001593 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001594 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001595 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001596spell_load_lang(lang)
1597 char_u *lang;
1598{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001599 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001600 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001601 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001602
Bram Moolenaarb765d632005-06-07 21:00:02 +00001603 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001604 * It's truncated when an error is detected. */
1605 STRCPY(langcp, lang);
1606
Bram Moolenaarb765d632005-06-07 21:00:02 +00001607 /*
1608 * Find the first spell file for "lang" in 'runtimepath' and load it.
1609 */
1610 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1611 "spell/%s.%s.spl", lang, spell_enc());
1612 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001613
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001614 if (r == FAIL && *langcp != NUL)
1615 {
1616 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001617 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001618 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001619 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001620 }
1621
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001622 if (r == FAIL)
1623 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1624 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001625 else if (*langcp != NUL)
1626 {
1627 /* Load all the additions. */
1628 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1629 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1630 }
1631}
1632
1633/*
1634 * Return the encoding used for spell checking: Use 'encoding', except that we
1635 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1636 */
1637 static char_u *
1638spell_enc()
1639{
1640
1641#ifdef FEAT_MBYTE
1642 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1643 return p_enc;
1644#endif
1645 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001646}
1647
1648/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001649 * Get the name of the .spl file for the internal wordlist into
1650 * "fname[MAXPATHL]".
1651 */
1652 static void
1653int_wordlist_spl(fname)
1654 char_u *fname;
1655{
1656 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1657 int_wordlist, spell_enc());
1658}
1659
1660/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001661 * Allocate a new slang_T.
1662 * Caller must fill "sl_next".
1663 */
1664 static slang_T *
1665slang_alloc(lang)
1666 char_u *lang;
1667{
1668 slang_T *lp;
1669
Bram Moolenaar51485f02005-06-04 21:55:20 +00001670 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 if (lp != NULL)
1672 {
1673 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001674 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001675 }
1676 return lp;
1677}
1678
1679/*
1680 * Free the contents of an slang_T and the structure itself.
1681 */
1682 static void
1683slang_free(lp)
1684 slang_T *lp;
1685{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001686 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001687 vim_free(lp->sl_fname);
1688 slang_clear(lp);
1689 vim_free(lp);
1690}
1691
1692/*
1693 * Clear an slang_T so that the file can be reloaded.
1694 */
1695 static void
1696slang_clear(lp)
1697 slang_T *lp;
1698{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001699 garray_T *gap;
1700 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001701 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001702 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001703
Bram Moolenaar51485f02005-06-04 21:55:20 +00001704 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001705 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001706 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001707 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001708 vim_free(lp->sl_pbyts);
1709 lp->sl_pbyts = NULL;
1710
Bram Moolenaar51485f02005-06-04 21:55:20 +00001711 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001712 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001713 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001714 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001715 vim_free(lp->sl_pidxs);
1716 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001717
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001718 gap = &lp->sl_rep;
1719 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001720 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001721 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1722 vim_free(ftp->ft_from);
1723 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001724 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001725 ga_clear(gap);
1726
1727 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001728 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001729 {
1730 /* "ga_len" is set to 1 without adding an item for latin1 */
1731 if (gap->ga_data != NULL)
1732 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1733 for (i = 0; i < gap->ga_len; ++i)
1734 vim_free(((int **)gap->ga_data)[i]);
1735 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001736 else
1737 /* SAL items: free salitem_T items */
1738 while (gap->ga_len > 0)
1739 {
1740 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1741 vim_free(smp->sm_lead);
1742 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1743 vim_free(smp->sm_to);
1744#ifdef FEAT_MBYTE
1745 vim_free(smp->sm_lead_w);
1746 vim_free(smp->sm_oneof_w);
1747 vim_free(smp->sm_to_w);
1748#endif
1749 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001750 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001751
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001752 for (i = 0; i < lp->sl_prefixcnt; ++i)
1753 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001754 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001755 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001756 lp->sl_prefprog = NULL;
1757
1758 vim_free(lp->sl_midword);
1759 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001760
Bram Moolenaarea424162005-06-16 21:51:00 +00001761#ifdef FEAT_MBYTE
1762 {
1763 int todo = lp->sl_map_hash.ht_used;
1764 hashitem_T *hi;
1765
1766 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1767 if (!HASHITEM_EMPTY(hi))
1768 {
1769 --todo;
1770 vim_free(hi->hi_key);
1771 }
1772 }
1773 hash_clear(&lp->sl_map_hash);
1774#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001775}
1776
1777/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001778 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001779 * Invoked through do_in_runtimepath().
1780 */
1781 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001782spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001783 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001784 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001785{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001787}
1788
1789/*
1790 * Load one spell file and store the info into a slang_T.
1791 *
1792 * This is invoked in two ways:
1793 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1794 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1795 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1796 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001798 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001799 static slang_T *
1800spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001801 char_u *fname;
1802 char_u *lang;
1803 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001804 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001805{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806 FILE *fd;
1807 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001808 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001809 char_u *bp;
1810 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001812 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001813 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001814 int round;
1815 char_u *save_sourcing_name = sourcing_name;
1816 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001817 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001818 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001819 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001820 garray_T *gap;
1821 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001822 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001823 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001824 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001825 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826
Bram Moolenaarb765d632005-06-07 21:00:02 +00001827 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001828 if (fd == NULL)
1829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001830 if (!silent)
1831 EMSG2(_(e_notopen), fname);
1832 else if (p_verbose > 2)
1833 {
1834 verbose_enter();
1835 smsg((char_u *)e_notopen, fname);
1836 verbose_leave();
1837 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001838 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001839 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001840 if (p_verbose > 2)
1841 {
1842 verbose_enter();
1843 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1844 verbose_leave();
1845 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001846
Bram Moolenaarb765d632005-06-07 21:00:02 +00001847 if (old_lp == NULL)
1848 {
1849 lp = slang_alloc(lang);
1850 if (lp == NULL)
1851 goto endFAIL;
1852
1853 /* Remember the file name, used to reload the file when it's updated. */
1854 lp->sl_fname = vim_strsave(fname);
1855 if (lp->sl_fname == NULL)
1856 goto endFAIL;
1857
1858 /* Check for .add.spl. */
1859 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1860 }
1861 else
1862 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001863
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001864 /* Set sourcing_name, so that error messages mention the file name. */
1865 sourcing_name = fname;
1866 sourcing_lnum = 0;
1867
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001868 /* <HEADER>: <fileID>
1869 * <regioncnt> <regionname> ...
1870 * <charflagslen> <charflags>
1871 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001872 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001873 * <prefcondcnt> <prefcond> ...
1874 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001875 for (i = 0; i < VIMSPELLMAGICL; ++i)
1876 buf[i] = getc(fd); /* <fileID> */
1877 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1878 {
1879 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001880 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881 }
1882
1883 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001884 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001885 {
1886truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001887 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001888 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889 }
1890 if (cnt > 8)
1891 {
1892formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001893 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001894 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001895 }
1896 for (i = 0; i < cnt; ++i)
1897 {
1898 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1899 lp->sl_regions[i * 2 + 1] = getc(fd);
1900 }
1901 lp->sl_regions[cnt * 2] = NUL;
1902
Bram Moolenaar7887d882005-07-01 22:33:52 +00001903 /* <charflagslen> <charflags> */
1904 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001905 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001906 goto endFAIL;
1907
1908 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001909 fol = read_cnt_string(fd, 2, &ccnt);
1910 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001911 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001912 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001913 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001914 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001915
1916 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1917 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001918 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001919
1920 vim_free(p);
1921 vim_free(fol);
1922
1923 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1924 if ((p == NULL) != (fol == NULL))
1925 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001926
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001927 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001928 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001929 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001930 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001931
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001932 /* <prefcondcnt> <prefcond> ... */
1933 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1934 if (cnt > 0)
1935 {
1936 lp->sl_prefprog = (regprog_T **)alloc_clear(
1937 (unsigned)sizeof(regprog_T *) * cnt);
1938 if (lp->sl_prefprog == NULL)
1939 goto endFAIL;
1940 lp->sl_prefixcnt = cnt;
1941
1942 for (i = 0; i < cnt; ++i)
1943 {
1944 /* <prefcond> : <condlen> <condstr> */
1945 n = getc(fd); /* <condlen> */
1946 if (n < 0)
1947 goto formerr;
1948 /* When <condlen> is zero we have an empty condition. Otherwise
1949 * compile the regexp program used to check for the condition. */
1950 if (n > 0)
1951 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001952 buf[0] = '^'; /* always match at one position only */
1953 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001954 while (n-- > 0)
1955 *p++ = getc(fd); /* <condstr> */
1956 *p = NUL;
1957 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1958 }
1959 }
1960 }
1961
1962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001963 /* <SUGGEST> : <repcount> <rep> ...
1964 * <salflags> <salcount> <sal> ...
1965 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001966
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001967 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1968 if (cnt < 0)
1969 goto formerr;
1970
1971 gap = &lp->sl_rep;
1972 if (ga_grow(gap, cnt) == FAIL)
1973 goto endFAIL;
1974
1975 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1976 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001977 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001978 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001979 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001980 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001981 goto endFAIL;
1982 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001983 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001984 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001985 vim_free(ftp->ft_from);
1986 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001987 }
1988 }
1989
1990 /* Fill the first-index table. */
1991 first = lp->sl_rep_first;
1992 for (i = 0; i < 256; ++i)
1993 first[i] = -1;
1994 for (i = 0; i < gap->ga_len; ++i)
1995 {
1996 ftp = &((fromto_T *)gap->ga_data)[i];
1997 if (first[*ftp->ft_from] == -1)
1998 first[*ftp->ft_from] = i;
1999 }
2000
2001 i = getc(fd); /* <salflags> */
2002 if (i & SAL_F0LLOWUP)
2003 lp->sl_followup = TRUE;
2004 if (i & SAL_COLLAPSE)
2005 lp->sl_collapse = TRUE;
2006 if (i & SAL_REM_ACCENTS)
2007 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002008 if (i & SAL_SOFO)
2009 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002010 else
2011 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002012
2013 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2014 if (cnt < 0)
2015 goto formerr;
2016
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002017 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002018 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002019 /*
2020 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2021 */
2022 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002023 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002024
Bram Moolenaar7887d882005-07-01 22:33:52 +00002025 /* <salfromlen> <salfrom> */
2026 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002027 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002028 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002029
Bram Moolenaar7887d882005-07-01 22:33:52 +00002030 /* <saltolen> <salto> */
2031 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002032 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002033 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002034 vim_free(bp);
2035 goto endFAIL;
2036 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002037
Bram Moolenaar7887d882005-07-01 22:33:52 +00002038 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002039 if (bp != NULL && fol != NULL)
2040 i = set_sofo(lp, bp, fol);
2041 else if (bp != NULL || fol != NULL)
2042 i = FAIL; /* only one of two strings is an error */
2043 else
2044 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002045
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002046 vim_free(bp);
2047 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002048 if (i == FAIL)
2049 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002050 }
2051 else
2052 {
2053 /*
2054 * SAL items
2055 */
2056 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002057 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002058 if (ga_grow(gap, cnt) == FAIL)
2059 goto endFAIL;
2060
2061 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2062 for (; gap->ga_len < cnt; ++gap->ga_len)
2063 {
2064 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2065 ccnt = getc(fd); /* <salfromlen> */
2066 if (ccnt < 0)
2067 goto formerr;
2068 if ((p = alloc(ccnt + 2)) == NULL)
2069 goto endFAIL;
2070 smp->sm_lead = p;
2071
2072 /* Read up to the first special char into sm_lead. */
2073 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002074 {
2075 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002076 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002077 break;
2078 *p++ = c;
2079 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002080 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002081 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002082
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002083 /* Put (abc) chars in sm_oneof, if any. */
2084 if (c == '(')
2085 {
2086 smp->sm_oneof = p;
2087 for (++i; i < ccnt; ++i)
2088 {
2089 c = getc(fd); /* <salfrom> */
2090 if (c == ')')
2091 break;
2092 *p++ = c;
2093 }
2094 *p++ = NUL;
2095 if (++i < ccnt)
2096 c = getc(fd);
2097 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002098 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002099 smp->sm_oneof = NULL;
2100
2101 /* Any following chars go in sm_rules. */
2102 smp->sm_rules = p;
2103 if (i < ccnt)
2104 /* store the char we got while checking for end of sm_lead */
2105 *p++ = c;
2106 for (++i; i < ccnt; ++i)
2107 *p++ = getc(fd); /* <salfrom> */
2108 *p++ = NUL;
2109
Bram Moolenaar7887d882005-07-01 22:33:52 +00002110 /* <saltolen> <salto> */
2111 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002112 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002113 {
2114 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002115 goto formerr;
2116 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002117
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002118#ifdef FEAT_MBYTE
2119 if (has_mbyte)
2120 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002121 /* convert the multi-byte strings to wide char strings */
2122 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2123 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2124 if (smp->sm_oneof == NULL)
2125 smp->sm_oneof_w = NULL;
2126 else
2127 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002128 if (smp->sm_to == NULL)
2129 smp->sm_to_w = NULL;
2130 else
2131 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002132 if (smp->sm_lead_w == NULL
2133 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002134 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002135 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002136 vim_free(smp->sm_lead);
2137 vim_free(smp->sm_to);
2138 vim_free(smp->sm_lead_w);
2139 vim_free(smp->sm_oneof_w);
2140 vim_free(smp->sm_to_w);
2141 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002142 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002143 }
2144#endif
2145 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002146
2147 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002148 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002149 }
2150
Bram Moolenaar7887d882005-07-01 22:33:52 +00002151 /* <maplen> <mapstr> */
2152 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002153 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002154 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002155 if (p != NULL)
2156 {
2157 set_map_str(lp, p);
2158 vim_free(p);
2159 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002160
Bram Moolenaar51485f02005-06-04 21:55:20 +00002161 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002162 * round 2: <KWORDTREE>
2163 * round 3: <PREFIXTREE> */
2164 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002166 /* The tree size was computed when writing the file, so that we can
2167 * allocate it as one long block. <nodecount> */
2168 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2169 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002170 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002171 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002172 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002173 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002174 bp = lalloc((long_u)len, TRUE);
2175 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002176 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002177 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002178 lp->sl_fbyts = bp;
2179 else if (round == 2)
2180 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002181 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002182 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002183
2184 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002185 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2186 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002187 goto endFAIL;
2188 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002189 lp->sl_fidxs = ip;
2190 else if (round == 2)
2191 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002192 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002193 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002194
2195 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002196 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002197 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002198 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002199 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002200 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002201 }
2202 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002203
Bram Moolenaarb765d632005-06-07 21:00:02 +00002204 /* For a new file link it in the list of spell files. */
2205 if (old_lp == NULL)
2206 {
2207 lp->sl_next = first_lang;
2208 first_lang = lp;
2209 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002210
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002211 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002212
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002213endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002214 if (lang != NULL)
2215 /* truncating the name signals the error to spell_load_lang() */
2216 *lang = NUL;
2217 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002218 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002219 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002220 lp = NULL;
2221 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002222
2223endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002224 if (fd != NULL)
2225 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002226 sourcing_name = save_sourcing_name;
2227 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002228
2229 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002230}
2231
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002232/*
2233 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002234 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002235 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002236 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002237 */
2238 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002239read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002240 FILE *fd;
2241 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002242 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002243{
2244 int cnt = 0;
2245 int i;
2246 char_u *str;
2247
2248 /* read the length bytes, MSB first */
2249 for (i = 0; i < cnt_bytes; ++i)
2250 cnt = (cnt << 8) + getc(fd);
2251 if (cnt < 0)
2252 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002253 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002254 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002255 return NULL;
2256 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002257 *cntp = cnt;
2258 if (cnt == 0)
2259 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002260
2261 /* allocate memory */
2262 str = alloc((unsigned)cnt + 1);
2263 if (str == NULL)
2264 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002265 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002266 return NULL;
2267 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002268
2269 /* Read the string. Doesn't check for truncated file. */
2270 for (i = 0; i < cnt; ++i)
2271 str[i] = getc(fd);
2272 str[i] = NUL;
2273
2274 return str;
2275}
2276
Bram Moolenaar7887d882005-07-01 22:33:52 +00002277/*
2278 * Set the SOFOFROM and SOFOTO items in language "lp".
2279 * Returns FAIL when there is something wrong.
2280 */
2281 static int
2282set_sofo(lp, from, to)
2283 slang_T *lp;
2284 char_u *from;
2285 char_u *to;
2286{
2287 int i;
2288
2289#ifdef FEAT_MBYTE
2290 garray_T *gap;
2291 char_u *s;
2292 char_u *p;
2293 int c;
2294 int *inp;
2295
2296 if (has_mbyte)
2297 {
2298 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2299 * characters. The index is the low byte of the character.
2300 * The list contains from-to pairs with a terminating NUL.
2301 * sl_sal_first[] is used for latin1 "from" characters. */
2302 gap = &lp->sl_sal;
2303 ga_init2(gap, sizeof(int *), 1);
2304 if (ga_grow(gap, 256) == FAIL)
2305 return FAIL;
2306 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2307 gap->ga_len = 256;
2308
2309 /* First count the number of items for each list. Temporarily use
2310 * sl_sal_first[] for this. */
2311 for (p = from, s = to; *p != NUL && *s != NUL; )
2312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002313 c = mb_cptr2char_adv(&p);
2314 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002315 if (c >= 256)
2316 ++lp->sl_sal_first[c & 0xff];
2317 }
2318 if (*p != NUL || *s != NUL) /* lengths differ */
2319 return FAIL;
2320
2321 /* Allocate the lists. */
2322 for (i = 0; i < 256; ++i)
2323 if (lp->sl_sal_first[i] > 0)
2324 {
2325 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2326 if (p == NULL)
2327 return FAIL;
2328 ((int **)gap->ga_data)[i] = (int *)p;
2329 *(int *)p = 0;
2330 }
2331
2332 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2333 * list. */
2334 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2335 for (p = from, s = to; *p != NUL && *s != NUL; )
2336 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002337 c = mb_cptr2char_adv(&p);
2338 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002339 if (c >= 256)
2340 {
2341 /* Append the from-to chars at the end of the list with
2342 * the low byte. */
2343 inp = ((int **)gap->ga_data)[c & 0xff];
2344 while (*inp != 0)
2345 ++inp;
2346 *inp++ = c; /* from char */
2347 *inp++ = i; /* to char */
2348 *inp++ = NUL; /* NUL at the end */
2349 }
2350 else
2351 /* mapping byte to char is done in sl_sal_first[] */
2352 lp->sl_sal_first[c] = i;
2353 }
2354 }
2355 else
2356#endif
2357 {
2358 /* mapping bytes to bytes is done in sl_sal_first[] */
2359 if (STRLEN(from) != STRLEN(to))
2360 return FAIL;
2361
2362 for (i = 0; to[i] != NUL; ++i)
2363 lp->sl_sal_first[from[i]] = to[i];
2364 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2365 }
2366
2367 return OK;
2368}
2369
2370/*
2371 * Fill the first-index table for "lp".
2372 */
2373 static void
2374set_sal_first(lp)
2375 slang_T *lp;
2376{
2377 salfirst_T *sfirst;
2378 int i;
2379 salitem_T *smp;
2380 int c;
2381 garray_T *gap = &lp->sl_sal;
2382
2383 sfirst = lp->sl_sal_first;
2384 for (i = 0; i < 256; ++i)
2385 sfirst[i] = -1;
2386 smp = (salitem_T *)gap->ga_data;
2387 for (i = 0; i < gap->ga_len; ++i)
2388 {
2389#ifdef FEAT_MBYTE
2390 if (has_mbyte)
2391 /* Use the lowest byte of the first character. For latin1 it's
2392 * the character, for other encodings it should differ for most
2393 * characters. */
2394 c = *smp[i].sm_lead_w & 0xff;
2395 else
2396#endif
2397 c = *smp[i].sm_lead;
2398 if (sfirst[c] == -1)
2399 {
2400 sfirst[c] = i;
2401#ifdef FEAT_MBYTE
2402 if (has_mbyte)
2403 {
2404 int n;
2405
2406 /* Make sure all entries with this byte are following each
2407 * other. Move the ones that are in the wrong position. Do
2408 * keep the same ordering! */
2409 while (i + 1 < gap->ga_len
2410 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2411 /* Skip over entry with same index byte. */
2412 ++i;
2413
2414 for (n = 1; i + n < gap->ga_len; ++n)
2415 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2416 {
2417 salitem_T tsal;
2418
2419 /* Move entry with same index byte after the entries
2420 * we already found. */
2421 ++i;
2422 --n;
2423 tsal = smp[i + n];
2424 mch_memmove(smp + i + 1, smp + i,
2425 sizeof(salitem_T) * n);
2426 smp[i] = tsal;
2427 }
2428 }
2429#endif
2430 }
2431 }
2432}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002433
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002434#ifdef FEAT_MBYTE
2435/*
2436 * Turn a multi-byte string into a wide character string.
2437 * Return it in allocated memory (NULL for out-of-memory)
2438 */
2439 static int *
2440mb_str2wide(s)
2441 char_u *s;
2442{
2443 int *res;
2444 char_u *p;
2445 int i = 0;
2446
2447 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2448 if (res != NULL)
2449 {
2450 for (p = s; *p != NUL; )
2451 res[i++] = mb_ptr2char_adv(&p);
2452 res[i] = NUL;
2453 }
2454 return res;
2455}
2456#endif
2457
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 * Read one row of siblings from the spell file and store it in the byte array
2460 * "byts" and index array "idxs". Recursively read the children.
2461 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002462 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002463 *
2464 * Returns the index follosing the siblings.
2465 * Returns -1 if the file is shorter than expected.
2466 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002468 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002469read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002470 FILE *fd;
2471 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002472 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002473 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002474 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002475 int prefixtree; /* TRUE for reading PREFIXTREE */
2476 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002477{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002478 int len;
2479 int i;
2480 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002481 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002482 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002483 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002484#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002485
Bram Moolenaar51485f02005-06-04 21:55:20 +00002486 len = getc(fd); /* <siblingcount> */
2487 if (len <= 0)
2488 return -1;
2489
2490 if (startidx + len >= maxidx)
2491 return -2;
2492 byts[idx++] = len;
2493
2494 /* Read the byte values, flag/region bytes and shared indexes. */
2495 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002496 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002497 c = getc(fd); /* <byte> */
2498 if (c < 0)
2499 return -1;
2500 if (c <= BY_SPECIAL)
2501 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002502 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002503 {
2504 /* No flags, all regions. */
2505 idxs[idx] = 0;
2506 c = 0;
2507 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002508 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002510 if (prefixtree)
2511 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002512 /* Read the optional pflags byte, the prefix ID and the
2513 * condition nr. In idxs[] store the prefix ID in the low
2514 * byte, the condition index shifted up 8 bits, the flags
2515 * shifted up 24 bits. */
2516 if (c == BY_FLAGS)
2517 c = getc(fd) << 24; /* <pflags> */
2518 else
2519 c = 0;
2520
2521 c |= getc(fd); /* <prefixID> */
2522
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002523 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2524 if (n >= maxprefcondnr)
2525 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002526 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002527 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002528 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002529 {
2530 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002531 * idxs[] the flags go in the low two bytes, region above
2532 * that and prefix ID above the region. */
2533 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002534 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002535 if (c2 == BY_FLAGS2)
2536 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002537 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002538 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002539 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002540 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002541 }
2542
Bram Moolenaar51485f02005-06-04 21:55:20 +00002543 idxs[idx] = c;
2544 c = 0;
2545 }
2546 else /* c == BY_INDEX */
2547 {
2548 /* <nodeidx> */
2549 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2550 if (n < 0 || n >= maxidx)
2551 return -2;
2552 idxs[idx] = n + SHARED_MASK;
2553 c = getc(fd); /* <xbyte> */
2554 }
2555 }
2556 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 }
2558
Bram Moolenaar51485f02005-06-04 21:55:20 +00002559 /* Recursively read the children for non-shared siblings.
2560 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2561 * remove SHARED_MASK) */
2562 for (i = 1; i <= len; ++i)
2563 if (byts[startidx + i] != 0)
2564 {
2565 if (idxs[startidx + i] & SHARED_MASK)
2566 idxs[startidx + i] &= ~SHARED_MASK;
2567 else
2568 {
2569 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002570 idx = read_tree(fd, byts, idxs, maxidx, idx,
2571 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002572 if (idx < 0)
2573 break;
2574 }
2575 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002576
Bram Moolenaar51485f02005-06-04 21:55:20 +00002577 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578}
2579
2580/*
2581 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002582 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002583 */
2584 char_u *
2585did_set_spelllang(buf)
2586 buf_T *buf;
2587{
2588 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002589 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002591 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002592 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002593 int region_mask;
2594 slang_T *lp;
2595 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002596 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002597 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002598 int len;
2599 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002600 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002601 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002602 char_u *use_region = NULL;
2603 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002604
2605 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002606 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002607
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002608 /* loop over comma separated language names. */
2609 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002611 /* Get one language name. */
2612 copy_option_part(&splp, lang, MAXWLEN, ",");
2613
Bram Moolenaar5482f332005-04-17 20:18:43 +00002614 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002615 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002617 /* If the name ends in ".spl" use it as the name of the spell file.
2618 * If there is a region name let "region" point to it and remove it
2619 * from the name. */
2620 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2621 {
2622 filename = TRUE;
2623
Bram Moolenaarb6356332005-07-18 21:40:44 +00002624 /* Locate a region and remove it from the file name. */
2625 p = vim_strchr(gettail(lang), '_');
2626 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2627 && !ASCII_ISALPHA(p[3]))
2628 {
2629 vim_strncpy(region_cp, p + 1, 2);
2630 mch_memmove(p, p + 3, len - (p - lang) - 2);
2631 len -= 3;
2632 region = region_cp;
2633 }
2634 else
2635 dont_use_region = TRUE;
2636
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002637 /* Check if we loaded this language before. */
2638 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2639 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2640 break;
2641 }
2642 else
2643 {
2644 filename = FALSE;
2645 if (len > 3 && lang[len - 3] == '_')
2646 {
2647 region = lang + len - 2;
2648 len -= 3;
2649 lang[len] = NUL;
2650 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002651 else
2652 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002653
2654 /* Check if we loaded this language before. */
2655 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2656 if (STRICMP(lang, lp->sl_name) == 0)
2657 break;
2658 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659
Bram Moolenaarb6356332005-07-18 21:40:44 +00002660 if (region != NULL)
2661 {
2662 /* If the region differs from what was used before then don't
2663 * use it for 'spellfile'. */
2664 if (use_region != NULL && STRCMP(region, use_region) != 0)
2665 dont_use_region = TRUE;
2666 use_region = region;
2667 }
2668
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002669 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002670 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002671 {
2672 if (filename)
2673 (void)spell_load_file(lang, lang, NULL, FALSE);
2674 else
2675 spell_load_lang(lang);
2676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002677
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002678 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002679 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002680 */
2681 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002682 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2683 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002684 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002685 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002686 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002687 {
2688 /* find region in sl_regions */
2689 c = find_region(lp->sl_regions, region);
2690 if (c == REGION_ALL)
2691 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002692 if (lp->sl_add)
2693 {
2694 if (*lp->sl_regions != NUL)
2695 /* This addition file is for other regions. */
2696 region_mask = 0;
2697 }
2698 else
2699 /* This is probably an error. Give a warning and
2700 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002701 smsg((char_u *)
2702 _("Warning: region %s not supported"),
2703 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002704 }
2705 else
2706 region_mask = 1 << c;
2707 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002709 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002710 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002711 if (ga_grow(&ga, 1) == FAIL)
2712 {
2713 ga_clear(&ga);
2714 return e_outofmem;
2715 }
2716 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2717 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2718 ++ga.ga_len;
2719 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002720 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002721 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002722 }
2723
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002724 /* round 0: load int_wordlist, if possible.
2725 * round 1: load first name in 'spellfile'.
2726 * round 2: load second name in 'spellfile.
2727 * etc. */
2728 spf = curbuf->b_p_spf;
2729 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002731 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002732 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002733 /* Internal wordlist, if there is one. */
2734 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002735 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002736 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002737 }
2738 else
2739 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002740 /* One entry in 'spellfile'. */
2741 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2742 STRCAT(spf_name, ".spl");
2743
2744 /* If it was already found above then skip it. */
2745 for (c = 0; c < ga.ga_len; ++c)
2746 if (fullpathcmp(spf_name,
2747 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2748 FALSE) == FPC_SAME)
2749 break;
2750 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002751 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002752 }
2753
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002754 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2756 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2757 break;
2758 if (lp == NULL)
2759 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002760 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002761 * region name, the region is ignored otherwise. for int_wordlist
2762 * use an arbitrary name. */
2763 if (round == 0)
2764 STRCPY(lang, "internal wordlist");
2765 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002766 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002767 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002768 p = vim_strchr(lang, '.');
2769 if (p != NULL)
2770 *p = NUL; /* truncate at ".encoding.add" */
2771 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002772 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 }
2774 if (lp != NULL && ga_grow(&ga, 1) == OK)
2775 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002776 region_mask = REGION_ALL;
2777 if (use_region != NULL && !dont_use_region)
2778 {
2779 /* find region in sl_regions */
2780 c = find_region(lp->sl_regions, use_region);
2781 if (c != REGION_ALL)
2782 region_mask = 1 << c;
2783 else if (*lp->sl_regions != NUL)
2784 /* This spell file is for other regions. */
2785 region_mask = 0;
2786 }
2787
2788 if (region_mask != 0)
2789 {
2790 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2791 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2792 ++ga.ga_len;
2793 use_midword(lp, buf);
2794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002795 }
2796 }
2797
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002798 /* Add a NULL entry to mark the end of the list. */
2799 if (ga_grow(&ga, 1) == FAIL)
2800 {
2801 ga_clear(&ga);
2802 return e_outofmem;
2803 }
2804 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2805 ++ga.ga_len;
2806
2807 /* Everything is fine, store the new b_langp value. */
2808 ga_clear(&buf->b_langp);
2809 buf->b_langp = ga;
2810
2811 return NULL;
2812}
2813
2814/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002815 * Clear the midword characters for buffer "buf".
2816 */
2817 static void
2818clear_midword(buf)
2819 buf_T *buf;
2820{
2821 vim_memset(buf->b_spell_ismw, 0, 256);
2822#ifdef FEAT_MBYTE
2823 vim_free(buf->b_spell_ismw_mb);
2824 buf->b_spell_ismw_mb = NULL;
2825#endif
2826}
2827
2828/*
2829 * Use the "sl_midword" field of language "lp" for buffer "buf".
2830 * They add up to any currently used midword characters.
2831 */
2832 static void
2833use_midword(lp, buf)
2834 slang_T *lp;
2835 buf_T *buf;
2836{
2837 char_u *p;
2838
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002839 if (lp->sl_midword == NULL) /* there aren't any */
2840 return;
2841
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002842 for (p = lp->sl_midword; *p != NUL; )
2843#ifdef FEAT_MBYTE
2844 if (has_mbyte)
2845 {
2846 int c, l, n;
2847 char_u *bp;
2848
2849 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002850 l = (*mb_ptr2len)(p);
2851 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002852 buf->b_spell_ismw[c] = TRUE;
2853 else if (buf->b_spell_ismw_mb == NULL)
2854 /* First multi-byte char in "b_spell_ismw_mb". */
2855 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2856 else
2857 {
2858 /* Append multi-byte chars to "b_spell_ismw_mb". */
2859 n = STRLEN(buf->b_spell_ismw_mb);
2860 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2861 if (bp != NULL)
2862 {
2863 vim_free(buf->b_spell_ismw_mb);
2864 buf->b_spell_ismw_mb = bp;
2865 vim_strncpy(bp + n, p, l);
2866 }
2867 }
2868 p += l;
2869 }
2870 else
2871#endif
2872 buf->b_spell_ismw[*p++] = TRUE;
2873}
2874
2875/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002876 * Find the region "region[2]" in "rp" (points to "sl_regions").
2877 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002878 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002879 */
2880 static int
2881find_region(rp, region)
2882 char_u *rp;
2883 char_u *region;
2884{
2885 int i;
2886
2887 for (i = 0; ; i += 2)
2888 {
2889 if (rp[i] == NUL)
2890 return REGION_ALL;
2891 if (rp[i] == region[0] && rp[i + 1] == region[1])
2892 break;
2893 }
2894 return i / 2;
2895}
2896
2897/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002898 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002899 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002900 * Word WF_ONECAP
2901 * W WORD WF_ALLCAP
2902 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002903 */
2904 static int
2905captype(word, end)
2906 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002908{
2909 char_u *p;
2910 int c;
2911 int firstcap;
2912 int allcap;
2913 int past_second = FALSE; /* past second word char */
2914
2915 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002916 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002917 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002918 return 0; /* only non-word characters, illegal word */
2919#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002920 if (has_mbyte)
2921 c = mb_ptr2char_adv(&p);
2922 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002923#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002924 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002925 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002926
2927 /*
2928 * Need to check all letters to find a word with mixed upper/lower.
2929 * But a word with an upper char only at start is a ONECAP.
2930 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002931 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002932 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002933 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002934 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002935 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002936 {
2937 /* UUl -> KEEPCAP */
2938 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002939 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002940 allcap = FALSE;
2941 }
2942 else if (!allcap)
2943 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002944 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945 past_second = TRUE;
2946 }
2947
2948 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002949 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002950 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002951 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002952 return 0;
2953}
2954
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002955/*
2956 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2957 * capital. So that make_case_word() can turn WOrd into Word.
2958 * Add ALLCAP for "WOrD".
2959 */
2960 static int
2961badword_captype(word, end)
2962 char_u *word;
2963 char_u *end;
2964{
2965 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002966 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002967 int l, u;
2968 int first;
2969 char_u *p;
2970
2971 if (flags & WF_KEEPCAP)
2972 {
2973 /* Count the number of UPPER and lower case letters. */
2974 l = u = 0;
2975 first = FALSE;
2976 for (p = word; p < end; mb_ptr_adv(p))
2977 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002978 c = PTR2CHAR(p);
2979 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002980 {
2981 ++u;
2982 if (p == word)
2983 first = TRUE;
2984 }
2985 else
2986 ++l;
2987 }
2988
2989 /* If there are more UPPER than lower case letters suggest an
2990 * ALLCAP word. Otherwise, if the first letter is UPPER then
2991 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2992 * require three upper case letters. */
2993 if (u > l && u > 2)
2994 flags |= WF_ALLCAP;
2995 else if (first)
2996 flags |= WF_ONECAP;
2997 }
2998 return flags;
2999}
3000
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003001# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3002/*
3003 * Free all languages.
3004 */
3005 void
3006spell_free_all()
3007{
3008 slang_T *lp;
3009 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003010 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003011
3012 /* Go through all buffers and handle 'spelllang'. */
3013 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3014 ga_clear(&buf->b_langp);
3015
3016 while (first_lang != NULL)
3017 {
3018 lp = first_lang;
3019 first_lang = lp->sl_next;
3020 slang_free(lp);
3021 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003022
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003023 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003024 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003025 /* Delete the internal wordlist and its .spl file */
3026 mch_remove(int_wordlist);
3027 int_wordlist_spl(fname);
3028 mch_remove(fname);
3029 vim_free(int_wordlist);
3030 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003031 }
3032
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003033 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003034}
3035# endif
3036
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003037# if defined(FEAT_MBYTE) || defined(PROTO)
3038/*
3039 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003040 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003041 */
3042 void
3043spell_reload()
3044{
3045 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003046 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003047
Bram Moolenaarea408852005-06-25 22:49:46 +00003048 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003049 init_spell_chartab();
3050
3051 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003052 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003053
3054 /* Go through all buffers and handle 'spelllang'. */
3055 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3056 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003057 /* Only load the wordlists when 'spelllang' is set and there is a
3058 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003059 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003060 {
3061 FOR_ALL_WINDOWS(wp)
3062 if (wp->w_buffer == buf && wp->w_p_spell)
3063 {
3064 (void)did_set_spelllang(buf);
3065# ifdef FEAT_WINDOWS
3066 break;
3067# endif
3068 }
3069 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003070 }
3071}
3072# endif
3073
Bram Moolenaarb765d632005-06-07 21:00:02 +00003074/*
3075 * Reload the spell file "fname" if it's loaded.
3076 */
3077 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003079 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003080 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003081{
3082 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003083 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003084
Bram Moolenaarb765d632005-06-07 21:00:02 +00003085 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3086 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3087 {
3088 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003090 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003092 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093
3094 /* When "zg" was used and the file wasn't loaded yet, should redo
3095 * 'spelllang' to get it loaded. */
3096 if (added_word && !didit)
3097 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003098}
3099
3100
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003101/*
3102 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003103 */
3104
Bram Moolenaar51485f02005-06-04 21:55:20 +00003105#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106 and .dic file. */
3107/*
3108 * Main structure to store the contents of a ".aff" file.
3109 */
3110typedef struct afffile_S
3111{
3112 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003113 int af_rar; /* RAR ID for rare word */
3114 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003115 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003116 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003117 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3118 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003119} afffile_T;
3120
3121typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3123struct affentry_S
3124{
3125 affentry_T *ae_next; /* next affix with same name/number */
3126 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3127 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003128 char_u *ae_cond; /* condition (NULL for ".") */
3129 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003130 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003131};
3132
Bram Moolenaar53805d12005-08-01 07:08:33 +00003133#define AH_KEY_LEN 10
3134
Bram Moolenaar51485f02005-06-04 21:55:20 +00003135/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3136typedef struct affheader_S
3137{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003138 /* key for hashtable == name of affix entry */
3139#ifdef FEAT_MBYTE
3140 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3141#else
3142 char_u ah_key[2]; /* one byte char plus NUL */
3143#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003144 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003145 int ah_combine; /* suffix may combine with prefix */
3146 affentry_T *ah_first; /* first affix entry */
3147} affheader_T;
3148
3149#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3150
3151/*
3152 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003153 * the need to keep track of each allocated thing, everything is freed all at
3154 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003155 */
3156#define SBLOCKSIZE 16000 /* size of sb_data */
3157typedef struct sblock_S sblock_T;
3158struct sblock_S
3159{
3160 sblock_T *sb_next; /* next block in list */
3161 int sb_used; /* nr of bytes already in use */
3162 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003163};
3164
3165/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003166 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003167 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003168typedef struct wordnode_S wordnode_T;
3169struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003170{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003171 union /* shared to save space */
3172 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003173 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003174 int index; /* index in written nodes (valid after first
3175 round) */
3176 } wn_u1;
3177 union /* shared to save space */
3178 {
3179 wordnode_T *next; /* next node with same hash key */
3180 wordnode_T *wnode; /* parent node that will write this node */
3181 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003182 wordnode_T *wn_child; /* child (next byte in word) */
3183 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3184 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003185 int wn_refs; /* Nr. of references to this node. Only
3186 relevant for first node in a list of
3187 siblings, in following siblings it is
3188 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003189 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003190 char_u wn_prefixID; /* when "wn_byte" is NUL: supported/required
3191 prefix ID or 0 */
3192 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3193 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003194 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003195#ifdef SPELL_PRINTTREE
3196 int wn_nr; /* sequence nr for printing */
3197#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003198};
3199
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003200#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3201
Bram Moolenaar51485f02005-06-04 21:55:20 +00003202#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003203
Bram Moolenaar51485f02005-06-04 21:55:20 +00003204/*
3205 * Info used while reading the spell files.
3206 */
3207typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003208{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003209 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003210 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003211 int si_fold_added; /* nr of words added since compressing */
3212
Bram Moolenaar51485f02005-06-04 21:55:20 +00003213 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003214 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003215 int si_keep_added; /* nr of words added since compressing */
3216
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003217 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003218
Bram Moolenaar51485f02005-06-04 21:55:20 +00003219 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003220 wordnode_T *si_first_free; /* List of nodes that have been freed during
3221 compression, linked by "wn_child" field. */
3222#ifdef SPELL_PRINTTREE
3223 int si_wordnode_nr; /* sequence nr for nodes */
3224#endif
3225
3226
Bram Moolenaar51485f02005-06-04 21:55:20 +00003227 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003228 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003229 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003230 int si_region; /* region mask */
3231 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003232 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003233 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003234 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003235 int si_region_count; /* number of regions supported (1 when there
3236 are no regions) */
3237 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003238
3239 garray_T si_rep; /* list of fromto_T entries from REP lines */
3240 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003241 char_u *si_sofofr; /* SOFOFROM text */
3242 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003243 int si_followup; /* soundsalike: ? */
3244 int si_collapse; /* soundsalike: ? */
3245 int si_rem_accents; /* soundsalike: remove accents */
3246 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003247 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003248 garray_T si_prefcond; /* table with conditions for postponed
3249 * prefixes, each stored as a string */
3250 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003251} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003252
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003253static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003254static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003255static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3256static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003257static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003258static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003259static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
3260static char_u *get_pfxlist __ARGS((spellinfo_T *spin, afffile_T *affile, char_u *afflist));
3261static 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));
3262static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3263static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3264static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003265static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003266static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
3267static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist));
3268static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int prefixID));
3269static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3270static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3271static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3272static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3273static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003274static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003275static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003276static void clear_node __ARGS((wordnode_T *node));
3277static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003278static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003279static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003280
Bram Moolenaar53805d12005-08-01 07:08:33 +00003281/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3282 * but it must be negative to indicate the prefix tree to tree_add_word().
3283 * Use a negative number with the lower 8 bits zero. */
3284#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003285
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003286#ifdef SPELL_PRINTTREE
3287/*
3288 * For debugging the tree code: print the current tree in a (more or less)
3289 * readable format, so that we can see what happens when adding a word and/or
3290 * compressing the tree.
3291 * Based on code from Olaf Seibert.
3292 */
3293#define PRINTLINESIZE 1000
3294#define PRINTWIDTH 6
3295
3296#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3297 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3298
3299static char line1[PRINTLINESIZE];
3300static char line2[PRINTLINESIZE];
3301static char line3[PRINTLINESIZE];
3302
3303 static void
3304spell_clear_flags(wordnode_T *node)
3305{
3306 wordnode_T *np;
3307
3308 for (np = node; np != NULL; np = np->wn_sibling)
3309 {
3310 np->wn_u1.index = FALSE;
3311 spell_clear_flags(np->wn_child);
3312 }
3313}
3314
3315 static void
3316spell_print_node(wordnode_T *node, int depth)
3317{
3318 if (node->wn_u1.index)
3319 {
3320 /* Done this node before, print the reference. */
3321 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3322 PRINTSOME(line2, depth, " ", 0, 0);
3323 PRINTSOME(line3, depth, " ", 0, 0);
3324 msg(line1);
3325 msg(line2);
3326 msg(line3);
3327 }
3328 else
3329 {
3330 node->wn_u1.index = TRUE;
3331
3332 if (node->wn_byte != NUL)
3333 {
3334 if (node->wn_child != NULL)
3335 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3336 else
3337 /* Cannot happen? */
3338 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3339 }
3340 else
3341 PRINTSOME(line1, depth, " $ ", 0, 0);
3342
3343 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3344
3345 if (node->wn_sibling != NULL)
3346 PRINTSOME(line3, depth, " | ", 0, 0);
3347 else
3348 PRINTSOME(line3, depth, " ", 0, 0);
3349
3350 if (node->wn_byte == NUL)
3351 {
3352 msg(line1);
3353 msg(line2);
3354 msg(line3);
3355 }
3356
3357 /* do the children */
3358 if (node->wn_byte != NUL && node->wn_child != NULL)
3359 spell_print_node(node->wn_child, depth + 1);
3360
3361 /* do the siblings */
3362 if (node->wn_sibling != NULL)
3363 {
3364 /* get rid of all parent details except | */
3365 STRCPY(line1, line3);
3366 STRCPY(line2, line3);
3367 spell_print_node(node->wn_sibling, depth);
3368 }
3369 }
3370}
3371
3372 static void
3373spell_print_tree(wordnode_T *root)
3374{
3375 if (root != NULL)
3376 {
3377 /* Clear the "wn_u1.index" fields, used to remember what has been
3378 * done. */
3379 spell_clear_flags(root);
3380
3381 /* Recursively print the tree. */
3382 spell_print_node(root, 0);
3383 }
3384}
3385#endif /* SPELL_PRINTTREE */
3386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003387/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003388 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003389 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003390 */
3391 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003393 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003394 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003395{
3396 FILE *fd;
3397 afffile_T *aff;
3398 char_u rline[MAXLINELEN];
3399 char_u *line;
3400 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003401#define MAXITEMCNT 7
3402 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003403 int itemcnt;
3404 char_u *p;
3405 int lnum = 0;
3406 affheader_T *cur_aff = NULL;
3407 int aff_todo = 0;
3408 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003409 char_u *low = NULL;
3410 char_u *fol = NULL;
3411 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003412 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 int do_rep;
3414 int do_sal;
3415 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003416 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003417 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003418 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003419 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003420 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003421
Bram Moolenaar51485f02005-06-04 21:55:20 +00003422 /*
3423 * Open the file.
3424 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003425 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003426 if (fd == NULL)
3427 {
3428 EMSG2(_(e_notopen), fname);
3429 return NULL;
3430 }
3431
Bram Moolenaarb765d632005-06-07 21:00:02 +00003432 if (spin->si_verbose || p_verbose > 2)
3433 {
3434 if (!spin->si_verbose)
3435 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003436 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003437 out_flush();
3438 if (!spin->si_verbose)
3439 verbose_leave();
3440 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003442 /* Only do REP lines when not done in another .aff file already. */
3443 do_rep = spin->si_rep.ga_len == 0;
3444
3445 /* Only do SAL lines when not done in another .aff file already. */
3446 do_sal = spin->si_sal.ga_len == 0;
3447
3448 /* Only do MAP lines when not done in another .aff file already. */
3449 do_map = spin->si_map.ga_len == 0;
3450
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003451 /* Only do MIDWORD line when not done in another .aff file already */
3452 do_midword = spin->si_midword == NULL;
3453
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003454 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3455 do_sofo = spin->si_sofofr == NULL;
3456
Bram Moolenaar51485f02005-06-04 21:55:20 +00003457 /*
3458 * Allocate and init the afffile_T structure.
3459 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003460 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003461 if (aff == NULL)
3462 return NULL;
3463 hash_init(&aff->af_pref);
3464 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003465
3466 /*
3467 * Read all the lines in the file one by one.
3468 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003469 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003470 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003471 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003472 ++lnum;
3473
3474 /* Skip comment lines. */
3475 if (*rline == '#')
3476 continue;
3477
3478 /* Convert from "SET" to 'encoding' when needed. */
3479 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003480#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003481 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003482 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003483 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003484 if (pc == NULL)
3485 {
3486 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3487 fname, lnum, rline);
3488 continue;
3489 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003490 line = pc;
3491 }
3492 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003493#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003494 {
3495 pc = NULL;
3496 line = rline;
3497 }
3498
3499 /* Split the line up in white separated items. Put a NUL after each
3500 * item. */
3501 itemcnt = 0;
3502 for (p = line; ; )
3503 {
3504 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3505 ++p;
3506 if (*p == NUL)
3507 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003508 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003509 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003510 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003511 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003512 ++p;
3513 if (*p == NUL)
3514 break;
3515 *p++ = NUL;
3516 }
3517
3518 /* Handle non-empty lines. */
3519 if (itemcnt > 0)
3520 {
3521 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3522 && aff->af_enc == NULL)
3523 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003524#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003525 /* Setup for conversion from "ENC" to 'encoding'. */
3526 aff->af_enc = enc_canonize(items[1]);
3527 if (aff->af_enc != NULL && !spin->si_ascii
3528 && convert_setup(&spin->si_conv, aff->af_enc,
3529 p_enc) == FAIL)
3530 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3531 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003532 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003533#else
3534 smsg((char_u *)_("Conversion in %s not supported"), fname);
3535#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003536 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003537 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3538 {
3539 if (do_midword)
3540 spin->si_midword = vim_strsave(items[1]);
3541 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003542 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003544 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003546 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003548 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003549 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003550 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3551 && aff->af_rar == 0)
3552 {
3553 aff->af_rar = items[1][0];
3554 if (items[1][1] != NUL)
3555 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3556 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003557 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3558 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003559 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003560 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003561 if (items[1][1] != NUL)
3562 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3563 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003564 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3565 && aff->af_bad == 0)
3566 {
3567 aff->af_bad = items[1][0];
3568 if (items[1][1] != NUL)
3569 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3570 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003571 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3572 {
3573 aff->af_pfxpostpone = TRUE;
3574 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003575 else if ((STRCMP(items[0], "PFX") == 0
3576 || STRCMP(items[0], "SFX") == 0)
3577 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003578 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003579 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003580 /* Myspell allows extra text after the item, but that might
3581 * mean mistakes go unnoticed. Require a comment-starter. */
3582 if (itemcnt > 4 && *items[4] != '#')
3583 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3584 fname, lnum, items[4]);
3585
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003586 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003587 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003588 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003589 if (cur_aff == NULL)
3590 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003591#ifdef FEAT_MBYTE
3592 if (has_mbyte)
3593 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003594 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003595 if (l >= AH_KEY_LEN)
3596 l = 1; /* too long, must be an overlong sequence */
3597 else
3598 mch_memmove(cur_aff->ah_key, items[1], l);
3599 }
3600 else
3601#endif
3602 {
3603 *cur_aff->ah_key = *items[1];
3604 l = 1;
3605 }
3606 cur_aff->ah_key[l] = NUL;
3607 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003608 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609 if (*items[2] == 'Y')
3610 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003611 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003612 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3613 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003614
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003615 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003616 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003617 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003618 /* Use a new number in the .spl file later, to be able to
3619 * handle multiple .aff files. */
3620 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003621 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003622 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003623 else
3624 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003625 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003626 hi = hash_find(tp, cur_aff->ah_key);
3627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003628 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003629 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3630 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003631 aff_todo = 0;
3632 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003633 else
3634 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003635 }
3636 else if ((STRCMP(items[0], "PFX") == 0
3637 || STRCMP(items[0], "SFX") == 0)
3638 && aff_todo > 0
3639 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003640 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003641 {
3642 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003643 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003644 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003645 int lasti = 5;
3646
3647 /* Check for "rare" after the other info. */
3648 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3649 {
3650 rare = TRUE;
3651 lasti = 6;
3652 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003653
Bram Moolenaar8db73182005-06-17 21:51:16 +00003654 /* Myspell allows extra text after the item, but that might
3655 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003656 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003657 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003658 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003659
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003660 /* New item for an affix letter. */
3661 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003662 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003663 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003664 if (aff_entry == NULL)
3665 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003666 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003667
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003668 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003669 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003670 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003671 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003672
Bram Moolenaar51485f02005-06-04 21:55:20 +00003673 /* Don't use an affix entry with non-ASCII characters when
3674 * "spin->si_ascii" is TRUE. */
3675 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003676 || has_non_ascii(aff_entry->ae_add)))
3677 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003678 aff_entry->ae_next = cur_aff->ah_first;
3679 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003680
3681 if (STRCMP(items[4], ".") != 0)
3682 {
3683 char_u buf[MAXLINELEN];
3684
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003685 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003686 if (*items[0] == 'P')
3687 sprintf((char *)buf, "^%s", items[4]);
3688 else
3689 sprintf((char *)buf, "%s$", items[4]);
3690 aff_entry->ae_prog = vim_regcomp(buf,
3691 RE_MAGIC + RE_STRING);
3692 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003693
3694 /* For postponed prefixes we need an entry in si_prefcond
3695 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003696 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003697 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003698 /* When the chop string is one lower-case letter and
3699 * the add string ends in the upper-case letter we set
3700 * the "upper" flag, clear "ae_chop" and remove the
3701 * letters from "ae_add". The condition must either
3702 * be empty or start with the same letter. */
3703 if (aff_entry->ae_chop != NULL
3704 && aff_entry->ae_add != NULL
3705#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003706 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00003707 aff_entry->ae_chop)] == NUL
3708#else
3709 && aff_entry->ae_chop[1] == NUL
3710#endif
3711 )
3712 {
3713 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003714
Bram Moolenaar53805d12005-08-01 07:08:33 +00003715 c = PTR2CHAR(aff_entry->ae_chop);
3716 c_up = SPELL_TOUPPER(c);
3717 if (c_up != c
3718 && (aff_entry->ae_cond == NULL
3719 || PTR2CHAR(aff_entry->ae_cond) == c))
3720 {
3721 p = aff_entry->ae_add
3722 + STRLEN(aff_entry->ae_add);
3723 mb_ptr_back(aff_entry->ae_add, p);
3724 if (PTR2CHAR(p) == c_up)
3725 {
3726 upper = TRUE;
3727 aff_entry->ae_chop = NULL;
3728 *p = NUL;
3729
3730 /* The condition is matched with the
3731 * actual word, thus must check for the
3732 * upper-case letter. */
3733 if (aff_entry->ae_cond != NULL)
3734 {
3735 char_u buf[MAXLINELEN];
3736#ifdef FEAT_MBYTE
3737 if (has_mbyte)
3738 {
3739 onecap_copy(items[4], buf, TRUE);
3740 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003741 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003742 }
3743 else
3744#endif
3745 *aff_entry->ae_cond = c_up;
3746 if (aff_entry->ae_cond != NULL)
3747 {
3748 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003749 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003750 vim_free(aff_entry->ae_prog);
3751 aff_entry->ae_prog = vim_regcomp(
3752 buf, RE_MAGIC + RE_STRING);
3753 }
3754 }
3755 }
3756 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003757 }
3758
Bram Moolenaar53805d12005-08-01 07:08:33 +00003759 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003760 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003761 int idx;
3762 char_u **pp;
3763 int n;
3764
3765 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3766 --idx)
3767 {
3768 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3769 if (str_equal(p, aff_entry->ae_cond))
3770 break;
3771 }
3772 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3773 {
3774 /* Not found, add a new condition. */
3775 idx = spin->si_prefcond.ga_len++;
3776 pp = ((char_u **)spin->si_prefcond.ga_data)
3777 + idx;
3778 if (aff_entry->ae_cond == NULL)
3779 *pp = NULL;
3780 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003781 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00003782 aff_entry->ae_cond);
3783 }
3784
3785 /* Add the prefix to the prefix tree. */
3786 if (aff_entry->ae_add == NULL)
3787 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003788 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003789 p = aff_entry->ae_add;
3790 /* PFX_FLAGS is a negative number, so that
3791 * tree_add_word() knows this is the prefix tree. */
3792 n = PFX_FLAGS;
3793 if (rare)
3794 n |= WFP_RARE;
3795 if (!cur_aff->ah_combine)
3796 n |= WFP_NC;
3797 if (upper)
3798 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003799 tree_add_word(spin, p, spin->si_prefroot, n,
3800 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003801 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003802 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003803 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003804 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003805 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3806 {
3807 if (fol != NULL)
3808 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3809 fname, lnum);
3810 else
3811 fol = vim_strsave(items[1]);
3812 }
3813 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3814 {
3815 if (low != NULL)
3816 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3817 fname, lnum);
3818 else
3819 low = vim_strsave(items[1]);
3820 }
3821 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3822 {
3823 if (upp != NULL)
3824 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3825 fname, lnum);
3826 else
3827 upp = vim_strsave(items[1]);
3828 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003829 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003831 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (!isdigit(*items[1]))
3833 smsg((char_u *)_("Expected REP count in %s line %d"),
3834 fname, lnum);
3835 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003836 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3837 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003838 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003839 if (do_rep)
3840 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3841 }
3842 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3843 {
3844 /* MAP item or count */
3845 if (!found_map)
3846 {
3847 /* First line contains the count. */
3848 found_map = TRUE;
3849 if (!isdigit(*items[1]))
3850 smsg((char_u *)_("Expected MAP count in %s line %d"),
3851 fname, lnum);
3852 }
3853 else if (do_map)
3854 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003855 int c;
3856
3857 /* Check that every character appears only once. */
3858 for (p = items[1]; *p != NUL; )
3859 {
3860#ifdef FEAT_MBYTE
3861 c = mb_ptr2char_adv(&p);
3862#else
3863 c = *p++;
3864#endif
3865 if ((spin->si_map.ga_len > 0
3866 && vim_strchr(spin->si_map.ga_data, c)
3867 != NULL)
3868 || vim_strchr(p, c) != NULL)
3869 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3870 fname, lnum);
3871 }
3872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 /* We simply concatenate all the MAP strings, separated by
3874 * slashes. */
3875 ga_concat(&spin->si_map, items[1]);
3876 ga_append(&spin->si_map, '/');
3877 }
3878 }
3879 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3880 {
3881 if (do_sal)
3882 {
3883 /* SAL item (sounds-a-like)
3884 * Either one of the known keys or a from-to pair. */
3885 if (STRCMP(items[1], "followup") == 0)
3886 spin->si_followup = sal_to_bool(items[2]);
3887 else if (STRCMP(items[1], "collapse_result") == 0)
3888 spin->si_collapse = sal_to_bool(items[2]);
3889 else if (STRCMP(items[1], "remove_accents") == 0)
3890 spin->si_rem_accents = sal_to_bool(items[2]);
3891 else
3892 /* when "to" is "_" it means empty */
3893 add_fromto(spin, &spin->si_sal, items[1],
3894 STRCMP(items[2], "_") == 0 ? (char_u *)""
3895 : items[2]);
3896 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003897 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003898 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3899 && (!do_sofo || spin->si_sofofr == NULL))
3900 {
3901 if (do_sofo)
3902 spin->si_sofofr = vim_strsave(items[1]);
3903 }
3904 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3905 && (!do_sofo || spin->si_sofoto == NULL))
3906 {
3907 if (do_sofo)
3908 spin->si_sofoto = vim_strsave(items[1]);
3909 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003910 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003911 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3912 fname, lnum, items[0]);
3913 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003914 }
3915
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003916 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3917 smsg((char_u *)_("Missing SOFO%s line in %s"),
3918 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3919 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3920 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3921
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003922 if (fol != NULL || low != NULL || upp != NULL)
3923 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003924 if (spin->si_clear_chartab)
3925 {
3926 /* Clear the char type tables, don't want to use any of the
3927 * currently used spell properties. */
3928 init_spell_chartab();
3929 spin->si_clear_chartab = FALSE;
3930 }
3931
Bram Moolenaar3982c542005-06-08 21:56:31 +00003932 /*
3933 * Don't write a word table for an ASCII file, so that we don't check
3934 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003935 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003936 * mb_get_class(), the list of chars in the file will be incomplete.
3937 */
3938 if (!spin->si_ascii
3939#ifdef FEAT_MBYTE
3940 && !enc_utf8
3941#endif
3942 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003943 {
3944 if (fol == NULL || low == NULL || upp == NULL)
3945 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3946 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003947 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003948 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003949
3950 vim_free(fol);
3951 vim_free(low);
3952 vim_free(upp);
3953 }
3954
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003955 vim_free(pc);
3956 fclose(fd);
3957 return aff;
3958}
3959
3960/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003961 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3962 * NULL as equal.
3963 */
3964 static int
3965str_equal(s1, s2)
3966 char_u *s1;
3967 char_u *s2;
3968{
3969 if (s1 == NULL || s2 == NULL)
3970 return s1 == s2;
3971 return STRCMP(s1, s2) == 0;
3972}
3973
3974/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003975 * Add a from-to item to "gap". Used for REP and SAL items.
3976 * They are stored case-folded.
3977 */
3978 static void
3979add_fromto(spin, gap, from, to)
3980 spellinfo_T *spin;
3981 garray_T *gap;
3982 char_u *from;
3983 char_u *to;
3984{
3985 fromto_T *ftp;
3986 char_u word[MAXWLEN];
3987
3988 if (ga_grow(gap, 1) == OK)
3989 {
3990 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3991 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003992 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003994 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 ++gap->ga_len;
3996 }
3997}
3998
3999/*
4000 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4001 */
4002 static int
4003sal_to_bool(s)
4004 char_u *s;
4005{
4006 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4007}
4008
4009/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004010 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4011 * When "s" is NULL FALSE is returned.
4012 */
4013 static int
4014has_non_ascii(s)
4015 char_u *s;
4016{
4017 char_u *p;
4018
4019 if (s != NULL)
4020 for (p = s; *p != NUL; ++p)
4021 if (*p >= 128)
4022 return TRUE;
4023 return FALSE;
4024}
4025
4026/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004027 * Free the structure filled by spell_read_aff().
4028 */
4029 static void
4030spell_free_aff(aff)
4031 afffile_T *aff;
4032{
4033 hashtab_T *ht;
4034 hashitem_T *hi;
4035 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004036 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004037 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004038
4039 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004040
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004041 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004042 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4043 {
4044 todo = ht->ht_used;
4045 for (hi = ht->ht_array; todo > 0; ++hi)
4046 {
4047 if (!HASHITEM_EMPTY(hi))
4048 {
4049 --todo;
4050 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004051 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4052 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004053 }
4054 }
4055 if (ht == &aff->af_suff)
4056 break;
4057 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059 hash_clear(&aff->af_pref);
4060 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004061}
4062
4063/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004064 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004065 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004066 */
4067 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004068spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004070 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004072{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004076 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004078 char_u *pc;
4079 char_u *w;
4080 int l;
4081 hash_T hash;
4082 hashitem_T *hi;
4083 FILE *fd;
4084 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004085 int non_ascii = 0;
4086 int retval = OK;
4087 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004088 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004089
Bram Moolenaar51485f02005-06-04 21:55:20 +00004090 /*
4091 * Open the file.
4092 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004093 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004094 if (fd == NULL)
4095 {
4096 EMSG2(_(e_notopen), fname);
4097 return FAIL;
4098 }
4099
Bram Moolenaar51485f02005-06-04 21:55:20 +00004100 /* The hashtable is only used to detect duplicated words. */
4101 hash_init(&ht);
4102
Bram Moolenaarb765d632005-06-07 21:00:02 +00004103 if (spin->si_verbose || p_verbose > 2)
4104 {
4105 if (!spin->si_verbose)
4106 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004107 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004108 out_flush();
4109 if (!spin->si_verbose)
4110 verbose_leave();
4111 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004112
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004113 /* start with a message for the first line */
4114 spin->si_msg_count = 999999;
4115
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004116 /* Read and ignore the first line: word count. */
4117 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004118 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004119 EMSG2(_("E760: No word count in %s"), fname);
4120
4121 /*
4122 * Read all the lines in the file one by one.
4123 * The words are converted to 'encoding' here, before being added to
4124 * the hashtable.
4125 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004126 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004128 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004129 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004130 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004131 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004132
Bram Moolenaar51485f02005-06-04 21:55:20 +00004133 /* Remove CR, LF and white space from the end. White space halfway
4134 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004135 l = STRLEN(line);
4136 while (l > 0 && line[l - 1] <= ' ')
4137 --l;
4138 if (l == 0)
4139 continue; /* empty line */
4140 line[l] = NUL;
4141
Bram Moolenaar51485f02005-06-04 21:55:20 +00004142 /* Find the optional affix names. */
4143 afflist = vim_strchr(line, '/');
4144 if (afflist != NULL)
4145 *afflist++ = NUL;
4146
4147 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4148 if (spin->si_ascii && has_non_ascii(line))
4149 {
4150 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004151 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004152 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004153
Bram Moolenaarb765d632005-06-07 21:00:02 +00004154#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004155 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004156 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004157 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004158 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004159 if (pc == NULL)
4160 {
4161 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4162 fname, lnum, line);
4163 continue;
4164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004165 w = pc;
4166 }
4167 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004168#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004169 {
4170 pc = NULL;
4171 w = line;
4172 }
4173
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004174 /* This takes time, print a message every 10000 words. */
4175 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004176 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004177 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004178 vim_snprintf((char *)message, sizeof(message),
4179 _("line %6d, word %6d - %s"),
4180 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4181 msg_start();
4182 msg_puts_long_attr(message, 0);
4183 msg_clr_eos();
4184 msg_didout = FALSE;
4185 msg_col = 0;
4186 out_flush();
4187 }
4188
Bram Moolenaar51485f02005-06-04 21:55:20 +00004189 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004190 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004191 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004192 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004194 if (retval == FAIL)
4195 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004196
Bram Moolenaar51485f02005-06-04 21:55:20 +00004197 hash = hash_hash(dw);
4198 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004199 if (!HASHITEM_EMPTY(hi))
4200 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004201 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004202 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004203 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004204
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004205 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004206 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004207 if (afflist != NULL)
4208 {
4209 /* Check for affix name that stands for keep-case word and stands
4210 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004211 if (affile->af_kep != NUL
4212 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004213 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004214 if (affile->af_rar != NUL
4215 && vim_strchr(afflist, affile->af_rar) != NULL)
4216 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004217 if (affile->af_bad != NUL
4218 && vim_strchr(afflist, affile->af_bad) != NULL)
4219 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004220
4221 if (affile->af_pfxpostpone)
4222 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004223 pfxlist = get_pfxlist(spin, affile, afflist);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004224 }
4225
Bram Moolenaar51485f02005-06-04 21:55:20 +00004226 /* Add the word to the word tree(s). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004227 if (store_word(spin, dw, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004228 retval = FAIL;
4229
4230 if (afflist != NULL)
4231 {
4232 /* Find all matching suffixes and add the resulting words.
4233 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004234 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004235 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004236 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004237 retval = FAIL;
4238
4239 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004240 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004241 &affile->af_pref, NULL,
4242 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004243 retval = FAIL;
4244 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004245 }
4246
Bram Moolenaar51485f02005-06-04 21:55:20 +00004247 if (spin->si_ascii && non_ascii > 0)
4248 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4249 non_ascii);
4250 hash_clear(&ht);
4251
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004252 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004253 return retval;
4254}
4255
4256/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004257 * Get the list of prefix IDs from the affix list "afflist".
4258 * Used for PFXPOSTPONE.
4259 * Returns a string allocated with getroom(). NULL when there are no prefixes
4260 * or when out of memory.
4261 */
4262 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004263get_pfxlist(spin, affile, afflist)
4264 spellinfo_T *spin;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004265 afffile_T *affile;
4266 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004267{
4268 char_u *p;
4269 int cnt;
4270 int round;
4271 char_u *res = NULL;
4272 char_u key[2];
4273 hashitem_T *hi;
4274
4275 key[1] = NUL;
4276
4277 /* round 1: count the number of prefix IDs.
4278 * round 2: move prefix IDs to "res" */
4279 for (round = 1; round <= 2; ++round)
4280 {
4281 cnt = 0;
4282 for (p = afflist; *p != NUL; ++p)
4283 {
4284 key[0] = *p;
4285 hi = hash_find(&affile->af_pref, key);
4286 if (!HASHITEM_EMPTY(hi))
4287 {
4288 /* This is a prefix ID, use the new number. */
4289 if (round == 2)
4290 res[cnt] = HI2AH(hi)->ah_newID;
4291 ++cnt;
4292 }
4293 }
4294 if (round == 1 && cnt > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004295 res = getroom(spin, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004296 if (res == NULL)
4297 break;
4298 }
4299
4300 if (res != NULL)
4301 res[cnt] = NUL;
4302 return res;
4303}
4304
4305/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004306 * Apply affixes to a word and store the resulting words.
4307 * "ht" is the hashtable with affentry_T that need to be applied, either
4308 * prefixes or suffixes.
4309 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4310 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004311 *
4312 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004313 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004314 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004315store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004316 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004317 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004318 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004319 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004320 hashtab_T *ht;
4321 hashtab_T *xht;
4322 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004323 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004324 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325{
4326 int todo;
4327 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004328 affheader_T *ah;
4329 affentry_T *ae;
4330 regmatch_T regmatch;
4331 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004332 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004333 int i;
4334 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004335 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004336 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004337 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004338
Bram Moolenaar51485f02005-06-04 21:55:20 +00004339 todo = ht->ht_used;
4340 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004341 {
4342 if (!HASHITEM_EMPTY(hi))
4343 {
4344 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004345 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004346
Bram Moolenaar51485f02005-06-04 21:55:20 +00004347 /* Check that the affix combines, if required, and that the word
4348 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004349 c = PTR2CHAR(ah->ah_key);
4350 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004351 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004352 /* Loop over all affix entries with this name. */
4353 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004354 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004355 /* Check the condition. It's not logical to match case
4356 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004357 * Myspell.
4358 * For prefixes, when "PFXPOSTPONE" was used, only do
4359 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004360 regmatch.regprog = ae->ae_prog;
4361 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004362 if ((xht != NULL || !affile->af_pfxpostpone
4363 || ae->ae_chop != NULL)
4364 && (ae->ae_prog == NULL
4365 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004366 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004367 /* Match. Remove the chop and add the affix. */
4368 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004369 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004370 /* prefix: chop/add at the start of the word */
4371 if (ae->ae_add == NULL)
4372 *newword = NUL;
4373 else
4374 STRCPY(newword, ae->ae_add);
4375 p = word;
4376 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004377 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004378 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004379#ifdef FEAT_MBYTE
4380 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004381 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004382 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004383 for ( ; i > 0; --i)
4384 mb_ptr_adv(p);
4385 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004386 else
4387#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004388 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004389 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004390 STRCAT(newword, p);
4391 }
4392 else
4393 {
4394 /* suffix: chop/add at the end of the word */
4395 STRCPY(newword, word);
4396 if (ae->ae_chop != NULL)
4397 {
4398 /* Remove chop string. */
4399 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004400 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004401 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004402 mb_ptr_back(newword, p);
4403 *p = NUL;
4404 }
4405 if (ae->ae_add != NULL)
4406 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004407 }
4408
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004409 /* Obey the "rare" flag of the affix. */
4410 if (ae->ae_rare)
4411 use_flags = flags | WF_RARE;
4412 else
4413 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004414 use_pfxlist = pfxlist;
4415
4416 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004417 if (spin->si_prefroot != NULL
4418 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004419 {
4420 /* ... add a flag to indicate an affix was used. */
4421 use_flags |= WF_HAS_AFF;
4422
4423 /* ... don't use a prefix list if combining
4424 * affixes is not allowed */
4425 if (!ah->ah_combine || comb)
4426 use_pfxlist = NULL;
4427 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004428
Bram Moolenaar51485f02005-06-04 21:55:20 +00004429 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004430 if (store_word(spin, newword, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004431 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004432 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004433
Bram Moolenaar51485f02005-06-04 21:55:20 +00004434 /* When added a suffix and combining is allowed also
4435 * try adding prefixes additionally. */
4436 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004437 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004438 xht, NULL, TRUE,
4439 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004440 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004441 }
4442 }
4443 }
4444 }
4445 }
4446
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004447 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004448}
4449
4450/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004451 * Read a file with a list of words.
4452 */
4453 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004454spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004455 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004456 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004457{
4458 FILE *fd;
4459 long lnum = 0;
4460 char_u rline[MAXLINELEN];
4461 char_u *line;
4462 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004463 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004464 int l;
4465 int retval = OK;
4466 int did_word = FALSE;
4467 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004468 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004469 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004470
4471 /*
4472 * Open the file.
4473 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004474 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004475 if (fd == NULL)
4476 {
4477 EMSG2(_(e_notopen), fname);
4478 return FAIL;
4479 }
4480
Bram Moolenaarb765d632005-06-07 21:00:02 +00004481 if (spin->si_verbose || p_verbose > 2)
4482 {
4483 if (!spin->si_verbose)
4484 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004485 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004486 out_flush();
4487 if (!spin->si_verbose)
4488 verbose_leave();
4489 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004490
4491 /*
4492 * Read all the lines in the file one by one.
4493 */
4494 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4495 {
4496 line_breakcheck();
4497 ++lnum;
4498
4499 /* Skip comment lines. */
4500 if (*rline == '#')
4501 continue;
4502
4503 /* Remove CR, LF and white space from the end. */
4504 l = STRLEN(rline);
4505 while (l > 0 && rline[l - 1] <= ' ')
4506 --l;
4507 if (l == 0)
4508 continue; /* empty or blank line */
4509 rline[l] = NUL;
4510
4511 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4512 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004513#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004514 if (spin->si_conv.vc_type != CONV_NONE)
4515 {
4516 pc = string_convert(&spin->si_conv, rline, NULL);
4517 if (pc == NULL)
4518 {
4519 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4520 fname, lnum, rline);
4521 continue;
4522 }
4523 line = pc;
4524 }
4525 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004526#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004527 {
4528 pc = NULL;
4529 line = rline;
4530 }
4531
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004532 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004533 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004534 ++line;
4535 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004536 {
4537 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004538 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4539 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004540 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004541 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4542 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543 else
4544 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004545#ifdef FEAT_MBYTE
4546 char_u *enc;
4547
Bram Moolenaar51485f02005-06-04 21:55:20 +00004548 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004549 line += 10;
4550 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004551 if (enc != NULL && !spin->si_ascii
4552 && convert_setup(&spin->si_conv, enc,
4553 p_enc) == FAIL)
4554 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004555 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004556 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004557 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004558#else
4559 smsg((char_u *)_("Conversion in %s not supported"), fname);
4560#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004561 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004562 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004563 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004564
Bram Moolenaar3982c542005-06-08 21:56:31 +00004565 if (STRNCMP(line, "regions=", 8) == 0)
4566 {
4567 if (spin->si_region_count > 1)
4568 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4569 fname, lnum, line);
4570 else
4571 {
4572 line += 8;
4573 if (STRLEN(line) > 16)
4574 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4575 fname, lnum, line);
4576 else
4577 {
4578 spin->si_region_count = STRLEN(line) / 2;
4579 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004580
4581 /* Adjust the mask for a word valid in all regions. */
4582 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004583 }
4584 }
4585 continue;
4586 }
4587
Bram Moolenaar7887d882005-07-01 22:33:52 +00004588 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4589 fname, lnum, line - 1);
4590 continue;
4591 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004592
Bram Moolenaar7887d882005-07-01 22:33:52 +00004593 flags = 0;
4594 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004595
Bram Moolenaar7887d882005-07-01 22:33:52 +00004596 /* Check for flags and region after a slash. */
4597 p = vim_strchr(line, '/');
4598 if (p != NULL)
4599 {
4600 *p++ = NUL;
4601 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004602 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004603 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004604 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004605 else if (*p == '!') /* Bad, bad, wicked word. */
4606 flags |= WF_BANNED;
4607 else if (*p == '?') /* Rare word. */
4608 flags |= WF_RARE;
4609 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004610 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004611 if ((flags & WF_REGION) == 0) /* first one */
4612 regionmask = 0;
4613 flags |= WF_REGION;
4614
4615 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004616 if (l > spin->si_region_count)
4617 {
4618 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004619 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004620 break;
4621 }
4622 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004623 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004624 else
4625 {
4626 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4627 fname, lnum, p);
4628 break;
4629 }
4630 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004631 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004632 }
4633
4634 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4635 if (spin->si_ascii && has_non_ascii(line))
4636 {
4637 ++non_ascii;
4638 continue;
4639 }
4640
4641 /* Normal word: store it. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004642 if (store_word(spin, line, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004643 {
4644 retval = FAIL;
4645 break;
4646 }
4647 did_word = TRUE;
4648 }
4649
4650 vim_free(pc);
4651 fclose(fd);
4652
Bram Moolenaarb765d632005-06-07 21:00:02 +00004653 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4654 {
4655 if (p_verbose > 2)
4656 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004657 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4658 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004659 if (p_verbose > 2)
4660 verbose_leave();
4661 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004662 return retval;
4663}
4664
4665/*
4666 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004667 * This avoids calling free() for every little struct we use (and keeping
4668 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004669 * The memory is cleared to all zeros.
4670 * Returns NULL when out of memory.
4671 */
4672 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004673getroom(spin, len, align)
4674 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004675 size_t len; /* length needed */
4676 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004677{
4678 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004679 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004680
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004681 if (align && bl != NULL)
4682 /* Round size up for alignment. On some systems structures need to be
4683 * aligned to the size of a pointer (e.g., SPARC). */
4684 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4685 & ~(sizeof(char *) - 1);
4686
Bram Moolenaar51485f02005-06-04 21:55:20 +00004687 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4688 {
4689 /* Allocate a block of memory. This is not freed until much later. */
4690 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4691 if (bl == NULL)
4692 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004693 bl->sb_next = spin->si_blocks;
4694 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004695 bl->sb_used = 0;
4696 }
4697
4698 p = bl->sb_data + bl->sb_used;
4699 bl->sb_used += len;
4700
4701 return p;
4702}
4703
4704/*
4705 * Make a copy of a string into memory allocated with getroom().
4706 */
4707 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004708getroom_save(spin, s)
4709 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004710 char_u *s;
4711{
4712 char_u *sc;
4713
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004714 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004715 if (sc != NULL)
4716 STRCPY(sc, s);
4717 return sc;
4718}
4719
4720
4721/*
4722 * Free the list of allocated sblock_T.
4723 */
4724 static void
4725free_blocks(bl)
4726 sblock_T *bl;
4727{
4728 sblock_T *next;
4729
4730 while (bl != NULL)
4731 {
4732 next = bl->sb_next;
4733 vim_free(bl);
4734 bl = next;
4735 }
4736}
4737
4738/*
4739 * Allocate the root of a word tree.
4740 */
4741 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004742wordtree_alloc(spin)
4743 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004744{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004745 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004746}
4747
4748/*
4749 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004750 * Always store it in the case-folded tree. For a keep-case word this is
4751 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4752 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004753 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004754 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004755 */
4756 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004757store_word(spin, word, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004758 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004759 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004760 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004761 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004762 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004763{
4764 int len = STRLEN(word);
4765 int ct = captype(word, word + len);
4766 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004767 int res = OK;
4768 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004771 for (p = pfxlist; res == OK; ++p)
4772 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004773 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4774 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004775 if (p == NULL || *p == NUL)
4776 break;
4777 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004778 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004779
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004780 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004781 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004782 for (p = pfxlist; res == OK; ++p)
4783 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004784 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4785 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004786 if (p == NULL || *p == NUL)
4787 break;
4788 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004789 ++spin->si_keepwcount;
4790 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004791 return res;
4792}
4793
4794/*
4795 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004796 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4797 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004798 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004800 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004801tree_add_word(spin, word, root, flags, region, prefixID)
4802 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803 char_u *word;
4804 wordnode_T *root;
4805 int flags;
4806 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004807 int prefixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004808{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 wordnode_T *node = root;
4810 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004811 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004812 wordnode_T **prev = NULL;
4813 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004814
Bram Moolenaar51485f02005-06-04 21:55:20 +00004815 /* Add each byte of the word to the tree, including the NUL at the end. */
4816 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004817 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004818 /* When there is more than one reference to this node we need to make
4819 * a copy, so that we can modify it. Copy the whole list of siblings
4820 * (we don't optimize for a partly shared list of siblings). */
4821 if (node != NULL && node->wn_refs > 1)
4822 {
4823 --node->wn_refs;
4824 copyprev = prev;
4825 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4826 {
4827 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004828 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004829 if (np == NULL)
4830 return FAIL;
4831 np->wn_child = copyp->wn_child;
4832 if (np->wn_child != NULL)
4833 ++np->wn_child->wn_refs; /* child gets extra ref */
4834 np->wn_byte = copyp->wn_byte;
4835 if (np->wn_byte == NUL)
4836 {
4837 np->wn_flags = copyp->wn_flags;
4838 np->wn_region = copyp->wn_region;
4839 np->wn_prefixID = copyp->wn_prefixID;
4840 }
4841
4842 /* Link the new node in the list, there will be one ref. */
4843 np->wn_refs = 1;
4844 *copyprev = np;
4845 copyprev = &np->wn_sibling;
4846
4847 /* Let "node" point to the head of the copied list. */
4848 if (copyp == node)
4849 node = np;
4850 }
4851 }
4852
Bram Moolenaar51485f02005-06-04 21:55:20 +00004853 /* Look for the sibling that has the same character. They are sorted
4854 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004855 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004856 * done on flags and then on prefixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004857 while (node != NULL
4858 && (node->wn_byte < word[i]
4859 || (node->wn_byte == NUL
4860 && (flags < 0
4861 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004862 : node->wn_flags < (flags & WN_MASK)
4863 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004864 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004865 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866 prev = &node->wn_sibling;
4867 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004868 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004869 if (node == NULL
4870 || node->wn_byte != word[i]
4871 || (word[i] == NUL
4872 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004873 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004874 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004876 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004877 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004878 if (np == NULL)
4879 return FAIL;
4880 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004881
4882 /* If "node" is NULL this is a new child or the end of the sibling
4883 * list: ref count is one. Otherwise use ref count of sibling and
4884 * make ref count of sibling one (matters when inserting in front
4885 * of the list of siblings). */
4886 if (node == NULL)
4887 np->wn_refs = 1;
4888 else
4889 {
4890 np->wn_refs = node->wn_refs;
4891 node->wn_refs = 1;
4892 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004893 *prev = np;
4894 np->wn_sibling = node;
4895 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897
Bram Moolenaar51485f02005-06-04 21:55:20 +00004898 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004899 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004900 node->wn_flags = flags;
4901 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004902 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004903 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004904 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004905 prev = &node->wn_child;
4906 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004907 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004908#ifdef SPELL_PRINTTREE
4909 smsg("Added \"%s\"", word);
4910 spell_print_tree(root->wn_sibling);
4911#endif
4912
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004913 /* count nr of words added since last message */
4914 ++spin->si_msg_count;
4915
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004916 /*
4917 * Every so many words compress the tree, so that we don't use too much
4918 * memory.
4919 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004920 i = FALSE;
4921 if (root == spin->si_foldroot)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004922 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004923 if (++spin->si_fold_added >= SPELL_COMPRESS_CNT)
4924 {
4925 i = TRUE;
4926 spin->si_fold_added = 0;
4927 }
4928 }
4929 else if (root == spin->si_keeproot)
4930 {
4931 if (++spin->si_keep_added >= SPELL_COMPRESS_CNT)
4932 {
4933 i = TRUE;
4934 spin->si_keep_added = 0;
4935 }
4936 }
4937 if (i)
4938 {
4939 if (spin->si_verbose)
4940 {
4941 msg_start();
4942 msg_puts((char_u *)_(msg_compressing));
4943 msg_clr_eos();
4944 msg_didout = FALSE;
4945 msg_col = 0;
4946 out_flush();
4947 }
4948 wordtree_compress(spin, root);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004949 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004950
4951 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952}
4953
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004954/*
4955 * Get a wordnode_T, either from the list of previously freed nodes or
4956 * allocate a new one.
4957 */
4958 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004959get_wordnode(spin)
4960 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004961{
4962 wordnode_T *n;
4963
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004964 if (spin->si_first_free == NULL)
4965 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004966 else
4967 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004968 n = spin->si_first_free;
4969 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004970 vim_memset(n, 0, sizeof(wordnode_T));
4971 }
4972#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004973 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004974#endif
4975 return n;
4976}
4977
4978/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004979 * Decrement the reference count on a node (which is the head of a list of
4980 * siblings). If the reference count becomes zero free the node and its
4981 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004982 */
4983 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004984deref_wordnode(spin, node)
4985 spellinfo_T *spin;
4986 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004987{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004988 wordnode_T *np;
4989
4990 if (--node->wn_refs == 0)
4991 for (np = node; np != NULL; np = np->wn_sibling)
4992 {
4993 if (np->wn_child != NULL)
4994 deref_wordnode(spin, np->wn_child);
4995 free_wordnode(spin, np);
4996 }
4997}
4998
4999/*
5000 * Free a wordnode_T for re-use later.
5001 * Only the "wn_child" field becomes invalid.
5002 */
5003 static void
5004free_wordnode(spin, n)
5005 spellinfo_T *spin;
5006 wordnode_T *n;
5007{
5008 n->wn_child = spin->si_first_free;
5009 spin->si_first_free = n;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005010}
5011
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005012/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005013 * Compress a tree: find tails that are identical and can be shared.
5014 */
5015 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005016wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005017 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005018 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005019{
5020 hashtab_T ht;
5021 int n;
5022 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005023 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005024
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005025 /* Skip the root itself, it's not actually used. The first sibling is the
5026 * start of the tree. */
5027 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005028 {
5029 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005030 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005031
5032#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005033 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005034#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005035 {
5036 if (!spin->si_verbose)
5037 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005038 if (tot > 1000000)
5039 perc = (tot - n) / (tot / 100);
5040 else
5041 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005042 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005043 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005044 if (p_verbose > 2)
5045 verbose_leave();
5046 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005047#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005048 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005049#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005050 hash_clear(&ht);
5051 }
5052}
5053
5054/*
5055 * Compress a node, its siblings and its children, depth first.
5056 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005057 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005058 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005059node_compress(spin, node, ht, tot)
5060 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005061 wordnode_T *node;
5062 hashtab_T *ht;
5063 int *tot; /* total count of nodes before compressing,
5064 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005065{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005066 wordnode_T *np;
5067 wordnode_T *tp;
5068 wordnode_T *child;
5069 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005070 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005071 int len = 0;
5072 unsigned nr, n;
5073 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005074
Bram Moolenaar51485f02005-06-04 21:55:20 +00005075 /*
5076 * Go through the list of siblings. Compress each child and then try
5077 * finding an identical child to replace it.
5078 * Note that with "child" we mean not just the node that is pointed to,
5079 * but the whole list of siblings, of which the node is the first.
5080 */
5081 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005082 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005083 ++len;
5084 if ((child = np->wn_child) != NULL)
5085 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005086 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005087 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005088
5089 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005090 hash = hash_hash(child->wn_u1.hashkey);
5091 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005092 tp = NULL;
5093 if (!HASHITEM_EMPTY(hi))
5094 {
5095 /* There are children with an identical hash value. Now check
5096 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005097 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005098 if (node_equal(child, tp))
5099 {
5100 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005101 * current one. This means the current child and all
5102 * its siblings is unlinked from the tree. */
5103 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005104 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005105 np->wn_child = tp;
5106 ++compressed;
5107 break;
5108 }
5109 if (tp == NULL)
5110 {
5111 /* No other child with this hash value equals the child of
5112 * the node, add it to the linked list after the first
5113 * item. */
5114 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005115 child->wn_u2.next = tp->wn_u2.next;
5116 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005117 }
5118 }
5119 else
5120 /* No other child has this hash value, add it to the
5121 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005122 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005123 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005124 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005125 *tot += len;
5126
5127 /*
5128 * Make a hash key for the node and its siblings, so that we can quickly
5129 * find a lookalike node. This must be done after compressing the sibling
5130 * list, otherwise the hash key would become invalid by the compression.
5131 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005132 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005133 nr = 0;
5134 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005135 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005136 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005137 /* end node: use wn_flags, wn_region and wn_prefixID */
5138 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005139 else
5140 /* byte node: use the byte value and the child pointer */
5141 n = np->wn_byte + ((long_u)np->wn_child << 8);
5142 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005143 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005144
5145 /* Avoid NUL bytes, it terminates the hash key. */
5146 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005147 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005148 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005149 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005150 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005151 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005152 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005153 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
5154 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005155
5156 return compressed;
5157}
5158
5159/*
5160 * Return TRUE when two nodes have identical siblings and children.
5161 */
5162 static int
5163node_equal(n1, n2)
5164 wordnode_T *n1;
5165 wordnode_T *n2;
5166{
5167 wordnode_T *p1;
5168 wordnode_T *p2;
5169
5170 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
5171 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
5172 if (p1->wn_byte != p2->wn_byte
5173 || (p1->wn_byte == NUL
5174 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005175 || p1->wn_region != p2->wn_region
5176 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005177 : (p1->wn_child != p2->wn_child)))
5178 break;
5179
5180 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005181}
5182
5183/*
5184 * Write a number to file "fd", MSB first, in "len" bytes.
5185 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005186 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005187put_bytes(fd, nr, len)
5188 FILE *fd;
5189 long_u nr;
5190 int len;
5191{
5192 int i;
5193
5194 for (i = len - 1; i >= 0; --i)
5195 putc((int)(nr >> (i * 8)), fd);
5196}
5197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005198static int
5199#ifdef __BORLANDC__
5200_RTLENTRYF
5201#endif
5202rep_compare __ARGS((const void *s1, const void *s2));
5203
5204/*
5205 * Function given to qsort() to sort the REP items on "from" string.
5206 */
5207 static int
5208#ifdef __BORLANDC__
5209_RTLENTRYF
5210#endif
5211rep_compare(s1, s2)
5212 const void *s1;
5213 const void *s2;
5214{
5215 fromto_T *p1 = (fromto_T *)s1;
5216 fromto_T *p2 = (fromto_T *)s2;
5217
5218 return STRCMP(p1->ft_from, p2->ft_from);
5219}
5220
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005221/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005222 * Write the Vim spell file "fname".
5223 */
5224 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005225write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005226 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005227 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005228{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005229 FILE *fd;
5230 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005231 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005232 wordnode_T *tree;
5233 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 int i;
5235 int l;
5236 garray_T *gap;
5237 fromto_T *ftp;
5238 char_u *p;
5239 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005240
Bram Moolenaarb765d632005-06-07 21:00:02 +00005241 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005242 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005243 {
5244 EMSG2(_(e_notopen), fname);
5245 return;
5246 }
5247
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005248 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005249 * <charflagslen> <charflags>
5250 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005251 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005252 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005253
5254 /* <fileID> */
5255 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
5256 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005257
5258 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005259 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005260 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005261 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
5262 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
5263 (size_t)1, fd);
5264 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005265 }
5266 else
5267 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005268 putc(0, fd);
5269 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005270 }
5271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005272 /*
5273 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005274 * <charflagslen> <charflags> <fcharlen> <fchars>
5275 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 * 'encoding'.
5277 * Also skip this for an .add.spl file, the main spell file must contain
5278 * the table (avoids that it conflicts). File is shorter too.
5279 */
5280 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005281 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005282 putc(0, fd);
5283 putc(0, fd);
5284 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005285 }
5286 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005287 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005288
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005289
5290 if (spin->si_midword == NULL)
5291 put_bytes(fd, 0L, 2); /* <midwordlen> */
5292 else
5293 {
5294 i = STRLEN(spin->si_midword);
5295 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
5296 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
5297 }
5298
5299
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005300 /* Write the prefix conditions. */
5301 write_spell_prefcond(fd, &spin->si_prefcond);
5302
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005303 /* <SUGGEST> : <repcount> <rep> ...
5304 * <salflags> <salcount> <sal> ...
5305 * <maplen> <mapstr> */
5306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 /* Sort the REP items. */
5308 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
5309 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005310
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005311 /* round 1: REP items
5312 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 for (round = 1; round <= 2; ++round)
5314 {
5315 if (round == 1)
5316 gap = &spin->si_rep;
5317 else
5318 {
5319 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 i = 0;
5322 if (spin->si_followup)
5323 i |= SAL_F0LLOWUP;
5324 if (spin->si_collapse)
5325 i |= SAL_COLLAPSE;
5326 if (spin->si_rem_accents)
5327 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005328 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5329 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005331 if (i & SAL_SOFO)
5332 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 }
5334
5335 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
5336 for (i = 0; i < gap->ga_len; ++i)
5337 {
5338 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
5339 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
5340 ftp = &((fromto_T *)gap->ga_data)[i];
5341 for (rr = 1; rr <= 2; ++rr)
5342 {
5343 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5344 l = STRLEN(p);
5345 putc(l, fd);
5346 fwrite(p, l, (size_t)1, fd);
5347 }
5348 }
5349 }
5350
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005351 /* SOFOFROM and SOFOTO */
5352 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5353 {
5354 put_bytes(fd, 1L, 2); /* <salcount> */
5355
5356 l = STRLEN(spin->si_sofofr);
5357 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5358 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5359
5360 l = STRLEN(spin->si_sofoto);
5361 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5362 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5363 }
5364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005365 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5366 if (spin->si_map.ga_len > 0) /* <mapstr> */
5367 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5368 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005369
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005370 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005371 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005372 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005374 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005375 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005376 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005377 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005378 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005379 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005380 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005381 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005382
Bram Moolenaar0c405862005-06-22 22:26:26 +00005383 /* Clear the index and wnode fields in the tree. */
5384 clear_node(tree);
5385
Bram Moolenaar51485f02005-06-04 21:55:20 +00005386 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005387 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005388 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005389 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005390
Bram Moolenaar51485f02005-06-04 21:55:20 +00005391 /* number of nodes in 4 bytes */
5392 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005393 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005394
Bram Moolenaar51485f02005-06-04 21:55:20 +00005395 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005396 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005397 }
5398
Bram Moolenaar51485f02005-06-04 21:55:20 +00005399 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005400}
5401
5402/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005403 * Clear the index and wnode fields of "node", it siblings and its
5404 * children. This is needed because they are a union with other items to save
5405 * space.
5406 */
5407 static void
5408clear_node(node)
5409 wordnode_T *node;
5410{
5411 wordnode_T *np;
5412
5413 if (node != NULL)
5414 for (np = node; np != NULL; np = np->wn_sibling)
5415 {
5416 np->wn_u1.index = 0;
5417 np->wn_u2.wnode = NULL;
5418
5419 if (np->wn_byte != NUL)
5420 clear_node(np->wn_child);
5421 }
5422}
5423
5424
5425/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005426 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005427 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005428 * This first writes the list of possible bytes (siblings). Then for each
5429 * byte recursively write the children.
5430 *
5431 * NOTE: The code here must match the code in read_tree(), since assumptions
5432 * are made about the indexes (so that we don't have to write them in the
5433 * file).
5434 *
5435 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005436 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005437 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005438put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005439 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005440 wordnode_T *node;
5441 int index;
5442 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005443 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005444{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005445 int newindex = index;
5446 int siblingcount = 0;
5447 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005448 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005449
Bram Moolenaar51485f02005-06-04 21:55:20 +00005450 /* If "node" is zero the tree is empty. */
5451 if (node == NULL)
5452 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005453
Bram Moolenaar51485f02005-06-04 21:55:20 +00005454 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005455 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005456
5457 /* Count the number of siblings. */
5458 for (np = node; np != NULL; np = np->wn_sibling)
5459 ++siblingcount;
5460
5461 /* Write the sibling count. */
5462 if (fd != NULL)
5463 putc(siblingcount, fd); /* <siblingcount> */
5464
5465 /* Write each sibling byte and optionally extra info. */
5466 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005467 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005468 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005469 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005470 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005471 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005472 /* For a NUL byte (end of word) write the flags etc. */
5473 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005474 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005475 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005476 * associated condition nr (stored in wn_region). The
5477 * byte value is misused to store the "rare" and "not
5478 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005479 if (np->wn_flags == (short_u)PFX_FLAGS)
5480 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005481 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005482 {
5483 putc(BY_FLAGS, fd); /* <byte> */
5484 putc(np->wn_flags, fd); /* <pflags> */
5485 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005486 putc(np->wn_prefixID, fd); /* <prefixID> */
5487 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005488 }
5489 else
5490 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005491 /* For word trees we write the flag/region items. */
5492 flags = np->wn_flags;
5493 if (regionmask != 0 && np->wn_region != regionmask)
5494 flags |= WF_REGION;
5495 if (np->wn_prefixID != 0)
5496 flags |= WF_PFX;
5497 if (flags == 0)
5498 {
5499 /* word without flags or region */
5500 putc(BY_NOFLAGS, fd); /* <byte> */
5501 }
5502 else
5503 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005504 if (np->wn_flags >= 0x100)
5505 {
5506 putc(BY_FLAGS2, fd); /* <byte> */
5507 putc(flags, fd); /* <flags> */
5508 putc((unsigned)flags >> 8, fd); /* <flags2> */
5509 }
5510 else
5511 {
5512 putc(BY_FLAGS, fd); /* <byte> */
5513 putc(flags, fd); /* <flags> */
5514 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005515 if (flags & WF_REGION)
5516 putc(np->wn_region, fd); /* <region> */
5517 if (flags & WF_PFX)
5518 putc(np->wn_prefixID, fd); /* <prefixID> */
5519 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005520 }
5521 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005522 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005523 else
5524 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005525 if (np->wn_child->wn_u1.index != 0
5526 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005527 {
5528 /* The child is written elsewhere, write the reference. */
5529 if (fd != NULL)
5530 {
5531 putc(BY_INDEX, fd); /* <byte> */
5532 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005533 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005534 }
5535 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005536 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005537 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005538 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005539
Bram Moolenaar51485f02005-06-04 21:55:20 +00005540 if (fd != NULL)
5541 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5542 {
5543 EMSG(_(e_write));
5544 return 0;
5545 }
5546 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005547 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005548
5549 /* Space used in the array when reading: one for each sibling and one for
5550 * the count. */
5551 newindex += siblingcount + 1;
5552
5553 /* Recursively dump the children of each sibling. */
5554 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005555 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5556 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005557 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005558
5559 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005560}
5561
5562
5563/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005564 * ":mkspell [-ascii] outfile infile ..."
5565 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005566 */
5567 void
5568ex_mkspell(eap)
5569 exarg_T *eap;
5570{
5571 int fcount;
5572 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005573 char_u *arg = eap->arg;
5574 int ascii = FALSE;
5575
5576 if (STRNCMP(arg, "-ascii", 6) == 0)
5577 {
5578 ascii = TRUE;
5579 arg = skipwhite(arg + 6);
5580 }
5581
5582 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5583 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005585 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005586 FreeWild(fcount, fnames);
5587 }
5588}
5589
5590/*
5591 * Create a Vim spell file from one or more word lists.
5592 * "fnames[0]" is the output file name.
5593 * "fnames[fcount - 1]" is the last input file name.
5594 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5595 * and ".spl" is appended to make the output file name.
5596 */
5597 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005598mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005599 int fcount;
5600 char_u **fnames;
5601 int ascii; /* -ascii argument given */
5602 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005603 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005604{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005605 char_u fname[MAXPATHL];
5606 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005607 char_u **innames;
5608 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005609 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005610 int i;
5611 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005612 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005613 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005614 spellinfo_T spin;
5615
5616 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005617 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005618 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005619 spin.si_followup = TRUE;
5620 spin.si_rem_accents = TRUE;
5621 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5622 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5623 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005624 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005625
Bram Moolenaarb765d632005-06-07 21:00:02 +00005626 /* default: fnames[0] is output file, following are input files */
5627 innames = &fnames[1];
5628 incount = fcount - 1;
5629
5630 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005631 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005632 len = STRLEN(fnames[0]);
5633 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5634 {
5635 /* For ":mkspell path/en.latin1.add" output file is
5636 * "path/en.latin1.add.spl". */
5637 innames = &fnames[0];
5638 incount = 1;
5639 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5640 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005641 else if (fcount == 1)
5642 {
5643 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5644 innames = &fnames[0];
5645 incount = 1;
5646 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5647 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5648 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005649 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5650 {
5651 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005652 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005653 }
5654 else
5655 /* Name should be language, make the file name from it. */
5656 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5657 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5658
5659 /* Check for .ascii.spl. */
5660 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5661 spin.si_ascii = TRUE;
5662
5663 /* Check for .add.spl. */
5664 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5665 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005666 }
5667
Bram Moolenaarb765d632005-06-07 21:00:02 +00005668 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005669 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005670 else if (vim_strchr(gettail(wfname), '_') != NULL)
5671 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005672 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005673 EMSG(_("E754: Only up to 8 regions supported"));
5674 else
5675 {
5676 /* Check for overwriting before doing things that may take a lot of
5677 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005678 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005679 {
5680 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005681 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005682 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005683 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005684 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005685 EMSG2(_(e_isadir2), wfname);
5686 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005687 }
5688
5689 /*
5690 * Init the aff and dic pointers.
5691 * Get the region names if there are more than 2 arguments.
5692 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005693 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005694 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005695 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005696
Bram Moolenaar3982c542005-06-08 21:56:31 +00005697 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005698 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005699 len = STRLEN(innames[i]);
5700 if (STRLEN(gettail(innames[i])) < 5
5701 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005702 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005703 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5704 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005705 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005706 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5707 spin.si_region_name[i * 2 + 1] =
5708 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005709 }
5710 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005711 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005712
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005713 spin.si_foldroot = wordtree_alloc(&spin);
5714 spin.si_keeproot = wordtree_alloc(&spin);
5715 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005716 if (spin.si_foldroot == NULL
5717 || spin.si_keeproot == NULL
5718 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005719 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005720 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005721 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005722 }
5723
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005724 /* When not producing a .add.spl file clear the character table when
5725 * we encounter one in the .aff file. This means we dump the current
5726 * one in the .spl file if the .aff file doesn't define one. That's
5727 * better than guessing the contents, the table will match a
5728 * previously loaded spell file. */
5729 if (!spin.si_add)
5730 spin.si_clear_chartab = TRUE;
5731
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005732 /*
5733 * Read all the .aff and .dic files.
5734 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005736 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005737 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005738 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005739 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005740 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005741
Bram Moolenaarb765d632005-06-07 21:00:02 +00005742 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005743 if (mch_stat((char *)fname, &st) >= 0)
5744 {
5745 /* Read the .aff file. Will init "spin->si_conv" based on the
5746 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005747 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005748 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005749 error = TRUE;
5750 else
5751 {
5752 /* Read the .dic file and store the words in the trees. */
5753 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005754 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005755 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005756 error = TRUE;
5757 }
5758 }
5759 else
5760 {
5761 /* No .aff file, try reading the file as a word list. Store
5762 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005763 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005764 error = TRUE;
5765 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005766
Bram Moolenaarb765d632005-06-07 21:00:02 +00005767#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005768 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005769 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005770#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005771 }
5772
Bram Moolenaar51485f02005-06-04 21:55:20 +00005773 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005774 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005775 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005776 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005777 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005778 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005779 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005780 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005781 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005782 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005783 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005784 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005785 verbose_leave();
5786 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005787 wordtree_compress(&spin, spin.si_foldroot);
5788 wordtree_compress(&spin, spin.si_keeproot);
5789 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005790 }
5791
Bram Moolenaar51485f02005-06-04 21:55:20 +00005792 if (!error)
5793 {
5794 /*
5795 * Write the info in the spell file.
5796 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005797 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005798 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005799 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005800 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005801 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005802 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005803 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005804 verbose_leave();
5805 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005806
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005807 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005808
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005809 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005811 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005812 verbose_enter();
5813 MSG(_("Done!"));
5814 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005815 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005816 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005817 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005818 verbose_leave();
5819 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005820
Bram Moolenaarb765d632005-06-07 21:00:02 +00005821 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005822 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005823 }
5824
5825 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005826 ga_clear(&spin.si_rep);
5827 ga_clear(&spin.si_sal);
5828 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005829 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005830 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005831 vim_free(spin.si_sofofr);
5832 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005833
5834 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005835 for (i = 0; i < incount; ++i)
5836 if (afile[i] != NULL)
5837 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005838
5839 /* Free all the bits and pieces at once. */
5840 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005841 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005842}
5843
Bram Moolenaarb765d632005-06-07 21:00:02 +00005844
5845/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005846 * ":[count]spellgood {word}"
5847 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005848 */
5849 void
5850ex_spell(eap)
5851 exarg_T *eap;
5852{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005853 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005854 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005855}
5856
5857/*
5858 * Add "word[len]" to 'spellfile' as a good or bad word.
5859 */
5860 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005861spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005862 char_u *word;
5863 int len;
5864 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005865 int index; /* "zG" and "zW": zero, otherwise index in
5866 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005867{
5868 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005869 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005870 int new_spf = FALSE;
5871 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005872 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005873 char_u fnamebuf[MAXPATHL];
5874 char_u line[MAXWLEN * 2];
5875 long fpos, fpos_next = 0;
5876 int i;
5877 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005878
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005879 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005880 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005881 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005882 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005883 int_wordlist = vim_tempname('s');
5884 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005885 return;
5886 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005887 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005888 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005889 else
5890 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005891 /* If 'spellfile' isn't set figure out a good default value. */
5892 if (*curbuf->b_p_spf == NUL)
5893 {
5894 init_spellfile();
5895 new_spf = TRUE;
5896 }
5897
5898 if (*curbuf->b_p_spf == NUL)
5899 {
5900 EMSG(_("E764: 'spellfile' is not set"));
5901 return;
5902 }
5903
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005904 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5905 {
5906 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5907 if (i == index)
5908 break;
5909 if (*spf == NUL)
5910 {
5911 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5912 return;
5913 }
5914 }
5915
Bram Moolenaarb765d632005-06-07 21:00:02 +00005916 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005917 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005918 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5919 buf = NULL;
5920 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005921 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005922 EMSG(_(e_bufloaded));
5923 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005924 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005925
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005926 fname = fnamebuf;
5927 }
5928
5929 if (bad)
5930 {
5931 /* When the word also appears as good word we need to remove that one,
5932 * since its flags sort before the one with WF_BANNED. */
5933 fd = mch_fopen((char *)fname, "r");
5934 if (fd != NULL)
5935 {
5936 while (!vim_fgets(line, MAXWLEN * 2, fd))
5937 {
5938 fpos = fpos_next;
5939 fpos_next = ftell(fd);
5940 if (STRNCMP(word, line, len) == 0
5941 && (line[len] == '/' || line[len] < ' '))
5942 {
5943 /* Found duplicate word. Remove it by writing a '#' at
5944 * the start of the line. Mixing reading and writing
5945 * doesn't work for all systems, close the file first. */
5946 fclose(fd);
5947 fd = mch_fopen((char *)fname, "r+");
5948 if (fd == NULL)
5949 break;
5950 if (fseek(fd, fpos, SEEK_SET) == 0)
5951 fputc('#', fd);
5952 fseek(fd, fpos_next, SEEK_SET);
5953 }
5954 }
5955 fclose(fd);
5956 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005957 }
5958
5959 fd = mch_fopen((char *)fname, "a");
5960 if (fd == NULL && new_spf)
5961 {
5962 /* We just initialized the 'spellfile' option and can't open the file.
5963 * We may need to create the "spell" directory first. We already
5964 * checked the runtime directory is writable in init_spellfile(). */
5965 STRCPY(NameBuff, fname);
5966 *gettail_sep(NameBuff) = NUL;
5967 if (mch_stat((char *)NameBuff, &st) < 0)
5968 {
5969 /* The directory doesn't exist. Try creating it and opening the
5970 * file again. */
5971 vim_mkdir(NameBuff, 0755);
5972 fd = mch_fopen((char *)fname, "a");
5973 }
5974 }
5975
5976 if (fd == NULL)
5977 EMSG2(_(e_notopen), fname);
5978 else
5979 {
5980 if (bad)
5981 fprintf(fd, "%.*s/!\n", len, word);
5982 else
5983 fprintf(fd, "%.*s\n", len, word);
5984 fclose(fd);
5985
5986 /* Update the .add.spl file. */
5987 mkspell(1, &fname, FALSE, TRUE, TRUE);
5988
5989 /* If the .add file is edited somewhere, reload it. */
5990 if (buf != NULL)
5991 buf_reload(buf);
5992
5993 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005994 }
5995}
5996
5997/*
5998 * Initialize 'spellfile' for the current buffer.
5999 */
6000 static void
6001init_spellfile()
6002{
6003 char_u buf[MAXPATHL];
6004 int l;
6005 slang_T *sl;
6006 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006007 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006008
6009 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
6010 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006011 /* Find the end of the language name. Exclude the region. */
6012 for (lend = curbuf->b_p_spl; *lend != NUL
6013 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6014 ;
6015
6016 /* Loop over all entries in 'runtimepath'. Use the first one where we
6017 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006018 rtp = p_rtp;
6019 while (*rtp != NUL)
6020 {
6021 /* Copy the path from 'runtimepath' to buf[]. */
6022 copy_option_part(&rtp, buf, MAXPATHL, ",");
6023 if (filewritable(buf) == 2)
6024 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00006025 /* Use the first language name from 'spelllang' and the
6026 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006027 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
6028 l = STRLEN(buf);
6029 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00006030 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006031 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00006032 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
6033 ? (char_u *)"ascii" : spell_enc());
6034 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6035 break;
6036 }
6037 }
6038 }
6039}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006040
Bram Moolenaar51485f02005-06-04 21:55:20 +00006041
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006042/*
6043 * Init the chartab used for spelling for ASCII.
6044 * EBCDIC is not supported!
6045 */
6046 static void
6047clear_spell_chartab(sp)
6048 spelltab_T *sp;
6049{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006050 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006051
6052 /* Init everything to FALSE. */
6053 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
6054 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
6055 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006057 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006058 sp->st_upper[i] = i;
6059 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006060
6061 /* We include digits. A word shouldn't start with a digit, but handling
6062 * that is done separately. */
6063 for (i = '0'; i <= '9'; ++i)
6064 sp->st_isw[i] = TRUE;
6065 for (i = 'A'; i <= 'Z'; ++i)
6066 {
6067 sp->st_isw[i] = TRUE;
6068 sp->st_isu[i] = TRUE;
6069 sp->st_fold[i] = i + 0x20;
6070 }
6071 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006072 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006073 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006074 sp->st_upper[i] = i - 0x20;
6075 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006076}
6077
6078/*
6079 * Init the chartab used for spelling. Only depends on 'encoding'.
6080 * Called once while starting up and when 'encoding' changes.
6081 * The default is to use isalpha(), but the spell file should define the word
6082 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006083 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006084 */
6085 void
6086init_spell_chartab()
6087{
6088 int i;
6089
6090 did_set_spelltab = FALSE;
6091 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006092#ifdef FEAT_MBYTE
6093 if (enc_dbcs)
6094 {
6095 /* DBCS: assume double-wide characters are word characters. */
6096 for (i = 128; i <= 255; ++i)
6097 if (MB_BYTE2LEN(i) == 2)
6098 spelltab.st_isw[i] = TRUE;
6099 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006100 else if (enc_utf8)
6101 {
6102 for (i = 128; i < 256; ++i)
6103 {
6104 spelltab.st_isu[i] = utf_isupper(i);
6105 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
6106 spelltab.st_fold[i] = utf_fold(i);
6107 spelltab.st_upper[i] = utf_toupper(i);
6108 }
6109 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006110 else
6111#endif
6112 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006113 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006114 for (i = 128; i < 256; ++i)
6115 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006116 if (MB_ISUPPER(i))
6117 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006118 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006119 spelltab.st_isu[i] = TRUE;
6120 spelltab.st_fold[i] = MB_TOLOWER(i);
6121 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006122 else if (MB_ISLOWER(i))
6123 {
6124 spelltab.st_isw[i] = TRUE;
6125 spelltab.st_upper[i] = MB_TOUPPER(i);
6126 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006127 }
6128 }
6129}
6130
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006131static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
6132static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
6133
6134/*
6135 * Set the spell character tables from strings in the affix file.
6136 */
6137 static int
6138set_spell_chartab(fol, low, upp)
6139 char_u *fol;
6140 char_u *low;
6141 char_u *upp;
6142{
6143 /* We build the new tables here first, so that we can compare with the
6144 * previous one. */
6145 spelltab_T new_st;
6146 char_u *pf = fol, *pl = low, *pu = upp;
6147 int f, l, u;
6148
6149 clear_spell_chartab(&new_st);
6150
6151 while (*pf != NUL)
6152 {
6153 if (*pl == NUL || *pu == NUL)
6154 {
6155 EMSG(_(e_affform));
6156 return FAIL;
6157 }
6158#ifdef FEAT_MBYTE
6159 f = mb_ptr2char_adv(&pf);
6160 l = mb_ptr2char_adv(&pl);
6161 u = mb_ptr2char_adv(&pu);
6162#else
6163 f = *pf++;
6164 l = *pl++;
6165 u = *pu++;
6166#endif
6167 /* Every character that appears is a word character. */
6168 if (f < 256)
6169 new_st.st_isw[f] = TRUE;
6170 if (l < 256)
6171 new_st.st_isw[l] = TRUE;
6172 if (u < 256)
6173 new_st.st_isw[u] = TRUE;
6174
6175 /* if "LOW" and "FOL" are not the same the "LOW" char needs
6176 * case-folding */
6177 if (l < 256 && l != f)
6178 {
6179 if (f >= 256)
6180 {
6181 EMSG(_(e_affrange));
6182 return FAIL;
6183 }
6184 new_st.st_fold[l] = f;
6185 }
6186
6187 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006188 * case-folding, it's upper case and the "UPP" is the upper case of
6189 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006190 if (u < 256 && u != f)
6191 {
6192 if (f >= 256)
6193 {
6194 EMSG(_(e_affrange));
6195 return FAIL;
6196 }
6197 new_st.st_fold[u] = f;
6198 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006199 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006200 }
6201 }
6202
6203 if (*pl != NUL || *pu != NUL)
6204 {
6205 EMSG(_(e_affform));
6206 return FAIL;
6207 }
6208
6209 return set_spell_finish(&new_st);
6210}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006211
6212/*
6213 * Set the spell character tables from strings in the .spl file.
6214 */
6215 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006216set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006217 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006218 int cnt; /* length of "flags" */
6219 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006220{
6221 /* We build the new tables here first, so that we can compare with the
6222 * previous one. */
6223 spelltab_T new_st;
6224 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006225 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006226 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006227
6228 clear_spell_chartab(&new_st);
6229
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006230 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006231 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006232 if (i < cnt)
6233 {
6234 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6235 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6236 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006237
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006238 if (*p != NUL)
6239 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006240#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006241 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006242#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006243 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006244#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006245 new_st.st_fold[i + 128] = c;
6246 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6247 new_st.st_upper[c] = i + 128;
6248 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006249 }
6250
6251 return set_spell_finish(&new_st);
6252}
6253
6254 static int
6255set_spell_finish(new_st)
6256 spelltab_T *new_st;
6257{
6258 int i;
6259
6260 if (did_set_spelltab)
6261 {
6262 /* check that it's the same table */
6263 for (i = 0; i < 256; ++i)
6264 {
6265 if (spelltab.st_isw[i] != new_st->st_isw[i]
6266 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006267 || spelltab.st_fold[i] != new_st->st_fold[i]
6268 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006269 {
6270 EMSG(_("E763: Word characters differ between spell files"));
6271 return FAIL;
6272 }
6273 }
6274 }
6275 else
6276 {
6277 /* copy the new spelltab into the one being used */
6278 spelltab = *new_st;
6279 did_set_spelltab = TRUE;
6280 }
6281
6282 return OK;
6283}
6284
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006285/*
Bram Moolenaarea408852005-06-25 22:49:46 +00006286 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006287 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00006288 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006289 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00006290 */
6291 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006292spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00006293 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006294 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00006295{
Bram Moolenaarea408852005-06-25 22:49:46 +00006296#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006297 char_u *s;
6298 int l;
6299 int c;
6300
6301 if (has_mbyte)
6302 {
6303 l = MB_BYTE2LEN(*p);
6304 s = p;
6305 if (l == 1)
6306 {
6307 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006308 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006309 {
6310 s = p + 1; /* skip a mid-word character */
6311 l = MB_BYTE2LEN(*s);
6312 }
6313 }
6314 else
6315 {
6316 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006317 if (c < 256 ? buf->b_spell_ismw[c]
6318 : (buf->b_spell_ismw_mb != NULL
6319 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006320 {
6321 s = p + l;
6322 l = MB_BYTE2LEN(*s);
6323 }
6324 }
6325
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006326 c = mb_ptr2char(s);
6327 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006328 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006329 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006330 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006331#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006332
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006333 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
6334}
6335
6336/*
6337 * Return TRUE if "p" points to a word character.
6338 * Unlike spell_iswordp() this doesn't check for "midword" characters.
6339 */
6340 static int
6341spell_iswordp_nmw(p)
6342 char_u *p;
6343{
6344#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006345 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006346
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006347 if (has_mbyte)
6348 {
6349 c = mb_ptr2char(p);
6350 if (c > 255)
6351 return mb_get_class(p) >= 2;
6352 return spelltab.st_isw[c];
6353 }
6354#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006355 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006356}
6357
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006358#ifdef FEAT_MBYTE
6359/*
6360 * Return TRUE if "p" points to a word character.
6361 * Wide version of spell_iswordp().
6362 */
6363 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006364spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006365 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006366 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006367{
6368 int *s;
6369
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006370 if (*p < 256 ? buf->b_spell_ismw[*p]
6371 : (buf->b_spell_ismw_mb != NULL
6372 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006373 s = p + 1;
6374 else
6375 s = p;
6376
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006377 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006378 {
6379 if (enc_utf8)
6380 return utf_class(*s) >= 2;
6381 if (enc_dbcs)
6382 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6383 return 0;
6384 }
6385 return spelltab.st_isw[*s];
6386}
6387#endif
6388
Bram Moolenaarea408852005-06-25 22:49:46 +00006389/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006390 * Write the table with prefix conditions to the .spl file.
6391 */
6392 static void
6393write_spell_prefcond(fd, gap)
6394 FILE *fd;
6395 garray_T *gap;
6396{
6397 int i;
6398 char_u *p;
6399 int len;
6400
6401 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6402
6403 for (i = 0; i < gap->ga_len; ++i)
6404 {
6405 /* <prefcond> : <condlen> <condstr> */
6406 p = ((char_u **)gap->ga_data)[i];
6407 if (p == NULL)
6408 fputc(0, fd);
6409 else
6410 {
6411 len = STRLEN(p);
6412 fputc(len, fd);
6413 fwrite(p, (size_t)len, (size_t)1, fd);
6414 }
6415 }
6416}
6417
6418/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006419 * Write the current tables into the .spl file.
6420 * This makes sure the same characters are recognized as word characters when
6421 * generating an when using a spell file.
6422 */
6423 static void
6424write_spell_chartab(fd)
6425 FILE *fd;
6426{
6427 char_u charbuf[256 * 4];
6428 int len = 0;
6429 int flags;
6430 int i;
6431
6432 fputc(128, fd); /* <charflagslen> */
6433 for (i = 128; i < 256; ++i)
6434 {
6435 flags = 0;
6436 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006437 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006438 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006439 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006440 fputc(flags, fd); /* <charflags> */
6441
Bram Moolenaarb765d632005-06-07 21:00:02 +00006442#ifdef FEAT_MBYTE
6443 if (has_mbyte)
6444 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6445 else
6446#endif
6447 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006448 }
6449
6450 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6451 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6452}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006453
6454/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006455 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6456 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006457 * When using a multi-byte 'encoding' the length may change!
6458 * Returns FAIL when something wrong.
6459 */
6460 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006461spell_casefold(str, len, buf, buflen)
6462 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006463 int len;
6464 char_u *buf;
6465 int buflen;
6466{
6467 int i;
6468
6469 if (len >= buflen)
6470 {
6471 buf[0] = NUL;
6472 return FAIL; /* result will not fit */
6473 }
6474
6475#ifdef FEAT_MBYTE
6476 if (has_mbyte)
6477 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006478 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006479 char_u *p;
6480 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006481
6482 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006483 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006484 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006485 if (outi + MB_MAXBYTES > buflen)
6486 {
6487 buf[outi] = NUL;
6488 return FAIL;
6489 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006490 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006491 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006492 }
6493 buf[outi] = NUL;
6494 }
6495 else
6496#endif
6497 {
6498 /* Be quick for non-multibyte encodings. */
6499 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006500 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006501 buf[i] = NUL;
6502 }
6503
6504 return OK;
6505}
6506
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006507#define SPS_BEST 1
6508#define SPS_FAST 2
6509#define SPS_DOUBLE 4
6510
6511static int sps_flags = SPS_BEST;
6512
6513/*
6514 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6515 * Sets "sps_flags".
6516 */
6517 int
6518spell_check_sps()
6519{
6520 char_u *p;
6521 char_u buf[MAXPATHL];
6522 int f;
6523
6524 sps_flags = 0;
6525
6526 for (p = p_sps; *p != NUL; )
6527 {
6528 copy_option_part(&p, buf, MAXPATHL, ",");
6529
6530 f = 0;
6531 if (STRCMP(buf, "best") == 0)
6532 f = SPS_BEST;
6533 else if (STRCMP(buf, "fast") == 0)
6534 f = SPS_FAST;
6535 else if (STRCMP(buf, "double") == 0)
6536 f = SPS_DOUBLE;
6537 else if (STRNCMP(buf, "expr:", 5) != 0
6538 && STRNCMP(buf, "file:", 5) != 0)
6539 f = -1;
6540
6541 if (f == -1 || (sps_flags != 0 && f != 0))
6542 {
6543 sps_flags = SPS_BEST;
6544 return FAIL;
6545 }
6546 if (f != 0)
6547 sps_flags = f;
6548 }
6549
6550 if (sps_flags == 0)
6551 sps_flags = SPS_BEST;
6552
6553 return OK;
6554}
6555
6556/* Remember what "z?" replaced. */
6557static char_u *repl_from = NULL;
6558static char_u *repl_to = NULL;
6559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006560/*
6561 * "z?": Find badly spelled word under or after the cursor.
6562 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006563 */
6564 void
6565spell_suggest()
6566{
6567 char_u *line;
6568 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006569 char_u wcopy[MAXWLEN + 2];
6570 char_u *p;
6571 int i;
6572 int c;
6573 suginfo_T sug;
6574 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006575 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006576 int need_cap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006577
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006578 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006579 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6580 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006581 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006582 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6583 return;
6584
6585 /* No bad word or it starts after the cursor: use the word under the
6586 * cursor. */
6587 curwin->w_cursor = prev_cursor;
6588 line = ml_get_curline();
6589 p = line + curwin->w_cursor.col;
6590 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006591 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006592 mb_ptr_back(line, p);
6593 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006594 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006595 mb_ptr_adv(p);
6596
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006597 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006598 {
6599 beep_flush();
6600 return;
6601 }
6602 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006603 }
6604
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006605 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006606
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006607 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006608 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006609
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006610 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006611
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006612 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006613 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6614 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006615
6616 if (sug.su_ga.ga_len == 0)
6617 MSG(_("Sorry, no suggestions"));
6618 else
6619 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006620 vim_free(repl_from);
6621 repl_from = NULL;
6622 vim_free(repl_to);
6623 repl_to = NULL;
6624
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006625#ifdef FEAT_RIGHTLEFT
6626 /* When 'rightleft' is set the list is drawn right-left. */
6627 cmdmsg_rl = curwin->w_p_rl;
6628 if (cmdmsg_rl)
6629 msg_col = Columns - 1;
6630#endif
6631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006632 /* List the suggestions. */
6633 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006634 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006635 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6636 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006637#ifdef FEAT_RIGHTLEFT
6638 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
6639 {
6640 /* And now the rabbit from the high hat: Avoid showing the
6641 * untranslated message rightleft. */
6642 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
6643 sug.su_badlen, sug.su_badptr);
6644 }
6645#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006646 msg_puts(IObuff);
6647 msg_clr_eos();
6648 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006650 msg_scroll = TRUE;
6651 for (i = 0; i < sug.su_ga.ga_len; ++i)
6652 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006653 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006654
6655 /* The suggested word may replace only part of the bad word, add
6656 * the not replaced part. */
6657 STRCPY(wcopy, stp->st_word);
6658 if (sug.su_badlen > stp->st_orglen)
6659 vim_strncpy(wcopy + STRLEN(wcopy),
6660 sug.su_badptr + stp->st_orglen,
6661 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006662 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
6663#ifdef FEAT_RIGHTLEFT
6664 if (cmdmsg_rl)
6665 rl_mirror(IObuff);
6666#endif
6667 msg_puts(IObuff);
6668
6669 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006670 msg_puts(IObuff);
6671
6672 /* The word may replace more than "su_badlen". */
6673 if (sug.su_badlen < stp->st_orglen)
6674 {
6675 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6676 stp->st_orglen, sug.su_badptr);
6677 msg_puts(IObuff);
6678 }
6679
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006680 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006681 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006682 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006683 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006684 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006685 stp->st_salscore ? "s " : "",
6686 stp->st_score, stp->st_altscore);
6687 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006688 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00006689 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006690#ifdef FEAT_RIGHTLEFT
6691 if (cmdmsg_rl)
6692 /* Mirror the numbers, but keep the leading space. */
6693 rl_mirror(IObuff + 1);
6694#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00006695 msg_advance(30);
6696 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006698 msg_putchar('\n');
6699 }
6700
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006701#ifdef FEAT_RIGHTLEFT
6702 cmdmsg_rl = FALSE;
6703 msg_col = 0;
6704#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006706 i = prompt_for_number(&mouse_used);
6707 if (mouse_used)
6708 i -= lines_left;
6709
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006710 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006711 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006712 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006713 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006714 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6715 repl_to = vim_strsave(stp->st_word);
6716
6717 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6719 if (p != NULL)
6720 {
6721 c = sug.su_badptr - line;
6722 mch_memmove(p, line, c);
6723 STRCPY(p + c, stp->st_word);
6724 STRCAT(p, sug.su_badptr + stp->st_orglen);
6725 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6726 curwin->w_cursor.col = c;
6727 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006728
6729 /* For redo we use a change-word command. */
6730 ResetRedobuff();
6731 AppendToRedobuff((char_u *)"ciw");
6732 AppendToRedobuff(stp->st_word);
6733 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006734 }
6735 }
6736 else
6737 curwin->w_cursor = prev_cursor;
6738 }
6739
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006740 spell_find_cleanup(&sug);
6741}
6742
6743/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006744 * Check if the word at line "lnum" column "col" is required to start with a
6745 * capital. This uses 'spellcapcheck' of the current buffer.
6746 */
6747 static int
6748check_need_cap(lnum, col)
6749 linenr_T lnum;
6750 colnr_T col;
6751{
6752 int need_cap = FALSE;
6753 char_u *line;
6754 char_u *line_copy = NULL;
6755 char_u *p;
6756 colnr_T endcol;
6757 regmatch_T regmatch;
6758
6759 if (curbuf->b_cap_prog == NULL)
6760 return FALSE;
6761
6762 line = ml_get_curline();
6763 endcol = 0;
6764 if ((int)(skipwhite(line) - line) >= (int)col)
6765 {
6766 /* At start of line, check if previous line is empty or sentence
6767 * ends there. */
6768 if (lnum == 1)
6769 need_cap = TRUE;
6770 else
6771 {
6772 line = ml_get(lnum - 1);
6773 if (*skipwhite(line) == NUL)
6774 need_cap = TRUE;
6775 else
6776 {
6777 /* Append a space in place of the line break. */
6778 line_copy = concat_str(line, (char_u *)" ");
6779 line = line_copy;
6780 endcol = STRLEN(line);
6781 }
6782 }
6783 }
6784 else
6785 endcol = col;
6786
6787 if (endcol > 0)
6788 {
6789 /* Check if sentence ends before the bad word. */
6790 regmatch.regprog = curbuf->b_cap_prog;
6791 regmatch.rm_ic = FALSE;
6792 p = line + endcol;
6793 for (;;)
6794 {
6795 mb_ptr_back(line, p);
6796 if (p == line || spell_iswordp_nmw(p))
6797 break;
6798 if (vim_regexec(&regmatch, p, 0)
6799 && regmatch.endp[0] == line + endcol)
6800 {
6801 need_cap = TRUE;
6802 break;
6803 }
6804 }
6805 }
6806
6807 vim_free(line_copy);
6808
6809 return need_cap;
6810}
6811
6812
6813/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006814 * ":spellrepall"
6815 */
6816/*ARGSUSED*/
6817 void
6818ex_spellrepall(eap)
6819 exarg_T *eap;
6820{
6821 pos_T pos = curwin->w_cursor;
6822 char_u *frompat;
6823 int addlen;
6824 char_u *line;
6825 char_u *p;
6826 int didone = FALSE;
6827 int save_ws = p_ws;
6828
6829 if (repl_from == NULL || repl_to == NULL)
6830 {
6831 EMSG(_("E752: No previous spell replacement"));
6832 return;
6833 }
6834 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6835
6836 frompat = alloc(STRLEN(repl_from) + 7);
6837 if (frompat == NULL)
6838 return;
6839 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6840 p_ws = FALSE;
6841
6842 curwin->w_cursor.lnum = 0;
6843 while (!got_int)
6844 {
6845 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6846 || u_save_cursor() == FAIL)
6847 break;
6848
6849 /* Only replace when the right word isn't there yet. This happens
6850 * when changing "etc" to "etc.". */
6851 line = ml_get_curline();
6852 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6853 repl_to, STRLEN(repl_to)) != 0)
6854 {
6855 p = alloc(STRLEN(line) + addlen + 1);
6856 if (p == NULL)
6857 break;
6858 mch_memmove(p, line, curwin->w_cursor.col);
6859 STRCPY(p + curwin->w_cursor.col, repl_to);
6860 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6861 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6862 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6863 didone = TRUE;
6864 }
6865 curwin->w_cursor.col += STRLEN(repl_to);
6866 }
6867
6868 p_ws = save_ws;
6869 curwin->w_cursor = pos;
6870 vim_free(frompat);
6871
6872 if (!didone)
6873 EMSG2(_("E753: Not found: %s"), repl_from);
6874}
6875
6876/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006877 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6878 * a list of allocated strings.
6879 */
6880 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006881spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006882 garray_T *gap;
6883 char_u *word;
6884 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006885 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006886{
6887 suginfo_T sug;
6888 int i;
6889 suggest_T *stp;
6890 char_u *wcopy;
6891
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006892 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006893
6894 /* Make room in "gap". */
6895 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6896 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6897 return;
6898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006899 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006900 {
6901 stp = &SUG(sug.su_ga, i);
6902
6903 /* The suggested word may replace only part of "word", add the not
6904 * replaced part. */
6905 wcopy = alloc(STRLEN(stp->st_word)
6906 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6907 if (wcopy == NULL)
6908 break;
6909 STRCPY(wcopy, stp->st_word);
6910 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6911 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6912 }
6913
6914 spell_find_cleanup(&sug);
6915}
6916
6917/*
6918 * Find spell suggestions for the word at the start of "badptr".
6919 * Return the suggestions in "su->su_ga".
6920 * The maximum number of suggestions is "maxcount".
6921 * Note: does use info for the current window.
6922 * This is based on the mechanisms of Aspell, but completely reimplemented.
6923 */
6924 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006925spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006926 char_u *badptr;
6927 suginfo_T *su;
6928 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006929 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006930 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006931{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006932 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006933 char_u buf[MAXPATHL];
6934 char_u *p;
6935 int do_combine = FALSE;
6936 char_u *sps_copy;
6937#ifdef FEAT_EVAL
6938 static int expr_busy = FALSE;
6939#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006940 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006941
6942 /*
6943 * Set the info in "*su".
6944 */
6945 vim_memset(su, 0, sizeof(suginfo_T));
6946 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6947 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006948 if (*badptr == NUL)
6949 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006950 hash_init(&su->su_banned);
6951
6952 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006953 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006954 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006955 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006956
6957 if (su->su_badlen >= MAXWLEN)
6958 su->su_badlen = MAXWLEN - 1; /* just in case */
6959 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6960 (void)spell_casefold(su->su_badptr, su->su_badlen,
6961 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006962 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006963 su->su_badflags = badword_captype(su->su_badptr,
6964 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006965 if (need_cap)
6966 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006967
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006968 /* If the word is not capitalised and spell_check() doesn't consider the
6969 * word to be bad then it might need to be capitalised. Add a suggestion
6970 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006971 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006972 if (!SPELL_ISUPPER(c) && attr == 0)
6973 {
6974 make_case_word(su->su_badword, buf, WF_ONECAP);
6975 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6976 0, TRUE);
6977 }
6978
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006979 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006980 if (banbadword)
6981 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006982
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006983 /* Make a copy of 'spellsuggest', because the expression may change it. */
6984 sps_copy = vim_strsave(p_sps);
6985 if (sps_copy == NULL)
6986 return;
6987
6988 /* Loop over the items in 'spellsuggest'. */
6989 for (p = sps_copy; *p != NUL; )
6990 {
6991 copy_option_part(&p, buf, MAXPATHL, ",");
6992
6993 if (STRNCMP(buf, "expr:", 5) == 0)
6994 {
6995#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006996 /* Evaluate an expression. Skip this when called recursively,
6997 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006998 if (!expr_busy)
6999 {
7000 expr_busy = TRUE;
7001 spell_suggest_expr(su, buf + 5);
7002 expr_busy = FALSE;
7003 }
7004#endif
7005 }
7006 else if (STRNCMP(buf, "file:", 5) == 0)
7007 /* Use list of suggestions in a file. */
7008 spell_suggest_file(su, buf + 5);
7009 else
7010 {
7011 /* Use internal method. */
7012 spell_suggest_intern(su);
7013 if (sps_flags & SPS_DOUBLE)
7014 do_combine = TRUE;
7015 }
7016 }
7017
7018 vim_free(sps_copy);
7019
7020 if (do_combine)
7021 /* Combine the two list of suggestions. This must be done last,
7022 * because sorting changes the order again. */
7023 score_combine(su);
7024}
7025
7026#ifdef FEAT_EVAL
7027/*
7028 * Find suggestions by evaluating expression "expr".
7029 */
7030 static void
7031spell_suggest_expr(su, expr)
7032 suginfo_T *su;
7033 char_u *expr;
7034{
7035 list_T *list;
7036 listitem_T *li;
7037 int score;
7038 char_u *p;
7039
7040 /* The work is split up in a few parts to avoid having to export
7041 * suginfo_T.
7042 * First evaluate the expression and get the resulting list. */
7043 list = eval_spell_expr(su->su_badword, expr);
7044 if (list != NULL)
7045 {
7046 /* Loop over the items in the list. */
7047 for (li = list->lv_first; li != NULL; li = li->li_next)
7048 if (li->li_tv.v_type == VAR_LIST)
7049 {
7050 /* Get the word and the score from the items. */
7051 score = get_spellword(li->li_tv.vval.v_list, &p);
7052 if (score >= 0)
7053 add_suggestion(su, &su->su_ga, p,
7054 su->su_badlen, score, 0, TRUE);
7055 }
7056 list_unref(list);
7057 }
7058
7059 /* Sort the suggestions and truncate at "maxcount". */
7060 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7061}
7062#endif
7063
7064/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007065 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007066 */
7067 static void
7068spell_suggest_file(su, fname)
7069 suginfo_T *su;
7070 char_u *fname;
7071{
7072 FILE *fd;
7073 char_u line[MAXWLEN * 2];
7074 char_u *p;
7075 int len;
7076 char_u cword[MAXWLEN];
7077
7078 /* Open the file. */
7079 fd = mch_fopen((char *)fname, "r");
7080 if (fd == NULL)
7081 {
7082 EMSG2(_(e_notopen), fname);
7083 return;
7084 }
7085
7086 /* Read it line by line. */
7087 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
7088 {
7089 line_breakcheck();
7090
7091 p = vim_strchr(line, '/');
7092 if (p == NULL)
7093 continue; /* No Tab found, just skip the line. */
7094 *p++ = NUL;
7095 if (STRICMP(su->su_badword, line) == 0)
7096 {
7097 /* Match! Isolate the good word, until CR or NL. */
7098 for (len = 0; p[len] >= ' '; ++len)
7099 ;
7100 p[len] = NUL;
7101
7102 /* If the suggestion doesn't have specific case duplicate the case
7103 * of the bad word. */
7104 if (captype(p, NULL) == 0)
7105 {
7106 make_case_word(p, cword, su->su_badflags);
7107 p = cword;
7108 }
7109
7110 add_suggestion(su, &su->su_ga, p, su->su_badlen,
7111 SCORE_FILE, 0, TRUE);
7112 }
7113 }
7114
7115 fclose(fd);
7116
7117 /* Sort the suggestions and truncate at "maxcount". */
7118 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7119}
7120
7121/*
7122 * Find suggestions for the internal method indicated by "sps_flags".
7123 */
7124 static void
7125spell_suggest_intern(su)
7126 suginfo_T *su;
7127{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007128 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007129 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007130 *
7131 * Set a maximum score to limit the combination of operations that is
7132 * tried.
7133 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007134 suggest_try_special(su);
7135
7136 /*
7137 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
7138 * from the .aff file and inserting a space (split the word).
7139 */
7140 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007141
7142 /* For the resulting top-scorers compute the sound-a-like score. */
7143 if (sps_flags & SPS_DOUBLE)
7144 score_comp_sal(su);
7145
7146 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007147 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007148 *
7149 * Only do this when we don't have a lot of suggestions yet, because it's
7150 * very slow and often doesn't find new suggestions.
7151 */
7152 if ((sps_flags & SPS_DOUBLE)
7153 || (!(sps_flags & SPS_FAST)
7154 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
7155 {
7156 /* Allow a higher score now. */
7157 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007158 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007159 }
7160
7161 /* When CTRL-C was hit while searching do show the results. */
7162 ui_breakcheck();
7163 if (got_int)
7164 {
7165 (void)vgetc();
7166 got_int = FALSE;
7167 }
7168
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007169 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007170 {
7171 if (sps_flags & SPS_BEST)
7172 /* Adjust the word score for how it sounds like. */
7173 rescore_suggestions(su);
7174
7175 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007176 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007177 }
7178}
7179
7180/*
7181 * Free the info put in "*su" by spell_find_suggest().
7182 */
7183 static void
7184spell_find_cleanup(su)
7185 suginfo_T *su;
7186{
7187 int i;
7188
7189 /* Free the suggestions. */
7190 for (i = 0; i < su->su_ga.ga_len; ++i)
7191 vim_free(SUG(su->su_ga, i).st_word);
7192 ga_clear(&su->su_ga);
7193 for (i = 0; i < su->su_sga.ga_len; ++i)
7194 vim_free(SUG(su->su_sga, i).st_word);
7195 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007196
7197 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007198 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007199}
7200
7201/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007202 * Make a copy of "word", with the first letter upper or lower cased, to
7203 * "wcopy[MAXWLEN]". "word" must not be empty.
7204 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 */
7206 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007207onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007208 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007209 char_u *wcopy;
7210 int upper; /* TRUE: first letter made upper case */
7211{
7212 char_u *p;
7213 int c;
7214 int l;
7215
7216 p = word;
7217#ifdef FEAT_MBYTE
7218 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007219 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007220 else
7221#endif
7222 c = *p++;
7223 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007224 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007226 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227#ifdef FEAT_MBYTE
7228 if (has_mbyte)
7229 l = mb_char2bytes(c, wcopy);
7230 else
7231#endif
7232 {
7233 l = 1;
7234 wcopy[0] = c;
7235 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007236 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237}
7238
7239/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007240 * Make a copy of "word" with all the letters upper cased into
7241 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007242 */
7243 static void
7244allcap_copy(word, wcopy)
7245 char_u *word;
7246 char_u *wcopy;
7247{
7248 char_u *s;
7249 char_u *d;
7250 int c;
7251
7252 d = wcopy;
7253 for (s = word; *s != NUL; )
7254 {
7255#ifdef FEAT_MBYTE
7256 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007257 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 else
7259#endif
7260 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007261 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262
7263#ifdef FEAT_MBYTE
7264 if (has_mbyte)
7265 {
7266 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
7267 break;
7268 d += mb_char2bytes(c, d);
7269 }
7270 else
7271#endif
7272 {
7273 if (d - wcopy >= MAXWLEN - 1)
7274 break;
7275 *d++ = c;
7276 }
7277 }
7278 *d = NUL;
7279}
7280
7281/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007282 * Try finding suggestions by recognizing specific situations.
7283 */
7284 static void
7285suggest_try_special(su)
7286 suginfo_T *su;
7287{
7288 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007289 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007290 int c;
7291 char_u word[MAXWLEN];
7292
7293 /*
7294 * Recognize a word that is repeated: "the the".
7295 */
7296 p = skiptowhite(su->su_fbadword);
7297 len = p - su->su_fbadword;
7298 p = skipwhite(p);
7299 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
7300 {
7301 /* Include badflags: if the badword is onecap or allcap
7302 * use that for the goodword too: "The the" -> "The". */
7303 c = su->su_fbadword[len];
7304 su->su_fbadword[len] = NUL;
7305 make_case_word(su->su_fbadword, word, su->su_badflags);
7306 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007307 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007308 }
7309}
7310
7311/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00007313 *
7314 * This uses a state machine. At each node in the tree we try various
7315 * operations. When trying if an operation work "depth" is increased and the
7316 * stack[] is used to store info. This allows combinations, thus insert one
7317 * character, replace one and delete another. The number of changes is
7318 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 */
7320 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007321suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007322 suginfo_T *su;
7323{
7324 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
7325 char_u tword[MAXWLEN]; /* good word collected so far */
7326 trystate_T stack[MAXWLEN];
7327 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
7328 * to for word split) */
7329 char_u prewordlen = 0; /* length of word in "preword" */
7330 int splitoff = 0; /* index in tword after last split */
7331 trystate_T *sp;
7332 int newscore;
7333 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007334 char_u *byts, *fbyts, *pbyts;
7335 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007336 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007337 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007338 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007339 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007341 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007342 int len;
7343 char_u *p;
7344 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00007345 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007346 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007347
7348 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00007349 * to find matches (esp. REP items). Append some more text, changing
7350 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007352 n = STRLEN(fword);
7353 p = su->su_badptr + su->su_badlen;
7354 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007355
7356 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7357 lp->lp_slang != NULL; ++lp)
7358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007359 /*
7360 * Go through the whole case-fold tree, try changes at each node.
7361 * "tword[]" contains the word collected from nodes in the tree.
7362 * "fword[]" the word we are trying to match with (initially the bad
7363 * word).
7364 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007365 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007366 sp = &stack[0];
7367 sp->ts_state = STATE_START;
7368 sp->ts_score = 0;
7369 sp->ts_curi = 1;
7370 sp->ts_fidx = 0;
7371 sp->ts_fidxtry = 0;
7372 sp->ts_twordlen = 0;
7373 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007374#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007375 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007376#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007377
Bram Moolenaarea424162005-06-16 21:51:00 +00007378 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007379 * When there are postponed prefixes we need to use these first. At
7380 * the end of the prefix we continue in the case-fold tree.
7381 */
7382 fbyts = lp->lp_slang->sl_fbyts;
7383 fidxs = lp->lp_slang->sl_fidxs;
7384 pbyts = lp->lp_slang->sl_pbyts;
7385 pidxs = lp->lp_slang->sl_pidxs;
7386 if (pbyts != NULL)
7387 {
7388 byts = pbyts;
7389 idxs = pidxs;
7390 sp->ts_prefixdepth = PREFIXTREE;
7391 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
7392 }
7393 else
7394 {
7395 byts = fbyts;
7396 idxs = fidxs;
7397 sp->ts_prefixdepth = NOPREFIX;
7398 }
7399
7400 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007401 * Loop to find all suggestions. At each round we either:
7402 * - For the current state try one operation, advance "ts_curi",
7403 * increase "depth".
7404 * - When a state is done go to the next, set "ts_state".
7405 * - When all states are tried decrease "depth".
7406 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407 while (depth >= 0 && !got_int)
7408 {
7409 sp = &stack[depth];
7410 switch (sp->ts_state)
7411 {
7412 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007413 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007414 /*
7415 * Start of node: Deal with NUL bytes, which means
7416 * tword[] may end here.
7417 */
7418 arridx = sp->ts_arridx; /* current node in the tree */
7419 len = byts[arridx]; /* bytes in this node */
7420 arridx += sp->ts_curi; /* index of current byte */
7421
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007422 if (sp->ts_prefixdepth == PREFIXTREE)
7423 {
7424 /* Skip over the NUL bytes, we use them later. */
7425 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7426 ;
7427 sp->ts_curi += n;
7428
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007429 /* Always past NUL bytes now. */
7430 n = (int)sp->ts_state;
7431 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007432 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007433
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007434 /* At end of a prefix or at start of prefixtree: check for
7435 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007436 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007437 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007438 /* Set su->su_badflags to the caps type at this
7439 * position. Use the caps type until here for the
7440 * prefix itself. */
7441#ifdef FEAT_MBYTE
7442 if (has_mbyte)
7443 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7444 else
7445#endif
7446 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007447 flags = badword_captype(su->su_badptr,
7448 su->su_badptr + n);
7449 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007450 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007451 ++depth;
7452 stack[depth] = stack[depth - 1];
7453 sp = &stack[depth];
7454 sp->ts_prefixdepth = depth - 1;
7455 byts = fbyts;
7456 idxs = fidxs;
7457 sp->ts_state = STATE_START;
7458 sp->ts_curi = 1; /* start just after length byte */
7459 sp->ts_arridx = 0;
7460
Bram Moolenaar53805d12005-08-01 07:08:33 +00007461 /* Move the prefix to preword[] with the right case
7462 * and make find_keepcap_word() works. */
7463 splitoff = sp->ts_twordlen;
7464 tword[splitoff] = NUL;
7465 make_case_word(tword, preword, flags);
7466 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007467 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007468 break;
7469 }
7470
Bram Moolenaar0c405862005-06-22 22:26:26 +00007471 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007472 {
7473 /* Past bytes in node and/or past NUL bytes. */
7474 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007475 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007476 break;
7477 }
7478
7479 /*
7480 * End of word in tree.
7481 */
7482 ++sp->ts_curi; /* eat one NUL byte */
7483
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007484 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007485
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007486 if (sp->ts_prefixdepth < MAXWLEN)
7487 {
7488 /* There was a prefix before the word. Check that the
7489 * prefix can be used with this word. */
7490 /* Count the length of the NULs in the prefix. If there
7491 * are none this must be the first try without a prefix.
7492 */
7493 n = stack[sp->ts_prefixdepth].ts_arridx;
7494 len = pbyts[n++];
7495 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7496 ;
7497 if (c > 0)
7498 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007499 /* The prefix ID is stored three bytes above the
7500 * flags. */
7501 c = valid_word_prefix(c, n, flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007502 tword + splitoff, lp->lp_slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007503 if (c == 0)
7504 break;
7505
7506 /* Use the WF_RARE flag for a rare prefix. */
7507 if (c & WF_RAREPFX)
7508 flags |= WF_RARE;
7509 }
7510 }
7511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007512 /*
7513 * Form the word with proper case in preword.
7514 * If there is a word from a previous split, append.
7515 */
7516 tword[sp->ts_twordlen] = NUL;
7517 if (flags & WF_KEEPCAP)
7518 /* Must find the word in the keep-case tree. */
7519 find_keepcap_word(lp->lp_slang, tword + splitoff,
7520 preword + prewordlen);
7521 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007523 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007524 * use that for the goodword too. But if the badword is
7525 * allcap and it's only one char long use onecap. */
7526 c = su->su_badflags;
7527 if ((c & WF_ALLCAP)
7528#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007529 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007530#else
7531 && su->su_badlen == 1
7532#endif
7533 )
7534 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007535 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007536 preword + prewordlen, flags | c);
7537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007538
7539 /* Don't use a banned word. It may appear again as a good
7540 * word, thus remember it. */
7541 if (flags & WF_BANNED)
7542 {
7543 add_banned(su, preword + prewordlen);
7544 break;
7545 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007546 if (was_banned(su, preword + prewordlen)
7547 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007548 break;
7549
7550 newscore = 0;
7551 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007552 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007553 newscore += SCORE_REGION;
7554 if (flags & WF_RARE)
7555 newscore += SCORE_RARE;
7556
Bram Moolenaar0c405862005-06-22 22:26:26 +00007557 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007558 captype(preword + prewordlen, NULL)))
7559 newscore += SCORE_ICASE;
7560
Bram Moolenaar0c405862005-06-22 22:26:26 +00007561 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007562 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007563 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007564 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007565 /* The badword also ends: add suggestions. Give a penalty
7566 * when changing non-word char to word char, e.g., "thes,"
7567 * -> "these". */
7568 p = fword + sp->ts_fidx;
7569#ifdef FEAT_MBYTE
7570 if (has_mbyte)
7571 mb_ptr_back(fword, p);
7572 else
7573#endif
7574 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007575 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007576 {
7577 p = preword + STRLEN(preword);
7578#ifdef FEAT_MBYTE
7579 if (has_mbyte)
7580 mb_ptr_back(preword, p);
7581 else
7582#endif
7583 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007584 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007585 newscore += SCORE_NONWORD;
7586 }
7587
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007588 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007589 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007590 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007592 else if (sp->ts_fidx >= sp->ts_fidxtry
7593#ifdef FEAT_MBYTE
7594 /* Don't split halfway a character. */
7595 && (!has_mbyte || sp->ts_tcharlen == 0)
7596#endif
7597 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007598 {
7599 /* The word in the tree ends but the badword
7600 * continues: try inserting a space and check that a valid
7601 * words starts at fword[sp->ts_fidx]. */
7602 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7603 {
7604 /* Save things to be restored at STATE_SPLITUNDO. */
7605 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007606 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007607 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007608 sp->ts_state = STATE_SPLITUNDO;
7609
7610 ++depth;
7611 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612
7613 /* Append a space to preword. */
7614 STRCAT(preword, " ");
7615 prewordlen = STRLEN(preword);
7616 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007617
7618 /* If the badword has a non-word character at this
7619 * position skip it. That means replacing the
7620 * non-word character with a space. */
7621 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7622 {
7623 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7624#ifdef FEAT_MBYTE
7625 if (has_mbyte)
7626 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7627 else
7628#endif
7629 ++sp->ts_fidx;
7630 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00007631
7632 /* set su->su_badflags to the caps type at this
7633 * position */
7634
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007635#ifdef FEAT_MBYTE
7636 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00007637 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007638 else
7639#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00007640 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007641 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007642 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007644 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007645 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007646 }
7647 }
7648 break;
7649
7650 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007651 /* Undo the changes done for word split. */
7652 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007653 splitoff = sp->ts_save_splitoff;
7654 prewordlen = sp->ts_save_prewordlen;
7655
7656 /* Continue looking for NUL bytes. */
7657 sp->ts_state = STATE_START;
7658 break;
7659
7660 case STATE_ENDNUL:
7661 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007662 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007663 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007664 {
7665 /* The badword ends, can't use the bytes in this node. */
7666 sp->ts_state = STATE_DEL;
7667 break;
7668 }
7669 sp->ts_state = STATE_PLAIN;
7670 /*FALLTHROUGH*/
7671
7672 case STATE_PLAIN:
7673 /*
7674 * Go over all possible bytes at this node, add each to
7675 * tword[] and use child node. "ts_curi" is the index.
7676 */
7677 arridx = sp->ts_arridx;
7678 if (sp->ts_curi > byts[arridx])
7679 {
7680 /* Done all bytes at this node, do next state. When still
7681 * at already changed bytes skip the other tricks. */
7682 if (sp->ts_fidx >= sp->ts_fidxtry)
7683 sp->ts_state = STATE_DEL;
7684 else
7685 sp->ts_state = STATE_FINAL;
7686 }
7687 else
7688 {
7689 arridx += sp->ts_curi++;
7690 c = byts[arridx];
7691
7692 /* Normal byte, go one level deeper. If it's not equal to
7693 * the byte in the bad word adjust the score. But don't
7694 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007695 if (c == fword[sp->ts_fidx]
7696#ifdef FEAT_MBYTE
7697 || (sp->ts_tcharlen > 0
7698 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007699#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007700 )
7701 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007702 else
7703 newscore = SCORE_SUBST;
7704 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7705 && try_deeper(su, stack, depth, newscore))
7706 {
7707 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007708 sp = &stack[depth];
7709 ++sp->ts_fidx;
7710 tword[sp->ts_twordlen++] = c;
7711 sp->ts_arridx = idxs[arridx];
7712#ifdef FEAT_MBYTE
7713 if (newscore == SCORE_SUBST)
7714 sp->ts_isdiff = DIFF_YES;
7715 if (has_mbyte)
7716 {
7717 /* Multi-byte characters are a bit complicated to
7718 * handle: They differ when any of the bytes
7719 * differ and then their length may also differ. */
7720 if (sp->ts_tcharlen == 0)
7721 {
7722 /* First byte. */
7723 sp->ts_tcharidx = 0;
7724 sp->ts_tcharlen = MB_BYTE2LEN(c);
7725 sp->ts_fcharstart = sp->ts_fidx - 1;
7726 sp->ts_isdiff = (newscore != 0)
7727 ? DIFF_YES : DIFF_NONE;
7728 }
7729 else if (sp->ts_isdiff == DIFF_INSERT)
7730 /* When inserting trail bytes don't advance in
7731 * the bad word. */
7732 --sp->ts_fidx;
7733 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7734 {
7735 /* Last byte of character. */
7736 if (sp->ts_isdiff == DIFF_YES)
7737 {
7738 /* Correct ts_fidx for the byte length of
7739 * the character (we didn't check that
7740 * before). */
7741 sp->ts_fidx = sp->ts_fcharstart
7742 + MB_BYTE2LEN(
7743 fword[sp->ts_fcharstart]);
7744
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00007745 /* For changing a composing character
7746 * adjust the score from SCORE_SUBST to
7747 * SCORE_SUBCOMP. */
7748 if (enc_utf8
7749 && utf_iscomposing(
7750 mb_ptr2char(tword
7751 + sp->ts_twordlen
7752 - sp->ts_tcharlen))
7753 && utf_iscomposing(
7754 mb_ptr2char(fword
7755 + sp->ts_fcharstart)))
7756 sp->ts_score -=
7757 SCORE_SUBST - SCORE_SUBCOMP;
7758
Bram Moolenaarea424162005-06-16 21:51:00 +00007759 /* For a similar character adjust score
7760 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00007761 else if (lp->lp_slang->sl_has_map
Bram Moolenaarea424162005-06-16 21:51:00 +00007762 && similar_chars(lp->lp_slang,
7763 mb_ptr2char(tword
7764 + sp->ts_twordlen
7765 - sp->ts_tcharlen),
7766 mb_ptr2char(fword
7767 + sp->ts_fcharstart)))
7768 sp->ts_score -=
7769 SCORE_SUBST - SCORE_SIMILAR;
7770 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007771 else if (sp->ts_isdiff == DIFF_INSERT
7772 && sp->ts_twordlen > sp->ts_tcharlen)
7773 {
Bram Moolenaarea408852005-06-25 22:49:46 +00007774 p = tword + sp->ts_twordlen
7775 - sp->ts_tcharlen;
7776 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007777 if (enc_utf8 && utf_iscomposing(c))
7778 {
7779 /* Inserting a composing char doesn't
7780 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00007781 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007782 - SCORE_INSCOMP;
7783 }
7784 else
7785 {
7786 /* If the previous character was the
7787 * same, thus doubling a character,
7788 * give a bonus to the score. */
7789 mb_ptr_back(tword, p);
7790 if (c == mb_ptr2char(p))
7791 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00007792 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007793 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007794 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007795
7796 /* Starting a new char, reset the length. */
7797 sp->ts_tcharlen = 0;
7798 }
7799 }
7800 else
7801#endif
7802 {
7803 /* If we found a similar char adjust the score.
7804 * We do this after calling try_deeper() because
7805 * it's slow. */
7806 if (newscore != 0
7807 && lp->lp_slang->sl_has_map
7808 && similar_chars(lp->lp_slang,
7809 c, fword[sp->ts_fidx - 1]))
7810 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7811 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007812 }
7813 }
7814 break;
7815
7816 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007817#ifdef FEAT_MBYTE
7818 /* When past the first byte of a multi-byte char don't try
7819 * delete/insert/swap a character. */
7820 if (has_mbyte && sp->ts_tcharlen > 0)
7821 {
7822 sp->ts_state = STATE_FINAL;
7823 break;
7824 }
7825#endif
7826 /*
7827 * Try skipping one character in the bad word (delete it).
7828 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007829 sp->ts_state = STATE_INS;
7830 sp->ts_curi = 1;
7831 if (fword[sp->ts_fidx] != NUL
7832 && try_deeper(su, stack, depth, SCORE_DEL))
7833 {
7834 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007835
7836 /* Advance over the character in fword[]. Give a bonus to
7837 * the score if the same character is following "nn" ->
7838 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007839#ifdef FEAT_MBYTE
7840 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007841 {
7842 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007843 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00007844 if (enc_utf8 && utf_iscomposing(c))
7845 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
7846 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00007847 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7848 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007849 else
7850#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007851 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007852 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007853 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7854 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007856 break;
7857 }
7858 /*FALLTHROUGH*/
7859
7860 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007861 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 * node. */
7863 n = sp->ts_arridx;
7864 if (sp->ts_curi > byts[n])
7865 {
7866 /* Done all bytes at this node, do next state. */
7867 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007868 }
7869 else
7870 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007871 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 n += sp->ts_curi++;
7873 c = byts[n];
7874 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7875 {
7876 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007877 sp = &stack[depth];
7878 tword[sp->ts_twordlen++] = c;
7879 sp->ts_arridx = idxs[n];
7880#ifdef FEAT_MBYTE
7881 if (has_mbyte)
7882 {
7883 fl = MB_BYTE2LEN(c);
7884 if (fl > 1)
7885 {
7886 /* There are following bytes for the same
7887 * character. We must find all bytes before
7888 * trying delete/insert/swap/etc. */
7889 sp->ts_tcharlen = fl;
7890 sp->ts_tcharidx = 1;
7891 sp->ts_isdiff = DIFF_INSERT;
7892 }
7893 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007894 else
7895 fl = 1;
7896 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007897#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007898 {
7899 /* If the previous character was the same, thus
7900 * doubling a character, give a bonus to the
7901 * score. */
7902 if (sp->ts_twordlen >= 2
7903 && tword[sp->ts_twordlen - 2] == c)
7904 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7905 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007906 }
7907 }
7908 break;
7909
7910 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007911 /*
7912 * Swap two bytes in the bad word: "12" -> "21".
7913 * We change "fword" here, it's changed back afterwards.
7914 */
7915 p = fword + sp->ts_fidx;
7916 c = *p;
7917 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007919 /* End of word, can't swap or replace. */
7920 sp->ts_state = STATE_FINAL;
7921 break;
7922 }
7923#ifdef FEAT_MBYTE
7924 if (has_mbyte)
7925 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007926 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00007927 c = mb_ptr2char(p);
7928 c2 = mb_ptr2char(p + n);
7929 }
7930 else
7931#endif
7932 c2 = p[1];
7933 if (c == c2)
7934 {
7935 /* Characters are identical, swap won't do anything. */
7936 sp->ts_state = STATE_SWAP3;
7937 break;
7938 }
7939 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7940 {
7941 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007942 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007943#ifdef FEAT_MBYTE
7944 if (has_mbyte)
7945 {
7946 fl = mb_char2len(c2);
7947 mch_memmove(p, p + n, fl);
7948 mb_char2bytes(c, p + fl);
7949 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7950 }
7951 else
7952#endif
7953 {
7954 p[0] = c2;
7955 p[1] = c;
7956 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007958 }
7959 else
7960 /* If this swap doesn't work then SWAP3 won't either. */
7961 sp->ts_state = STATE_REP_INI;
7962 break;
7963
Bram Moolenaarea424162005-06-16 21:51:00 +00007964 case STATE_UNSWAP:
7965 /* Undo the STATE_SWAP swap: "21" -> "12". */
7966 p = fword + sp->ts_fidx;
7967#ifdef FEAT_MBYTE
7968 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007969 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007970 n = MB_BYTE2LEN(*p);
7971 c = mb_ptr2char(p + n);
7972 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7973 mb_char2bytes(c, p);
7974 }
7975 else
7976#endif
7977 {
7978 c = *p;
7979 *p = p[1];
7980 p[1] = c;
7981 }
7982 /*FALLTHROUGH*/
7983
7984 case STATE_SWAP3:
7985 /* Swap two bytes, skipping one: "123" -> "321". We change
7986 * "fword" here, it's changed back afterwards. */
7987 p = fword + sp->ts_fidx;
7988#ifdef FEAT_MBYTE
7989 if (has_mbyte)
7990 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007991 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00007992 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007993 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00007994 c2 = mb_ptr2char(p + n);
7995 c3 = mb_ptr2char(p + n + fl);
7996 }
7997 else
7998#endif
7999 {
8000 c = *p;
8001 c2 = p[1];
8002 c3 = p[2];
8003 }
8004
8005 /* When characters are identical: "121" then SWAP3 result is
8006 * identical, ROT3L result is same as SWAP: "211", ROT3L
8007 * result is same as SWAP on next char: "112". Thus skip all
8008 * swapping. Also skip when c3 is NUL. */
8009 if (c == c3 || c3 == NUL)
8010 {
8011 sp->ts_state = STATE_REP_INI;
8012 break;
8013 }
8014 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8015 {
8016 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008017 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008018#ifdef FEAT_MBYTE
8019 if (has_mbyte)
8020 {
8021 tl = mb_char2len(c3);
8022 mch_memmove(p, p + n + fl, tl);
8023 mb_char2bytes(c2, p + tl);
8024 mb_char2bytes(c, p + fl + tl);
8025 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
8026 }
8027 else
8028#endif
8029 {
8030 p[0] = p[2];
8031 p[2] = c;
8032 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8033 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008034 }
8035 else
8036 sp->ts_state = STATE_REP_INI;
8037 break;
8038
Bram Moolenaarea424162005-06-16 21:51:00 +00008039 case STATE_UNSWAP3:
8040 /* Undo STATE_SWAP3: "321" -> "123" */
8041 p = fword + sp->ts_fidx;
8042#ifdef FEAT_MBYTE
8043 if (has_mbyte)
8044 {
8045 n = MB_BYTE2LEN(*p);
8046 c2 = mb_ptr2char(p + n);
8047 fl = MB_BYTE2LEN(p[n]);
8048 c = mb_ptr2char(p + n + fl);
8049 tl = MB_BYTE2LEN(p[n + fl]);
8050 mch_memmove(p + fl + tl, p, n);
8051 mb_char2bytes(c, p);
8052 mb_char2bytes(c2, p + tl);
8053 }
8054 else
8055#endif
8056 {
8057 c = *p;
8058 *p = p[2];
8059 p[2] = c;
8060 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008061
Bram Moolenaarea424162005-06-16 21:51:00 +00008062 /* Rotate three characters left: "123" -> "231". We change
8063 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008064 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8065 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008066 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008068 p = fword + sp->ts_fidx;
8069#ifdef FEAT_MBYTE
8070 if (has_mbyte)
8071 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008072 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008073 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008074 fl = mb_cptr2len(p + n);
8075 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00008076 mch_memmove(p, p + n, fl);
8077 mb_char2bytes(c, p + fl);
8078 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8079 }
8080 else
8081#endif
8082 {
8083 c = *p;
8084 *p = p[1];
8085 p[1] = p[2];
8086 p[2] = c;
8087 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 }
8090 else
8091 sp->ts_state = STATE_REP_INI;
8092 break;
8093
Bram Moolenaarea424162005-06-16 21:51:00 +00008094 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008095 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008096 p = fword + sp->ts_fidx;
8097#ifdef FEAT_MBYTE
8098 if (has_mbyte)
8099 {
8100 n = MB_BYTE2LEN(*p);
8101 n += MB_BYTE2LEN(p[n]);
8102 c = mb_ptr2char(p + n);
8103 tl = MB_BYTE2LEN(p[n]);
8104 mch_memmove(p + tl, p, n);
8105 mb_char2bytes(c, p);
8106 }
8107 else
8108#endif
8109 {
8110 c = p[2];
8111 p[2] = p[1];
8112 p[1] = *p;
8113 *p = c;
8114 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00008117 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8119 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008120 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008121 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008122 p = fword + sp->ts_fidx;
8123#ifdef FEAT_MBYTE
8124 if (has_mbyte)
8125 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008126 n = mb_cptr2len(p);
8127 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008128 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008129 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008130 mch_memmove(p + tl, p, n);
8131 mb_char2bytes(c, p);
8132 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
8133 }
8134 else
8135#endif
8136 {
8137 c = p[2];
8138 p[2] = p[1];
8139 p[1] = *p;
8140 *p = c;
8141 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8142 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008143 }
8144 else
8145 sp->ts_state = STATE_REP_INI;
8146 break;
8147
Bram Moolenaarea424162005-06-16 21:51:00 +00008148 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008149 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008150 p = fword + sp->ts_fidx;
8151#ifdef FEAT_MBYTE
8152 if (has_mbyte)
8153 {
8154 c = mb_ptr2char(p);
8155 tl = MB_BYTE2LEN(*p);
8156 n = MB_BYTE2LEN(p[tl]);
8157 n += MB_BYTE2LEN(p[tl + n]);
8158 mch_memmove(p, p + tl, n);
8159 mb_char2bytes(c, p + n);
8160 }
8161 else
8162#endif
8163 {
8164 c = *p;
8165 *p = p[1];
8166 p[1] = p[2];
8167 p[2] = c;
8168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008169 /*FALLTHROUGH*/
8170
8171 case STATE_REP_INI:
8172 /* Check if matching with REP items from the .aff file would
8173 * work. Quickly skip if there are no REP items or the score
8174 * is going to be too high anyway. */
8175 gap = &lp->lp_slang->sl_rep;
8176 if (gap->ga_len == 0
8177 || sp->ts_score + SCORE_REP >= su->su_maxscore)
8178 {
8179 sp->ts_state = STATE_FINAL;
8180 break;
8181 }
8182
8183 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00008184 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008185 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
8186 if (sp->ts_curi < 0)
8187 {
8188 sp->ts_state = STATE_FINAL;
8189 break;
8190 }
8191
8192 sp->ts_state = STATE_REP;
8193 /*FALLTHROUGH*/
8194
8195 case STATE_REP:
8196 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00008197 * match replace the characters and check if the resulting
8198 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008199 p = fword + sp->ts_fidx;
8200
8201 gap = &lp->lp_slang->sl_rep;
8202 while (sp->ts_curi < gap->ga_len)
8203 {
8204 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
8205 if (*ftp->ft_from != *p)
8206 {
8207 /* past possible matching entries */
8208 sp->ts_curi = gap->ga_len;
8209 break;
8210 }
8211 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
8212 && try_deeper(su, stack, depth, SCORE_REP))
8213 {
8214 /* Need to undo this afterwards. */
8215 sp->ts_state = STATE_REP_UNDO;
8216
8217 /* Change the "from" to the "to" string. */
8218 ++depth;
8219 fl = STRLEN(ftp->ft_from);
8220 tl = STRLEN(ftp->ft_to);
8221 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008223 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008224 repextra += tl - fl;
8225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008226 mch_memmove(p, ftp->ft_to, tl);
8227 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00008228#ifdef FEAT_MBYTE
8229 stack[depth].ts_tcharlen = 0;
8230#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008231 break;
8232 }
8233 }
8234
8235 if (sp->ts_curi >= gap->ga_len)
8236 /* No (more) matches. */
8237 sp->ts_state = STATE_FINAL;
8238
8239 break;
8240
8241 case STATE_REP_UNDO:
8242 /* Undo a REP replacement and continue with the next one. */
8243 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
8244 + sp->ts_curi - 1;
8245 fl = STRLEN(ftp->ft_from);
8246 tl = STRLEN(ftp->ft_to);
8247 p = fword + sp->ts_fidx;
8248 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008251 repextra -= tl - fl;
8252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008253 mch_memmove(p, ftp->ft_from, fl);
8254 sp->ts_state = STATE_REP;
8255 break;
8256
8257 default:
8258 /* Did all possible states at this level, go up one level. */
8259 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008260
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008261 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
8262 {
8263 /* Continue in or go back to the prefix tree. */
8264 byts = pbyts;
8265 idxs = pidxs;
8266 splitoff = 0;
8267 }
8268
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008269 /* Don't check for CTRL-C too often, it takes time. */
8270 line_breakcheck();
8271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008272 }
8273 }
8274}
8275
8276/*
8277 * Try going one level deeper in the tree.
8278 */
8279 static int
8280try_deeper(su, stack, depth, score_add)
8281 suginfo_T *su;
8282 trystate_T *stack;
8283 int depth;
8284 int score_add;
8285{
8286 int newscore;
8287
8288 /* Refuse to go deeper if the scrore is getting too big. */
8289 newscore = stack[depth].ts_score + score_add;
8290 if (newscore >= su->su_maxscore)
8291 return FALSE;
8292
Bram Moolenaarea424162005-06-16 21:51:00 +00008293 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008294 stack[depth + 1].ts_state = STATE_START;
8295 stack[depth + 1].ts_score = newscore;
8296 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008297 return TRUE;
8298}
8299
Bram Moolenaar53805d12005-08-01 07:08:33 +00008300#ifdef FEAT_MBYTE
8301/*
8302 * Case-folding may change the number of bytes: Count nr of chars in
8303 * fword[flen] and return the byte length of that many chars in "word".
8304 */
8305 static int
8306nofold_len(fword, flen, word)
8307 char_u *fword;
8308 int flen;
8309 char_u *word;
8310{
8311 char_u *p;
8312 int i = 0;
8313
8314 for (p = fword; p < fword + flen; mb_ptr_adv(p))
8315 ++i;
8316 for (p = word; i > 0; mb_ptr_adv(p))
8317 --i;
8318 return (int)(p - word);
8319}
8320#endif
8321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322/*
8323 * "fword" is a good word with case folded. Find the matching keep-case
8324 * words and put it in "kword".
8325 * Theoretically there could be several keep-case words that result in the
8326 * same case-folded word, but we only find one...
8327 */
8328 static void
8329find_keepcap_word(slang, fword, kword)
8330 slang_T *slang;
8331 char_u *fword;
8332 char_u *kword;
8333{
8334 char_u uword[MAXWLEN]; /* "fword" in upper-case */
8335 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008336 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008337
8338 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008339 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008340 int round[MAXWLEN];
8341 int fwordidx[MAXWLEN];
8342 int uwordidx[MAXWLEN];
8343 int kwordlen[MAXWLEN];
8344
8345 int flen, ulen;
8346 int l;
8347 int len;
8348 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008349 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350 char_u *p;
8351 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008352 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008353
8354 if (byts == NULL)
8355 {
8356 /* array is empty: "cannot happen" */
8357 *kword = NUL;
8358 return;
8359 }
8360
8361 /* Make an all-cap version of "fword". */
8362 allcap_copy(fword, uword);
8363
8364 /*
8365 * Each character needs to be tried both case-folded and upper-case.
8366 * All this gets very complicated if we keep in mind that changing case
8367 * may change the byte length of a multi-byte character...
8368 */
8369 depth = 0;
8370 arridx[0] = 0;
8371 round[0] = 0;
8372 fwordidx[0] = 0;
8373 uwordidx[0] = 0;
8374 kwordlen[0] = 0;
8375 while (depth >= 0)
8376 {
8377 if (fword[fwordidx[depth]] == NUL)
8378 {
8379 /* We are at the end of "fword". If the tree allows a word to end
8380 * here we have found a match. */
8381 if (byts[arridx[depth] + 1] == 0)
8382 {
8383 kword[kwordlen[depth]] = NUL;
8384 return;
8385 }
8386
8387 /* kword is getting too long, continue one level up */
8388 --depth;
8389 }
8390 else if (++round[depth] > 2)
8391 {
8392 /* tried both fold-case and upper-case character, continue one
8393 * level up */
8394 --depth;
8395 }
8396 else
8397 {
8398 /*
8399 * round[depth] == 1: Try using the folded-case character.
8400 * round[depth] == 2: Try using the upper-case character.
8401 */
8402#ifdef FEAT_MBYTE
8403 if (has_mbyte)
8404 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008405 flen = mb_cptr2len(fword + fwordidx[depth]);
8406 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 }
8408 else
8409#endif
8410 ulen = flen = 1;
8411 if (round[depth] == 1)
8412 {
8413 p = fword + fwordidx[depth];
8414 l = flen;
8415 }
8416 else
8417 {
8418 p = uword + uwordidx[depth];
8419 l = ulen;
8420 }
8421
8422 for (tryidx = arridx[depth]; l > 0; --l)
8423 {
8424 /* Perform a binary search in the list of accepted bytes. */
8425 len = byts[tryidx++];
8426 c = *p++;
8427 lo = tryidx;
8428 hi = tryidx + len - 1;
8429 while (lo < hi)
8430 {
8431 m = (lo + hi) / 2;
8432 if (byts[m] > c)
8433 hi = m - 1;
8434 else if (byts[m] < c)
8435 lo = m + 1;
8436 else
8437 {
8438 lo = hi = m;
8439 break;
8440 }
8441 }
8442
8443 /* Stop if there is no matching byte. */
8444 if (hi < lo || byts[lo] != c)
8445 break;
8446
8447 /* Continue at the child (if there is one). */
8448 tryidx = idxs[lo];
8449 }
8450
8451 if (l == 0)
8452 {
8453 /*
8454 * Found the matching char. Copy it to "kword" and go a
8455 * level deeper.
8456 */
8457 if (round[depth] == 1)
8458 {
8459 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8460 flen);
8461 kwordlen[depth + 1] = kwordlen[depth] + flen;
8462 }
8463 else
8464 {
8465 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8466 ulen);
8467 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8468 }
8469 fwordidx[depth + 1] = fwordidx[depth] + flen;
8470 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8471
8472 ++depth;
8473 arridx[depth] = tryidx;
8474 round[depth] = 0;
8475 }
8476 }
8477 }
8478
8479 /* Didn't find it: "cannot happen". */
8480 *kword = NUL;
8481}
8482
8483/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008484 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8485 * su->su_sga.
8486 */
8487 static void
8488score_comp_sal(su)
8489 suginfo_T *su;
8490{
8491 langp_T *lp;
8492 char_u badsound[MAXWLEN];
8493 int i;
8494 suggest_T *stp;
8495 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008496 int score;
8497
8498 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8499 return;
8500
8501 /* Use the sound-folding of the first language that supports it. */
8502 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8503 lp->lp_slang != NULL; ++lp)
8504 if (lp->lp_slang->sl_sal.ga_len > 0)
8505 {
8506 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008507 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008508
8509 for (i = 0; i < su->su_ga.ga_len; ++i)
8510 {
8511 stp = &SUG(su->su_ga, i);
8512
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008513 /* Case-fold the suggested word, sound-fold it and compute the
8514 * sound-a-like score. */
8515 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008516 if (score < SCORE_MAXMAX)
8517 {
8518 /* Add the suggestion. */
8519 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8520 sstp->st_word = vim_strsave(stp->st_word);
8521 if (sstp->st_word != NULL)
8522 {
8523 sstp->st_score = score;
8524 sstp->st_altscore = 0;
8525 sstp->st_orglen = stp->st_orglen;
8526 ++su->su_sga.ga_len;
8527 }
8528 }
8529 }
8530 break;
8531 }
8532}
8533
8534/*
8535 * Combine the list of suggestions in su->su_ga and su->su_sga.
8536 * They are intwined.
8537 */
8538 static void
8539score_combine(su)
8540 suginfo_T *su;
8541{
8542 int i;
8543 int j;
8544 garray_T ga;
8545 garray_T *gap;
8546 langp_T *lp;
8547 suggest_T *stp;
8548 char_u *p;
8549 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008550 int round;
8551
8552 /* Add the alternate score to su_ga. */
8553 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8554 lp->lp_slang != NULL; ++lp)
8555 {
8556 if (lp->lp_slang->sl_sal.ga_len > 0)
8557 {
8558 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008559 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008560
8561 for (i = 0; i < su->su_ga.ga_len; ++i)
8562 {
8563 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008564 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8565 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008566 if (stp->st_altscore == SCORE_MAXMAX)
8567 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8568 else
8569 stp->st_score = (stp->st_score * 3
8570 + stp->st_altscore) / 4;
8571 stp->st_salscore = FALSE;
8572 }
8573 break;
8574 }
8575 }
8576
8577 /* Add the alternate score to su_sga. */
8578 for (i = 0; i < su->su_sga.ga_len; ++i)
8579 {
8580 stp = &SUG(su->su_sga, i);
8581 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8582 if (stp->st_score == SCORE_MAXMAX)
8583 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8584 else
8585 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8586 stp->st_salscore = TRUE;
8587 }
8588
8589 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8590 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8591 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8592
8593 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8594 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8595 return;
8596
8597 stp = &SUG(ga, 0);
8598 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8599 {
8600 /* round 1: get a suggestion from su_ga
8601 * round 2: get a suggestion from su_sga */
8602 for (round = 1; round <= 2; ++round)
8603 {
8604 gap = round == 1 ? &su->su_ga : &su->su_sga;
8605 if (i < gap->ga_len)
8606 {
8607 /* Don't add a word if it's already there. */
8608 p = SUG(*gap, i).st_word;
8609 for (j = 0; j < ga.ga_len; ++j)
8610 if (STRCMP(stp[j].st_word, p) == 0)
8611 break;
8612 if (j == ga.ga_len)
8613 stp[ga.ga_len++] = SUG(*gap, i);
8614 else
8615 vim_free(p);
8616 }
8617 }
8618 }
8619
8620 ga_clear(&su->su_ga);
8621 ga_clear(&su->su_sga);
8622
8623 /* Truncate the list to the number of suggestions that will be displayed. */
8624 if (ga.ga_len > su->su_maxcount)
8625 {
8626 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8627 vim_free(stp[i].st_word);
8628 ga.ga_len = su->su_maxcount;
8629 }
8630
8631 su->su_ga = ga;
8632}
8633
8634/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008635 * For the goodword in "stp" compute the soundalike score compared to the
8636 * badword.
8637 */
8638 static int
8639stp_sal_score(stp, su, slang, badsound)
8640 suggest_T *stp;
8641 suginfo_T *su;
8642 slang_T *slang;
8643 char_u *badsound; /* sound-folded badword */
8644{
8645 char_u *p;
8646 char_u badsound2[MAXWLEN];
8647 char_u fword[MAXWLEN];
8648 char_u goodsound[MAXWLEN];
8649
8650 if (stp->st_orglen <= su->su_badlen)
8651 p = badsound;
8652 else
8653 {
8654 /* soundfold the bad word with more characters following */
8655 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8656
8657 /* When joining two words the sound often changes a lot. E.g., "t he"
8658 * sounds like "t h" while "the" sounds like "@". Avoid that by
8659 * removing the space. Don't do it when the good word also contains a
8660 * space. */
8661 if (vim_iswhite(su->su_badptr[su->su_badlen])
8662 && *skiptowhite(stp->st_word) == NUL)
8663 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8664 mch_memmove(p, p + 1, STRLEN(p));
8665
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008666 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008667 p = badsound2;
8668 }
8669
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008670 /* Sound-fold the word and compute the score for the difference. */
8671 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008672
8673 return soundalike_score(goodsound, p);
8674}
8675
8676/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008677 * Find suggestions by comparing the word in a sound-a-like form.
8678 */
8679 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008680suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 suginfo_T *su;
8682{
8683 char_u salword[MAXWLEN];
8684 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008685 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008686 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 int curi[MAXWLEN];
8688 langp_T *lp;
8689 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008690 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008691 int depth;
8692 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008693 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008694 int round;
8695 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008696 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008697
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008698 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008699 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8700 lp->lp_slang != NULL; ++lp)
8701 {
8702 if (lp->lp_slang->sl_sal.ga_len > 0)
8703 {
8704 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008705 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008706
8707 /*
8708 * Go through the whole tree, soundfold each word and compare.
8709 * round 1: use the case-folded tree.
8710 * round 2: use the keep-case tree.
8711 */
8712 for (round = 1; round <= 2; ++round)
8713 {
8714 if (round == 1)
8715 {
8716 byts = lp->lp_slang->sl_fbyts;
8717 idxs = lp->lp_slang->sl_fidxs;
8718 }
8719 else
8720 {
8721 byts = lp->lp_slang->sl_kbyts;
8722 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008723 if (byts == NULL) /* no keep-case words */
8724 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725 }
8726
8727 depth = 0;
8728 arridx[0] = 0;
8729 curi[0] = 1;
8730 while (depth >= 0 && !got_int)
8731 {
8732 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008734 /* Done all bytes at this node, go up one level. */
8735 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008736 line_breakcheck();
8737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008738 else
8739 {
8740 /* Do one more byte at this node. */
8741 n = arridx[depth] + curi[depth];
8742 ++curi[depth];
8743 c = byts[n];
8744 if (c == 0)
8745 {
8746 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008747 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8749 {
8750 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008751 /* Sound-fold. Only in keep-case tree need to
8752 * case-fold the word. */
8753 spell_soundfold(lp->lp_slang, tword,
8754 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008755
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008756 /* Compute the edit distance between the
8757 * sound-a-like words. */
8758 sound_score = soundalike_score(salword,
8759 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008760 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008761 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008762 char_u cword[MAXWLEN];
8763 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008764 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008765
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008766 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008767 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008769 /* Need to fix case according to
8770 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008772 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 }
8774 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008775 p = tword;
8776
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008777 if (sps_flags & SPS_DOUBLE)
8778 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008779 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008780 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008781 else
8782 {
8783 /* Compute the score. */
8784 score = spell_edit_score(
8785 su->su_badword, p);
8786 if (sps_flags & SPS_BEST)
8787 /* give a bonus for the good word
8788 * sounding the same as the bad
8789 * word */
8790 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008791 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008792 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008793 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008794 else
8795 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008796 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008797 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008798 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 }
8800 }
8801
8802 /* Skip over other NUL bytes. */
8803 while (byts[n + 1] == 0)
8804 {
8805 ++n;
8806 ++curi[depth];
8807 }
8808 }
8809 else
8810 {
8811 /* Normal char, go one level deeper. */
8812 tword[depth++] = c;
8813 arridx[depth] = idxs[n];
8814 curi[depth] = 1;
8815 }
8816 }
8817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 }
8819 }
8820 }
8821}
8822
8823/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008824 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008825 */
8826 static void
8827make_case_word(fword, cword, flags)
8828 char_u *fword;
8829 char_u *cword;
8830 int flags;
8831{
8832 if (flags & WF_ALLCAP)
8833 /* Make it all upper-case */
8834 allcap_copy(fword, cword);
8835 else if (flags & WF_ONECAP)
8836 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008837 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008838 else
8839 /* Use goodword as-is. */
8840 STRCPY(cword, fword);
8841}
8842
Bram Moolenaarea424162005-06-16 21:51:00 +00008843/*
8844 * Use map string "map" for languages "lp".
8845 */
8846 static void
8847set_map_str(lp, map)
8848 slang_T *lp;
8849 char_u *map;
8850{
8851 char_u *p;
8852 int headc = 0;
8853 int c;
8854 int i;
8855
8856 if (*map == NUL)
8857 {
8858 lp->sl_has_map = FALSE;
8859 return;
8860 }
8861 lp->sl_has_map = TRUE;
8862
8863 /* Init the array and hash table empty. */
8864 for (i = 0; i < 256; ++i)
8865 lp->sl_map_array[i] = 0;
8866#ifdef FEAT_MBYTE
8867 hash_init(&lp->sl_map_hash);
8868#endif
8869
8870 /*
8871 * The similar characters are stored separated with slashes:
8872 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8873 * before the same slash. For characters above 255 sl_map_hash is used.
8874 */
8875 for (p = map; *p != NUL; )
8876 {
8877#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008878 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008879#else
8880 c = *p++;
8881#endif
8882 if (c == '/')
8883 headc = 0;
8884 else
8885 {
8886 if (headc == 0)
8887 headc = c;
8888
8889#ifdef FEAT_MBYTE
8890 /* Characters above 255 don't fit in sl_map_array[], put them in
8891 * the hash table. Each entry is the char, a NUL the headchar and
8892 * a NUL. */
8893 if (c >= 256)
8894 {
8895 int cl = mb_char2len(c);
8896 int headcl = mb_char2len(headc);
8897 char_u *b;
8898 hash_T hash;
8899 hashitem_T *hi;
8900
8901 b = alloc((unsigned)(cl + headcl + 2));
8902 if (b == NULL)
8903 return;
8904 mb_char2bytes(c, b);
8905 b[cl] = NUL;
8906 mb_char2bytes(headc, b + cl + 1);
8907 b[cl + 1 + headcl] = NUL;
8908 hash = hash_hash(b);
8909 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8910 if (HASHITEM_EMPTY(hi))
8911 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8912 else
8913 {
8914 /* This should have been checked when generating the .spl
8915 * file. */
8916 EMSG(_("E999: duplicate char in MAP entry"));
8917 vim_free(b);
8918 }
8919 }
8920 else
8921#endif
8922 lp->sl_map_array[c] = headc;
8923 }
8924 }
8925}
8926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927/*
8928 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8929 * lines in the .aff file.
8930 */
8931 static int
8932similar_chars(slang, c1, c2)
8933 slang_T *slang;
8934 int c1;
8935 int c2;
8936{
Bram Moolenaarea424162005-06-16 21:51:00 +00008937 int m1, m2;
8938#ifdef FEAT_MBYTE
8939 char_u buf[MB_MAXBYTES];
8940 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941
Bram Moolenaarea424162005-06-16 21:51:00 +00008942 if (c1 >= 256)
8943 {
8944 buf[mb_char2bytes(c1, buf)] = 0;
8945 hi = hash_find(&slang->sl_map_hash, buf);
8946 if (HASHITEM_EMPTY(hi))
8947 m1 = 0;
8948 else
8949 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8950 }
8951 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008952#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008953 m1 = slang->sl_map_array[c1];
8954 if (m1 == 0)
8955 return FALSE;
8956
8957
8958#ifdef FEAT_MBYTE
8959 if (c2 >= 256)
8960 {
8961 buf[mb_char2bytes(c2, buf)] = 0;
8962 hi = hash_find(&slang->sl_map_hash, buf);
8963 if (HASHITEM_EMPTY(hi))
8964 m2 = 0;
8965 else
8966 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8967 }
8968 else
8969#endif
8970 m2 = slang->sl_map_array[c2];
8971
8972 return m1 == m2;
8973}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974
8975/*
8976 * Add a suggestion to the list of suggestions.
8977 * Do not add a duplicate suggestion or suggestions with a bad score.
8978 * When "use_score" is not zero it's used, otherwise the score is computed
8979 * with spell_edit_score().
8980 */
8981 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008982add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008984 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008986 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008987 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008988 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008989 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990{
8991 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008993 char_u *p = NULL;
8994 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995
8996 /* Check that the word wasn't banned. */
8997 if (was_banned(su, goodword))
8998 return;
8999
Bram Moolenaar0c405862005-06-22 22:26:26 +00009000 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
9001 * Remove the common part from "goodword". */
9002 i = badlen - su->su_badlen;
9003 if (i > 0)
9004 {
9005 /* This assumes there was no case folding or it didn't change the
9006 * length... */
9007 p = goodword + STRLEN(goodword) - i;
9008 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
9009 {
9010 badlen = su->su_badlen;
9011 c = *p;
9012 *p = NUL;
9013 }
9014 else
9015 p = NULL;
9016 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009017 else if (i < 0)
9018 {
9019 /* When replacing part of the word check that we actually change
9020 * something. For "the the" a suggestion can be replacing the first
9021 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009022 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009023 && STRNCMP(su->su_badword, goodword, badlen) == 0)
9024 return;
9025 }
9026
Bram Moolenaar0c405862005-06-22 22:26:26 +00009027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009028 if (score <= su->su_maxscore)
9029 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009030 /* Check if the word is already there. Also check the length that is
9031 * being replaced "thes," -> "these" is a different suggestion from
9032 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009033 stp = &SUG(*gap, 0);
9034 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009035 if (STRCMP(stp[i].st_word, goodword) == 0
9036 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009037 {
9038 /* Found it. Remember the lowest score. */
9039 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009042 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009043 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 break;
9046 }
9047
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009048 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 {
9050 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009051 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 stp->st_word = vim_strsave(goodword);
9053 if (stp->st_word != NULL)
9054 {
9055 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009056 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009057 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009058 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009059 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009060
9061 /* If we have too many suggestions now, sort the list and keep
9062 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009063 if (gap->ga_len > SUG_MAX_COUNT(su))
9064 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
9065 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 }
9067 }
9068 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00009069
9070 if (p != NULL)
9071 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072}
9073
9074/*
9075 * Add a word to be banned.
9076 */
9077 static void
9078add_banned(su, word)
9079 suginfo_T *su;
9080 char_u *word;
9081{
9082 char_u *s = vim_strsave(word);
9083 hash_T hash;
9084 hashitem_T *hi;
9085
9086 if (s != NULL)
9087 {
9088 hash = hash_hash(s);
9089 hi = hash_lookup(&su->su_banned, s, hash);
9090 if (HASHITEM_EMPTY(hi))
9091 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009092 else
9093 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 }
9095}
9096
9097/*
9098 * Return TRUE if a word appears in the list of banned words.
9099 */
9100 static int
9101was_banned(su, word)
9102 suginfo_T *su;
9103 char_u *word;
9104{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009105 hashitem_T *hi = hash_find(&su->su_banned, word);
9106
9107 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108}
9109
9110/*
9111 * Free the banned words in "su".
9112 */
9113 static void
9114free_banned(su)
9115 suginfo_T *su;
9116{
9117 int todo;
9118 hashitem_T *hi;
9119
9120 todo = su->su_banned.ht_used;
9121 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
9122 {
9123 if (!HASHITEM_EMPTY(hi))
9124 {
9125 vim_free(hi->hi_key);
9126 --todo;
9127 }
9128 }
9129 hash_clear(&su->su_banned);
9130}
9131
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009132/*
9133 * Recompute the score if sound-folding is possible. This is slow,
9134 * thus only done for the final results.
9135 */
9136 static void
9137rescore_suggestions(su)
9138 suginfo_T *su;
9139{
9140 langp_T *lp;
9141 suggest_T *stp;
9142 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009143 int i;
9144
9145 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9146 lp->lp_slang != NULL; ++lp)
9147 {
9148 if (lp->lp_slang->sl_sal.ga_len > 0)
9149 {
9150 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009151 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009152
9153 for (i = 0; i < su->su_ga.ga_len; ++i)
9154 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009155 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009156 if (!stp->st_had_bonus)
9157 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009158 stp->st_altscore = stp_sal_score(stp, su,
9159 lp->lp_slang, sal_badword);
9160 if (stp->st_altscore == SCORE_MAXMAX)
9161 stp->st_altscore = SCORE_BIG;
9162 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009163 }
9164 }
9165 break;
9166 }
9167 }
9168}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170static int
9171#ifdef __BORLANDC__
9172_RTLENTRYF
9173#endif
9174sug_compare __ARGS((const void *s1, const void *s2));
9175
9176/*
9177 * Function given to qsort() to sort the suggestions on st_score.
9178 */
9179 static int
9180#ifdef __BORLANDC__
9181_RTLENTRYF
9182#endif
9183sug_compare(s1, s2)
9184 const void *s1;
9185 const void *s2;
9186{
9187 suggest_T *p1 = (suggest_T *)s1;
9188 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009189 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009191 if (n == 0)
9192 return p1->st_altscore - p2->st_altscore;
9193 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009194}
9195
9196/*
9197 * Cleanup the suggestions:
9198 * - Sort on score.
9199 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009200 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009202 static int
9203cleanup_suggestions(gap, maxscore, keep)
9204 garray_T *gap;
9205 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009206 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009208 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 int i;
9210
9211 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009212 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213
9214 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009215 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009216 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009217 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009219 gap->ga_len = keep;
9220 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009222 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009223}
9224
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009225#if defined(FEAT_EVAL) || defined(PROTO)
9226/*
9227 * Soundfold a string, for soundfold().
9228 * Result is in allocated memory, NULL for an error.
9229 */
9230 char_u *
9231eval_soundfold(word)
9232 char_u *word;
9233{
9234 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009235 char_u sound[MAXWLEN];
9236
9237 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
9238 /* Use the sound-folding of the first language that supports it. */
9239 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9240 lp->lp_slang != NULL; ++lp)
9241 if (lp->lp_slang->sl_sal.ga_len > 0)
9242 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009243 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009244 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009245 return vim_strsave(sound);
9246 }
9247
9248 /* No language with sound folding, return word as-is. */
9249 return vim_strsave(word);
9250}
9251#endif
9252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253/*
9254 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9255 */
9256 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009257spell_soundfold(slang, inword, folded, res)
9258 slang_T *slang;
9259 char_u *inword;
9260 int folded; /* "inword" is already case-folded */
9261 char_u *res;
9262{
9263 char_u fword[MAXWLEN];
9264 char_u *word;
9265
9266 if (slang->sl_sofo)
9267 /* SOFOFROM and SOFOTO used */
9268 spell_soundfold_sofo(slang, inword, res);
9269 else
9270 {
9271 /* SAL items used. Requires the word to be case-folded. */
9272 if (folded)
9273 word = inword;
9274 else
9275 {
9276 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
9277 word = fword;
9278 }
9279
9280#ifdef FEAT_MBYTE
9281 if (has_mbyte)
9282 spell_soundfold_wsal(slang, word, res);
9283 else
9284#endif
9285 spell_soundfold_sal(slang, word, res);
9286 }
9287}
9288
9289/*
9290 * Perform sound folding of "inword" into "res" according to SOFOFROM and
9291 * SOFOTO lines.
9292 */
9293 static void
9294spell_soundfold_sofo(slang, inword, res)
9295 slang_T *slang;
9296 char_u *inword;
9297 char_u *res;
9298{
9299 char_u *s;
9300 int ri = 0;
9301 int c;
9302
9303#ifdef FEAT_MBYTE
9304 if (has_mbyte)
9305 {
9306 int prevc = 0;
9307 int *ip;
9308
9309 /* The sl_sal_first[] table contains the translation for chars up to
9310 * 255, sl_sal the rest. */
9311 for (s = inword; *s != NUL; )
9312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009313 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009314 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9315 c = ' ';
9316 else if (c < 256)
9317 c = slang->sl_sal_first[c];
9318 else
9319 {
9320 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
9321 if (ip == NULL) /* empty list, can't match */
9322 c = NUL;
9323 else
9324 for (;;) /* find "c" in the list */
9325 {
9326 if (*ip == 0) /* not found */
9327 {
9328 c = NUL;
9329 break;
9330 }
9331 if (*ip == c) /* match! */
9332 {
9333 c = ip[1];
9334 break;
9335 }
9336 ip += 2;
9337 }
9338 }
9339
9340 if (c != NUL && c != prevc)
9341 {
9342 ri += mb_char2bytes(c, res + ri);
9343 if (ri + MB_MAXBYTES > MAXWLEN)
9344 break;
9345 prevc = c;
9346 }
9347 }
9348 }
9349 else
9350#endif
9351 {
9352 /* The sl_sal_first[] table contains the translation. */
9353 for (s = inword; (c = *s) != NUL; ++s)
9354 {
9355 if (vim_iswhite(c))
9356 c = ' ';
9357 else
9358 c = slang->sl_sal_first[c];
9359 if (c != NUL && (ri == 0 || res[ri - 1] != c))
9360 res[ri++] = c;
9361 }
9362 }
9363
9364 res[ri] = NUL;
9365}
9366
9367 static void
9368spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 slang_T *slang;
9370 char_u *inword;
9371 char_u *res;
9372{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009373 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009375 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009377 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009379 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 int n, k = 0;
9381 int z0;
9382 int k0;
9383 int n0;
9384 int c;
9385 int pri;
9386 int p0 = -333;
9387 int c0;
9388
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009389 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009390 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009391 if (slang->sl_rem_accents)
9392 {
9393 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009394 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009395 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009396 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009397 {
9398 *t++ = ' ';
9399 s = skipwhite(s);
9400 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009401 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009402 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009403 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 *t++ = *s;
9405 ++s;
9406 }
9407 }
9408 *t = NUL;
9409 }
9410 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009411 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009413 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414
9415 /*
9416 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009417 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009419 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 while ((c = word[i]) != NUL)
9421 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009422 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 n = slang->sl_sal_first[c];
9424 z0 = 0;
9425
9426 if (n >= 0)
9427 {
9428 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009429 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009430 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009431 /* Quickly skip entries that don't match the word. Most
9432 * entries are less then three chars, optimize for that. */
9433 k = smp[n].sm_leadlen;
9434 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009436 if (word[i + 1] != s[1])
9437 continue;
9438 if (k > 2)
9439 {
9440 for (j = 2; j < k; ++j)
9441 if (word[i + j] != s[j])
9442 break;
9443 if (j < k)
9444 continue;
9445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 }
9447
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009448 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009450 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009451 while (*pf != NUL && *pf != word[i + k])
9452 ++pf;
9453 if (*pf == NUL)
9454 continue;
9455 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009457 s = smp[n].sm_rules;
9458 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459
9460 p0 = *s;
9461 k0 = k;
9462 while (*s == '-' && k > 1)
9463 {
9464 k--;
9465 s++;
9466 }
9467 if (*s == '<')
9468 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009469 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 {
9471 /* determine priority */
9472 pri = *s - '0';
9473 s++;
9474 }
9475 if (*s == '^' && *(s + 1) == '^')
9476 s++;
9477
9478 if (*s == NUL
9479 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009480 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009481 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009483 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009484 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009485 && spell_iswordp(word + i - 1, curbuf)
9486 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 {
9488 /* search for followup rules, if: */
9489 /* followup and k > 1 and NO '-' in searchstring */
9490 c0 = word[i + k - 1];
9491 n0 = slang->sl_sal_first[c0];
9492
9493 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009494 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009495 {
9496 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009497 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009499 /* Quickly skip entries that don't match the word.
9500 * */
9501 k0 = smp[n0].sm_leadlen;
9502 if (k0 > 1)
9503 {
9504 if (word[i + k] != s[1])
9505 continue;
9506 if (k0 > 2)
9507 {
9508 pf = word + i + k + 1;
9509 for (j = 2; j < k0; ++j)
9510 if (*pf++ != s[j])
9511 break;
9512 if (j < k0)
9513 continue;
9514 }
9515 }
9516 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009518 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009519 {
9520 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009521 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009522 while (*pf != NUL && *pf != word[i + k0])
9523 ++pf;
9524 if (*pf == NUL)
9525 continue;
9526 ++k0;
9527 }
9528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009529 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009530 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 while (*s == '-')
9532 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009533 /* "k0" gets NOT reduced because
9534 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 s++;
9536 }
9537 if (*s == '<')
9538 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009539 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 {
9541 p0 = *s - '0';
9542 s++;
9543 }
9544
9545 if (*s == NUL
9546 /* *s == '^' cuts */
9547 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009548 && !spell_iswordp(word + i + k0,
9549 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 {
9551 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554
9555 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009558 /* rule fits; stop search */
9559 break;
9560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 }
9562
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009563 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 }
9566
9567 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009568 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009569 if (s == NULL)
9570 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009571 pf = smp[n].sm_rules;
9572 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 if (p0 == 1 && z == 0)
9574 {
9575 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009576 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9577 || res[reslen - 1] == *s))
9578 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 z0 = 1;
9580 z = 1;
9581 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009582 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 {
9584 word[i + k0] = *s;
9585 k0++;
9586 s++;
9587 }
9588 if (k > k0)
9589 mch_memmove(word + i + k0, word + i + k,
9590 STRLEN(word + i + k) + 1);
9591
9592 /* new "actual letter" */
9593 c = word[i];
9594 }
9595 else
9596 {
9597 /* no '<' rule used */
9598 i += k - 1;
9599 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009600 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009602 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009603 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604 s++;
9605 }
9606 /* new "actual letter" */
9607 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009608 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 {
9610 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009611 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 mch_memmove(word, word + i + 1,
9613 STRLEN(word + i + 1) + 1);
9614 i = 0;
9615 z0 = 1;
9616 }
9617 }
9618 break;
9619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 }
9621 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009622 else if (vim_iswhite(c))
9623 {
9624 c = ' ';
9625 k = 1;
9626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627
9628 if (z0 == 0)
9629 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009630 if (k && !p0 && reslen < MAXWLEN && c != NUL
9631 && (!slang->sl_collapse || reslen == 0
9632 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009633 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009634 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635
9636 i++;
9637 z = 0;
9638 k = 0;
9639 }
9640 }
9641
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009642 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643}
9644
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009645#ifdef FEAT_MBYTE
9646/*
9647 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9648 * Multi-byte version of spell_soundfold().
9649 */
9650 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009651spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009652 slang_T *slang;
9653 char_u *inword;
9654 char_u *res;
9655{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009656 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009657 int word[MAXWLEN];
9658 int wres[MAXWLEN];
9659 int l;
9660 char_u *s;
9661 int *ws;
9662 char_u *t;
9663 int *pf;
9664 int i, j, z;
9665 int reslen;
9666 int n, k = 0;
9667 int z0;
9668 int k0;
9669 int n0;
9670 int c;
9671 int pri;
9672 int p0 = -333;
9673 int c0;
9674 int did_white = FALSE;
9675
9676 /*
9677 * Convert the multi-byte string to a wide-character string.
9678 * Remove accents, if wanted. We actually remove all non-word characters.
9679 * But keep white space.
9680 */
9681 n = 0;
9682 for (s = inword; *s != NUL; )
9683 {
9684 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009685 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009686 if (slang->sl_rem_accents)
9687 {
9688 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9689 {
9690 if (did_white)
9691 continue;
9692 c = ' ';
9693 did_white = TRUE;
9694 }
9695 else
9696 {
9697 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009698 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009699 continue;
9700 }
9701 }
9702 word[n++] = c;
9703 }
9704 word[n] = NUL;
9705
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009706 /*
9707 * This comes from Aspell phonet.cpp.
9708 * Converted from C++ to C. Added support for multi-byte chars.
9709 * Changed to keep spaces.
9710 */
9711 i = reslen = z = 0;
9712 while ((c = word[i]) != NUL)
9713 {
9714 /* Start with the first rule that has the character in the word. */
9715 n = slang->sl_sal_first[c & 0xff];
9716 z0 = 0;
9717
9718 if (n >= 0)
9719 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009720 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009721 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9722 {
9723 /* Quickly skip entries that don't match the word. Most
9724 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009725 if (c != ws[0])
9726 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009727 k = smp[n].sm_leadlen;
9728 if (k > 1)
9729 {
9730 if (word[i + 1] != ws[1])
9731 continue;
9732 if (k > 2)
9733 {
9734 for (j = 2; j < k; ++j)
9735 if (word[i + j] != ws[j])
9736 break;
9737 if (j < k)
9738 continue;
9739 }
9740 }
9741
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009742 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009743 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009744 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009745 while (*pf != NUL && *pf != word[i + k])
9746 ++pf;
9747 if (*pf == NUL)
9748 continue;
9749 ++k;
9750 }
9751 s = smp[n].sm_rules;
9752 pri = 5; /* default priority */
9753
9754 p0 = *s;
9755 k0 = k;
9756 while (*s == '-' && k > 1)
9757 {
9758 k--;
9759 s++;
9760 }
9761 if (*s == '<')
9762 s++;
9763 if (VIM_ISDIGIT(*s))
9764 {
9765 /* determine priority */
9766 pri = *s - '0';
9767 s++;
9768 }
9769 if (*s == '^' && *(s + 1) == '^')
9770 s++;
9771
9772 if (*s == NUL
9773 || (*s == '^'
9774 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009775 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009776 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009777 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009778 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009779 && spell_iswordp_w(word + i - 1, curbuf)
9780 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009781 {
9782 /* search for followup rules, if: */
9783 /* followup and k > 1 and NO '-' in searchstring */
9784 c0 = word[i + k - 1];
9785 n0 = slang->sl_sal_first[c0 & 0xff];
9786
9787 if (slang->sl_followup && k > 1 && n0 >= 0
9788 && p0 != '-' && word[i + k] != NUL)
9789 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009790 /* Test follow-up rule for "word[i + k]"; loop over
9791 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009792 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9793 == (c0 & 0xff); ++n0)
9794 {
9795 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009796 */
9797 if (c0 != ws[0])
9798 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009799 k0 = smp[n0].sm_leadlen;
9800 if (k0 > 1)
9801 {
9802 if (word[i + k] != ws[1])
9803 continue;
9804 if (k0 > 2)
9805 {
9806 pf = word + i + k + 1;
9807 for (j = 2; j < k0; ++j)
9808 if (*pf++ != ws[j])
9809 break;
9810 if (j < k0)
9811 continue;
9812 }
9813 }
9814 k0 += k - 1;
9815
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009816 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009817 {
9818 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009819 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009820 while (*pf != NUL && *pf != word[i + k0])
9821 ++pf;
9822 if (*pf == NUL)
9823 continue;
9824 ++k0;
9825 }
9826
9827 p0 = 5;
9828 s = smp[n0].sm_rules;
9829 while (*s == '-')
9830 {
9831 /* "k0" gets NOT reduced because
9832 * "if (k0 == k)" */
9833 s++;
9834 }
9835 if (*s == '<')
9836 s++;
9837 if (VIM_ISDIGIT(*s))
9838 {
9839 p0 = *s - '0';
9840 s++;
9841 }
9842
9843 if (*s == NUL
9844 /* *s == '^' cuts */
9845 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009846 && !spell_iswordp_w(word + i + k0,
9847 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009848 {
9849 if (k0 == k)
9850 /* this is just a piece of the string */
9851 continue;
9852
9853 if (p0 < pri)
9854 /* priority too low */
9855 continue;
9856 /* rule fits; stop search */
9857 break;
9858 }
9859 }
9860
9861 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9862 == (c0 & 0xff))
9863 continue;
9864 }
9865
9866 /* replace string */
9867 ws = smp[n].sm_to_w;
9868 s = smp[n].sm_rules;
9869 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9870 if (p0 == 1 && z == 0)
9871 {
9872 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009873 if (reslen > 0 && ws != NULL && *ws != NUL
9874 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009875 || wres[reslen - 1] == *ws))
9876 reslen--;
9877 z0 = 1;
9878 z = 1;
9879 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009880 if (ws != NULL)
9881 while (*ws != NUL && word[i + k0] != NUL)
9882 {
9883 word[i + k0] = *ws;
9884 k0++;
9885 ws++;
9886 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009887 if (k > k0)
9888 mch_memmove(word + i + k0, word + i + k,
9889 sizeof(int) * (STRLEN(word + i + k) + 1));
9890
9891 /* new "actual letter" */
9892 c = word[i];
9893 }
9894 else
9895 {
9896 /* no '<' rule used */
9897 i += k - 1;
9898 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009899 if (ws != NULL)
9900 while (*ws != NUL && ws[1] != NUL
9901 && reslen < MAXWLEN)
9902 {
9903 if (reslen == 0 || wres[reslen - 1] != *ws)
9904 wres[reslen++] = *ws;
9905 ws++;
9906 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009907 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009908 if (ws == NULL)
9909 c = NUL;
9910 else
9911 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009912 if (strstr((char *)s, "^^") != NULL)
9913 {
9914 if (c != NUL)
9915 wres[reslen++] = c;
9916 mch_memmove(word, word + i + 1,
9917 sizeof(int) * (STRLEN(word + i + 1) + 1));
9918 i = 0;
9919 z0 = 1;
9920 }
9921 }
9922 break;
9923 }
9924 }
9925 }
9926 else if (vim_iswhite(c))
9927 {
9928 c = ' ';
9929 k = 1;
9930 }
9931
9932 if (z0 == 0)
9933 {
9934 if (k && !p0 && reslen < MAXWLEN && c != NUL
9935 && (!slang->sl_collapse || reslen == 0
9936 || wres[reslen - 1] != c))
9937 /* condense only double letters */
9938 wres[reslen++] = c;
9939
9940 i++;
9941 z = 0;
9942 k = 0;
9943 }
9944 }
9945
9946 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9947 l = 0;
9948 for (n = 0; n < reslen; ++n)
9949 {
9950 l += mb_char2bytes(wres[n], res + l);
9951 if (l + MB_MAXBYTES > MAXWLEN)
9952 break;
9953 }
9954 res[l] = NUL;
9955}
9956#endif
9957
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009958/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009959 * Compute a score for two sound-a-like words.
9960 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9961 * Instead of a generic loop we write out the code. That keeps it fast by
9962 * avoiding checks that will not be possible.
9963 */
9964 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009965soundalike_score(goodstart, badstart)
9966 char_u *goodstart; /* sound-folded good word */
9967 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009968{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009969 char_u *goodsound = goodstart;
9970 char_u *badsound = badstart;
9971 int goodlen;
9972 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009973 int n;
9974 char_u *pl, *ps;
9975 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009976 int score = 0;
9977
9978 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9979 * counted so much, vowels halfway the word aren't counted at all. */
9980 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9981 {
9982 score = SCORE_DEL / 2;
9983 if (*badsound == '*')
9984 ++badsound;
9985 else
9986 ++goodsound;
9987 }
9988
9989 goodlen = STRLEN(goodsound);
9990 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009991
9992 /* Return quickly if the lenghts are too different to be fixed by two
9993 * changes. */
9994 n = goodlen - badlen;
9995 if (n < -2 || n > 2)
9996 return SCORE_MAXMAX;
9997
9998 if (n > 0)
9999 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010000 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010001 ps = badsound;
10002 }
10003 else
10004 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010005 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010006 ps = goodsound;
10007 }
10008
10009 /* Skip over the identical part. */
10010 while (*pl == *ps && *pl != NUL)
10011 {
10012 ++pl;
10013 ++ps;
10014 }
10015
10016 switch (n)
10017 {
10018 case -2:
10019 case 2:
10020 /*
10021 * Must delete two characters from "pl".
10022 */
10023 ++pl; /* first delete */
10024 while (*pl == *ps)
10025 {
10026 ++pl;
10027 ++ps;
10028 }
10029 /* strings must be equal after second delete */
10030 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010031 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010032
10033 /* Failed to compare. */
10034 break;
10035
10036 case -1:
10037 case 1:
10038 /*
10039 * Minimal one delete from "pl" required.
10040 */
10041
10042 /* 1: delete */
10043 pl2 = pl + 1;
10044 ps2 = ps;
10045 while (*pl2 == *ps2)
10046 {
10047 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010048 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010049 ++pl2;
10050 ++ps2;
10051 }
10052
10053 /* 2: delete then swap, then rest must be equal */
10054 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10055 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010056 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010057
10058 /* 3: delete then substitute, then the rest must be equal */
10059 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010060 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010061
10062 /* 4: first swap then delete */
10063 if (pl[0] == ps[1] && pl[1] == ps[0])
10064 {
10065 pl2 = pl + 2; /* swap, skip two chars */
10066 ps2 = ps + 2;
10067 while (*pl2 == *ps2)
10068 {
10069 ++pl2;
10070 ++ps2;
10071 }
10072 /* delete a char and then strings must be equal */
10073 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010074 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010075 }
10076
10077 /* 5: first substitute then delete */
10078 pl2 = pl + 1; /* substitute, skip one char */
10079 ps2 = ps + 1;
10080 while (*pl2 == *ps2)
10081 {
10082 ++pl2;
10083 ++ps2;
10084 }
10085 /* delete a char and then strings must be equal */
10086 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010087 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010088
10089 /* Failed to compare. */
10090 break;
10091
10092 case 0:
10093 /*
10094 * Lenghts are equal, thus changes must result in same length: An
10095 * insert is only possible in combination with a delete.
10096 * 1: check if for identical strings
10097 */
10098 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010099 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010100
10101 /* 2: swap */
10102 if (pl[0] == ps[1] && pl[1] == ps[0])
10103 {
10104 pl2 = pl + 2; /* swap, skip two chars */
10105 ps2 = ps + 2;
10106 while (*pl2 == *ps2)
10107 {
10108 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010109 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010110 ++pl2;
10111 ++ps2;
10112 }
10113 /* 3: swap and swap again */
10114 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10115 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010116 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010117
10118 /* 4: swap and substitute */
10119 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010120 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010121 }
10122
10123 /* 5: substitute */
10124 pl2 = pl + 1;
10125 ps2 = ps + 1;
10126 while (*pl2 == *ps2)
10127 {
10128 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010129 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010130 ++pl2;
10131 ++ps2;
10132 }
10133
10134 /* 6: substitute and swap */
10135 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10136 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010137 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010138
10139 /* 7: substitute and substitute */
10140 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010141 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010142
10143 /* 8: insert then delete */
10144 pl2 = pl;
10145 ps2 = ps + 1;
10146 while (*pl2 == *ps2)
10147 {
10148 ++pl2;
10149 ++ps2;
10150 }
10151 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010152 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010153
10154 /* 9: delete then insert */
10155 pl2 = pl + 1;
10156 ps2 = ps;
10157 while (*pl2 == *ps2)
10158 {
10159 ++pl2;
10160 ++ps2;
10161 }
10162 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010163 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010164
10165 /* Failed to compare. */
10166 break;
10167 }
10168
10169 return SCORE_MAXMAX;
10170}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172/*
10173 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010174 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010175 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010177 * It has been converted from C++ to C and modified to support multi-byte
10178 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 */
10180 static int
10181spell_edit_score(badword, goodword)
10182 char_u *badword;
10183 char_u *goodword;
10184{
10185 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010186 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 int j, i;
10188 int t;
10189 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010190 int pbc, pgc;
10191#ifdef FEAT_MBYTE
10192 char_u *p;
10193 int wbadword[MAXWLEN];
10194 int wgoodword[MAXWLEN];
10195
10196 if (has_mbyte)
10197 {
10198 /* Get the characters from the multi-byte strings and put them in an
10199 * int array for easy access. */
10200 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010201 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010202 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010203 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010204 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010205 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010206 }
10207 else
10208#endif
10209 {
10210 badlen = STRLEN(badword) + 1;
10211 goodlen = STRLEN(goodword) + 1;
10212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213
10214 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
10215#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
10217 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010218 if (cnt == NULL)
10219 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220
10221 CNT(0, 0) = 0;
10222 for (j = 1; j <= goodlen; ++j)
10223 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
10224
10225 for (i = 1; i <= badlen; ++i)
10226 {
10227 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
10228 for (j = 1; j <= goodlen; ++j)
10229 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010230#ifdef FEAT_MBYTE
10231 if (has_mbyte)
10232 {
10233 bc = wbadword[i - 1];
10234 gc = wgoodword[j - 1];
10235 }
10236 else
10237#endif
10238 {
10239 bc = badword[i - 1];
10240 gc = goodword[j - 1];
10241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 if (bc == gc)
10243 CNT(i, j) = CNT(i - 1, j - 1);
10244 else
10245 {
10246 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010247 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
10249 else
10250 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
10251
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010252 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010254#ifdef FEAT_MBYTE
10255 if (has_mbyte)
10256 {
10257 pbc = wbadword[i - 2];
10258 pgc = wgoodword[j - 2];
10259 }
10260 else
10261#endif
10262 {
10263 pbc = badword[i - 2];
10264 pgc = goodword[j - 2];
10265 }
10266 if (bc == pgc && pbc == gc)
10267 {
10268 t = SCORE_SWAP + CNT(i - 2, j - 2);
10269 if (t < CNT(i, j))
10270 CNT(i, j) = t;
10271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 }
10273 t = SCORE_DEL + CNT(i - 1, j);
10274 if (t < CNT(i, j))
10275 CNT(i, j) = t;
10276 t = SCORE_INS + CNT(i, j - 1);
10277 if (t < CNT(i, j))
10278 CNT(i, j) = t;
10279 }
10280 }
10281 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010282
10283 i = CNT(badlen - 1, goodlen - 1);
10284 vim_free(cnt);
10285 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010287
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010288/*
10289 * ":spelldump"
10290 */
10291/*ARGSUSED*/
10292 void
10293ex_spelldump(eap)
10294 exarg_T *eap;
10295{
10296 buf_T *buf = curbuf;
10297 langp_T *lp;
10298 slang_T *slang;
10299 idx_T arridx[MAXWLEN];
10300 int curi[MAXWLEN];
10301 char_u word[MAXWLEN];
10302 int c;
10303 char_u *byts;
10304 idx_T *idxs;
10305 linenr_T lnum = 0;
10306 int round;
10307 int depth;
10308 int n;
10309 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010310 char_u *region_names = NULL; /* region names being used */
10311 int do_region = TRUE; /* dump region names and numbers */
10312 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010313
10314 if (no_spell_checking())
10315 return;
10316
10317 /* Create a new empty buffer by splitting the window. */
10318 do_cmdline_cmd((char_u *)"new");
10319 if (!bufempty() || !buf_valid(buf))
10320 return;
10321
Bram Moolenaar7887d882005-07-01 22:33:52 +000010322 /* Find out if we can support regions: All languages must support the same
10323 * regions or none at all. */
10324 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10325 {
10326 p = lp->lp_slang->sl_regions;
10327 if (p[0] != 0)
10328 {
10329 if (region_names == NULL) /* first language with regions */
10330 region_names = p;
10331 else if (STRCMP(region_names, p) != 0)
10332 {
10333 do_region = FALSE; /* region names are different */
10334 break;
10335 }
10336 }
10337 }
10338
10339 if (do_region && region_names != NULL)
10340 {
10341 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
10342 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10343 }
10344 else
10345 do_region = FALSE;
10346
10347 /*
10348 * Loop over all files loaded for the entries in 'spelllang'.
10349 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010350 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10351 {
10352 slang = lp->lp_slang;
10353
10354 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
10355 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10356
10357 /* round 1: case-folded tree
10358 * round 2: keep-case tree */
10359 for (round = 1; round <= 2; ++round)
10360 {
10361 if (round == 1)
10362 {
10363 byts = slang->sl_fbyts;
10364 idxs = slang->sl_fidxs;
10365 }
10366 else
10367 {
10368 byts = slang->sl_kbyts;
10369 idxs = slang->sl_kidxs;
10370 }
10371 if (byts == NULL)
10372 continue; /* array is empty */
10373
10374 depth = 0;
10375 arridx[0] = 0;
10376 curi[0] = 1;
10377 while (depth >= 0 && !got_int)
10378 {
10379 if (curi[depth] > byts[arridx[depth]])
10380 {
10381 /* Done all bytes at this node, go up one level. */
10382 --depth;
10383 line_breakcheck();
10384 }
10385 else
10386 {
10387 /* Do one more byte at this node. */
10388 n = arridx[depth] + curi[depth];
10389 ++curi[depth];
10390 c = byts[n];
10391 if (c == 0)
10392 {
10393 /* End of word, deal with the word.
10394 * Don't use keep-case words in the fold-case tree,
10395 * they will appear in the keep-case tree.
10396 * Only use the word when the region matches. */
10397 flags = (int)idxs[n];
10398 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000010399 && (do_region
10400 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010401 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010402 & lp->lp_region) != 0))
10403 {
10404 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010405 if (!do_region)
10406 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010407
10408 /* Dump the basic word if there is no prefix or
10409 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010410 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010411 if (c == 0 || curi[depth] == 2)
10412 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010413
10414 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010415 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010416 lnum = apply_prefixes(slang, word, round,
10417 flags, lnum);
10418 }
10419 }
10420 else
10421 {
10422 /* Normal char, go one level deeper. */
10423 word[depth++] = c;
10424 arridx[depth] = idxs[n];
10425 curi[depth] = 1;
10426 }
10427 }
10428 }
10429 }
10430 }
10431
10432 /* Delete the empty line that we started with. */
10433 if (curbuf->b_ml.ml_line_count > 1)
10434 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10435
10436 redraw_later(NOT_VALID);
10437}
10438
10439/*
10440 * Dump one word: apply case modifications and append a line to the buffer.
10441 */
10442 static void
10443dump_word(word, round, flags, lnum)
10444 char_u *word;
10445 int round;
10446 int flags;
10447 linenr_T lnum;
10448{
10449 int keepcap = FALSE;
10450 char_u *p;
10451 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010452 char_u badword[MAXWLEN + 10];
10453 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010454
10455 if (round == 1 && (flags & WF_CAPMASK) != 0)
10456 {
10457 /* Need to fix case according to "flags". */
10458 make_case_word(word, cword, flags);
10459 p = cword;
10460 }
10461 else
10462 {
10463 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010464 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10465 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010466 keepcap = TRUE;
10467 }
10468
Bram Moolenaar7887d882005-07-01 22:33:52 +000010469 /* Add flags and regions after a slash. */
10470 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010471 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010472 STRCPY(badword, p);
10473 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010474 if (keepcap)
10475 STRCAT(badword, "=");
10476 if (flags & WF_BANNED)
10477 STRCAT(badword, "!");
10478 else if (flags & WF_RARE)
10479 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010480 if (flags & WF_REGION)
10481 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010482 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010483 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010484 p = badword;
10485 }
10486
10487 ml_append(lnum, p, (colnr_T)0, FALSE);
10488}
10489
10490/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010491 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10492 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010493 * Return the updated line number.
10494 */
10495 static linenr_T
10496apply_prefixes(slang, word, round, flags, startlnum)
10497 slang_T *slang;
10498 char_u *word; /* case-folded word */
10499 int round;
10500 int flags; /* flags with prefix ID */
10501 linenr_T startlnum;
10502{
10503 idx_T arridx[MAXWLEN];
10504 int curi[MAXWLEN];
10505 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010506 char_u word_up[MAXWLEN];
10507 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010508 int c;
10509 char_u *byts;
10510 idx_T *idxs;
10511 linenr_T lnum = startlnum;
10512 int depth;
10513 int n;
10514 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010515 int i;
10516
Bram Moolenaar53805d12005-08-01 07:08:33 +000010517 /* if the word starts with a lower-case letter make the word with an
10518 * upper-case letter in word_up[]. */
10519 c = PTR2CHAR(word);
10520 if (SPELL_TOUPPER(c) != c)
10521 {
10522 onecap_copy(word, word_up, TRUE);
10523 has_word_up = TRUE;
10524 }
10525
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010526 byts = slang->sl_pbyts;
10527 idxs = slang->sl_pidxs;
10528 if (byts != NULL) /* array not is empty */
10529 {
10530 /*
10531 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010532 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010533 */
10534 depth = 0;
10535 arridx[0] = 0;
10536 curi[0] = 1;
10537 while (depth >= 0 && !got_int)
10538 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010539 n = arridx[depth];
10540 len = byts[n];
10541 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010542 {
10543 /* Done all bytes at this node, go up one level. */
10544 --depth;
10545 line_breakcheck();
10546 }
10547 else
10548 {
10549 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010550 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010551 ++curi[depth];
10552 c = byts[n];
10553 if (c == 0)
10554 {
10555 /* End of prefix, find out how many IDs there are. */
10556 for (i = 1; i < len; ++i)
10557 if (byts[n + i] != 0)
10558 break;
10559 curi[depth] += i - 1;
10560
Bram Moolenaar53805d12005-08-01 07:08:33 +000010561 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10562 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010563 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010564 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010565 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010566 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010567 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010568 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010569
10570 /* Check for prefix that matches the word when the
10571 * first letter is upper-case, but only if the prefix has
10572 * a condition. */
10573 if (has_word_up)
10574 {
10575 c = valid_word_prefix(i, n, flags, word_up, slang,
10576 TRUE);
10577 if (c != 0)
10578 {
10579 vim_strncpy(prefix + depth, word_up,
10580 MAXWLEN - depth - 1);
10581 dump_word(prefix, round,
10582 (c & WF_RAREPFX) ? (flags | WF_RARE)
10583 : flags, lnum++);
10584 }
10585 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010586 }
10587 else
10588 {
10589 /* Normal char, go one level deeper. */
10590 prefix[depth++] = c;
10591 arridx[depth] = idxs[n];
10592 curi[depth] = 1;
10593 }
10594 }
10595 }
10596 }
10597
10598 return lnum;
10599}
10600
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010601#if defined(FEAT_INS_EXPAND) || defined(PROTO)
10602static int spell_expand_need_cap;
10603
10604/*
10605 * Find start of the word in front of the cursor. We don't check if it is
10606 * badly spelled, with completion we can only change the word in front of the
10607 * cursor.
10608 * Used for Insert mode completion CTRL-X ?.
10609 * Returns the column number of the word.
10610 */
10611 int
10612spell_word_start(startcol)
10613 int startcol;
10614{
10615 char_u *line;
10616 char_u *p;
10617 int col = 0;
10618
10619 if (no_spell_checking())
10620 return startcol;
10621
10622 /* Find a word character before "startcol". */
10623 line = ml_get_curline();
10624 for (p = line + startcol; p > line; )
10625 {
10626 mb_ptr_back(line, p);
10627 if (spell_iswordp_nmw(p))
10628 break;
10629 }
10630
10631 /* Go back to start of the word. */
10632 while (p > line)
10633 {
10634 col = p - line;
10635 mb_ptr_back(line, p);
10636 if (!spell_iswordp(p, curbuf))
10637 break;
10638 col = 0;
10639 }
10640
10641 /* Need to check for 'spellcapcheck' now, the word is removed before
10642 * expand_spelling() is called. Therefore the ugly global variable. */
10643 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
10644
10645 return col;
10646}
10647
10648/*
10649 * Get list of spelling suggestions.
10650 * Used for Insert mode completion CTRL-X ?.
10651 * Returns the number of matches. The matches are in "matchp[]", array of
10652 * allocated strings.
10653 */
10654/*ARGSUSED*/
10655 int
10656expand_spelling(lnum, col, pat, matchp)
10657 linenr_T lnum;
10658 int col;
10659 char_u *pat;
10660 char_u ***matchp;
10661{
10662 garray_T ga;
10663
10664 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
10665 *matchp = ga.ga_data;
10666 return ga.ga_len;
10667}
10668#endif
10669
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010670#endif /* FEAT_SYN_HL */