blob: ab9bf3109f8b3b70ff549a9b1afc2734c9fdee6a [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
80 * compute the the maximum word score that can be used.
81 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
120 * sectionID == SN_REGION: <regionname> ...
121 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000122 * First <regionname> is region 1.
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
125 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
127 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000128 * 0x01 word character CF_WORD
129 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000130 * <folcharslen> 2 bytes Number of bytes in <folchars>.
131 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000132 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000133 * sectionID == SN_MIDWORD: <midword>
134 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000135 * in the middle of a word.
136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000138 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000139 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000140 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000141 * <condstr> N bytes Condition for the prefix.
142 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000143 * sectionID == SN_REP: <repcount> <rep> ...
144 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000145 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000146 * <repfromlen> 1 byte length of <repfrom>
147 * <repfrom> N bytes "from" part of replacement
148 * <reptolen> 1 byte length of <repto>
149 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000150 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000151 * sectionID == SN_REPSAL: <repcount> <rep> ...
152 * just like SN_REP but for soundfolded words
153 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000154 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
155 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000156 * SAL_F0LLOWUP
157 * SAL_COLLAPSE
158 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000159 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000161 * <salfromlen> 1 byte length of <salfrom>
162 * <salfrom> N bytes "from" part of soundsalike
163 * <saltolen> 1 byte length of <salto>
164 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000165 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000166 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
167 * <sofofromlen> 2 bytes length of <sofofrom>
168 * <sofofrom> N bytes "from" part of soundfold
169 * <sofotolen> 2 bytes length of <sofoto>
170 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000171 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000172 * sectionID == SN_SUGFILE: <timestamp>
173 * <timestamp> 8 bytes time in seconds that must match with .sug file
174 *
175 * sectionID == SN_WORDS: <word> ...
176 * <word> N bytes NUL terminated common word
177 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000178 * sectionID == SN_MAP: <mapstr>
179 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000180 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000181 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000182 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
183 * <compmax> 1 byte Maximum nr of words in compound word.
184 * <compminlen> 1 byte Minimal word length for compounding.
185 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
186 * <compflags> N bytes Flags from COMPOUNDFLAGS items, separated by
187 * slashes.
188 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000189 * sectionID == SN_NOBREAK: (empty, its presence is enough)
190 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000191 * sectionID == SN_SYLLABLE: <syllable>
192 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000193 *
194 * <LWORDTREE>: <wordtree>
195 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000196 * <KWORDTREE>: <wordtree>
197 *
198 * <PREFIXTREE>: <wordtree>
199 *
200 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * <wordtree>: <nodecount> <nodedata> ...
202 *
203 * <nodecount> 4 bytes Number of nodes following. MSB first.
204 *
205 * <nodedata>: <siblingcount> <sibling> ...
206 *
207 * <siblingcount> 1 byte Number of siblings in this node. The siblings
208 * follow in sorted order.
209 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000210 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000211 * | <flags> [<flags2>] [<region>] [<affixID>]
212 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000213 *
214 * <byte> 1 byte Byte value of the sibling. Special cases:
215 * BY_NOFLAGS: End of word without flags and for all
216 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000217 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000218 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000219 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000220 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000221 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000222 * BY_FLAGS2: End of word, <flags> and <flags2>
223 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000224 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000225 * and <xbyte> follow.
226 *
227 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
228 *
229 * <xbyte> 1 byte byte value of the sibling.
230 *
231 * <flags> 1 byte bitmask of:
232 * WF_ALLCAP word must have only capitals
233 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000234 * WF_KEEPCAP keep-case word
235 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000236 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000237 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000239 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000240 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000241 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000242 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000243 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000244 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000245 * <pflags> 1 byte bitmask of:
246 * WFP_RARE rare prefix
247 * WFP_NC non-combining prefix
248 * WFP_UP letter after prefix made upper case
249 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000250 * <region> 1 byte Bitmask for regions in which word is valid. When
251 * omitted it's valid in all regions.
252 * Lowest bit is for region 1.
253 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000254 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000255 * PREFIXTREE used for the required prefix ID.
256 *
257 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
258 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000259 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000260 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000261 */
262
Bram Moolenaar4770d092006-01-12 23:22:24 +0000263/*
264 * Vim .sug file format: <SUGHEADER>
265 * <SUGWORDTREE>
266 * <SUGTABLE>
267 *
268 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
269 *
270 * <fileID> 6 bytes "VIMsug"
271 * <versionnr> 1 byte VIMSUGVERSION
272 * <timestamp> 8 bytes timestamp that must match with .spl file
273 *
274 *
275 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
276 *
277 *
278 * <SUGTABLE>: <sugwcount> <sugline> ...
279 *
280 * <sugwcount> 4 bytes number of <sugline> following
281 *
282 * <sugline>: <sugnr> ... NUL
283 *
284 * <sugnr>: X bytes word number that results in this soundfolded word,
285 * stored as an offset to the previous number in as
286 * few bytes as possible, see offset2bytes())
287 */
288
Bram Moolenaare19defe2005-03-21 08:23:33 +0000289#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
290# include <io.h> /* for lseek(), must be before vim.h */
291#endif
292
293#include "vim.h"
294
295#if defined(FEAT_SYN_HL) || defined(PROTO)
296
297#ifdef HAVE_FCNTL_H
298# include <fcntl.h>
299#endif
300
Bram Moolenaar4770d092006-01-12 23:22:24 +0000301#ifndef UNIX /* it's in os_unix.h for Unix */
302# include <time.h> /* for time_t */
303#endif
304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000305#define MAXWLEN 250 /* Assume max. word len is this many bytes.
306 Some places assume a word length fits in a
307 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000308
Bram Moolenaare52325c2005-08-22 22:54:29 +0000309/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000310 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000311#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000312typedef int idx_T;
313#else
314typedef long idx_T;
315#endif
316
317/* Flags used for a word. Only the lowest byte can be used, the region byte
318 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000319#define WF_REGION 0x01 /* region byte follows */
320#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
321#define WF_ALLCAP 0x04 /* word must be all capitals */
322#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000323#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000324#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000325#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000326#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000327
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000328/* for <flags2>, shifted up one byte to be used in wn_flags */
329#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000330#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000331
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000332/* only used for su_badflags */
333#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
334
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000335#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336
Bram Moolenaar53805d12005-08-01 07:08:33 +0000337/* flags for <pflags> */
338#define WFP_RARE 0x01 /* rare prefix */
339#define WFP_NC 0x02 /* prefix is not combining */
340#define WFP_UP 0x04 /* to-upper prefix */
341
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000342/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000343 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000344#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
345 * postponed prefix */
346#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
347 * postponed prefix */
348#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
349 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000350
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000351/* Special byte values for <byte>. Some are only used in the tree for
352 * postponed prefixes, some only in the other trees. This is a bit messy... */
353#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000354 * postponed prefix: no <pflags> */
355#define BY_INDEX 1 /* child is shared, index follows */
356#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
357 * postponed prefix: <pflags> follows */
358#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
359 * follow; never used in prefix tree */
360#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361
Bram Moolenaar4770d092006-01-12 23:22:24 +0000362/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
363 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364 * One replacement: from "ft_from" to "ft_to". */
365typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000366{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000367 char_u *ft_from;
368 char_u *ft_to;
369} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000370
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000371/* Info from "SAL" entries in ".aff" file used in sl_sal.
372 * The info is split for quick processing by spell_soundfold().
373 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
374typedef struct salitem_S
375{
376 char_u *sm_lead; /* leading letters */
377 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000378 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000379 char_u *sm_rules; /* rules like ^, $, priority */
380 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000381#ifdef FEAT_MBYTE
382 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000383 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000384 int *sm_to_w; /* wide character copy of "sm_to" */
385#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000386} salitem_T;
387
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000388#ifdef FEAT_MBYTE
389typedef int salfirst_T;
390#else
391typedef short salfirst_T;
392#endif
393
Bram Moolenaar5195e452005-08-19 20:32:47 +0000394/* Values for SP_*ERROR are negative, positive values are used by
395 * read_cnt_string(). */
396#define SP_TRUNCERROR -1 /* spell file truncated error */
397#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000398#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000399
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000400/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000401 * Structure used to store words and other info for one language, loaded from
402 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000403 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
404 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
405 *
406 * The "byts" array stores the possible bytes in each tree node, preceded by
407 * the number of possible bytes, sorted on byte value:
408 * <len> <byte1> <byte2> ...
409 * The "idxs" array stores the index of the child node corresponding to the
410 * byte in "byts".
411 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000412 * the flags, region mask and affixID for the word. There may be several
413 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414 */
415typedef struct slang_S slang_T;
416struct slang_S
417{
418 slang_T *sl_next; /* next language */
419 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000420 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000421 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000422
Bram Moolenaar51485f02005-06-04 21:55:20 +0000423 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000424 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000425 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000426 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000427 char_u *sl_pbyts; /* prefix tree word bytes */
428 idx_T *sl_pidxs; /* prefix tree word indexes */
429
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000430 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000431
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000432 char_u *sl_midword; /* MIDWORD string or NULL */
433
Bram Moolenaar4770d092006-01-12 23:22:24 +0000434 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
435
Bram Moolenaar5195e452005-08-19 20:32:47 +0000436 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000437 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000438 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
439 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
440 * (NULL when no compounding) */
441 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000442 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000443 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000444 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
445 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000446
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000447 int sl_prefixcnt; /* number of items in "sl_prefprog" */
448 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000450 garray_T sl_rep; /* list of fromto_T entries from REP lines */
451 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
452 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000453 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000454 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000455 there is none */
456 int sl_followup; /* SAL followup */
457 int sl_collapse; /* SAL collapse_result */
458 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000459 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
460 * "sl_sal_first" maps chars, when has_mbyte
461 * "sl_sal" is a list of wide char lists. */
462 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
463 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
464
465 /* Info from the .sug file. Loaded on demand. */
466 time_t sl_sugtime; /* timestamp for .sug file */
467 char_u *sl_sbyts; /* soundfolded word bytes */
468 idx_T *sl_sidxs; /* soundfolded word indexes */
469 buf_T *sl_sugbuf; /* buffer with word number table */
470 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
471 load */
472
Bram Moolenaarea424162005-06-16 21:51:00 +0000473 int sl_has_map; /* TRUE if there is a MAP line */
474#ifdef FEAT_MBYTE
475 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
476 int sl_map_array[256]; /* MAP for first 256 chars */
477#else
478 char_u sl_map_array[256]; /* MAP for first 256 chars */
479#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000480 hashtab_T sl_sounddone; /* table with soundfolded words that have
481 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000482};
483
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000484/* First language that is loaded, start of the linked list of loaded
485 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000486static slang_T *first_lang = NULL;
487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488/* Flags used in .spl file for soundsalike flags. */
489#define SAL_F0LLOWUP 1
490#define SAL_COLLAPSE 2
491#define SAL_REM_ACCENTS 4
492
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000493/*
494 * Structure used in "b_langp", filled from 'spelllang'.
495 */
496typedef struct langp_S
497{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000498 slang_T *lp_slang; /* info for this language */
499 slang_T *lp_sallang; /* language used for sound folding or NULL */
500 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000501 int lp_region; /* bitmask for region or REGION_ALL */
502} langp_T;
503
504#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
505
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000506#define REGION_ALL 0xff /* word valid in all regions */
507
Bram Moolenaar5195e452005-08-19 20:32:47 +0000508#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
509#define VIMSPELLMAGICL 8
510#define VIMSPELLVERSION 50
511
Bram Moolenaar4770d092006-01-12 23:22:24 +0000512#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
513#define VIMSUGMAGICL 6
514#define VIMSUGVERSION 1
515
Bram Moolenaar5195e452005-08-19 20:32:47 +0000516/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
517#define SN_REGION 0 /* <regionname> section */
518#define SN_CHARFLAGS 1 /* charflags section */
519#define SN_MIDWORD 2 /* <midword> section */
520#define SN_PREFCOND 3 /* <prefcond> section */
521#define SN_REP 4 /* REP items section */
522#define SN_SAL 5 /* SAL items section */
523#define SN_SOFO 6 /* soundfolding section */
524#define SN_MAP 7 /* MAP items section */
525#define SN_COMPOUND 8 /* compound words section */
526#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000527#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000528#define SN_SUGFILE 11 /* timestamp for .sug file */
529#define SN_REPSAL 12 /* REPSAL items section */
530#define SN_WORDS 13 /* common words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000531#define SN_END 255 /* end of sections */
532
533#define SNF_REQUIRED 1 /* <sectionflags>: required section */
534
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000535/* Result values. Lower number is accepted over higher one. */
536#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000538#define SP_RARE 1
539#define SP_LOCAL 2
540#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000541
Bram Moolenaar7887d882005-07-01 22:33:52 +0000542/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000543static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000544
Bram Moolenaar4770d092006-01-12 23:22:24 +0000545typedef struct wordcount_S
546{
547 short_u wc_count; /* nr of times word was seen */
548 char_u wc_word[1]; /* word, actually longer */
549} wordcount_T;
550
551static wordcount_T dumwc;
552#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
553#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
554#define MAXWORDCOUNT 0xffff
555
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000556/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000557 * Information used when looking for suggestions.
558 */
559typedef struct suginfo_S
560{
561 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000562 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000563 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000564 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000565 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000566 char_u *su_badptr; /* start of bad word in line */
567 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000568 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000569 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
570 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000571 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000572 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000573 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000574} suginfo_T;
575
576/* One word suggestion. Used in "si_ga". */
577typedef struct suggest_S
578{
579 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000580 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000581 int st_orglen; /* length of replaced text */
582 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000583 int st_altscore; /* used when st_score compares equal */
584 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000585 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000586 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587} suggest_T;
588
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000590
Bram Moolenaar4770d092006-01-12 23:22:24 +0000591/* TRUE if a word appears in the list of banned words. */
592#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
593
594/* Number of suggestions kept when cleaning up. we need to keep more than
595 * what is displayed, because when rescore_suggestions() is called the score
596 * may change and wrong suggestions may be removed later. */
597#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000598
599/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
600 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000601#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000602
603/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000604#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000606#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000610#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611#define SCORE_SUBST 93 /* substitute a character */
612#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000613#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000614#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000615#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000616#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000617#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000618#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000619#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000620#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000622#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000623#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
624 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625
Bram Moolenaar4770d092006-01-12 23:22:24 +0000626#define SCORE_COMMON1 30 /* subtracted for words seen before */
627#define SCORE_COMMON2 40 /* subtracted for words often seen */
628#define SCORE_COMMON3 50 /* subtracted for words very often seen */
629#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
630#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
631
632/* When trying changed soundfold words it becomes slow when trying more than
633 * two changes. With less then two changes it's slightly faster but we miss a
634 * few good suggestions. In rare cases we need to try three of four changes.
635 */
636#define SCORE_SFMAX1 200 /* maximum score for first try */
637#define SCORE_SFMAX2 300 /* maximum score for second try */
638#define SCORE_SFMAX3 400 /* maximum score for third try */
639
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000641#define SCORE_MAXMAX 999999 /* accept any score */
642#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
643
644/* for spell_edit_score_limit() we need to know the minimum value of
645 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
646#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647
648/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000649 * Structure to store info for word matching.
650 */
651typedef struct matchinf_S
652{
653 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000654
655 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000656 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000657 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000658 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000659 char_u *mi_cend; /* char after what was used for
660 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000661
662 /* case-folded text */
663 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000664 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000665
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000666 /* for when checking word after a prefix */
667 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000668 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000669 int mi_prefcnt; /* number of entries at mi_prefarridx */
670 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000671#ifdef FEAT_MBYTE
672 int mi_cprefixlen; /* byte length of prefix in original
673 case */
674#else
675# define mi_cprefixlen mi_prefixlen /* it's the same value */
676#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000677
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000678 /* for when checking a compound word */
679 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000680 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
681 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000682
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000683 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000684 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000685 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000686 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000687
688 /* for NOBREAK */
689 int mi_result2; /* "mi_resul" without following word */
690 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691} matchinf_T;
692
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000693/*
694 * The tables used for recognizing word characters according to spelling.
695 * These are only used for the first 256 characters of 'encoding'.
696 */
697typedef struct spelltab_S
698{
699 char_u st_isw[256]; /* flags: is word char */
700 char_u st_isu[256]; /* flags: is uppercase char */
701 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000702 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703} spelltab_T;
704
705static spelltab_T spelltab;
706static int did_set_spelltab;
707
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000708#define CF_WORD 0x01
709#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000710
711static void clear_spell_chartab __ARGS((spelltab_T *sp));
712static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000713static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
714static int spell_iswordp_nmw __ARGS((char_u *p));
715#ifdef FEAT_MBYTE
716static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
717#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000718static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000719
720/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000722 */
723typedef enum
724{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725 STATE_START = 0, /* At start of node check for NUL bytes (goodword
726 * ends); if badword ends there is a match, otherwise
727 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000728 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000729 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000730 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
731 STATE_PLAIN, /* Use each byte of the node. */
732 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000733 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000734 STATE_INS, /* Insert a byte in the bad word. */
735 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 STATE_UNSWAP, /* Undo swap two characters. */
737 STATE_SWAP3, /* Swap two characters over three. */
738 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000740 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000741 STATE_REP_INI, /* Prepare for using REP items. */
742 STATE_REP, /* Use matching REP items from the .aff file. */
743 STATE_REP_UNDO, /* Undo a REP item replacement. */
744 STATE_FINAL /* End of this node. */
745} state_T;
746
747/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000748 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000749 */
750typedef struct trystate_S
751{
Bram Moolenaarea424162005-06-16 21:51:00 +0000752 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000753 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000754 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000755 short ts_curi; /* index in list of child nodes */
756 char_u ts_fidx; /* index in fword[], case-folded bad word */
757 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
758 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000759 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000760 * PFD_PREFIXTREE or PFD_NOPREFIX */
761 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000762#ifdef FEAT_MBYTE
763 char_u ts_tcharlen; /* number of bytes in tword character */
764 char_u ts_tcharidx; /* current byte index in tword character */
765 char_u ts_isdiff; /* DIFF_ values */
766 char_u ts_fcharstart; /* index in fword where badword char started */
767#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000768 char_u ts_prewordlen; /* length of word in "preword[]" */
769 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000770 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000771 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000772 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000773 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000774 char_u ts_delidx; /* index in fword for char that was deleted,
775 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000776} trystate_T;
777
Bram Moolenaarea424162005-06-16 21:51:00 +0000778/* values for ts_isdiff */
779#define DIFF_NONE 0 /* no different byte (yet) */
780#define DIFF_YES 1 /* different byte found */
781#define DIFF_INSERT 2 /* inserting character */
782
Bram Moolenaard12a1322005-08-21 22:08:24 +0000783/* values for ts_flags */
784#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
785#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000786#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000787
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000788/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000789#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000790#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000791#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000792
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000793/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000794#define FIND_FOLDWORD 0 /* find word case-folded */
795#define FIND_KEEPWORD 1 /* find keep-case word */
796#define FIND_PREFIX 2 /* find word after prefix */
797#define FIND_COMPOUND 3 /* find case-folded compound word */
798#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000799
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000800static slang_T *slang_alloc __ARGS((char_u *lang));
801static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000802static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000803static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000804static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000805static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000806static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000807static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000808static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000809static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000810static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000811static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000812static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000813static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000814static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000815static 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 +0000816static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000817static char_u *read_string __ARGS((FILE *fd, int cnt));
818static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
819static int read_charflags_section __ARGS((FILE *fd));
820static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000821static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000822static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000823static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
824static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
825static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000826static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
827static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000828static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000829static int init_syl_tab __ARGS((slang_T *slang));
830static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000831static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
832static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000833#ifdef FEAT_MBYTE
834static int *mb_str2wide __ARGS((char_u *s));
835#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000836static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
837static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000838static void clear_midword __ARGS((buf_T *buf));
839static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840static int find_region __ARGS((char_u *rp, char_u *region));
841static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000842static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000843static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000845static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000846static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000847static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000848static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000849#ifdef FEAT_EVAL
850static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
851#endif
852static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000853static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
854static void suggest_load_files __ARGS((void));
855static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000856static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000857static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000858static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000859static void suggest_try_special __ARGS((suginfo_T *su));
860static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000861static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
862static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000863#ifdef FEAT_MBYTE
864static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
865#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000866static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000867static void score_comp_sal __ARGS((suginfo_T *su));
868static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000869static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000870static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000871static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000872static void suggest_try_soundalike_finish __ARGS((void));
873static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
874static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000875static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000876static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000877static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000878static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
879static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000880static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000881static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000882static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000883static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000884static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
885static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
886static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000887#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000888static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000889#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000890static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000891static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
892static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
893#ifdef FEAT_MBYTE
894static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
895#endif
896static void dump_word __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000897static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000898static buf_T *open_spellbuf __ARGS((void));
899static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000900
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901/*
902 * Use our own character-case definitions, because the current locale may
903 * differ from what the .spl file uses.
904 * These must not be called with negative number!
905 */
906#ifndef FEAT_MBYTE
907/* Non-multi-byte implementation. */
908# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
909# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
910# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
911#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000912# if defined(HAVE_WCHAR_H)
913# include <wchar.h> /* for towupper() and towlower() */
914# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000915/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
916 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
917 * the "w" library function for characters above 255 if available. */
918# ifdef HAVE_TOWLOWER
919# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
920 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
921# else
922# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
923 : (c) < 256 ? spelltab.st_fold[c] : (c))
924# endif
925
926# ifdef HAVE_TOWUPPER
927# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
928 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
929# else
930# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
931 : (c) < 256 ? spelltab.st_upper[c] : (c))
932# endif
933
934# ifdef HAVE_ISWUPPER
935# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
936 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
937# else
938# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000939 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000940# endif
941#endif
942
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000943
944static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000945static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000946static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000947static char *e_affname = N_("Affix name too long in %s line %d: %s");
948static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
949static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000950static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000951
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000952/* Remember what "z?" replaced. */
953static char_u *repl_from = NULL;
954static char_u *repl_to = NULL;
955
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000956/*
957 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000959 * "*attrp" is set to the highlight index for a badly spelled word. For a
960 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000961 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000962 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000963 * "capcol" is used to check for a Capitalised word after the end of a
964 * sentence. If it's zero then perform the check. Return the column where to
965 * check next, or -1 when no sentence end was found. If it's NULL then don't
966 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000967 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000968 * Returns the length of the word in bytes, also when it's OK, so that the
969 * caller can skip over the word.
970 */
971 int
Bram Moolenaar4770d092006-01-12 23:22:24 +0000972spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000973 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000974 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000975 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000976 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000977 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000978{
979 matchinf_T mi; /* Most things are put in "mi" so that it can
980 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000981 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000982 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000983 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000984 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000985 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000986
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000987 /* A word never starts at a space or a control character. Return quickly
988 * then, skipping over the character. */
989 if (*ptr <= ' ')
990 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000991
992 /* Return here when loading language files failed. */
993 if (wp->w_buffer->b_langp.ga_len == 0)
994 return 1;
995
Bram Moolenaar5195e452005-08-19 20:32:47 +0000996 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000998 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000999 * 0X99FF. But always do check spelling to find "3GPP" and "11
1000 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001002 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001003 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1004 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001005 else
1006 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +00001007 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001008 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001009
Bram Moolenaar0c405862005-06-22 22:26:26 +00001010 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001011 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001012 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001013 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001014 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001015 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001016 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001017 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001018 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001019
1020 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1021 {
1022 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001023 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001024 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001025 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001026 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001027 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001028 if (capcol != NULL)
1029 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001030
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001031 /* We always use the characters up to the next non-word character,
1032 * also for bad words. */
1033 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001034
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001035 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001036 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037
Bram Moolenaar5195e452005-08-19 20:32:47 +00001038 /* case-fold the word with one non-word character, so that we can check
1039 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001040 if (*mi.mi_fend != NUL)
1041 mb_ptr_adv(mi.mi_fend);
1042
1043 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1044 MAXWLEN + 1);
1045 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1046
1047 /* The word is bad unless we recognize it. */
1048 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001049 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001050
1051 /*
1052 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001053 * We check them all, because a word may be matched longer in another
1054 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001056 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001057 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001058 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1059
1060 /* If reloading fails the language is still in the list but everything
1061 * has been cleared. */
1062 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1063 continue;
1064
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001065 /* Check for a matching word in case-folded words. */
1066 find_word(&mi, FIND_FOLDWORD);
1067
1068 /* Check for a matching word in keep-case words. */
1069 find_word(&mi, FIND_KEEPWORD);
1070
1071 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001072 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001073
1074 /* For a NOBREAK language, may want to use a word without a following
1075 * word as a backup. */
1076 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1077 && mi.mi_result2 != SP_BAD)
1078 {
1079 mi.mi_result = mi.mi_result2;
1080 mi.mi_end = mi.mi_end2;
1081 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001082
1083 /* Count the word in the first language where it's found to be OK. */
1084 if (count_word && mi.mi_result == SP_OK)
1085 {
1086 count_common_word(mi.mi_lp->lp_slang, ptr,
1087 (int)(mi.mi_end - ptr), 1);
1088 count_word = FALSE;
1089 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090 }
1091
1092 if (mi.mi_result != SP_OK)
1093 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001094 /* If we found a number skip over it. Allows for "42nd". Do flag
1095 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001096 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001097 {
1098 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1099 return nrlen;
1100 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101
1102 /* When we are at a non-word character there is no error, just
1103 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001104 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001105 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001106 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1107 {
1108 regmatch_T regmatch;
1109
1110 /* Check for end of sentence. */
1111 regmatch.regprog = wp->w_buffer->b_cap_prog;
1112 regmatch.rm_ic = FALSE;
1113 if (vim_regexec(&regmatch, ptr, 0))
1114 *capcol = (int)(regmatch.endp[0] - ptr);
1115 }
1116
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001117#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001118 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001119 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001120#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001121 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001122 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001123 else if (mi.mi_end == ptr)
1124 /* Always include at least one character. Required for when there
1125 * is a mixup in "midword". */
1126 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001127 else if (mi.mi_result == SP_BAD
1128 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1129 {
1130 char_u *p, *fp;
1131 int save_result = mi.mi_result;
1132
1133 /* First language in 'spelllang' is NOBREAK. Find first position
1134 * at which any word would be valid. */
1135 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001136 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001137 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001138 p = mi.mi_word;
1139 fp = mi.mi_fword;
1140 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001141 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001142 mb_ptr_adv(p);
1143 mb_ptr_adv(fp);
1144 if (p >= mi.mi_end)
1145 break;
1146 mi.mi_compoff = fp - mi.mi_fword;
1147 find_word(&mi, FIND_COMPOUND);
1148 if (mi.mi_result != SP_BAD)
1149 {
1150 mi.mi_end = p;
1151 break;
1152 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001153 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001154 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001155 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001156 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001157
1158 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001159 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001160 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001161 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001163 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164 }
1165
Bram Moolenaar5195e452005-08-19 20:32:47 +00001166 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1167 {
1168 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001169 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001170 return wrongcaplen;
1171 }
1172
Bram Moolenaar51485f02005-06-04 21:55:20 +00001173 return (int)(mi.mi_end - ptr);
1174}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001175
Bram Moolenaar51485f02005-06-04 21:55:20 +00001176/*
1177 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001178 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1179 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1180 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1181 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001182 *
1183 * For a match mip->mi_result is updated.
1184 */
1185 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001186find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001187 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001188 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001189{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001190 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001191 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001192 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001193 int endidxcnt = 0;
1194 int len;
1195 int wlen = 0;
1196 int flen;
1197 int c;
1198 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001199 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001200#ifdef FEAT_MBYTE
1201 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001202#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001203 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001204 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001205 slang_T *slang = mip->mi_lp->lp_slang;
1206 unsigned flags;
1207 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001208 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001209 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001210 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001211 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001212
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001213 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001214 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001215 /* Check for word with matching case in keep-case tree. */
1216 ptr = mip->mi_word;
1217 flen = 9999; /* no case folding, always enough bytes */
1218 byts = slang->sl_kbyts;
1219 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001220
1221 if (mode == FIND_KEEPCOMPOUND)
1222 /* Skip over the previously found word(s). */
1223 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 }
1225 else
1226 {
1227 /* Check for case-folded in case-folded tree. */
1228 ptr = mip->mi_fword;
1229 flen = mip->mi_fwordlen; /* available case-folded bytes */
1230 byts = slang->sl_fbyts;
1231 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232
1233 if (mode == FIND_PREFIX)
1234 {
1235 /* Skip over the prefix. */
1236 wlen = mip->mi_prefixlen;
1237 flen -= mip->mi_prefixlen;
1238 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001239 else if (mode == FIND_COMPOUND)
1240 {
1241 /* Skip over the previously found word(s). */
1242 wlen = mip->mi_compoff;
1243 flen -= mip->mi_compoff;
1244 }
1245
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001246 }
1247
Bram Moolenaar51485f02005-06-04 21:55:20 +00001248 if (byts == NULL)
1249 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001250
Bram Moolenaar51485f02005-06-04 21:55:20 +00001251 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001252 * Repeat advancing in the tree until:
1253 * - there is a byte that doesn't match,
1254 * - we reach the end of the tree,
1255 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256 */
1257 for (;;)
1258 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001259 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001260 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001261
1262 len = byts[arridx++];
1263
1264 /* If the first possible byte is a zero the word could end here.
1265 * Remember this index, we first check for the longest word. */
1266 if (byts[arridx] == 0)
1267 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001268 if (endidxcnt == MAXWLEN)
1269 {
1270 /* Must be a corrupted spell file. */
1271 EMSG(_(e_format));
1272 return;
1273 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001274 endlen[endidxcnt] = wlen;
1275 endidx[endidxcnt++] = arridx++;
1276 --len;
1277
1278 /* Skip over the zeros, there can be several flag/region
1279 * combinations. */
1280 while (len > 0 && byts[arridx] == 0)
1281 {
1282 ++arridx;
1283 --len;
1284 }
1285 if (len == 0)
1286 break; /* no children, word must end here */
1287 }
1288
1289 /* Stop looking at end of the line. */
1290 if (ptr[wlen] == NUL)
1291 break;
1292
1293 /* Perform a binary search in the list of accepted bytes. */
1294 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001295 if (c == TAB) /* <Tab> is handled like <Space> */
1296 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001297 lo = arridx;
1298 hi = arridx + len - 1;
1299 while (lo < hi)
1300 {
1301 m = (lo + hi) / 2;
1302 if (byts[m] > c)
1303 hi = m - 1;
1304 else if (byts[m] < c)
1305 lo = m + 1;
1306 else
1307 {
1308 lo = hi = m;
1309 break;
1310 }
1311 }
1312
1313 /* Stop if there is no matching byte. */
1314 if (hi < lo || byts[lo] != c)
1315 break;
1316
1317 /* Continue at the child (if there is one). */
1318 arridx = idxs[lo];
1319 ++wlen;
1320 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001321
1322 /* One space in the good word may stand for several spaces in the
1323 * checked word. */
1324 if (c == ' ')
1325 {
1326 for (;;)
1327 {
1328 if (flen <= 0 && *mip->mi_fend != NUL)
1329 flen = fold_more(mip);
1330 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1331 break;
1332 ++wlen;
1333 --flen;
1334 }
1335 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001336 }
1337
1338 /*
1339 * Verify that one of the possible endings is valid. Try the longest
1340 * first.
1341 */
1342 while (endidxcnt > 0)
1343 {
1344 --endidxcnt;
1345 arridx = endidx[endidxcnt];
1346 wlen = endlen[endidxcnt];
1347
1348#ifdef FEAT_MBYTE
1349 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1350 continue; /* not at first byte of character */
1351#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001352 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001353 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001354 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001355 continue; /* next char is a word character */
1356 word_ends = FALSE;
1357 }
1358 else
1359 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001360 /* The prefix flag is before compound flags. Once a valid prefix flag
1361 * has been found we try compound flags. */
1362 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001363
1364#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001365 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001366 {
1367 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001368 * when folding case. This can be slow, take a shortcut when the
1369 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001370 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001371 if (STRNCMP(ptr, p, wlen) != 0)
1372 {
1373 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1374 mb_ptr_adv(p);
1375 wlen = p - mip->mi_word;
1376 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377 }
1378#endif
1379
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001380 /* Check flags and region. For FIND_PREFIX check the condition and
1381 * prefix ID.
1382 * Repeat this if there are more flags/region alternatives until there
1383 * is a match. */
1384 res = SP_BAD;
1385 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1386 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001387 {
1388 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001389
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001390 /* For the fold-case tree check that the case of the checked word
1391 * matches with what the word in the tree requires.
1392 * For keep-case tree the case is always right. For prefixes we
1393 * don't bother to check. */
1394 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001395 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001396 if (mip->mi_cend != mip->mi_word + wlen)
1397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001398 /* mi_capflags was set for a different word length, need
1399 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001400 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001401 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001402 }
1403
Bram Moolenaar0c405862005-06-22 22:26:26 +00001404 if (mip->mi_capflags == WF_KEEPCAP
1405 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001406 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407 }
1408
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 /* When mode is FIND_PREFIX the word must support the prefix:
1410 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001411 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001412 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001413 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001414 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001415 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001416 mip->mi_word + mip->mi_cprefixlen, slang,
1417 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001418 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001419 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001420
1421 /* Use the WF_RARE flag for a rare prefix. */
1422 if (c & WF_RAREPFX)
1423 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001424 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001425 }
1426
Bram Moolenaar78622822005-08-23 21:00:13 +00001427 if (slang->sl_nobreak)
1428 {
1429 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1430 && (flags & WF_BANNED) == 0)
1431 {
1432 /* NOBREAK: found a valid following word. That's all we
1433 * need to know, so return. */
1434 mip->mi_result = SP_OK;
1435 break;
1436 }
1437 }
1438
1439 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1440 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001441 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001442 /* If there is no flag or the word is shorter than
1443 * COMPOUNDMIN reject it quickly.
1444 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001445 * that's too short... Myspell compatibility requires this
1446 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001447 if (((unsigned)flags >> 24) == 0
1448 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001449 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001450#ifdef FEAT_MBYTE
1451 /* For multi-byte chars check character length against
1452 * COMPOUNDMIN. */
1453 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001454 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001455 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1456 wlen - mip->mi_compoff) < slang->sl_compminlen)
1457 continue;
1458#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001459
Bram Moolenaare52325c2005-08-22 22:54:29 +00001460 /* Limit the number of compound words to COMPOUNDMAX if no
1461 * maximum for syllables is specified. */
1462 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1463 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001464 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001465
Bram Moolenaard12a1322005-08-21 22:08:24 +00001466 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001467 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001468 ? slang->sl_compstartflags
1469 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001470 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001471 continue;
1472
Bram Moolenaare52325c2005-08-22 22:54:29 +00001473 if (mode == FIND_COMPOUND)
1474 {
1475 int capflags;
1476
1477 /* Need to check the caps type of the appended compound
1478 * word. */
1479#ifdef FEAT_MBYTE
1480 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1481 mip->mi_compoff) != 0)
1482 {
1483 /* case folding may have changed the length */
1484 p = mip->mi_word;
1485 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1486 mb_ptr_adv(p);
1487 }
1488 else
1489#endif
1490 p = mip->mi_word + mip->mi_compoff;
1491 capflags = captype(p, mip->mi_word + wlen);
1492 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1493 && (flags & WF_FIXCAP) != 0))
1494 continue;
1495
1496 if (capflags != WF_ALLCAP)
1497 {
1498 /* When the character before the word is a word
1499 * character we do not accept a Onecap word. We do
1500 * accept a no-caps word, even when the dictionary
1501 * word specifies ONECAP. */
1502 mb_ptr_back(mip->mi_word, p);
1503 if (spell_iswordp_nmw(p)
1504 ? capflags == WF_ONECAP
1505 : (flags & WF_ONECAP) != 0
1506 && capflags != WF_ONECAP)
1507 continue;
1508 }
1509 }
1510
Bram Moolenaar5195e452005-08-19 20:32:47 +00001511 /* If the word ends the sequence of compound flags of the
1512 * words must match with one of the COMPOUNDFLAGS items and
1513 * the number of syllables must not be too large. */
1514 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1515 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1516 if (word_ends)
1517 {
1518 char_u fword[MAXWLEN];
1519
1520 if (slang->sl_compsylmax < MAXWLEN)
1521 {
1522 /* "fword" is only needed for checking syllables. */
1523 if (ptr == mip->mi_word)
1524 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1525 else
1526 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1527 }
1528 if (!can_compound(slang, fword, mip->mi_compflags))
1529 continue;
1530 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001531 }
1532
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001533 /* Check NEEDCOMPOUND: can't use word without compounding. */
1534 else if (flags & WF_NEEDCOMP)
1535 continue;
1536
Bram Moolenaar78622822005-08-23 21:00:13 +00001537 nobreak_result = SP_OK;
1538
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001539 if (!word_ends)
1540 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001541 int save_result = mip->mi_result;
1542 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001543 langp_T *save_lp = mip->mi_lp;
1544 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001545
1546 /* Check that a valid word follows. If there is one and we
1547 * are compounding, it will set "mi_result", thus we are
1548 * always finished here. For NOBREAK we only check that a
1549 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001550 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001551 if (slang->sl_nobreak)
1552 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001553
1554 /* Find following word in case-folded tree. */
1555 mip->mi_compoff = endlen[endidxcnt];
1556#ifdef FEAT_MBYTE
1557 if (has_mbyte && mode == FIND_KEEPWORD)
1558 {
1559 /* Compute byte length in case-folded word from "wlen":
1560 * byte length in keep-case word. Length may change when
1561 * folding case. This can be slow, take a shortcut when
1562 * the case-folded word is equal to the keep-case word. */
1563 p = mip->mi_fword;
1564 if (STRNCMP(ptr, p, wlen) != 0)
1565 {
1566 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1567 mb_ptr_adv(p);
1568 mip->mi_compoff = p - mip->mi_fword;
1569 }
1570 }
1571#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001572 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001573 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001574
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001575 /* For NOBREAK we need to try all NOBREAK languages, at least
1576 * to find the ".add" file(s). */
1577 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001578 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001579 if (slang->sl_nobreak)
1580 {
1581 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1582 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1583 || !mip->mi_lp->lp_slang->sl_nobreak)
1584 continue;
1585 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001586
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001587 find_word(mip, FIND_COMPOUND);
1588
1589 /* When NOBREAK any word that matches is OK. Otherwise we
1590 * need to find the longest match, thus try with keep-case
1591 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001592 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1593 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001594 /* Find following word in keep-case tree. */
1595 mip->mi_compoff = wlen;
1596 find_word(mip, FIND_KEEPCOMPOUND);
1597
1598 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1599 {
1600 /* Check for following word with prefix. */
1601 mip->mi_compoff = c;
1602 find_prefix(mip, FIND_COMPOUND);
1603 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001604 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001605
1606 if (!slang->sl_nobreak)
1607 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001608 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001609 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001610 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001611
Bram Moolenaar78622822005-08-23 21:00:13 +00001612 if (slang->sl_nobreak)
1613 {
1614 nobreak_result = mip->mi_result;
1615 mip->mi_result = save_result;
1616 mip->mi_end = save_end;
1617 }
1618 else
1619 {
1620 if (mip->mi_result == SP_OK)
1621 break;
1622 continue;
1623 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001624 }
1625
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001626 if (flags & WF_BANNED)
1627 res = SP_BANNED;
1628 else if (flags & WF_REGION)
1629 {
1630 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001631 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001632 res = SP_OK;
1633 else
1634 res = SP_LOCAL;
1635 }
1636 else if (flags & WF_RARE)
1637 res = SP_RARE;
1638 else
1639 res = SP_OK;
1640
Bram Moolenaar78622822005-08-23 21:00:13 +00001641 /* Always use the longest match and the best result. For NOBREAK
1642 * we separately keep the longest match without a following good
1643 * word as a fall-back. */
1644 if (nobreak_result == SP_BAD)
1645 {
1646 if (mip->mi_result2 > res)
1647 {
1648 mip->mi_result2 = res;
1649 mip->mi_end2 = mip->mi_word + wlen;
1650 }
1651 else if (mip->mi_result2 == res
1652 && mip->mi_end2 < mip->mi_word + wlen)
1653 mip->mi_end2 = mip->mi_word + wlen;
1654 }
1655 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001656 {
1657 mip->mi_result = res;
1658 mip->mi_end = mip->mi_word + wlen;
1659 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001660 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001661 mip->mi_end = mip->mi_word + wlen;
1662
Bram Moolenaar78622822005-08-23 21:00:13 +00001663 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001664 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001665 }
1666
Bram Moolenaar78622822005-08-23 21:00:13 +00001667 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001668 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001669 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001670}
1671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001672/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001673 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1674 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001675 */
1676 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001677can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001678 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001679 char_u *word;
1680 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001681{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001682 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001683#ifdef FEAT_MBYTE
1684 char_u uflags[MAXWLEN * 2];
1685 int i;
1686#endif
1687 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001688
1689 if (slang->sl_compprog == NULL)
1690 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001691#ifdef FEAT_MBYTE
1692 if (enc_utf8)
1693 {
1694 /* Need to convert the single byte flags to utf8 characters. */
1695 p = uflags;
1696 for (i = 0; flags[i] != NUL; ++i)
1697 p += mb_char2bytes(flags[i], p);
1698 *p = NUL;
1699 p = uflags;
1700 }
1701 else
1702#endif
1703 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001704 regmatch.regprog = slang->sl_compprog;
1705 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001706 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001707 return FALSE;
1708
Bram Moolenaare52325c2005-08-22 22:54:29 +00001709 /* Count the number of syllables. This may be slow, do it last. If there
1710 * are too many syllables AND the number of compound words is above
1711 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001712 if (slang->sl_compsylmax < MAXWLEN
1713 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001714 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001715 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001716}
1717
1718/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001719 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1720 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001721 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001722 */
1723 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001724valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001725 int totprefcnt; /* nr of prefix IDs */
1726 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001727 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001728 char_u *word;
1729 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001730 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001731{
1732 int prefcnt;
1733 int pidx;
1734 regprog_T *rp;
1735 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001736 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001737
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001738 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001739 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1740 {
1741 pidx = slang->sl_pidxs[arridx + prefcnt];
1742
1743 /* Check the prefix ID. */
1744 if (prefid != (pidx & 0xff))
1745 continue;
1746
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001747 /* Check if the prefix doesn't combine and the word already has a
1748 * suffix. */
1749 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1750 continue;
1751
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001752 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001753 * stored in the two bytes above the prefix ID byte. */
1754 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001755 if (rp != NULL)
1756 {
1757 regmatch.regprog = rp;
1758 regmatch.rm_ic = FALSE;
1759 if (!vim_regexec(&regmatch, word, 0))
1760 continue;
1761 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001762 else if (cond_req)
1763 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001764
Bram Moolenaar53805d12005-08-01 07:08:33 +00001765 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001766 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001767 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001768 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001769}
1770
1771/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001772 * Check if the word at "mip->mi_word" has a matching prefix.
1773 * If it does, then check the following word.
1774 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001775 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1776 * prefix in a compound word.
1777 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001778 * For a match mip->mi_result is updated.
1779 */
1780 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001781find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001782 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001783 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001784{
1785 idx_T arridx = 0;
1786 int len;
1787 int wlen = 0;
1788 int flen;
1789 int c;
1790 char_u *ptr;
1791 idx_T lo, hi, m;
1792 slang_T *slang = mip->mi_lp->lp_slang;
1793 char_u *byts;
1794 idx_T *idxs;
1795
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001796 byts = slang->sl_pbyts;
1797 if (byts == NULL)
1798 return; /* array is empty */
1799
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001800 /* We use the case-folded word here, since prefixes are always
1801 * case-folded. */
1802 ptr = mip->mi_fword;
1803 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001804 if (mode == FIND_COMPOUND)
1805 {
1806 /* Skip over the previously found word(s). */
1807 ptr += mip->mi_compoff;
1808 flen -= mip->mi_compoff;
1809 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001810 idxs = slang->sl_pidxs;
1811
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001812 /*
1813 * Repeat advancing in the tree until:
1814 * - there is a byte that doesn't match,
1815 * - we reach the end of the tree,
1816 * - or we reach the end of the line.
1817 */
1818 for (;;)
1819 {
1820 if (flen == 0 && *mip->mi_fend != NUL)
1821 flen = fold_more(mip);
1822
1823 len = byts[arridx++];
1824
1825 /* If the first possible byte is a zero the prefix could end here.
1826 * Check if the following word matches and supports the prefix. */
1827 if (byts[arridx] == 0)
1828 {
1829 /* There can be several prefixes with different conditions. We
1830 * try them all, since we don't know which one will give the
1831 * longest match. The word is the same each time, pass the list
1832 * of possible prefixes to find_word(). */
1833 mip->mi_prefarridx = arridx;
1834 mip->mi_prefcnt = len;
1835 while (len > 0 && byts[arridx] == 0)
1836 {
1837 ++arridx;
1838 --len;
1839 }
1840 mip->mi_prefcnt -= len;
1841
1842 /* Find the word that comes after the prefix. */
1843 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001844 if (mode == FIND_COMPOUND)
1845 /* Skip over the previously found word(s). */
1846 mip->mi_prefixlen += mip->mi_compoff;
1847
Bram Moolenaar53805d12005-08-01 07:08:33 +00001848#ifdef FEAT_MBYTE
1849 if (has_mbyte)
1850 {
1851 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001852 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1853 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001854 }
1855 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001856 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001857#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001858 find_word(mip, FIND_PREFIX);
1859
1860
1861 if (len == 0)
1862 break; /* no children, word must end here */
1863 }
1864
1865 /* Stop looking at end of the line. */
1866 if (ptr[wlen] == NUL)
1867 break;
1868
1869 /* Perform a binary search in the list of accepted bytes. */
1870 c = ptr[wlen];
1871 lo = arridx;
1872 hi = arridx + len - 1;
1873 while (lo < hi)
1874 {
1875 m = (lo + hi) / 2;
1876 if (byts[m] > c)
1877 hi = m - 1;
1878 else if (byts[m] < c)
1879 lo = m + 1;
1880 else
1881 {
1882 lo = hi = m;
1883 break;
1884 }
1885 }
1886
1887 /* Stop if there is no matching byte. */
1888 if (hi < lo || byts[lo] != c)
1889 break;
1890
1891 /* Continue at the child (if there is one). */
1892 arridx = idxs[lo];
1893 ++wlen;
1894 --flen;
1895 }
1896}
1897
1898/*
1899 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001900 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001901 * Return the length of the folded chars in bytes.
1902 */
1903 static int
1904fold_more(mip)
1905 matchinf_T *mip;
1906{
1907 int flen;
1908 char_u *p;
1909
1910 p = mip->mi_fend;
1911 do
1912 {
1913 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001914 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001915
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001916 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001917 if (*mip->mi_fend != NUL)
1918 mb_ptr_adv(mip->mi_fend);
1919
1920 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1921 mip->mi_fword + mip->mi_fwordlen,
1922 MAXWLEN - mip->mi_fwordlen);
1923 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1924 mip->mi_fwordlen += flen;
1925 return flen;
1926}
1927
1928/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001929 * Check case flags for a word. Return TRUE if the word has the requested
1930 * case.
1931 */
1932 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001933spell_valid_case(wordflags, treeflags)
1934 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001935 int treeflags; /* flags for the word in the spell tree */
1936{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001937 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001938 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001939 && ((treeflags & WF_ONECAP) == 0
1940 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001941}
1942
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001943/*
1944 * Return TRUE if spell checking is not enabled.
1945 */
1946 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001947no_spell_checking(wp)
1948 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001949{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001950 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
1951 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001952 {
1953 EMSG(_("E756: Spell checking is not enabled"));
1954 return TRUE;
1955 }
1956 return FALSE;
1957}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001958
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001959/*
1960 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001961 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1962 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001963 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1964 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001965 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001966 */
1967 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001968spell_move_to(wp, dir, allwords, curline, attrp)
1969 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001971 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001972 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001973 hlf_T *attrp; /* return: attributes of bad word or NULL
1974 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001975{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001976 linenr_T lnum;
1977 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001978 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001979 char_u *line;
1980 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001981 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001982 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001983 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001984 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001985 int col;
1986 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001987 char_u *buf = NULL;
1988 int buflen = 0;
1989 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001990 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001991 int found_one = FALSE;
1992 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001993
Bram Moolenaar95529562005-08-25 21:21:38 +00001994 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001995 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001996
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001997 /*
1998 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001999 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002000 *
2001 * When searching backwards, we continue in the line to find the last
2002 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002003 *
2004 * We concatenate the start of the next line, so that wrapped words work
2005 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2006 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002007 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002008 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002009 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002010
2011 while (!got_int)
2012 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002013 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002014
Bram Moolenaar0c405862005-06-22 22:26:26 +00002015 len = STRLEN(line);
2016 if (buflen < len + MAXWLEN + 2)
2017 {
2018 vim_free(buf);
2019 buflen = len + MAXWLEN + 2;
2020 buf = alloc(buflen);
2021 if (buf == NULL)
2022 break;
2023 }
2024
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002025 /* In first line check first word for Capital. */
2026 if (lnum == 1)
2027 capcol = 0;
2028
2029 /* For checking first word with a capital skip white space. */
2030 if (capcol == 0)
2031 capcol = skipwhite(line) - line;
2032
Bram Moolenaar0c405862005-06-22 22:26:26 +00002033 /* Copy the line into "buf" and append the start of the next line if
2034 * possible. */
2035 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002036 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002037 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2038
2039 p = buf + skip;
2040 endp = buf + len;
2041 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002042 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002043 /* When searching backward don't search after the cursor. Unless
2044 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002045 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002046 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002047 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002048 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002049 break;
2050
2051 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002052 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002053 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002054
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002055 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002056 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002057 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002058 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002059 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002060 found_one = TRUE;
2061
Bram Moolenaar51485f02005-06-04 21:55:20 +00002062 /* When searching forward only accept a bad word after
2063 * the cursor. */
2064 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002065 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002066 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002067 && (wrapped
2068 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002069 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002070 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002071 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002072 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002073 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002074 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002075 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002076 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002077 }
2078 else
2079 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002080
Bram Moolenaar51485f02005-06-04 21:55:20 +00002081 if (can_spell)
2082 {
2083 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002084 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002085#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002086 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002087#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002088 if (dir == FORWARD)
2089 {
2090 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002091 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002092 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002093 if (attrp != NULL)
2094 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002095 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002096 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002097 else if (curline)
2098 /* Insert mode completion: put cursor after
2099 * the bad word. */
2100 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002101 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002102 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002104 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002105 }
2106
Bram Moolenaar51485f02005-06-04 21:55:20 +00002107 /* advance to character after the word */
2108 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002109 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002110 }
2111
Bram Moolenaar5195e452005-08-19 20:32:47 +00002112 if (dir == BACKWARD && found_pos.lnum != 0)
2113 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002114 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002115 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002116 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002117 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002118 }
2119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002121 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002122
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002123 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002124 if (dir == BACKWARD)
2125 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002126 /* If we are back at the starting line and searched it again there
2127 * is no match, give up. */
2128 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002129 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002130
2131 if (lnum > 1)
2132 --lnum;
2133 else if (!p_ws)
2134 break; /* at first line and 'nowrapscan' */
2135 else
2136 {
2137 /* Wrap around to the end of the buffer. May search the
2138 * starting line again and accept the last match. */
2139 lnum = wp->w_buffer->b_ml.ml_line_count;
2140 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002141 if (!shortmess(SHM_SEARCH))
2142 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002143 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002144 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002145 }
2146 else
2147 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002148 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2149 ++lnum;
2150 else if (!p_ws)
2151 break; /* at first line and 'nowrapscan' */
2152 else
2153 {
2154 /* Wrap around to the start of the buffer. May search the
2155 * starting line again and accept the first match. */
2156 lnum = 1;
2157 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002158 if (!shortmess(SHM_SEARCH))
2159 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002160 }
2161
2162 /* If we are back at the starting line and there is no match then
2163 * give up. */
2164 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002165 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002166
2167 /* Skip the characters at the start of the next line that were
2168 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002169 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002170 skip = p - endp;
2171 else
2172 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002173
2174 /* Capscol skips over the inserted space. */
2175 --capcol;
2176
2177 /* But after empty line check first word in next line */
2178 if (*skipwhite(line) == NUL)
2179 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002180 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002181
2182 line_breakcheck();
2183 }
2184
Bram Moolenaar0c405862005-06-22 22:26:26 +00002185 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002186 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002187}
2188
2189/*
2190 * For spell checking: concatenate the start of the following line "line" into
2191 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2192 */
2193 void
2194spell_cat_line(buf, line, maxlen)
2195 char_u *buf;
2196 char_u *line;
2197 int maxlen;
2198{
2199 char_u *p;
2200 int n;
2201
2202 p = skipwhite(line);
2203 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2204 p = skipwhite(p + 1);
2205
2206 if (*p != NUL)
2207 {
2208 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002209 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002210 n = p - line;
2211 if (n >= maxlen)
2212 n = maxlen - 1;
2213 vim_memset(buf + 1, ' ', n);
2214 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002215}
2216
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002217/*
2218 * Structure used for the cookie argument of do_in_runtimepath().
2219 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002220typedef struct spelload_S
2221{
2222 char_u sl_lang[MAXWLEN + 1]; /* language name */
2223 slang_T *sl_slang; /* resulting slang_T struct */
2224 int sl_nobreak; /* NOBREAK language found */
2225} spelload_T;
2226
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002227/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002228 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002229 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002230 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002231 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002232spell_load_lang(lang)
2233 char_u *lang;
2234{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002235 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002236 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002237 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002238#ifdef FEAT_AUTOCMD
2239 int round;
2240#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002241
Bram Moolenaarb765d632005-06-07 21:00:02 +00002242 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002243 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002244 STRCPY(sl.sl_lang, lang);
2245 sl.sl_slang = NULL;
2246 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002247
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002248#ifdef FEAT_AUTOCMD
2249 /* We may retry when no spell file is found for the language, an
2250 * autocommand may load it then. */
2251 for (round = 1; round <= 2; ++round)
2252#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002253 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002254 /*
2255 * Find the first spell file for "lang" in 'runtimepath' and load it.
2256 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002257 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002258 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002259 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002260
2261 if (r == FAIL && *sl.sl_lang != NUL)
2262 {
2263 /* Try loading the ASCII version. */
2264 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2265 "spell/%s.ascii.spl", lang);
2266 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2267
2268#ifdef FEAT_AUTOCMD
2269 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2270 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2271 curbuf->b_fname, FALSE, curbuf))
2272 continue;
2273 break;
2274#endif
2275 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002276 }
2277
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002278 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002279 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002280 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2281 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002282 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002283 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002284 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002285 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002286 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002287 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002288 }
2289}
2290
2291/*
2292 * Return the encoding used for spell checking: Use 'encoding', except that we
2293 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2294 */
2295 static char_u *
2296spell_enc()
2297{
2298
2299#ifdef FEAT_MBYTE
2300 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2301 return p_enc;
2302#endif
2303 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002304}
2305
2306/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002307 * Get the name of the .spl file for the internal wordlist into
2308 * "fname[MAXPATHL]".
2309 */
2310 static void
2311int_wordlist_spl(fname)
2312 char_u *fname;
2313{
2314 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2315 int_wordlist, spell_enc());
2316}
2317
2318/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002319 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 * Caller must fill "sl_next".
2321 */
2322 static slang_T *
2323slang_alloc(lang)
2324 char_u *lang;
2325{
2326 slang_T *lp;
2327
Bram Moolenaar51485f02005-06-04 21:55:20 +00002328 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002329 if (lp != NULL)
2330 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002331 if (lang != NULL)
2332 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002334 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002335 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002336 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002337 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002338 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002339
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002340 return lp;
2341}
2342
2343/*
2344 * Free the contents of an slang_T and the structure itself.
2345 */
2346 static void
2347slang_free(lp)
2348 slang_T *lp;
2349{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002350 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002351 vim_free(lp->sl_fname);
2352 slang_clear(lp);
2353 vim_free(lp);
2354}
2355
2356/*
2357 * Clear an slang_T so that the file can be reloaded.
2358 */
2359 static void
2360slang_clear(lp)
2361 slang_T *lp;
2362{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002363 garray_T *gap;
2364 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002365 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002366 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002367 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368
Bram Moolenaar51485f02005-06-04 21:55:20 +00002369 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002370 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002371 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002372 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002373 vim_free(lp->sl_pbyts);
2374 lp->sl_pbyts = NULL;
2375
Bram Moolenaar51485f02005-06-04 21:55:20 +00002376 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002377 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002378 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002379 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002380 vim_free(lp->sl_pidxs);
2381 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382
Bram Moolenaar4770d092006-01-12 23:22:24 +00002383 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002385 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2386 while (gap->ga_len > 0)
2387 {
2388 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2389 vim_free(ftp->ft_from);
2390 vim_free(ftp->ft_to);
2391 }
2392 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002394
2395 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002396 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002397 {
2398 /* "ga_len" is set to 1 without adding an item for latin1 */
2399 if (gap->ga_data != NULL)
2400 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2401 for (i = 0; i < gap->ga_len; ++i)
2402 vim_free(((int **)gap->ga_data)[i]);
2403 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002404 else
2405 /* SAL items: free salitem_T items */
2406 while (gap->ga_len > 0)
2407 {
2408 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2409 vim_free(smp->sm_lead);
2410 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2411 vim_free(smp->sm_to);
2412#ifdef FEAT_MBYTE
2413 vim_free(smp->sm_lead_w);
2414 vim_free(smp->sm_oneof_w);
2415 vim_free(smp->sm_to_w);
2416#endif
2417 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002418 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002420 for (i = 0; i < lp->sl_prefixcnt; ++i)
2421 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002422 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002423 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002424 lp->sl_prefprog = NULL;
2425
2426 vim_free(lp->sl_midword);
2427 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002428
Bram Moolenaar5195e452005-08-19 20:32:47 +00002429 vim_free(lp->sl_compprog);
2430 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002431 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002432 lp->sl_compprog = NULL;
2433 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002434 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002435
2436 vim_free(lp->sl_syllable);
2437 lp->sl_syllable = NULL;
2438 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002439
Bram Moolenaar4770d092006-01-12 23:22:24 +00002440 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2441 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002442
Bram Moolenaar4770d092006-01-12 23:22:24 +00002443#ifdef FEAT_MBYTE
2444 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002445#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002446
Bram Moolenaar4770d092006-01-12 23:22:24 +00002447 /* Clear info from .sug file. */
2448 slang_clear_sug(lp);
2449
Bram Moolenaar5195e452005-08-19 20:32:47 +00002450 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002451 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002452 lp->sl_compsylmax = MAXWLEN;
2453 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454}
2455
2456/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002457 * Clear the info from the .sug file in "lp".
2458 */
2459 static void
2460slang_clear_sug(lp)
2461 slang_T *lp;
2462{
2463 vim_free(lp->sl_sbyts);
2464 lp->sl_sbyts = NULL;
2465 vim_free(lp->sl_sidxs);
2466 lp->sl_sidxs = NULL;
2467 close_spellbuf(lp->sl_sugbuf);
2468 lp->sl_sugbuf = NULL;
2469 lp->sl_sugloaded = FALSE;
2470 lp->sl_sugtime = 0;
2471}
2472
2473/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002474 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002475 * Invoked through do_in_runtimepath().
2476 */
2477 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002478spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002479 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002480 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002481{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002482 spelload_T *slp = (spelload_T *)cookie;
2483 slang_T *slang;
2484
2485 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2486 if (slang != NULL)
2487 {
2488 /* When a previously loaded file has NOBREAK also use it for the
2489 * ".add" files. */
2490 if (slp->sl_nobreak && slang->sl_add)
2491 slang->sl_nobreak = TRUE;
2492 else if (slang->sl_nobreak)
2493 slp->sl_nobreak = TRUE;
2494
2495 slp->sl_slang = slang;
2496 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002497}
2498
2499/*
2500 * Load one spell file and store the info into a slang_T.
2501 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002502 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002503 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2504 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2505 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2506 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002507 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002508 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2509 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002511 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 static slang_T *
2513spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002514 char_u *fname;
2515 char_u *lang;
2516 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002518{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002519 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002520 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002521 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002522 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002523 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002524 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002525 char_u *save_sourcing_name = sourcing_name;
2526 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002527 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002528 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002529 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002530
Bram Moolenaarb765d632005-06-07 21:00:02 +00002531 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002532 if (fd == NULL)
2533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 if (!silent)
2535 EMSG2(_(e_notopen), fname);
2536 else if (p_verbose > 2)
2537 {
2538 verbose_enter();
2539 smsg((char_u *)e_notopen, fname);
2540 verbose_leave();
2541 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002542 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002544 if (p_verbose > 2)
2545 {
2546 verbose_enter();
2547 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2548 verbose_leave();
2549 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550
Bram Moolenaarb765d632005-06-07 21:00:02 +00002551 if (old_lp == NULL)
2552 {
2553 lp = slang_alloc(lang);
2554 if (lp == NULL)
2555 goto endFAIL;
2556
2557 /* Remember the file name, used to reload the file when it's updated. */
2558 lp->sl_fname = vim_strsave(fname);
2559 if (lp->sl_fname == NULL)
2560 goto endFAIL;
2561
2562 /* Check for .add.spl. */
2563 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2564 }
2565 else
2566 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002567
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002568 /* Set sourcing_name, so that error messages mention the file name. */
2569 sourcing_name = fname;
2570 sourcing_lnum = 0;
2571
Bram Moolenaar4770d092006-01-12 23:22:24 +00002572 /*
2573 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002574 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 for (i = 0; i < VIMSPELLMAGICL; ++i)
2576 buf[i] = getc(fd); /* <fileID> */
2577 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2578 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002579 EMSG(_("E757: This does not look like a spell file"));
2580 goto endFAIL;
2581 }
2582 c = getc(fd); /* <versionnr> */
2583 if (c < VIMSPELLVERSION)
2584 {
2585 EMSG(_("E771: Old spell file, needs to be updated"));
2586 goto endFAIL;
2587 }
2588 else if (c > VIMSPELLVERSION)
2589 {
2590 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002591 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002592 }
2593
Bram Moolenaar5195e452005-08-19 20:32:47 +00002594
2595 /*
2596 * <SECTIONS>: <section> ... <sectionend>
2597 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2598 */
2599 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002601 n = getc(fd); /* <sectionID> or <sectionend> */
2602 if (n == SN_END)
2603 break;
2604 c = getc(fd); /* <sectionflags> */
2605 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2606 /* <sectionlen> */
2607 if (len < 0)
2608 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609
Bram Moolenaar5195e452005-08-19 20:32:47 +00002610 res = 0;
2611 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002612 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002613 case SN_REGION:
2614 res = read_region_section(fd, lp, len);
2615 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002616
Bram Moolenaar5195e452005-08-19 20:32:47 +00002617 case SN_CHARFLAGS:
2618 res = read_charflags_section(fd);
2619 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002620
Bram Moolenaar5195e452005-08-19 20:32:47 +00002621 case SN_MIDWORD:
2622 lp->sl_midword = read_string(fd, len); /* <midword> */
2623 if (lp->sl_midword == NULL)
2624 goto endFAIL;
2625 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002626
Bram Moolenaar5195e452005-08-19 20:32:47 +00002627 case SN_PREFCOND:
2628 res = read_prefcond_section(fd, lp);
2629 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002630
Bram Moolenaar5195e452005-08-19 20:32:47 +00002631 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002632 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2633 break;
2634
2635 case SN_REPSAL:
2636 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002637 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002638
Bram Moolenaar5195e452005-08-19 20:32:47 +00002639 case SN_SAL:
2640 res = read_sal_section(fd, lp);
2641 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002642
Bram Moolenaar5195e452005-08-19 20:32:47 +00002643 case SN_SOFO:
2644 res = read_sofo_section(fd, lp);
2645 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002646
Bram Moolenaar5195e452005-08-19 20:32:47 +00002647 case SN_MAP:
2648 p = read_string(fd, len); /* <mapstr> */
2649 if (p == NULL)
2650 goto endFAIL;
2651 set_map_str(lp, p);
2652 vim_free(p);
2653 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002654
Bram Moolenaar4770d092006-01-12 23:22:24 +00002655 case SN_WORDS:
2656 res = read_words_section(fd, lp, len);
2657 break;
2658
2659 case SN_SUGFILE:
2660 for (i = 7; i >= 0; --i) /* <timestamp> */
2661 lp->sl_sugtime += getc(fd) << (i * 8);
2662 break;
2663
Bram Moolenaar5195e452005-08-19 20:32:47 +00002664 case SN_COMPOUND:
2665 res = read_compound(fd, lp, len);
2666 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002667
Bram Moolenaar78622822005-08-23 21:00:13 +00002668 case SN_NOBREAK:
2669 lp->sl_nobreak = TRUE;
2670 break;
2671
Bram Moolenaar5195e452005-08-19 20:32:47 +00002672 case SN_SYLLABLE:
2673 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2674 if (lp->sl_syllable == NULL)
2675 goto endFAIL;
2676 if (init_syl_tab(lp) == FAIL)
2677 goto endFAIL;
2678 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002679
Bram Moolenaar5195e452005-08-19 20:32:47 +00002680 default:
2681 /* Unsupported section. When it's required give an error
2682 * message. When it's not required skip the contents. */
2683 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002684 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002685 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002686 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002687 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002688 while (--len >= 0)
2689 if (getc(fd) < 0)
2690 goto truncerr;
2691 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002692 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002693someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 if (res == SP_FORMERROR)
2695 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002696 EMSG(_(e_format));
2697 goto endFAIL;
2698 }
2699 if (res == SP_TRUNCERROR)
2700 {
2701truncerr:
2702 EMSG(_(e_spell_trunc));
2703 goto endFAIL;
2704 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002705 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002706 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002707 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708
Bram Moolenaar4770d092006-01-12 23:22:24 +00002709 /* <LWORDTREE> */
2710 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2711 if (res != 0)
2712 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002713
Bram Moolenaar4770d092006-01-12 23:22:24 +00002714 /* <KWORDTREE> */
2715 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2716 if (res != 0)
2717 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002718
Bram Moolenaar4770d092006-01-12 23:22:24 +00002719 /* <PREFIXTREE> */
2720 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2721 lp->sl_prefixcnt);
2722 if (res != 0)
2723 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002724
Bram Moolenaarb765d632005-06-07 21:00:02 +00002725 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002726 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002727 {
2728 lp->sl_next = first_lang;
2729 first_lang = lp;
2730 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002731
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002732 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002733
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002734endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002735 if (lang != NULL)
2736 /* truncating the name signals the error to spell_load_lang() */
2737 *lang = NUL;
2738 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002739 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002740 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002741
2742endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002743 if (fd != NULL)
2744 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002745 sourcing_name = save_sourcing_name;
2746 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747
2748 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002749}
2750
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002751/*
2752 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002753 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002754 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002755 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2756 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002757 */
2758 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002759read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002760 FILE *fd;
2761 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002762 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002763{
2764 int cnt = 0;
2765 int i;
2766 char_u *str;
2767
2768 /* read the length bytes, MSB first */
2769 for (i = 0; i < cnt_bytes; ++i)
2770 cnt = (cnt << 8) + getc(fd);
2771 if (cnt < 0)
2772 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002773 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002774 return NULL;
2775 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002776 *cntp = cnt;
2777 if (cnt == 0)
2778 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002779
Bram Moolenaar5195e452005-08-19 20:32:47 +00002780 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002781 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002782 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002783 return str;
2784}
2785
Bram Moolenaar7887d882005-07-01 22:33:52 +00002786/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002787 * Read a string of length "cnt" from "fd" into allocated memory.
2788 * Returns NULL when out of memory.
2789 */
2790 static char_u *
2791read_string(fd, cnt)
2792 FILE *fd;
2793 int cnt;
2794{
2795 char_u *str;
2796 int i;
2797
2798 /* allocate memory */
2799 str = alloc((unsigned)cnt + 1);
2800 if (str != NULL)
2801 {
2802 /* Read the string. Doesn't check for truncated file. */
2803 for (i = 0; i < cnt; ++i)
2804 str[i] = getc(fd);
2805 str[i] = NUL;
2806 }
2807 return str;
2808}
2809
2810/*
2811 * Read SN_REGION: <regionname> ...
2812 * Return SP_*ERROR flags.
2813 */
2814 static int
2815read_region_section(fd, lp, len)
2816 FILE *fd;
2817 slang_T *lp;
2818 int len;
2819{
2820 int i;
2821
2822 if (len > 16)
2823 return SP_FORMERROR;
2824 for (i = 0; i < len; ++i)
2825 lp->sl_regions[i] = getc(fd); /* <regionname> */
2826 lp->sl_regions[len] = NUL;
2827 return 0;
2828}
2829
2830/*
2831 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2832 * <folcharslen> <folchars>
2833 * Return SP_*ERROR flags.
2834 */
2835 static int
2836read_charflags_section(fd)
2837 FILE *fd;
2838{
2839 char_u *flags;
2840 char_u *fol;
2841 int flagslen, follen;
2842
2843 /* <charflagslen> <charflags> */
2844 flags = read_cnt_string(fd, 1, &flagslen);
2845 if (flagslen < 0)
2846 return flagslen;
2847
2848 /* <folcharslen> <folchars> */
2849 fol = read_cnt_string(fd, 2, &follen);
2850 if (follen < 0)
2851 {
2852 vim_free(flags);
2853 return follen;
2854 }
2855
2856 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2857 if (flags != NULL && fol != NULL)
2858 set_spell_charflags(flags, flagslen, fol);
2859
2860 vim_free(flags);
2861 vim_free(fol);
2862
2863 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2864 if ((flags == NULL) != (fol == NULL))
2865 return SP_FORMERROR;
2866 return 0;
2867}
2868
2869/*
2870 * Read SN_PREFCOND section.
2871 * Return SP_*ERROR flags.
2872 */
2873 static int
2874read_prefcond_section(fd, lp)
2875 FILE *fd;
2876 slang_T *lp;
2877{
2878 int cnt;
2879 int i;
2880 int n;
2881 char_u *p;
2882 char_u buf[MAXWLEN + 1];
2883
2884 /* <prefcondcnt> <prefcond> ... */
2885 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2886 if (cnt <= 0)
2887 return SP_FORMERROR;
2888
2889 lp->sl_prefprog = (regprog_T **)alloc_clear(
2890 (unsigned)sizeof(regprog_T *) * cnt);
2891 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002892 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002893 lp->sl_prefixcnt = cnt;
2894
2895 for (i = 0; i < cnt; ++i)
2896 {
2897 /* <prefcond> : <condlen> <condstr> */
2898 n = getc(fd); /* <condlen> */
2899 if (n < 0 || n >= MAXWLEN)
2900 return SP_FORMERROR;
2901
2902 /* When <condlen> is zero we have an empty condition. Otherwise
2903 * compile the regexp program used to check for the condition. */
2904 if (n > 0)
2905 {
2906 buf[0] = '^'; /* always match at one position only */
2907 p = buf + 1;
2908 while (n-- > 0)
2909 *p++ = getc(fd); /* <condstr> */
2910 *p = NUL;
2911 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2912 }
2913 }
2914 return 0;
2915}
2916
2917/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002918 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 * Return SP_*ERROR flags.
2920 */
2921 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002922read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002923 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002924 garray_T *gap;
2925 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002926{
2927 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002928 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002929 int i;
2930
2931 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2932 if (cnt < 0)
2933 return SP_TRUNCERROR;
2934
Bram Moolenaar5195e452005-08-19 20:32:47 +00002935 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002936 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002937
2938 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2939 for (; gap->ga_len < cnt; ++gap->ga_len)
2940 {
2941 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2942 ftp->ft_from = read_cnt_string(fd, 1, &i);
2943 if (i < 0)
2944 return i;
2945 if (i == 0)
2946 return SP_FORMERROR;
2947 ftp->ft_to = read_cnt_string(fd, 1, &i);
2948 if (i <= 0)
2949 {
2950 vim_free(ftp->ft_from);
2951 if (i < 0)
2952 return i;
2953 return SP_FORMERROR;
2954 }
2955 }
2956
2957 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002958 for (i = 0; i < 256; ++i)
2959 first[i] = -1;
2960 for (i = 0; i < gap->ga_len; ++i)
2961 {
2962 ftp = &((fromto_T *)gap->ga_data)[i];
2963 if (first[*ftp->ft_from] == -1)
2964 first[*ftp->ft_from] = i;
2965 }
2966 return 0;
2967}
2968
2969/*
2970 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2971 * Return SP_*ERROR flags.
2972 */
2973 static int
2974read_sal_section(fd, slang)
2975 FILE *fd;
2976 slang_T *slang;
2977{
2978 int i;
2979 int cnt;
2980 garray_T *gap;
2981 salitem_T *smp;
2982 int ccnt;
2983 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002984 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002985
2986 slang->sl_sofo = FALSE;
2987
2988 i = getc(fd); /* <salflags> */
2989 if (i & SAL_F0LLOWUP)
2990 slang->sl_followup = TRUE;
2991 if (i & SAL_COLLAPSE)
2992 slang->sl_collapse = TRUE;
2993 if (i & SAL_REM_ACCENTS)
2994 slang->sl_rem_accents = TRUE;
2995
2996 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2997 if (cnt < 0)
2998 return SP_TRUNCERROR;
2999
3000 gap = &slang->sl_sal;
3001 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003002 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003003 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003004
3005 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3006 for (; gap->ga_len < cnt; ++gap->ga_len)
3007 {
3008 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3009 ccnt = getc(fd); /* <salfromlen> */
3010 if (ccnt < 0)
3011 return SP_TRUNCERROR;
3012 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003013 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003014 smp->sm_lead = p;
3015
3016 /* Read up to the first special char into sm_lead. */
3017 for (i = 0; i < ccnt; ++i)
3018 {
3019 c = getc(fd); /* <salfrom> */
3020 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3021 break;
3022 *p++ = c;
3023 }
3024 smp->sm_leadlen = p - smp->sm_lead;
3025 *p++ = NUL;
3026
3027 /* Put (abc) chars in sm_oneof, if any. */
3028 if (c == '(')
3029 {
3030 smp->sm_oneof = p;
3031 for (++i; i < ccnt; ++i)
3032 {
3033 c = getc(fd); /* <salfrom> */
3034 if (c == ')')
3035 break;
3036 *p++ = c;
3037 }
3038 *p++ = NUL;
3039 if (++i < ccnt)
3040 c = getc(fd);
3041 }
3042 else
3043 smp->sm_oneof = NULL;
3044
3045 /* Any following chars go in sm_rules. */
3046 smp->sm_rules = p;
3047 if (i < ccnt)
3048 /* store the char we got while checking for end of sm_lead */
3049 *p++ = c;
3050 for (++i; i < ccnt; ++i)
3051 *p++ = getc(fd); /* <salfrom> */
3052 *p++ = NUL;
3053
3054 /* <saltolen> <salto> */
3055 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3056 if (ccnt < 0)
3057 {
3058 vim_free(smp->sm_lead);
3059 return ccnt;
3060 }
3061
3062#ifdef FEAT_MBYTE
3063 if (has_mbyte)
3064 {
3065 /* convert the multi-byte strings to wide char strings */
3066 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3067 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3068 if (smp->sm_oneof == NULL)
3069 smp->sm_oneof_w = NULL;
3070 else
3071 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3072 if (smp->sm_to == NULL)
3073 smp->sm_to_w = NULL;
3074 else
3075 smp->sm_to_w = mb_str2wide(smp->sm_to);
3076 if (smp->sm_lead_w == NULL
3077 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3078 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3079 {
3080 vim_free(smp->sm_lead);
3081 vim_free(smp->sm_to);
3082 vim_free(smp->sm_lead_w);
3083 vim_free(smp->sm_oneof_w);
3084 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003085 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003086 }
3087 }
3088#endif
3089 }
3090
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003091 if (gap->ga_len > 0)
3092 {
3093 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3094 * that we need to check the index every time. */
3095 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3096 if ((p = alloc(1)) == NULL)
3097 return SP_OTHERERROR;
3098 p[0] = NUL;
3099 smp->sm_lead = p;
3100 smp->sm_leadlen = 0;
3101 smp->sm_oneof = NULL;
3102 smp->sm_rules = p;
3103 smp->sm_to = NULL;
3104#ifdef FEAT_MBYTE
3105 if (has_mbyte)
3106 {
3107 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3108 smp->sm_leadlen = 0;
3109 smp->sm_oneof_w = NULL;
3110 smp->sm_to_w = NULL;
3111 }
3112#endif
3113 ++gap->ga_len;
3114 }
3115
Bram Moolenaar5195e452005-08-19 20:32:47 +00003116 /* Fill the first-index table. */
3117 set_sal_first(slang);
3118
3119 return 0;
3120}
3121
3122/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003123 * Read SN_WORDS: <word> ...
3124 * Return SP_*ERROR flags.
3125 */
3126 static int
3127read_words_section(fd, lp, len)
3128 FILE *fd;
3129 slang_T *lp;
3130 int len;
3131{
3132 int done = 0;
3133 int i;
3134 char_u word[MAXWLEN];
3135
3136 while (done < len)
3137 {
3138 /* Read one word at a time. */
3139 for (i = 0; ; ++i)
3140 {
3141 word[i] = getc(fd);
3142 if (word[i] == NUL)
3143 break;
3144 if (i == MAXWLEN - 1)
3145 return SP_FORMERROR;
3146 }
3147
3148 /* Init the count to 10. */
3149 count_common_word(lp, word, -1, 10);
3150 done += i + 1;
3151 }
3152 return 0;
3153}
3154
3155/*
3156 * Add a word to the hashtable of common words.
3157 * If it's already there then the counter is increased.
3158 */
3159 static void
3160count_common_word(lp, word, len, count)
3161 slang_T *lp;
3162 char_u *word;
3163 int len; /* word length, -1 for upto NUL */
3164 int count; /* 1 to count once, 10 to init */
3165{
3166 hash_T hash;
3167 hashitem_T *hi;
3168 wordcount_T *wc;
3169 char_u buf[MAXWLEN];
3170 char_u *p;
3171
3172 if (len == -1)
3173 p = word;
3174 else
3175 {
3176 vim_strncpy(buf, word, len);
3177 p = buf;
3178 }
3179
3180 hash = hash_hash(p);
3181 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3182 if (HASHITEM_EMPTY(hi))
3183 {
3184 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3185 if (wc == NULL)
3186 return;
3187 STRCPY(wc->wc_word, p);
3188 wc->wc_count = count;
3189 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3190 }
3191 else
3192 {
3193 wc = HI2WC(hi);
3194 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3195 wc->wc_count = MAXWORDCOUNT;
3196 }
3197}
3198
3199/*
3200 * Adjust the score of common words.
3201 */
3202 static int
3203score_wordcount_adj(slang, score, word, split)
3204 slang_T *slang;
3205 int score;
3206 char_u *word;
3207 int split; /* word was split, less bonus */
3208{
3209 hashitem_T *hi;
3210 wordcount_T *wc;
3211 int bonus;
3212 int newscore;
3213
3214 hi = hash_find(&slang->sl_wordcount, word);
3215 if (!HASHITEM_EMPTY(hi))
3216 {
3217 wc = HI2WC(hi);
3218 if (wc->wc_count < SCORE_THRES2)
3219 bonus = SCORE_COMMON1;
3220 else if (wc->wc_count < SCORE_THRES3)
3221 bonus = SCORE_COMMON2;
3222 else
3223 bonus = SCORE_COMMON3;
3224 if (split)
3225 newscore = score - bonus / 2;
3226 else
3227 newscore = score - bonus;
3228 if (newscore < 0)
3229 return 0;
3230 return newscore;
3231 }
3232 return score;
3233}
3234
3235/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003236 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3237 * Return SP_*ERROR flags.
3238 */
3239 static int
3240read_sofo_section(fd, slang)
3241 FILE *fd;
3242 slang_T *slang;
3243{
3244 int cnt;
3245 char_u *from, *to;
3246 int res;
3247
3248 slang->sl_sofo = TRUE;
3249
3250 /* <sofofromlen> <sofofrom> */
3251 from = read_cnt_string(fd, 2, &cnt);
3252 if (cnt < 0)
3253 return cnt;
3254
3255 /* <sofotolen> <sofoto> */
3256 to = read_cnt_string(fd, 2, &cnt);
3257 if (cnt < 0)
3258 {
3259 vim_free(from);
3260 return cnt;
3261 }
3262
3263 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3264 if (from != NULL && to != NULL)
3265 res = set_sofo(slang, from, to);
3266 else if (from != NULL || to != NULL)
3267 res = SP_FORMERROR; /* only one of two strings is an error */
3268 else
3269 res = 0;
3270
3271 vim_free(from);
3272 vim_free(to);
3273 return res;
3274}
3275
3276/*
3277 * Read the compound section from the .spl file:
3278 * <compmax> <compminlen> <compsylmax> <compflags>
3279 * Returns SP_*ERROR flags.
3280 */
3281 static int
3282read_compound(fd, slang, len)
3283 FILE *fd;
3284 slang_T *slang;
3285 int len;
3286{
3287 int todo = len;
3288 int c;
3289 int atstart;
3290 char_u *pat;
3291 char_u *pp;
3292 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003293 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003294
3295 if (todo < 2)
3296 return SP_FORMERROR; /* need at least two bytes */
3297
3298 --todo;
3299 c = getc(fd); /* <compmax> */
3300 if (c < 2)
3301 c = MAXWLEN;
3302 slang->sl_compmax = c;
3303
3304 --todo;
3305 c = getc(fd); /* <compminlen> */
3306 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003307 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003308 slang->sl_compminlen = c;
3309
3310 --todo;
3311 c = getc(fd); /* <compsylmax> */
3312 if (c < 1)
3313 c = MAXWLEN;
3314 slang->sl_compsylmax = c;
3315
3316 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3317 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003318 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3319 * Conversion to utf-8 may double the size. */
3320 c = todo * 2 + 7;
3321#ifdef FEAT_MBYTE
3322 if (enc_utf8)
3323 c += todo * 2;
3324#endif
3325 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003326 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003327 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003328
Bram Moolenaard12a1322005-08-21 22:08:24 +00003329 /* We also need a list of all flags that can appear at the start and one
3330 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003331 cp = alloc(todo + 1);
3332 if (cp == NULL)
3333 {
3334 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003335 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003336 }
3337 slang->sl_compstartflags = cp;
3338 *cp = NUL;
3339
Bram Moolenaard12a1322005-08-21 22:08:24 +00003340 ap = alloc(todo + 1);
3341 if (ap == NULL)
3342 {
3343 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003344 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003345 }
3346 slang->sl_compallflags = ap;
3347 *ap = NUL;
3348
Bram Moolenaar5195e452005-08-19 20:32:47 +00003349 pp = pat;
3350 *pp++ = '^';
3351 *pp++ = '\\';
3352 *pp++ = '(';
3353
3354 atstart = 1;
3355 while (todo-- > 0)
3356 {
3357 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003358
3359 /* Add all flags to "sl_compallflags". */
3360 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003361 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003362 {
3363 *ap++ = c;
3364 *ap = NUL;
3365 }
3366
Bram Moolenaar5195e452005-08-19 20:32:47 +00003367 if (atstart != 0)
3368 {
3369 /* At start of item: copy flags to "sl_compstartflags". For a
3370 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3371 if (c == '[')
3372 atstart = 2;
3373 else if (c == ']')
3374 atstart = 0;
3375 else
3376 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003377 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003378 {
3379 *cp++ = c;
3380 *cp = NUL;
3381 }
3382 if (atstart == 1)
3383 atstart = 0;
3384 }
3385 }
3386 if (c == '/') /* slash separates two items */
3387 {
3388 *pp++ = '\\';
3389 *pp++ = '|';
3390 atstart = 1;
3391 }
3392 else /* normal char, "[abc]" and '*' are copied as-is */
3393 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003394 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003395 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003396#ifdef FEAT_MBYTE
3397 if (enc_utf8)
3398 pp += mb_char2bytes(c, pp);
3399 else
3400#endif
3401 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003402 }
3403 }
3404
3405 *pp++ = '\\';
3406 *pp++ = ')';
3407 *pp++ = '$';
3408 *pp = NUL;
3409
3410 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3411 vim_free(pat);
3412 if (slang->sl_compprog == NULL)
3413 return SP_FORMERROR;
3414
3415 return 0;
3416}
3417
Bram Moolenaar6de68532005-08-24 22:08:48 +00003418/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003419 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003420 * Like strchr() but independent of locale.
3421 */
3422 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003423byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003424 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003425 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003426{
3427 char_u *p;
3428
3429 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003430 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003431 return TRUE;
3432 return FALSE;
3433}
3434
Bram Moolenaar5195e452005-08-19 20:32:47 +00003435#define SY_MAXLEN 30
3436typedef struct syl_item_S
3437{
3438 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3439 int sy_len;
3440} syl_item_T;
3441
3442/*
3443 * Truncate "slang->sl_syllable" at the first slash and put the following items
3444 * in "slang->sl_syl_items".
3445 */
3446 static int
3447init_syl_tab(slang)
3448 slang_T *slang;
3449{
3450 char_u *p;
3451 char_u *s;
3452 int l;
3453 syl_item_T *syl;
3454
3455 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3456 p = vim_strchr(slang->sl_syllable, '/');
3457 while (p != NULL)
3458 {
3459 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003460 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003461 break;
3462 s = p;
3463 p = vim_strchr(p, '/');
3464 if (p == NULL)
3465 l = STRLEN(s);
3466 else
3467 l = p - s;
3468 if (l >= SY_MAXLEN)
3469 return SP_FORMERROR;
3470 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003471 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003472 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3473 + slang->sl_syl_items.ga_len++;
3474 vim_strncpy(syl->sy_chars, s, l);
3475 syl->sy_len = l;
3476 }
3477 return OK;
3478}
3479
3480/*
3481 * Count the number of syllables in "word".
3482 * When "word" contains spaces the syllables after the last space are counted.
3483 * Returns zero if syllables are not defines.
3484 */
3485 static int
3486count_syllables(slang, word)
3487 slang_T *slang;
3488 char_u *word;
3489{
3490 int cnt = 0;
3491 int skip = FALSE;
3492 char_u *p;
3493 int len;
3494 int i;
3495 syl_item_T *syl;
3496 int c;
3497
3498 if (slang->sl_syllable == NULL)
3499 return 0;
3500
3501 for (p = word; *p != NUL; p += len)
3502 {
3503 /* When running into a space reset counter. */
3504 if (*p == ' ')
3505 {
3506 len = 1;
3507 cnt = 0;
3508 continue;
3509 }
3510
3511 /* Find longest match of syllable items. */
3512 len = 0;
3513 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3514 {
3515 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3516 if (syl->sy_len > len
3517 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3518 len = syl->sy_len;
3519 }
3520 if (len != 0) /* found a match, count syllable */
3521 {
3522 ++cnt;
3523 skip = FALSE;
3524 }
3525 else
3526 {
3527 /* No recognized syllable item, at least a syllable char then? */
3528#ifdef FEAT_MBYTE
3529 c = mb_ptr2char(p);
3530 len = (*mb_ptr2len)(p);
3531#else
3532 c = *p;
3533 len = 1;
3534#endif
3535 if (vim_strchr(slang->sl_syllable, c) == NULL)
3536 skip = FALSE; /* No, search for next syllable */
3537 else if (!skip)
3538 {
3539 ++cnt; /* Yes, count it */
3540 skip = TRUE; /* don't count following syllable chars */
3541 }
3542 }
3543 }
3544 return cnt;
3545}
3546
3547/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003548 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003549 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003550 */
3551 static int
3552set_sofo(lp, from, to)
3553 slang_T *lp;
3554 char_u *from;
3555 char_u *to;
3556{
3557 int i;
3558
3559#ifdef FEAT_MBYTE
3560 garray_T *gap;
3561 char_u *s;
3562 char_u *p;
3563 int c;
3564 int *inp;
3565
3566 if (has_mbyte)
3567 {
3568 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3569 * characters. The index is the low byte of the character.
3570 * The list contains from-to pairs with a terminating NUL.
3571 * sl_sal_first[] is used for latin1 "from" characters. */
3572 gap = &lp->sl_sal;
3573 ga_init2(gap, sizeof(int *), 1);
3574 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003575 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003576 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3577 gap->ga_len = 256;
3578
3579 /* First count the number of items for each list. Temporarily use
3580 * sl_sal_first[] for this. */
3581 for (p = from, s = to; *p != NUL && *s != NUL; )
3582 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003583 c = mb_cptr2char_adv(&p);
3584 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003585 if (c >= 256)
3586 ++lp->sl_sal_first[c & 0xff];
3587 }
3588 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003589 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003590
3591 /* Allocate the lists. */
3592 for (i = 0; i < 256; ++i)
3593 if (lp->sl_sal_first[i] > 0)
3594 {
3595 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3596 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003597 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003598 ((int **)gap->ga_data)[i] = (int *)p;
3599 *(int *)p = 0;
3600 }
3601
3602 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3603 * list. */
3604 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3605 for (p = from, s = to; *p != NUL && *s != NUL; )
3606 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003607 c = mb_cptr2char_adv(&p);
3608 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003609 if (c >= 256)
3610 {
3611 /* Append the from-to chars at the end of the list with
3612 * the low byte. */
3613 inp = ((int **)gap->ga_data)[c & 0xff];
3614 while (*inp != 0)
3615 ++inp;
3616 *inp++ = c; /* from char */
3617 *inp++ = i; /* to char */
3618 *inp++ = NUL; /* NUL at the end */
3619 }
3620 else
3621 /* mapping byte to char is done in sl_sal_first[] */
3622 lp->sl_sal_first[c] = i;
3623 }
3624 }
3625 else
3626#endif
3627 {
3628 /* mapping bytes to bytes is done in sl_sal_first[] */
3629 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003630 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003631
3632 for (i = 0; to[i] != NUL; ++i)
3633 lp->sl_sal_first[from[i]] = to[i];
3634 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3635 }
3636
Bram Moolenaar5195e452005-08-19 20:32:47 +00003637 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003638}
3639
3640/*
3641 * Fill the first-index table for "lp".
3642 */
3643 static void
3644set_sal_first(lp)
3645 slang_T *lp;
3646{
3647 salfirst_T *sfirst;
3648 int i;
3649 salitem_T *smp;
3650 int c;
3651 garray_T *gap = &lp->sl_sal;
3652
3653 sfirst = lp->sl_sal_first;
3654 for (i = 0; i < 256; ++i)
3655 sfirst[i] = -1;
3656 smp = (salitem_T *)gap->ga_data;
3657 for (i = 0; i < gap->ga_len; ++i)
3658 {
3659#ifdef FEAT_MBYTE
3660 if (has_mbyte)
3661 /* Use the lowest byte of the first character. For latin1 it's
3662 * the character, for other encodings it should differ for most
3663 * characters. */
3664 c = *smp[i].sm_lead_w & 0xff;
3665 else
3666#endif
3667 c = *smp[i].sm_lead;
3668 if (sfirst[c] == -1)
3669 {
3670 sfirst[c] = i;
3671#ifdef FEAT_MBYTE
3672 if (has_mbyte)
3673 {
3674 int n;
3675
3676 /* Make sure all entries with this byte are following each
3677 * other. Move the ones that are in the wrong position. Do
3678 * keep the same ordering! */
3679 while (i + 1 < gap->ga_len
3680 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3681 /* Skip over entry with same index byte. */
3682 ++i;
3683
3684 for (n = 1; i + n < gap->ga_len; ++n)
3685 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3686 {
3687 salitem_T tsal;
3688
3689 /* Move entry with same index byte after the entries
3690 * we already found. */
3691 ++i;
3692 --n;
3693 tsal = smp[i + n];
3694 mch_memmove(smp + i + 1, smp + i,
3695 sizeof(salitem_T) * n);
3696 smp[i] = tsal;
3697 }
3698 }
3699#endif
3700 }
3701 }
3702}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003703
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003704#ifdef FEAT_MBYTE
3705/*
3706 * Turn a multi-byte string into a wide character string.
3707 * Return it in allocated memory (NULL for out-of-memory)
3708 */
3709 static int *
3710mb_str2wide(s)
3711 char_u *s;
3712{
3713 int *res;
3714 char_u *p;
3715 int i = 0;
3716
3717 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3718 if (res != NULL)
3719 {
3720 for (p = s; *p != NUL; )
3721 res[i++] = mb_ptr2char_adv(&p);
3722 res[i] = NUL;
3723 }
3724 return res;
3725}
3726#endif
3727
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003728/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003729 * Read a tree from the .spl or .sug file.
3730 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3731 * This is skipped when the tree has zero length.
3732 * Returns zero when OK, SP_ value for an error.
3733 */
3734 static int
3735spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3736 FILE *fd;
3737 char_u **bytsp;
3738 idx_T **idxsp;
3739 int prefixtree; /* TRUE for the prefix tree */
3740 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3741{
3742 int len;
3743 int idx;
3744 char_u *bp;
3745 idx_T *ip;
3746
3747 /* The tree size was computed when writing the file, so that we can
3748 * allocate it as one long block. <nodecount> */
3749 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3750 if (len < 0)
3751 return SP_TRUNCERROR;
3752 if (len > 0)
3753 {
3754 /* Allocate the byte array. */
3755 bp = lalloc((long_u)len, TRUE);
3756 if (bp == NULL)
3757 return SP_OTHERERROR;
3758 *bytsp = bp;
3759
3760 /* Allocate the index array. */
3761 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3762 if (ip == NULL)
3763 return SP_OTHERERROR;
3764 *idxsp = ip;
3765
3766 /* Recursively read the tree and store it in the array. */
3767 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3768 if (idx < 0)
3769 return idx;
3770 }
3771 return 0;
3772}
3773
3774/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003775 * Read one row of siblings from the spell file and store it in the byte array
3776 * "byts" and index array "idxs". Recursively read the children.
3777 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003778 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003779 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003780 * Returns the index (>= 0) following the siblings.
3781 * Returns SP_TRUNCERROR if the file is shorter than expected.
3782 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003783 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003784 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003785read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003786 FILE *fd;
3787 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003788 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003789 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003790 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003791 int prefixtree; /* TRUE for reading PREFIXTREE */
3792 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003793{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003794 int len;
3795 int i;
3796 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003797 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003798 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003799 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003800#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003801
Bram Moolenaar51485f02005-06-04 21:55:20 +00003802 len = getc(fd); /* <siblingcount> */
3803 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003804 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003805
3806 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003807 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003808 byts[idx++] = len;
3809
3810 /* Read the byte values, flag/region bytes and shared indexes. */
3811 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003812 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003813 c = getc(fd); /* <byte> */
3814 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003815 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003816 if (c <= BY_SPECIAL)
3817 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003818 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003819 {
3820 /* No flags, all regions. */
3821 idxs[idx] = 0;
3822 c = 0;
3823 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003824 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003825 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003826 if (prefixtree)
3827 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003828 /* Read the optional pflags byte, the prefix ID and the
3829 * condition nr. In idxs[] store the prefix ID in the low
3830 * byte, the condition index shifted up 8 bits, the flags
3831 * shifted up 24 bits. */
3832 if (c == BY_FLAGS)
3833 c = getc(fd) << 24; /* <pflags> */
3834 else
3835 c = 0;
3836
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003837 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003838
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003839 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3840 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003841 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003842 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003843 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003844 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003845 {
3846 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003847 * idxs[] the flags go in the low two bytes, region above
3848 * that and prefix ID above the region. */
3849 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003850 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003851 if (c2 == BY_FLAGS2)
3852 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003853 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003854 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003855 if (c & WF_AFX)
3856 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003857 }
3858
Bram Moolenaar51485f02005-06-04 21:55:20 +00003859 idxs[idx] = c;
3860 c = 0;
3861 }
3862 else /* c == BY_INDEX */
3863 {
3864 /* <nodeidx> */
3865 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3866 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003867 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003868 idxs[idx] = n + SHARED_MASK;
3869 c = getc(fd); /* <xbyte> */
3870 }
3871 }
3872 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003873 }
3874
Bram Moolenaar51485f02005-06-04 21:55:20 +00003875 /* Recursively read the children for non-shared siblings.
3876 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3877 * remove SHARED_MASK) */
3878 for (i = 1; i <= len; ++i)
3879 if (byts[startidx + i] != 0)
3880 {
3881 if (idxs[startidx + i] & SHARED_MASK)
3882 idxs[startidx + i] &= ~SHARED_MASK;
3883 else
3884 {
3885 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003886 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003887 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003888 if (idx < 0)
3889 break;
3890 }
3891 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892
Bram Moolenaar51485f02005-06-04 21:55:20 +00003893 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003894}
3895
3896/*
3897 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003898 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003899 */
3900 char_u *
3901did_set_spelllang(buf)
3902 buf_T *buf;
3903{
3904 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003905 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003906 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003907 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003908 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003909 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003910 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003911 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003912 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003913 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003914 int len;
3915 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003916 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003917 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003918 char_u *use_region = NULL;
3919 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003920 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003921 int i, j;
3922 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003923
3924 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003925 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003926
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003927 /* loop over comma separated language names. */
3928 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003930 /* Get one language name. */
3931 copy_option_part(&splp, lang, MAXWLEN, ",");
3932
Bram Moolenaar5482f332005-04-17 20:18:43 +00003933 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003934 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003935
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003936 /* If the name ends in ".spl" use it as the name of the spell file.
3937 * If there is a region name let "region" point to it and remove it
3938 * from the name. */
3939 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3940 {
3941 filename = TRUE;
3942
Bram Moolenaarb6356332005-07-18 21:40:44 +00003943 /* Locate a region and remove it from the file name. */
3944 p = vim_strchr(gettail(lang), '_');
3945 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3946 && !ASCII_ISALPHA(p[3]))
3947 {
3948 vim_strncpy(region_cp, p + 1, 2);
3949 mch_memmove(p, p + 3, len - (p - lang) - 2);
3950 len -= 3;
3951 region = region_cp;
3952 }
3953 else
3954 dont_use_region = TRUE;
3955
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003956 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003957 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3958 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003959 break;
3960 }
3961 else
3962 {
3963 filename = FALSE;
3964 if (len > 3 && lang[len - 3] == '_')
3965 {
3966 region = lang + len - 2;
3967 len -= 3;
3968 lang[len] = NUL;
3969 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003970 else
3971 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003972
3973 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003974 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3975 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003976 break;
3977 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003978
Bram Moolenaarb6356332005-07-18 21:40:44 +00003979 if (region != NULL)
3980 {
3981 /* If the region differs from what was used before then don't
3982 * use it for 'spellfile'. */
3983 if (use_region != NULL && STRCMP(region, use_region) != 0)
3984 dont_use_region = TRUE;
3985 use_region = region;
3986 }
3987
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003988 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003989 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003990 {
3991 if (filename)
3992 (void)spell_load_file(lang, lang, NULL, FALSE);
3993 else
3994 spell_load_lang(lang);
3995 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003996
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003997 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003998 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003999 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004000 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4001 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4002 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004003 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004004 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004005 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004006 {
4007 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004008 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004009 if (c == REGION_ALL)
4010 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004011 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004012 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004013 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004014 /* This addition file is for other regions. */
4015 region_mask = 0;
4016 }
4017 else
4018 /* This is probably an error. Give a warning and
4019 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004020 smsg((char_u *)
4021 _("Warning: region %s not supported"),
4022 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004023 }
4024 else
4025 region_mask = 1 << c;
4026 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004027
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004028 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004029 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004030 if (ga_grow(&ga, 1) == FAIL)
4031 {
4032 ga_clear(&ga);
4033 return e_outofmem;
4034 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004035 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004036 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4037 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004038 use_midword(slang, buf);
4039 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004040 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004041 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004042 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004043 }
4044
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004045 /* round 0: load int_wordlist, if possible.
4046 * round 1: load first name in 'spellfile'.
4047 * round 2: load second name in 'spellfile.
4048 * etc. */
4049 spf = curbuf->b_p_spf;
4050 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004051 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004052 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004053 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004054 /* Internal wordlist, if there is one. */
4055 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004056 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004057 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004058 }
4059 else
4060 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004061 /* One entry in 'spellfile'. */
4062 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4063 STRCAT(spf_name, ".spl");
4064
4065 /* If it was already found above then skip it. */
4066 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004067 {
4068 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4069 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004070 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004071 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004072 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004073 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004074 }
4075
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004076 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004077 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4078 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004080 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004082 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004083 * region name, the region is ignored otherwise. for int_wordlist
4084 * use an arbitrary name. */
4085 if (round == 0)
4086 STRCPY(lang, "internal wordlist");
4087 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004088 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004089 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004090 p = vim_strchr(lang, '.');
4091 if (p != NULL)
4092 *p = NUL; /* truncate at ".encoding.add" */
4093 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004094 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004095
4096 /* If one of the languages has NOBREAK we assume the addition
4097 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004098 if (slang != NULL && nobreak)
4099 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004101 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004103 region_mask = REGION_ALL;
4104 if (use_region != NULL && !dont_use_region)
4105 {
4106 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004107 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004108 if (c != REGION_ALL)
4109 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004110 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004111 /* This spell file is for other regions. */
4112 region_mask = 0;
4113 }
4114
4115 if (region_mask != 0)
4116 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004117 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4118 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4119 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004120 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4121 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004122 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004123 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 }
4125 }
4126
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 /* Everything is fine, store the new b_langp value. */
4128 ga_clear(&buf->b_langp);
4129 buf->b_langp = ga;
4130
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004131 /* For each language figure out what language to use for sound folding and
4132 * REP items. If the language doesn't support it itself use another one
4133 * with the same name. E.g. for "en-math" use "en". */
4134 for (i = 0; i < ga.ga_len; ++i)
4135 {
4136 lp = LANGP_ENTRY(ga, i);
4137
4138 /* sound folding */
4139 if (lp->lp_slang->sl_sal.ga_len > 0)
4140 /* language does sound folding itself */
4141 lp->lp_sallang = lp->lp_slang;
4142 else
4143 /* find first similar language that does sound folding */
4144 for (j = 0; j < ga.ga_len; ++j)
4145 {
4146 lp2 = LANGP_ENTRY(ga, j);
4147 if (lp2->lp_slang->sl_sal.ga_len > 0
4148 && STRNCMP(lp->lp_slang->sl_name,
4149 lp2->lp_slang->sl_name, 2) == 0)
4150 {
4151 lp->lp_sallang = lp2->lp_slang;
4152 break;
4153 }
4154 }
4155
4156 /* REP items */
4157 if (lp->lp_slang->sl_rep.ga_len > 0)
4158 /* language has REP items itself */
4159 lp->lp_replang = lp->lp_slang;
4160 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004161 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004162 for (j = 0; j < ga.ga_len; ++j)
4163 {
4164 lp2 = LANGP_ENTRY(ga, j);
4165 if (lp2->lp_slang->sl_rep.ga_len > 0
4166 && STRNCMP(lp->lp_slang->sl_name,
4167 lp2->lp_slang->sl_name, 2) == 0)
4168 {
4169 lp->lp_replang = lp2->lp_slang;
4170 break;
4171 }
4172 }
4173 }
4174
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004175 return NULL;
4176}
4177
4178/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004179 * Clear the midword characters for buffer "buf".
4180 */
4181 static void
4182clear_midword(buf)
4183 buf_T *buf;
4184{
4185 vim_memset(buf->b_spell_ismw, 0, 256);
4186#ifdef FEAT_MBYTE
4187 vim_free(buf->b_spell_ismw_mb);
4188 buf->b_spell_ismw_mb = NULL;
4189#endif
4190}
4191
4192/*
4193 * Use the "sl_midword" field of language "lp" for buffer "buf".
4194 * They add up to any currently used midword characters.
4195 */
4196 static void
4197use_midword(lp, buf)
4198 slang_T *lp;
4199 buf_T *buf;
4200{
4201 char_u *p;
4202
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004203 if (lp->sl_midword == NULL) /* there aren't any */
4204 return;
4205
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004206 for (p = lp->sl_midword; *p != NUL; )
4207#ifdef FEAT_MBYTE
4208 if (has_mbyte)
4209 {
4210 int c, l, n;
4211 char_u *bp;
4212
4213 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004214 l = (*mb_ptr2len)(p);
4215 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004216 buf->b_spell_ismw[c] = TRUE;
4217 else if (buf->b_spell_ismw_mb == NULL)
4218 /* First multi-byte char in "b_spell_ismw_mb". */
4219 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4220 else
4221 {
4222 /* Append multi-byte chars to "b_spell_ismw_mb". */
4223 n = STRLEN(buf->b_spell_ismw_mb);
4224 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4225 if (bp != NULL)
4226 {
4227 vim_free(buf->b_spell_ismw_mb);
4228 buf->b_spell_ismw_mb = bp;
4229 vim_strncpy(bp + n, p, l);
4230 }
4231 }
4232 p += l;
4233 }
4234 else
4235#endif
4236 buf->b_spell_ismw[*p++] = TRUE;
4237}
4238
4239/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004240 * Find the region "region[2]" in "rp" (points to "sl_regions").
4241 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004242 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243 */
4244 static int
4245find_region(rp, region)
4246 char_u *rp;
4247 char_u *region;
4248{
4249 int i;
4250
4251 for (i = 0; ; i += 2)
4252 {
4253 if (rp[i] == NUL)
4254 return REGION_ALL;
4255 if (rp[i] == region[0] && rp[i + 1] == region[1])
4256 break;
4257 }
4258 return i / 2;
4259}
4260
4261/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004263 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 * Word WF_ONECAP
4265 * W WORD WF_ALLCAP
4266 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004267 */
4268 static int
4269captype(word, end)
4270 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004272{
4273 char_u *p;
4274 int c;
4275 int firstcap;
4276 int allcap;
4277 int past_second = FALSE; /* past second word char */
4278
4279 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004280 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004282 return 0; /* only non-word characters, illegal word */
4283#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004284 if (has_mbyte)
4285 c = mb_ptr2char_adv(&p);
4286 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004287#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004288 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004289 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004290
4291 /*
4292 * Need to check all letters to find a word with mixed upper/lower.
4293 * But a word with an upper char only at start is a ONECAP.
4294 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004296 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004297 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004298 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004299 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004300 {
4301 /* UUl -> KEEPCAP */
4302 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004303 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004304 allcap = FALSE;
4305 }
4306 else if (!allcap)
4307 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004308 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004309 past_second = TRUE;
4310 }
4311
4312 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004313 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004314 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004315 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004316 return 0;
4317}
4318
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004319/*
4320 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4321 * capital. So that make_case_word() can turn WOrd into Word.
4322 * Add ALLCAP for "WOrD".
4323 */
4324 static int
4325badword_captype(word, end)
4326 char_u *word;
4327 char_u *end;
4328{
4329 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004330 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004331 int l, u;
4332 int first;
4333 char_u *p;
4334
4335 if (flags & WF_KEEPCAP)
4336 {
4337 /* Count the number of UPPER and lower case letters. */
4338 l = u = 0;
4339 first = FALSE;
4340 for (p = word; p < end; mb_ptr_adv(p))
4341 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004342 c = PTR2CHAR(p);
4343 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004344 {
4345 ++u;
4346 if (p == word)
4347 first = TRUE;
4348 }
4349 else
4350 ++l;
4351 }
4352
4353 /* If there are more UPPER than lower case letters suggest an
4354 * ALLCAP word. Otherwise, if the first letter is UPPER then
4355 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4356 * require three upper case letters. */
4357 if (u > l && u > 2)
4358 flags |= WF_ALLCAP;
4359 else if (first)
4360 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004361
4362 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4363 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004364 }
4365 return flags;
4366}
4367
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004368# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4369/*
4370 * Free all languages.
4371 */
4372 void
4373spell_free_all()
4374{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004375 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004376 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004377 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004378
4379 /* Go through all buffers and handle 'spelllang'. */
4380 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4381 ga_clear(&buf->b_langp);
4382
4383 while (first_lang != NULL)
4384 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004385 slang = first_lang;
4386 first_lang = slang->sl_next;
4387 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004388 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004389
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004390 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004391 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004392 /* Delete the internal wordlist and its .spl file */
4393 mch_remove(int_wordlist);
4394 int_wordlist_spl(fname);
4395 mch_remove(fname);
4396 vim_free(int_wordlist);
4397 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004398 }
4399
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004400 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004401
4402 vim_free(repl_to);
4403 repl_to = NULL;
4404 vim_free(repl_from);
4405 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004406}
4407# endif
4408
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004409# if defined(FEAT_MBYTE) || defined(PROTO)
4410/*
4411 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004412 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004413 */
4414 void
4415spell_reload()
4416{
4417 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004418 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004419
Bram Moolenaarea408852005-06-25 22:49:46 +00004420 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004421 init_spell_chartab();
4422
4423 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004424 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004425
4426 /* Go through all buffers and handle 'spelllang'. */
4427 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4428 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004429 /* Only load the wordlists when 'spelllang' is set and there is a
4430 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004431 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004432 {
4433 FOR_ALL_WINDOWS(wp)
4434 if (wp->w_buffer == buf && wp->w_p_spell)
4435 {
4436 (void)did_set_spelllang(buf);
4437# ifdef FEAT_WINDOWS
4438 break;
4439# endif
4440 }
4441 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004442 }
4443}
4444# endif
4445
Bram Moolenaarb765d632005-06-07 21:00:02 +00004446/*
4447 * Reload the spell file "fname" if it's loaded.
4448 */
4449 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004450spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004451 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004453{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004454 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004456
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004457 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004458 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004459 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004460 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004461 slang_clear(slang);
4462 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004463 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004464 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004465 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004466 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004467 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004469
4470 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004471 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004472 if (added_word && !didit)
4473 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004474}
4475
4476
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004477/*
4478 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479 */
4480
Bram Moolenaar51485f02005-06-04 21:55:20 +00004481#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004482 and .dic file. */
4483/*
4484 * Main structure to store the contents of a ".aff" file.
4485 */
4486typedef struct afffile_S
4487{
4488 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004489 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004490 unsigned af_rare; /* RARE ID for rare word */
4491 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004492 unsigned af_bad; /* BAD ID for banned word */
4493 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004494 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004495 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004496 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4497 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004498 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004499} afffile_T;
4500
Bram Moolenaar6de68532005-08-24 22:08:48 +00004501#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004502#define AFT_LONG 1 /* flags are two characters */
4503#define AFT_CAPLONG 2 /* flags are one or two characters */
4504#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004505
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004506typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004507/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4508struct affentry_S
4509{
4510 affentry_T *ae_next; /* next affix with same name/number */
4511 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4512 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004513 char_u *ae_cond; /* condition (NULL for ".") */
4514 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004515 char_u ae_rare; /* rare affix */
4516 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004517};
4518
Bram Moolenaar6de68532005-08-24 22:08:48 +00004519#ifdef FEAT_MBYTE
4520# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4521#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004522# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004523#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004524
Bram Moolenaar51485f02005-06-04 21:55:20 +00004525/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4526typedef struct affheader_S
4527{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004528 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4529 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4530 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004531 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004532 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004533 affentry_T *ah_first; /* first affix entry */
4534} affheader_T;
4535
4536#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4537
Bram Moolenaar6de68532005-08-24 22:08:48 +00004538/* Flag used in compound items. */
4539typedef struct compitem_S
4540{
4541 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4542 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4543 int ci_newID; /* affix ID after renumbering. */
4544} compitem_T;
4545
4546#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4547
Bram Moolenaar51485f02005-06-04 21:55:20 +00004548/*
4549 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004550 * the need to keep track of each allocated thing, everything is freed all at
4551 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004552 */
4553#define SBLOCKSIZE 16000 /* size of sb_data */
4554typedef struct sblock_S sblock_T;
4555struct sblock_S
4556{
4557 sblock_T *sb_next; /* next block in list */
4558 int sb_used; /* nr of bytes already in use */
4559 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004560};
4561
4562/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004563 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004565typedef struct wordnode_S wordnode_T;
4566struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004567{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004568 union /* shared to save space */
4569 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004570 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004571 int index; /* index in written nodes (valid after first
4572 round) */
4573 } wn_u1;
4574 union /* shared to save space */
4575 {
4576 wordnode_T *next; /* next node with same hash key */
4577 wordnode_T *wnode; /* parent node that will write this node */
4578 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004579 wordnode_T *wn_child; /* child (next byte in word) */
4580 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4581 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004582 int wn_refs; /* Nr. of references to this node. Only
4583 relevant for first node in a list of
4584 siblings, in following siblings it is
4585 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004586 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004587
4588 /* Info for when "wn_byte" is NUL.
4589 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4590 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4591 * "wn_region" the LSW of the wordnr. */
4592 char_u wn_affixID; /* supported/required prefix ID or 0 */
4593 short_u wn_flags; /* WF_ flags */
4594 short wn_region; /* region mask */
4595
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004596#ifdef SPELL_PRINTTREE
4597 int wn_nr; /* sequence nr for printing */
4598#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599};
4600
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004601#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4602
Bram Moolenaar51485f02005-06-04 21:55:20 +00004603#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004604
Bram Moolenaar51485f02005-06-04 21:55:20 +00004605/*
4606 * Info used while reading the spell files.
4607 */
4608typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004609{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004610 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004611 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004612
Bram Moolenaar51485f02005-06-04 21:55:20 +00004613 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004614 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004615
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004616 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004617
Bram Moolenaar4770d092006-01-12 23:22:24 +00004618 long si_sugtree; /* creating the soundfolding trie */
4619
Bram Moolenaar51485f02005-06-04 21:55:20 +00004620 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004621 long si_blocks_cnt; /* memory blocks allocated */
4622 long si_compress_cnt; /* words to add before lowering
4623 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004624 wordnode_T *si_first_free; /* List of nodes that have been freed during
4625 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004626 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004627#ifdef SPELL_PRINTTREE
4628 int si_wordnode_nr; /* sequence nr for nodes */
4629#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004630 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004631
Bram Moolenaar51485f02005-06-04 21:55:20 +00004632 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004633 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004634 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004635 int si_region; /* region mask */
4636 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004637 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004638 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004639 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004640 int si_region_count; /* number of regions supported (1 when there
4641 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004642 char_u si_region_name[16]; /* region names; used only if
4643 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644
4645 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004646 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004648 char_u *si_sofofr; /* SOFOFROM text */
4649 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004650 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 int si_followup; /* soundsalike: ? */
4652 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004653 hashtab_T si_commonwords; /* hashtable for common words */
4654 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655 int si_rem_accents; /* soundsalike: remove accents */
4656 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004657 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004658 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004659 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004660 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004661 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004662 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004663 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004664 garray_T si_prefcond; /* table with conditions for postponed
4665 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004666 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004667 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004668} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004669
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004670static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004671static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4672static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4673static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004674static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004675static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4676static void aff_check_number __ARGS((int spinval, int affval, char *name));
4677static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004678static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004679static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4680static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004681static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004682static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004683static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004684static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004685static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004686static 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, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004687static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4688static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4689static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004690static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004691static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004692static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004693static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004694static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004695static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004696static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4697static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4698static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004699static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004700static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004701static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004702static void clear_node __ARGS((wordnode_T *node));
4703static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004704static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4705static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4706static int sug_maketable __ARGS((spellinfo_T *spin));
4707static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4708static int offset2bytes __ARGS((int nr, char_u *buf));
4709static int bytes2offset __ARGS((char_u **pp));
4710static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004712static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004713static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004714
Bram Moolenaar53805d12005-08-01 07:08:33 +00004715/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4716 * but it must be negative to indicate the prefix tree to tree_add_word().
4717 * Use a negative number with the lower 8 bits zero. */
4718#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004719
Bram Moolenaar5195e452005-08-19 20:32:47 +00004720/*
4721 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4722 */
4723static long compress_start = 30000; /* memory / SBLOCKSIZE */
4724static long compress_inc = 100; /* memory / SBLOCKSIZE */
4725static long compress_added = 500000; /* word count */
4726
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004727#ifdef SPELL_PRINTTREE
4728/*
4729 * For debugging the tree code: print the current tree in a (more or less)
4730 * readable format, so that we can see what happens when adding a word and/or
4731 * compressing the tree.
4732 * Based on code from Olaf Seibert.
4733 */
4734#define PRINTLINESIZE 1000
4735#define PRINTWIDTH 6
4736
4737#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4738 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4739
4740static char line1[PRINTLINESIZE];
4741static char line2[PRINTLINESIZE];
4742static char line3[PRINTLINESIZE];
4743
4744 static void
4745spell_clear_flags(wordnode_T *node)
4746{
4747 wordnode_T *np;
4748
4749 for (np = node; np != NULL; np = np->wn_sibling)
4750 {
4751 np->wn_u1.index = FALSE;
4752 spell_clear_flags(np->wn_child);
4753 }
4754}
4755
4756 static void
4757spell_print_node(wordnode_T *node, int depth)
4758{
4759 if (node->wn_u1.index)
4760 {
4761 /* Done this node before, print the reference. */
4762 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4763 PRINTSOME(line2, depth, " ", 0, 0);
4764 PRINTSOME(line3, depth, " ", 0, 0);
4765 msg(line1);
4766 msg(line2);
4767 msg(line3);
4768 }
4769 else
4770 {
4771 node->wn_u1.index = TRUE;
4772
4773 if (node->wn_byte != NUL)
4774 {
4775 if (node->wn_child != NULL)
4776 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4777 else
4778 /* Cannot happen? */
4779 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4780 }
4781 else
4782 PRINTSOME(line1, depth, " $ ", 0, 0);
4783
4784 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4785
4786 if (node->wn_sibling != NULL)
4787 PRINTSOME(line3, depth, " | ", 0, 0);
4788 else
4789 PRINTSOME(line3, depth, " ", 0, 0);
4790
4791 if (node->wn_byte == NUL)
4792 {
4793 msg(line1);
4794 msg(line2);
4795 msg(line3);
4796 }
4797
4798 /* do the children */
4799 if (node->wn_byte != NUL && node->wn_child != NULL)
4800 spell_print_node(node->wn_child, depth + 1);
4801
4802 /* do the siblings */
4803 if (node->wn_sibling != NULL)
4804 {
4805 /* get rid of all parent details except | */
4806 STRCPY(line1, line3);
4807 STRCPY(line2, line3);
4808 spell_print_node(node->wn_sibling, depth);
4809 }
4810 }
4811}
4812
4813 static void
4814spell_print_tree(wordnode_T *root)
4815{
4816 if (root != NULL)
4817 {
4818 /* Clear the "wn_u1.index" fields, used to remember what has been
4819 * done. */
4820 spell_clear_flags(root);
4821
4822 /* Recursively print the tree. */
4823 spell_print_node(root, 0);
4824 }
4825}
4826#endif /* SPELL_PRINTTREE */
4827
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004828/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004830 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831 */
4832 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004833spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004834 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004835 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004836{
4837 FILE *fd;
4838 afffile_T *aff;
4839 char_u rline[MAXLINELEN];
4840 char_u *line;
4841 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004842#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00004843 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004844 int itemcnt;
4845 char_u *p;
4846 int lnum = 0;
4847 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004848 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004849 int aff_todo = 0;
4850 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004851 char_u *low = NULL;
4852 char_u *fol = NULL;
4853 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004855 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 int do_sal;
4857 int do_map;
4858 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004859 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004860 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004861 int compminlen = 0; /* COMPOUNDMIN value */
4862 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4863 int compmax = 0; /* COMPOUNDMAX value */
4864 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4865 concatenated */
4866 char_u *midword = NULL; /* MIDWORD value */
4867 char_u *syllable = NULL; /* SYLLABLE value */
4868 char_u *sofofrom = NULL; /* SOFOFROM value */
4869 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004870
Bram Moolenaar51485f02005-06-04 21:55:20 +00004871 /*
4872 * Open the file.
4873 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004874 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 if (fd == NULL)
4876 {
4877 EMSG2(_(e_notopen), fname);
4878 return NULL;
4879 }
4880
Bram Moolenaar4770d092006-01-12 23:22:24 +00004881 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
4882 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 /* Only do REP lines when not done in another .aff file already. */
4885 do_rep = spin->si_rep.ga_len == 0;
4886
Bram Moolenaar4770d092006-01-12 23:22:24 +00004887 /* Only do REPSAL lines when not done in another .aff file already. */
4888 do_repsal = spin->si_repsal.ga_len == 0;
4889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004890 /* Only do SAL lines when not done in another .aff file already. */
4891 do_sal = spin->si_sal.ga_len == 0;
4892
4893 /* Only do MAP lines when not done in another .aff file already. */
4894 do_map = spin->si_map.ga_len == 0;
4895
Bram Moolenaar51485f02005-06-04 21:55:20 +00004896 /*
4897 * Allocate and init the afffile_T structure.
4898 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004899 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004900 if (aff == NULL)
4901 return NULL;
4902 hash_init(&aff->af_pref);
4903 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004904 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004905
4906 /*
4907 * Read all the lines in the file one by one.
4908 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004909 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004910 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004911 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004912 ++lnum;
4913
4914 /* Skip comment lines. */
4915 if (*rline == '#')
4916 continue;
4917
4918 /* Convert from "SET" to 'encoding' when needed. */
4919 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004920#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004922 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004923 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004924 if (pc == NULL)
4925 {
4926 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4927 fname, lnum, rline);
4928 continue;
4929 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004930 line = pc;
4931 }
4932 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004933#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004934 {
4935 pc = NULL;
4936 line = rline;
4937 }
4938
4939 /* Split the line up in white separated items. Put a NUL after each
4940 * item. */
4941 itemcnt = 0;
4942 for (p = line; ; )
4943 {
4944 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4945 ++p;
4946 if (*p == NUL)
4947 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004948 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004949 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004951 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952 ++p;
4953 if (*p == NUL)
4954 break;
4955 *p++ = NUL;
4956 }
4957
4958 /* Handle non-empty lines. */
4959 if (itemcnt > 0)
4960 {
4961 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4962 && aff->af_enc == NULL)
4963 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004964#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004965 /* Setup for conversion from "ENC" to 'encoding'. */
4966 aff->af_enc = enc_canonize(items[1]);
4967 if (aff->af_enc != NULL && !spin->si_ascii
4968 && convert_setup(&spin->si_conv, aff->af_enc,
4969 p_enc) == FAIL)
4970 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4971 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004972 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004973#else
4974 smsg((char_u *)_("Conversion in %s not supported"), fname);
4975#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004977 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4978 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004979 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004980 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004981 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004982 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004983 aff->af_flagtype = AFT_NUM;
4984 else if (STRCMP(items[1], "caplong") == 0)
4985 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004986 else
4987 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4988 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00004989 if (aff->af_rare != 0
4990 || aff->af_keepcase != 0
4991 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004992 || aff->af_needaffix != 0
4993 || aff->af_needcomp != 0
4994 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004995 || aff->af_suff.ht_used > 0
4996 || aff->af_pref.ht_used > 0)
4997 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4998 fname, lnum, items[1]);
4999 }
5000 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5001 && midword == NULL)
5002 {
5003 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005004 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005005 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005007 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005009 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005011 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005012 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005013 /* TODO: remove "RAR" later */
5014 else if ((STRCMP(items[0], "RAR") == 0
5015 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5016 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005017 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005018 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005019 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005020 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005021 /* TODO: remove "KEP" later */
5022 else if ((STRCMP(items[0], "KEP") == 0
5023 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5024 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005025 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005026 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005027 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005028 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005029 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5030 && aff->af_bad == 0)
5031 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005032 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5033 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005034 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005035 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5036 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005037 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005038 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5039 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005040 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005041 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5042 && aff->af_needcomp == 0)
5043 {
5044 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5045 fname, lnum);
5046 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005047 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005048 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005049 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005050 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
5051 * "Na" into "Na+", "1234" into "1234+". */
5052 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005053 if (p != NULL)
5054 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005055 STRCPY(p, items[1]);
5056 STRCAT(p, "+");
5057 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005058 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005059 }
5060 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
5061 {
5062 /* Concatenate this string to previously defined ones, using a
5063 * slash to separate them. */
5064 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005065 if (compflags != NULL)
5066 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005067 p = getroom(spin, l, FALSE);
5068 if (p != NULL)
5069 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005070 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005071 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005072 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005073 STRCAT(p, "/");
5074 }
5075 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005076 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005077 }
5078 }
5079 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005080 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005081 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005082 compmax = atoi((char *)items[1]);
5083 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005084 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
5085 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005086 }
5087 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005088 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005089 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005090 compminlen = atoi((char *)items[1]);
5091 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005092 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5093 fname, lnum, items[1]);
5094 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005095 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005096 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005097 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005098 compsylmax = atoi((char *)items[1]);
5099 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005100 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5101 fname, lnum, items[1]);
5102 }
5103 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005104 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005105 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005106 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005107 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005108 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5109 {
5110 spin->si_nobreak = TRUE;
5111 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005112 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5113 {
5114 spin->si_nosugfile = TRUE;
5115 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005116 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5117 {
5118 aff->af_pfxpostpone = TRUE;
5119 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005120 else if ((STRCMP(items[0], "PFX") == 0
5121 || STRCMP(items[0], "SFX") == 0)
5122 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005123 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005124 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005125 int lasti = 4;
5126 char_u key[AH_KEY_LEN];
5127
5128 if (*items[0] == 'P')
5129 tp = &aff->af_pref;
5130 else
5131 tp = &aff->af_suff;
5132
5133 /* Myspell allows the same affix name to be used multiple
5134 * times. The affix files that do this have an undocumented
5135 * "S" flag on all but the last block, thus we check for that
5136 * and store it in ah_follows. */
5137 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5138 hi = hash_find(tp, key);
5139 if (!HASHITEM_EMPTY(hi))
5140 {
5141 cur_aff = HI2AH(hi);
5142 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5143 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5144 fname, lnum, items[1]);
5145 if (!cur_aff->ah_follows)
5146 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5147 fname, lnum, items[1]);
5148 }
5149 else
5150 {
5151 /* New affix letter. */
5152 cur_aff = (affheader_T *)getroom(spin,
5153 sizeof(affheader_T), TRUE);
5154 if (cur_aff == NULL)
5155 break;
5156 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5157 fname, lnum);
5158 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5159 break;
5160 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005161 || cur_aff->ah_flag == aff->af_rare
5162 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005163 || cur_aff->ah_flag == aff->af_needaffix
5164 || cur_aff->ah_flag == aff->af_needcomp)
Bram Moolenaar371baa92005-12-29 22:43:53 +00005165 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00005166 fname, lnum, items[1]);
5167 STRCPY(cur_aff->ah_key, items[1]);
5168 hash_add(tp, cur_aff->ah_key);
5169
5170 cur_aff->ah_combine = (*items[2] == 'Y');
5171 }
5172
5173 /* Check for the "S" flag, which apparently means that another
5174 * block with the same affix name is following. */
5175 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5176 {
5177 ++lasti;
5178 cur_aff->ah_follows = TRUE;
5179 }
5180 else
5181 cur_aff->ah_follows = FALSE;
5182
Bram Moolenaar8db73182005-06-17 21:51:16 +00005183 /* Myspell allows extra text after the item, but that might
5184 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005185 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00005186 smsg((char_u *)_("Trailing text in %s line %d: %s"),
5187 fname, lnum, items[4]);
5188
Bram Moolenaar95529562005-08-25 21:21:38 +00005189 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005190 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5191 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005192
Bram Moolenaar95529562005-08-25 21:21:38 +00005193 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005194 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005195 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005196 {
5197 /* Use a new number in the .spl file later, to be able
5198 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005199 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005200 cur_aff->ah_newID = ++spin->si_newprefID;
5201
5202 /* We only really use ah_newID if the prefix is
5203 * postponed. We know that only after handling all
5204 * the items. */
5205 did_postpone_prefix = FALSE;
5206 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005207 else
5208 /* Did use the ID in a previous block. */
5209 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005210 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005211
Bram Moolenaar51485f02005-06-04 21:55:20 +00005212 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005213 }
5214 else if ((STRCMP(items[0], "PFX") == 0
5215 || STRCMP(items[0], "SFX") == 0)
5216 && aff_todo > 0
5217 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005218 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005219 {
5220 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005221 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005222 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005223 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005224 int lasti = 5;
5225
Bram Moolenaar5195e452005-08-19 20:32:47 +00005226 /* Check for "rare" and "nocomp" after the other info. */
5227 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005228 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005229 if (!rare && STRICMP(items[lasti], "rare") == 0)
5230 {
5231 rare = TRUE;
5232 ++lasti;
5233 }
5234 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
5235 {
5236 nocomp = TRUE;
5237 ++lasti;
5238 }
5239 else
5240 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005241 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005242
Bram Moolenaar8db73182005-06-17 21:51:16 +00005243 /* Myspell allows extra text after the item, but that might
5244 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005245 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005246 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005247
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005248 /* New item for an affix letter. */
5249 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005250 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005251 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252 if (aff_entry == NULL)
5253 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005254 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005255 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005256
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005257 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005258 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005260 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005261
Bram Moolenaar51485f02005-06-04 21:55:20 +00005262 /* Don't use an affix entry with non-ASCII characters when
5263 * "spin->si_ascii" is TRUE. */
5264 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005265 || has_non_ascii(aff_entry->ae_add)))
5266 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005267 aff_entry->ae_next = cur_aff->ah_first;
5268 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005269
5270 if (STRCMP(items[4], ".") != 0)
5271 {
5272 char_u buf[MAXLINELEN];
5273
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005274 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005275 if (*items[0] == 'P')
5276 sprintf((char *)buf, "^%s", items[4]);
5277 else
5278 sprintf((char *)buf, "%s$", items[4]);
5279 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005280 RE_MAGIC + RE_STRING + RE_STRICT);
5281 if (aff_entry->ae_prog == NULL)
5282 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5283 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005284 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005285
5286 /* For postponed prefixes we need an entry in si_prefcond
5287 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005288 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005289 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005290 /* When the chop string is one lower-case letter and
5291 * the add string ends in the upper-case letter we set
5292 * the "upper" flag, clear "ae_chop" and remove the
5293 * letters from "ae_add". The condition must either
5294 * be empty or start with the same letter. */
5295 if (aff_entry->ae_chop != NULL
5296 && aff_entry->ae_add != NULL
5297#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005298 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005299 aff_entry->ae_chop)] == NUL
5300#else
5301 && aff_entry->ae_chop[1] == NUL
5302#endif
5303 )
5304 {
5305 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005306
Bram Moolenaar53805d12005-08-01 07:08:33 +00005307 c = PTR2CHAR(aff_entry->ae_chop);
5308 c_up = SPELL_TOUPPER(c);
5309 if (c_up != c
5310 && (aff_entry->ae_cond == NULL
5311 || PTR2CHAR(aff_entry->ae_cond) == c))
5312 {
5313 p = aff_entry->ae_add
5314 + STRLEN(aff_entry->ae_add);
5315 mb_ptr_back(aff_entry->ae_add, p);
5316 if (PTR2CHAR(p) == c_up)
5317 {
5318 upper = TRUE;
5319 aff_entry->ae_chop = NULL;
5320 *p = NUL;
5321
5322 /* The condition is matched with the
5323 * actual word, thus must check for the
5324 * upper-case letter. */
5325 if (aff_entry->ae_cond != NULL)
5326 {
5327 char_u buf[MAXLINELEN];
5328#ifdef FEAT_MBYTE
5329 if (has_mbyte)
5330 {
5331 onecap_copy(items[4], buf, TRUE);
5332 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005333 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005334 }
5335 else
5336#endif
5337 *aff_entry->ae_cond = c_up;
5338 if (aff_entry->ae_cond != NULL)
5339 {
5340 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005341 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005342 vim_free(aff_entry->ae_prog);
5343 aff_entry->ae_prog = vim_regcomp(
5344 buf, RE_MAGIC + RE_STRING);
5345 }
5346 }
5347 }
5348 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005349 }
5350
Bram Moolenaar53805d12005-08-01 07:08:33 +00005351 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005352 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005353 int idx;
5354 char_u **pp;
5355 int n;
5356
Bram Moolenaar6de68532005-08-24 22:08:48 +00005357 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005358 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5359 --idx)
5360 {
5361 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5362 if (str_equal(p, aff_entry->ae_cond))
5363 break;
5364 }
5365 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5366 {
5367 /* Not found, add a new condition. */
5368 idx = spin->si_prefcond.ga_len++;
5369 pp = ((char_u **)spin->si_prefcond.ga_data)
5370 + idx;
5371 if (aff_entry->ae_cond == NULL)
5372 *pp = NULL;
5373 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005374 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005375 aff_entry->ae_cond);
5376 }
5377
5378 /* Add the prefix to the prefix tree. */
5379 if (aff_entry->ae_add == NULL)
5380 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005381 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005382 p = aff_entry->ae_add;
5383 /* PFX_FLAGS is a negative number, so that
5384 * tree_add_word() knows this is the prefix tree. */
5385 n = PFX_FLAGS;
5386 if (rare)
5387 n |= WFP_RARE;
5388 if (!cur_aff->ah_combine)
5389 n |= WFP_NC;
5390 if (upper)
5391 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005392 tree_add_word(spin, p, spin->si_prefroot, n,
5393 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005394 did_postpone_prefix = TRUE;
5395 }
5396
5397 /* Didn't actually use ah_newID, backup si_newprefID. */
5398 if (aff_todo == 0 && !did_postpone_prefix)
5399 {
5400 --spin->si_newprefID;
5401 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005402 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005403 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005404 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005405 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005406 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5407 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005408 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005409 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005410 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005411 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5412 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005413 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005414 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005415 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005416 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5417 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005418 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005419 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005420 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005421 else if ((STRCMP(items[0], "REP") == 0
5422 || STRCMP(items[0], "REPSAL") == 0)
5423 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005424 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005425 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005426 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005427 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005428 fname, lnum);
5429 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005430 else if ((STRCMP(items[0], "REP") == 0
5431 || STRCMP(items[0], "REPSAL") == 0)
5432 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005433 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005434 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005435 /* Myspell ignores extra arguments, we require it starts with
5436 * # to detect mistakes. */
5437 if (itemcnt > 3 && items[3][0] != '#')
5438 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005439 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005440 {
5441 /* Replace underscore with space (can't include a space
5442 * directly). */
5443 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5444 if (*p == '_')
5445 *p = ' ';
5446 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5447 if (*p == '_')
5448 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005449 add_fromto(spin, items[0][3] == 'S'
5450 ? &spin->si_repsal
5451 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005452 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005453 }
5454 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5455 {
5456 /* MAP item or count */
5457 if (!found_map)
5458 {
5459 /* First line contains the count. */
5460 found_map = TRUE;
5461 if (!isdigit(*items[1]))
5462 smsg((char_u *)_("Expected MAP count in %s line %d"),
5463 fname, lnum);
5464 }
5465 else if (do_map)
5466 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005467 int c;
5468
5469 /* Check that every character appears only once. */
5470 for (p = items[1]; *p != NUL; )
5471 {
5472#ifdef FEAT_MBYTE
5473 c = mb_ptr2char_adv(&p);
5474#else
5475 c = *p++;
5476#endif
5477 if ((spin->si_map.ga_len > 0
5478 && vim_strchr(spin->si_map.ga_data, c)
5479 != NULL)
5480 || vim_strchr(p, c) != NULL)
5481 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5482 fname, lnum);
5483 }
5484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005485 /* We simply concatenate all the MAP strings, separated by
5486 * slashes. */
5487 ga_concat(&spin->si_map, items[1]);
5488 ga_append(&spin->si_map, '/');
5489 }
5490 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005491 /* Accept "SAL from to" and "SAL from to # comment". */
5492 else if (STRCMP(items[0], "SAL") == 0
5493 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005494 {
5495 if (do_sal)
5496 {
5497 /* SAL item (sounds-a-like)
5498 * Either one of the known keys or a from-to pair. */
5499 if (STRCMP(items[1], "followup") == 0)
5500 spin->si_followup = sal_to_bool(items[2]);
5501 else if (STRCMP(items[1], "collapse_result") == 0)
5502 spin->si_collapse = sal_to_bool(items[2]);
5503 else if (STRCMP(items[1], "remove_accents") == 0)
5504 spin->si_rem_accents = sal_to_bool(items[2]);
5505 else
5506 /* when "to" is "_" it means empty */
5507 add_fromto(spin, &spin->si_sal, items[1],
5508 STRCMP(items[2], "_") == 0 ? (char_u *)""
5509 : items[2]);
5510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005511 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005512 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005513 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005514 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005515 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005516 }
5517 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005518 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005519 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005520 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005521 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005522 else if (STRCMP(items[0], "COMMON") == 0)
5523 {
5524 int i;
5525
5526 for (i = 1; i < itemcnt; ++i)
5527 {
5528 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5529 items[i])))
5530 {
5531 p = vim_strsave(items[i]);
5532 if (p == NULL)
5533 break;
5534 hash_add(&spin->si_commonwords, p);
5535 }
5536 }
5537 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005538 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005539 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005540 fname, lnum, items[0]);
5541 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005542 }
5543
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005544 if (fol != NULL || low != NULL || upp != NULL)
5545 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005546 if (spin->si_clear_chartab)
5547 {
5548 /* Clear the char type tables, don't want to use any of the
5549 * currently used spell properties. */
5550 init_spell_chartab();
5551 spin->si_clear_chartab = FALSE;
5552 }
5553
Bram Moolenaar3982c542005-06-08 21:56:31 +00005554 /*
5555 * Don't write a word table for an ASCII file, so that we don't check
5556 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005557 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005558 * mb_get_class(), the list of chars in the file will be incomplete.
5559 */
5560 if (!spin->si_ascii
5561#ifdef FEAT_MBYTE
5562 && !enc_utf8
5563#endif
5564 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005565 {
5566 if (fol == NULL || low == NULL || upp == NULL)
5567 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5568 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005569 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005570 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005571
5572 vim_free(fol);
5573 vim_free(low);
5574 vim_free(upp);
5575 }
5576
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005577 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005578 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005579 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005580 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5581 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005582 }
5583
Bram Moolenaar6de68532005-08-24 22:08:48 +00005584 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005585 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005586 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5587 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005588 }
5589
Bram Moolenaar6de68532005-08-24 22:08:48 +00005590 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005591 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005592 if (syllable == NULL)
5593 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5594 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5595 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005596 }
5597
Bram Moolenaar6de68532005-08-24 22:08:48 +00005598 if (compflags != NULL)
5599 process_compflags(spin, aff, compflags);
5600
5601 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005602 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005603 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005604 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005605 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005606 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005607 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005608 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005609 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005610 }
5611
Bram Moolenaar6de68532005-08-24 22:08:48 +00005612 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005613 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005614 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5615 spin->si_syllable = syllable;
5616 }
5617
5618 if (sofofrom != NULL || sofoto != NULL)
5619 {
5620 if (sofofrom == NULL || sofoto == NULL)
5621 smsg((char_u *)_("Missing SOFO%s line in %s"),
5622 sofofrom == NULL ? "FROM" : "TO", fname);
5623 else if (spin->si_sal.ga_len > 0)
5624 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005625 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005626 {
5627 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5628 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5629 spin->si_sofofr = sofofrom;
5630 spin->si_sofoto = sofoto;
5631 }
5632 }
5633
5634 if (midword != NULL)
5635 {
5636 aff_check_string(spin->si_midword, midword, "MIDWORD");
5637 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005638 }
5639
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005640 vim_free(pc);
5641 fclose(fd);
5642 return aff;
5643}
5644
5645/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005646 * Turn an affix flag name into a number, according to the FLAG type.
5647 * returns zero for failure.
5648 */
5649 static unsigned
5650affitem2flag(flagtype, item, fname, lnum)
5651 int flagtype;
5652 char_u *item;
5653 char_u *fname;
5654 int lnum;
5655{
5656 unsigned res;
5657 char_u *p = item;
5658
5659 res = get_affitem(flagtype, &p);
5660 if (res == 0)
5661 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005662 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005663 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5664 fname, lnum, item);
5665 else
5666 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5667 fname, lnum, item);
5668 }
5669 if (*p != NUL)
5670 {
5671 smsg((char_u *)_(e_affname), fname, lnum, item);
5672 return 0;
5673 }
5674
5675 return res;
5676}
5677
5678/*
5679 * Get one affix name from "*pp" and advance the pointer.
5680 * Returns zero for an error, still advances the pointer then.
5681 */
5682 static unsigned
5683get_affitem(flagtype, pp)
5684 int flagtype;
5685 char_u **pp;
5686{
5687 int res;
5688
Bram Moolenaar95529562005-08-25 21:21:38 +00005689 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005690 {
5691 if (!VIM_ISDIGIT(**pp))
5692 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005693 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005694 return 0;
5695 }
5696 res = getdigits(pp);
5697 }
5698 else
5699 {
5700#ifdef FEAT_MBYTE
5701 res = mb_ptr2char_adv(pp);
5702#else
5703 res = *(*pp)++;
5704#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005705 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005706 && res >= 'A' && res <= 'Z'))
5707 {
5708 if (**pp == NUL)
5709 return 0;
5710#ifdef FEAT_MBYTE
5711 res = mb_ptr2char_adv(pp) + (res << 16);
5712#else
5713 res = *(*pp)++ + (res << 16);
5714#endif
5715 }
5716 }
5717 return res;
5718}
5719
5720/*
5721 * Process the "compflags" string used in an affix file and append it to
5722 * spin->si_compflags.
5723 * The processing involves changing the affix names to ID numbers, so that
5724 * they fit in one byte.
5725 */
5726 static void
5727process_compflags(spin, aff, compflags)
5728 spellinfo_T *spin;
5729 afffile_T *aff;
5730 char_u *compflags;
5731{
5732 char_u *p;
5733 char_u *prevp;
5734 unsigned flag;
5735 compitem_T *ci;
5736 int id;
5737 int len;
5738 char_u *tp;
5739 char_u key[AH_KEY_LEN];
5740 hashitem_T *hi;
5741
5742 /* Make room for the old and the new compflags, concatenated with a / in
5743 * between. Processing it makes it shorter, but we don't know by how
5744 * much, thus allocate the maximum. */
5745 len = STRLEN(compflags) + 1;
5746 if (spin->si_compflags != NULL)
5747 len += STRLEN(spin->si_compflags) + 1;
5748 p = getroom(spin, len, FALSE);
5749 if (p == NULL)
5750 return;
5751 if (spin->si_compflags != NULL)
5752 {
5753 STRCPY(p, spin->si_compflags);
5754 STRCAT(p, "/");
5755 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005756 spin->si_compflags = p;
5757 tp = p + STRLEN(p);
5758
5759 for (p = compflags; *p != NUL; )
5760 {
5761 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5762 /* Copy non-flag characters directly. */
5763 *tp++ = *p++;
5764 else
5765 {
5766 /* First get the flag number, also checks validity. */
5767 prevp = p;
5768 flag = get_affitem(aff->af_flagtype, &p);
5769 if (flag != 0)
5770 {
5771 /* Find the flag in the hashtable. If it was used before, use
5772 * the existing ID. Otherwise add a new entry. */
5773 vim_strncpy(key, prevp, p - prevp);
5774 hi = hash_find(&aff->af_comp, key);
5775 if (!HASHITEM_EMPTY(hi))
5776 id = HI2CI(hi)->ci_newID;
5777 else
5778 {
5779 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5780 if (ci == NULL)
5781 break;
5782 STRCPY(ci->ci_key, key);
5783 ci->ci_flag = flag;
5784 /* Avoid using a flag ID that has a special meaning in a
5785 * regexp (also inside []). */
5786 do
5787 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005788 check_renumber(spin);
5789 id = spin->si_newcompID--;
5790 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005791 ci->ci_newID = id;
5792 hash_add(&aff->af_comp, ci->ci_key);
5793 }
5794 *tp++ = id;
5795 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005796 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005797 ++p;
5798 }
5799 }
5800
5801 *tp = NUL;
5802}
5803
5804/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005805 * Check that the new IDs for postponed affixes and compounding don't overrun
5806 * each other. We have almost 255 available, but start at 0-127 to avoid
5807 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5808 * When that is used up an error message is given.
5809 */
5810 static void
5811check_renumber(spin)
5812 spellinfo_T *spin;
5813{
5814 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5815 {
5816 spin->si_newprefID = 127;
5817 spin->si_newcompID = 255;
5818 }
5819}
5820
5821/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005822 * Return TRUE if flag "flag" appears in affix list "afflist".
5823 */
5824 static int
5825flag_in_afflist(flagtype, afflist, flag)
5826 int flagtype;
5827 char_u *afflist;
5828 unsigned flag;
5829{
5830 char_u *p;
5831 unsigned n;
5832
5833 switch (flagtype)
5834 {
5835 case AFT_CHAR:
5836 return vim_strchr(afflist, flag) != NULL;
5837
Bram Moolenaar95529562005-08-25 21:21:38 +00005838 case AFT_CAPLONG:
5839 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005840 for (p = afflist; *p != NUL; )
5841 {
5842#ifdef FEAT_MBYTE
5843 n = mb_ptr2char_adv(&p);
5844#else
5845 n = *p++;
5846#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005847 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005848 && *p != NUL)
5849#ifdef FEAT_MBYTE
5850 n = mb_ptr2char_adv(&p) + (n << 16);
5851#else
5852 n = *p++ + (n << 16);
5853#endif
5854 if (n == flag)
5855 return TRUE;
5856 }
5857 break;
5858
Bram Moolenaar95529562005-08-25 21:21:38 +00005859 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005860 for (p = afflist; *p != NUL; )
5861 {
5862 n = getdigits(&p);
5863 if (n == flag)
5864 return TRUE;
5865 if (*p != NUL) /* skip over comma */
5866 ++p;
5867 }
5868 break;
5869 }
5870 return FALSE;
5871}
5872
5873/*
5874 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5875 */
5876 static void
5877aff_check_number(spinval, affval, name)
5878 int spinval;
5879 int affval;
5880 char *name;
5881{
5882 if (spinval != 0 && spinval != affval)
5883 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5884}
5885
5886/*
5887 * Give a warning when "spinval" and "affval" strings are set and not the same.
5888 */
5889 static void
5890aff_check_string(spinval, affval, name)
5891 char_u *spinval;
5892 char_u *affval;
5893 char *name;
5894{
5895 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5896 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5897}
5898
5899/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005900 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5901 * NULL as equal.
5902 */
5903 static int
5904str_equal(s1, s2)
5905 char_u *s1;
5906 char_u *s2;
5907{
5908 if (s1 == NULL || s2 == NULL)
5909 return s1 == s2;
5910 return STRCMP(s1, s2) == 0;
5911}
5912
5913/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005914 * Add a from-to item to "gap". Used for REP and SAL items.
5915 * They are stored case-folded.
5916 */
5917 static void
5918add_fromto(spin, gap, from, to)
5919 spellinfo_T *spin;
5920 garray_T *gap;
5921 char_u *from;
5922 char_u *to;
5923{
5924 fromto_T *ftp;
5925 char_u word[MAXWLEN];
5926
5927 if (ga_grow(gap, 1) == OK)
5928 {
5929 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5930 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005931 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005932 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005933 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005934 ++gap->ga_len;
5935 }
5936}
5937
5938/*
5939 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5940 */
5941 static int
5942sal_to_bool(s)
5943 char_u *s;
5944{
5945 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5946}
5947
5948/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005949 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5950 * When "s" is NULL FALSE is returned.
5951 */
5952 static int
5953has_non_ascii(s)
5954 char_u *s;
5955{
5956 char_u *p;
5957
5958 if (s != NULL)
5959 for (p = s; *p != NUL; ++p)
5960 if (*p >= 128)
5961 return TRUE;
5962 return FALSE;
5963}
5964
5965/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005966 * Free the structure filled by spell_read_aff().
5967 */
5968 static void
5969spell_free_aff(aff)
5970 afffile_T *aff;
5971{
5972 hashtab_T *ht;
5973 hashitem_T *hi;
5974 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005975 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005976 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005977
5978 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005979
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005980 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005981 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5982 {
5983 todo = ht->ht_used;
5984 for (hi = ht->ht_array; todo > 0; ++hi)
5985 {
5986 if (!HASHITEM_EMPTY(hi))
5987 {
5988 --todo;
5989 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005990 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5991 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005992 }
5993 }
5994 if (ht == &aff->af_suff)
5995 break;
5996 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005997
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005998 hash_clear(&aff->af_pref);
5999 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006000 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006001}
6002
6003/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006004 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006005 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006006 */
6007 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006008spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006009 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006010 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006011 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006012{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006013 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006014 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006015 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006016 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006017 char_u store_afflist[MAXWLEN];
6018 int pfxlen;
6019 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006020 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006021 char_u *pc;
6022 char_u *w;
6023 int l;
6024 hash_T hash;
6025 hashitem_T *hi;
6026 FILE *fd;
6027 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006028 int non_ascii = 0;
6029 int retval = OK;
6030 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006031 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006032 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006033
Bram Moolenaar51485f02005-06-04 21:55:20 +00006034 /*
6035 * Open the file.
6036 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006037 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006038 if (fd == NULL)
6039 {
6040 EMSG2(_(e_notopen), fname);
6041 return FAIL;
6042 }
6043
Bram Moolenaar51485f02005-06-04 21:55:20 +00006044 /* The hashtable is only used to detect duplicated words. */
6045 hash_init(&ht);
6046
Bram Moolenaar4770d092006-01-12 23:22:24 +00006047 vim_snprintf((char *)IObuff, IOSIZE,
6048 _("Reading dictionary file %s ..."), fname);
6049 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006050
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006051 /* start with a message for the first line */
6052 spin->si_msg_count = 999999;
6053
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006054 /* Read and ignore the first line: word count. */
6055 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006056 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006057 EMSG2(_("E760: No word count in %s"), fname);
6058
6059 /*
6060 * Read all the lines in the file one by one.
6061 * The words are converted to 'encoding' here, before being added to
6062 * the hashtable.
6063 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006064 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006065 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006066 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006067 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006068 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006069 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006070
Bram Moolenaar51485f02005-06-04 21:55:20 +00006071 /* Remove CR, LF and white space from the end. White space halfway
6072 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006073 l = STRLEN(line);
6074 while (l > 0 && line[l - 1] <= ' ')
6075 --l;
6076 if (l == 0)
6077 continue; /* empty line */
6078 line[l] = NUL;
6079
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006080 /* Truncate the word at the "/", set "afflist" to what follows.
6081 * Replace "\/" by "/" and "\\" by "\". */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006082 afflist = NULL;
6083 for (p = line; *p != NUL; mb_ptr_adv(p))
6084 {
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006085 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6086 mch_memmove(p, p + 1, STRLEN(p));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006087 else if (*p == '/')
6088 {
6089 *p = NUL;
6090 afflist = p + 1;
6091 break;
6092 }
6093 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006094
6095 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6096 if (spin->si_ascii && has_non_ascii(line))
6097 {
6098 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006099 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006100 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00006101
Bram Moolenaarb765d632005-06-07 21:00:02 +00006102#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006103 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006104 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006105 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006106 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006107 if (pc == NULL)
6108 {
6109 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6110 fname, lnum, line);
6111 continue;
6112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006113 w = pc;
6114 }
6115 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006116#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006117 {
6118 pc = NULL;
6119 w = line;
6120 }
6121
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006122 /* This takes time, print a message every 10000 words. */
6123 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006124 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006125 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006126 vim_snprintf((char *)message, sizeof(message),
6127 _("line %6d, word %6d - %s"),
6128 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6129 msg_start();
6130 msg_puts_long_attr(message, 0);
6131 msg_clr_eos();
6132 msg_didout = FALSE;
6133 msg_col = 0;
6134 out_flush();
6135 }
6136
Bram Moolenaar51485f02005-06-04 21:55:20 +00006137 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006138 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006139 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006140 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006141 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006142 if (retval == FAIL)
6143 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006144
Bram Moolenaar51485f02005-06-04 21:55:20 +00006145 hash = hash_hash(dw);
6146 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006147 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006148 {
6149 if (p_verbose > 0)
6150 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006151 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006152 else if (duplicate == 0)
6153 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6154 fname, lnum, dw);
6155 ++duplicate;
6156 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006157 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006158 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006159
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006160 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006161 store_afflist[0] = NUL;
6162 pfxlen = 0;
6163 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006164 if (afflist != NULL)
6165 {
6166 /* Check for affix name that stands for keep-case word and stands
6167 * for rare word (if defined). */
Bram Moolenaar371baa92005-12-29 22:43:53 +00006168 if (affile->af_keepcase != 0 && flag_in_afflist(
6169 affile->af_flagtype, afflist, affile->af_keepcase))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006170 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar371baa92005-12-29 22:43:53 +00006171 if (affile->af_rare != 0 && flag_in_afflist(
6172 affile->af_flagtype, afflist, affile->af_rare))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006173 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006174 if (affile->af_bad != 0 && flag_in_afflist(
6175 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006176 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006177 if (affile->af_needaffix != 0 && flag_in_afflist(
6178 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006179 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006180 if (affile->af_needcomp != 0 && flag_in_afflist(
6181 affile->af_flagtype, afflist, affile->af_needcomp))
6182 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006183
6184 if (affile->af_pfxpostpone)
6185 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006186 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006187
Bram Moolenaar5195e452005-08-19 20:32:47 +00006188 if (spin->si_compflags != NULL)
6189 /* Need to store the list of compound flags with the word.
6190 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006191 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006192 }
6193
Bram Moolenaar51485f02005-06-04 21:55:20 +00006194 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006195 if (store_word(spin, dw, flags, spin->si_region,
6196 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006197 retval = FAIL;
6198
6199 if (afflist != NULL)
6200 {
6201 /* Find all matching suffixes and add the resulting words.
6202 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006203 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006204 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006205 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006206 retval = FAIL;
6207
6208 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006209 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006210 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006211 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006212 retval = FAIL;
6213 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006214 }
6215
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006216 if (duplicate > 0)
6217 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006218 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006219 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6220 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006221 hash_clear(&ht);
6222
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006223 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006224 return retval;
6225}
6226
6227/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006228 * Get the list of prefix IDs from the affix list "afflist".
6229 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006230 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6231 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006232 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006233 static int
6234get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006235 afffile_T *affile;
6236 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006237 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006238{
6239 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006240 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006241 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006242 int id;
6243 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006244 hashitem_T *hi;
6245
Bram Moolenaar6de68532005-08-24 22:08:48 +00006246 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006247 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006248 prevp = p;
6249 if (get_affitem(affile->af_flagtype, &p) != 0)
6250 {
6251 /* A flag is a postponed prefix flag if it appears in "af_pref"
6252 * and it's ID is not zero. */
6253 vim_strncpy(key, prevp, p - prevp);
6254 hi = hash_find(&affile->af_pref, key);
6255 if (!HASHITEM_EMPTY(hi))
6256 {
6257 id = HI2AH(hi)->ah_newID;
6258 if (id != 0)
6259 store_afflist[cnt++] = id;
6260 }
6261 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006262 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006263 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006264 }
6265
Bram Moolenaar5195e452005-08-19 20:32:47 +00006266 store_afflist[cnt] = NUL;
6267 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006268}
6269
6270/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006271 * Get the list of compound IDs from the affix list "afflist" that are used
6272 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006273 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006274 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006275 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006276get_compflags(affile, afflist, store_afflist)
6277 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006278 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006279 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006280{
6281 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006282 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006283 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006284 char_u key[AH_KEY_LEN];
6285 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006286
Bram Moolenaar6de68532005-08-24 22:08:48 +00006287 for (p = afflist; *p != NUL; )
6288 {
6289 prevp = p;
6290 if (get_affitem(affile->af_flagtype, &p) != 0)
6291 {
6292 /* A flag is a compound flag if it appears in "af_comp". */
6293 vim_strncpy(key, prevp, p - prevp);
6294 hi = hash_find(&affile->af_comp, key);
6295 if (!HASHITEM_EMPTY(hi))
6296 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6297 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006298 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006299 ++p;
6300 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006301
Bram Moolenaar5195e452005-08-19 20:32:47 +00006302 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006303}
6304
6305/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006306 * Apply affixes to a word and store the resulting words.
6307 * "ht" is the hashtable with affentry_T that need to be applied, either
6308 * prefixes or suffixes.
6309 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6310 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006311 *
6312 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006313 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006314 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006315store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
6316 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006317 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006318 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006319 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006320 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006321 hashtab_T *ht;
6322 hashtab_T *xht;
6323 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006324 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006325 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006326 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6327 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006328{
6329 int todo;
6330 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006331 affheader_T *ah;
6332 affentry_T *ae;
6333 regmatch_T regmatch;
6334 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006335 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006336 int i;
6337 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006338 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006339 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006340 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006341 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006342
Bram Moolenaar51485f02005-06-04 21:55:20 +00006343 todo = ht->ht_used;
6344 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006345 {
6346 if (!HASHITEM_EMPTY(hi))
6347 {
6348 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006349 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006350
Bram Moolenaar51485f02005-06-04 21:55:20 +00006351 /* Check that the affix combines, if required, and that the word
6352 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006353 if ((!comb || ah->ah_combine) && flag_in_afflist(
6354 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006355 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006356 /* Loop over all affix entries with this name. */
6357 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006358 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006359 /* Check the condition. It's not logical to match case
6360 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006361 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006362 * Another requirement from Myspell is that the chop
6363 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006364 * For prefixes, when "PFXPOSTPONE" was used, only do
6365 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006366 regmatch.regprog = ae->ae_prog;
6367 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006368 if ((xht != NULL || !affile->af_pfxpostpone
6369 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006370 && (ae->ae_chop == NULL
6371 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006372 && (ae->ae_prog == NULL
6373 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006374 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006375 /* Match. Remove the chop and add the affix. */
6376 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006377 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006378 /* prefix: chop/add at the start of the word */
6379 if (ae->ae_add == NULL)
6380 *newword = NUL;
6381 else
6382 STRCPY(newword, ae->ae_add);
6383 p = word;
6384 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006385 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006386 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006387#ifdef FEAT_MBYTE
6388 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006389 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006390 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006391 for ( ; i > 0; --i)
6392 mb_ptr_adv(p);
6393 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006394 else
6395#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006396 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006397 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006398 STRCAT(newword, p);
6399 }
6400 else
6401 {
6402 /* suffix: chop/add at the end of the word */
6403 STRCPY(newword, word);
6404 if (ae->ae_chop != NULL)
6405 {
6406 /* Remove chop string. */
6407 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006408 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006409 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006410 mb_ptr_back(newword, p);
6411 *p = NUL;
6412 }
6413 if (ae->ae_add != NULL)
6414 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006415 }
6416
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006417 /* Obey the "rare" flag of the affix. */
6418 if (ae->ae_rare)
6419 use_flags = flags | WF_RARE;
6420 else
6421 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006422
6423 /* Obey the "nocomp" flag of the affix: don't use the
6424 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006425 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006426 if (ae->ae_nocomp && pfxlist != NULL)
6427 {
6428 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6429 use_pfxlist = pfx_pfxlist;
6430 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006431
6432 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006433 if (spin->si_prefroot != NULL
6434 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006435 {
6436 /* ... add a flag to indicate an affix was used. */
6437 use_flags |= WF_HAS_AFF;
6438
6439 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006440 * affixes is not allowed. But do use the
6441 * compound flags after them. */
6442 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6443 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006444 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006445
Bram Moolenaar51485f02005-06-04 21:55:20 +00006446 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006447 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006448 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006449 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006450
Bram Moolenaar51485f02005-06-04 21:55:20 +00006451 /* When added a suffix and combining is allowed also
6452 * try adding prefixes additionally. */
6453 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006454 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006455 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006456 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006457 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006458 }
6459 }
6460 }
6461 }
6462 }
6463
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006464 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006465}
6466
6467/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006468 * Read a file with a list of words.
6469 */
6470 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006471spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006472 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006473 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006474{
6475 FILE *fd;
6476 long lnum = 0;
6477 char_u rline[MAXLINELEN];
6478 char_u *line;
6479 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006480 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006481 int l;
6482 int retval = OK;
6483 int did_word = FALSE;
6484 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006485 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006486 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006487
6488 /*
6489 * Open the file.
6490 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006491 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006492 if (fd == NULL)
6493 {
6494 EMSG2(_(e_notopen), fname);
6495 return FAIL;
6496 }
6497
Bram Moolenaar4770d092006-01-12 23:22:24 +00006498 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6499 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006500
6501 /*
6502 * Read all the lines in the file one by one.
6503 */
6504 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6505 {
6506 line_breakcheck();
6507 ++lnum;
6508
6509 /* Skip comment lines. */
6510 if (*rline == '#')
6511 continue;
6512
6513 /* Remove CR, LF and white space from the end. */
6514 l = STRLEN(rline);
6515 while (l > 0 && rline[l - 1] <= ' ')
6516 --l;
6517 if (l == 0)
6518 continue; /* empty or blank line */
6519 rline[l] = NUL;
6520
6521 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6522 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006523#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006524 if (spin->si_conv.vc_type != CONV_NONE)
6525 {
6526 pc = string_convert(&spin->si_conv, rline, NULL);
6527 if (pc == NULL)
6528 {
6529 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6530 fname, lnum, rline);
6531 continue;
6532 }
6533 line = pc;
6534 }
6535 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006536#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006537 {
6538 pc = NULL;
6539 line = rline;
6540 }
6541
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006542 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006543 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006544 ++line;
6545 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 {
6547 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006548 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6549 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006550 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006551 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6552 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006553 else
6554 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006555#ifdef FEAT_MBYTE
6556 char_u *enc;
6557
Bram Moolenaar51485f02005-06-04 21:55:20 +00006558 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006559 line += 10;
6560 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 if (enc != NULL && !spin->si_ascii
6562 && convert_setup(&spin->si_conv, enc,
6563 p_enc) == FAIL)
6564 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006565 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006566 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006567 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006568#else
6569 smsg((char_u *)_("Conversion in %s not supported"), fname);
6570#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006571 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006572 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006573 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006574
Bram Moolenaar3982c542005-06-08 21:56:31 +00006575 if (STRNCMP(line, "regions=", 8) == 0)
6576 {
6577 if (spin->si_region_count > 1)
6578 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6579 fname, lnum, line);
6580 else
6581 {
6582 line += 8;
6583 if (STRLEN(line) > 16)
6584 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6585 fname, lnum, line);
6586 else
6587 {
6588 spin->si_region_count = STRLEN(line) / 2;
6589 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006590
6591 /* Adjust the mask for a word valid in all regions. */
6592 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006593 }
6594 }
6595 continue;
6596 }
6597
Bram Moolenaar7887d882005-07-01 22:33:52 +00006598 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6599 fname, lnum, line - 1);
6600 continue;
6601 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006602
Bram Moolenaar7887d882005-07-01 22:33:52 +00006603 flags = 0;
6604 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006605
Bram Moolenaar7887d882005-07-01 22:33:52 +00006606 /* Check for flags and region after a slash. */
6607 p = vim_strchr(line, '/');
6608 if (p != NULL)
6609 {
6610 *p++ = NUL;
6611 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006612 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006613 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006614 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006615 else if (*p == '!') /* Bad, bad, wicked word. */
6616 flags |= WF_BANNED;
6617 else if (*p == '?') /* Rare word. */
6618 flags |= WF_RARE;
6619 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006620 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006621 if ((flags & WF_REGION) == 0) /* first one */
6622 regionmask = 0;
6623 flags |= WF_REGION;
6624
6625 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006626 if (l > spin->si_region_count)
6627 {
6628 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006629 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006630 break;
6631 }
6632 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006633 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006634 else
6635 {
6636 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6637 fname, lnum, p);
6638 break;
6639 }
6640 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006641 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006642 }
6643
6644 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6645 if (spin->si_ascii && has_non_ascii(line))
6646 {
6647 ++non_ascii;
6648 continue;
6649 }
6650
6651 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006652 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006653 {
6654 retval = FAIL;
6655 break;
6656 }
6657 did_word = TRUE;
6658 }
6659
6660 vim_free(pc);
6661 fclose(fd);
6662
Bram Moolenaar4770d092006-01-12 23:22:24 +00006663 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006664 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006665 vim_snprintf((char *)IObuff, IOSIZE,
6666 _("Ignored %d words with non-ASCII characters"), non_ascii);
6667 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006668 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006669
Bram Moolenaar51485f02005-06-04 21:55:20 +00006670 return retval;
6671}
6672
6673/*
6674 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006675 * This avoids calling free() for every little struct we use (and keeping
6676 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006677 * The memory is cleared to all zeros.
6678 * Returns NULL when out of memory.
6679 */
6680 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006681getroom(spin, len, align)
6682 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006683 size_t len; /* length needed */
6684 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006685{
6686 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006687 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006688
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006689 if (align && bl != NULL)
6690 /* Round size up for alignment. On some systems structures need to be
6691 * aligned to the size of a pointer (e.g., SPARC). */
6692 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6693 & ~(sizeof(char *) - 1);
6694
Bram Moolenaar51485f02005-06-04 21:55:20 +00006695 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6696 {
6697 /* Allocate a block of memory. This is not freed until much later. */
6698 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6699 if (bl == NULL)
6700 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006701 bl->sb_next = spin->si_blocks;
6702 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006703 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006704 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006705 }
6706
6707 p = bl->sb_data + bl->sb_used;
6708 bl->sb_used += len;
6709
6710 return p;
6711}
6712
6713/*
6714 * Make a copy of a string into memory allocated with getroom().
6715 */
6716 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006717getroom_save(spin, s)
6718 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006719 char_u *s;
6720{
6721 char_u *sc;
6722
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006723 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006724 if (sc != NULL)
6725 STRCPY(sc, s);
6726 return sc;
6727}
6728
6729
6730/*
6731 * Free the list of allocated sblock_T.
6732 */
6733 static void
6734free_blocks(bl)
6735 sblock_T *bl;
6736{
6737 sblock_T *next;
6738
6739 while (bl != NULL)
6740 {
6741 next = bl->sb_next;
6742 vim_free(bl);
6743 bl = next;
6744 }
6745}
6746
6747/*
6748 * Allocate the root of a word tree.
6749 */
6750 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006751wordtree_alloc(spin)
6752 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006753{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006754 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006755}
6756
6757/*
6758 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006759 * Always store it in the case-folded tree. For a keep-case word this is
6760 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6761 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006762 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006763 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6764 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006765 */
6766 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006767store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006769 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006770 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006771 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006772 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006773 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006774{
6775 int len = STRLEN(word);
6776 int ct = captype(word, word + len);
6777 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006778 int res = OK;
6779 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006781 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006782 for (p = pfxlist; res == OK; ++p)
6783 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006784 if (!need_affix || (p != NULL && *p != NUL))
6785 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006786 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006787 if (p == NULL || *p == NUL)
6788 break;
6789 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006790 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006791
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006792 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006793 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006794 for (p = pfxlist; res == OK; ++p)
6795 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006796 if (!need_affix || (p != NULL && *p != NUL))
6797 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006798 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006799 if (p == NULL || *p == NUL)
6800 break;
6801 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006802 ++spin->si_keepwcount;
6803 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006804 return res;
6805}
6806
6807/*
6808 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00006809 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006810 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006811 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006812 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006813 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006814tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006815 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006816 char_u *word;
6817 wordnode_T *root;
6818 int flags;
6819 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006820 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006821{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006822 wordnode_T *node = root;
6823 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006824 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006825 wordnode_T **prev = NULL;
6826 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006827
Bram Moolenaar51485f02005-06-04 21:55:20 +00006828 /* Add each byte of the word to the tree, including the NUL at the end. */
6829 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006830 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006831 /* When there is more than one reference to this node we need to make
6832 * a copy, so that we can modify it. Copy the whole list of siblings
6833 * (we don't optimize for a partly shared list of siblings). */
6834 if (node != NULL && node->wn_refs > 1)
6835 {
6836 --node->wn_refs;
6837 copyprev = prev;
6838 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6839 {
6840 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006841 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006842 if (np == NULL)
6843 return FAIL;
6844 np->wn_child = copyp->wn_child;
6845 if (np->wn_child != NULL)
6846 ++np->wn_child->wn_refs; /* child gets extra ref */
6847 np->wn_byte = copyp->wn_byte;
6848 if (np->wn_byte == NUL)
6849 {
6850 np->wn_flags = copyp->wn_flags;
6851 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006852 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006853 }
6854
6855 /* Link the new node in the list, there will be one ref. */
6856 np->wn_refs = 1;
6857 *copyprev = np;
6858 copyprev = &np->wn_sibling;
6859
6860 /* Let "node" point to the head of the copied list. */
6861 if (copyp == node)
6862 node = np;
6863 }
6864 }
6865
Bram Moolenaar51485f02005-06-04 21:55:20 +00006866 /* Look for the sibling that has the same character. They are sorted
6867 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006868 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006869 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006870 while (node != NULL
6871 && (node->wn_byte < word[i]
6872 || (node->wn_byte == NUL
6873 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00006874 ? node->wn_affixID < (unsigned)affixID
6875 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006876 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006877 && (spin->si_sugtree
6878 ? (node->wn_region & 0xffff) < region
6879 : node->wn_affixID
6880 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006881 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006882 prev = &node->wn_sibling;
6883 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006884 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006885 if (node == NULL
6886 || node->wn_byte != word[i]
6887 || (word[i] == NUL
6888 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00006889 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006890 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006891 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006892 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006893 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006894 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006895 if (np == NULL)
6896 return FAIL;
6897 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006898
6899 /* If "node" is NULL this is a new child or the end of the sibling
6900 * list: ref count is one. Otherwise use ref count of sibling and
6901 * make ref count of sibling one (matters when inserting in front
6902 * of the list of siblings). */
6903 if (node == NULL)
6904 np->wn_refs = 1;
6905 else
6906 {
6907 np->wn_refs = node->wn_refs;
6908 node->wn_refs = 1;
6909 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006910 *prev = np;
6911 np->wn_sibling = node;
6912 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006913 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006914
Bram Moolenaar51485f02005-06-04 21:55:20 +00006915 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006916 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006917 node->wn_flags = flags;
6918 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006919 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006921 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006922 prev = &node->wn_child;
6923 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006924 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006925#ifdef SPELL_PRINTTREE
6926 smsg("Added \"%s\"", word);
6927 spell_print_tree(root->wn_sibling);
6928#endif
6929
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006930 /* count nr of words added since last message */
6931 ++spin->si_msg_count;
6932
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006933 if (spin->si_compress_cnt > 1)
6934 {
6935 if (--spin->si_compress_cnt == 1)
6936 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006937 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006938 }
6939
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006940 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006941 * When we have allocated lots of memory we need to compress the word tree
6942 * to free up some room. But compression is slow, and we might actually
6943 * need that room, thus only compress in the following situations:
6944 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006945 * "compress_start" blocks.
6946 * 2. When compressed before and used "compress_inc" blocks before
6947 * adding "compress_added" words (si_compress_cnt > 1).
6948 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006949 * (si_compress_cnt == 1) and the number of free nodes drops below the
6950 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006951 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006952#ifndef SPELL_PRINTTREE
6953 if (spin->si_compress_cnt == 1
6954 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006955 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006956#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006957 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006958 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006959 * when the freed up room has been used and another "compress_inc"
6960 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006961 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006962 spin->si_blocks_cnt -= compress_inc;
6963 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006964
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006965 if (spin->si_verbose)
6966 {
6967 msg_start();
6968 msg_puts((char_u *)_(msg_compressing));
6969 msg_clr_eos();
6970 msg_didout = FALSE;
6971 msg_col = 0;
6972 out_flush();
6973 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006974
6975 /* Compress both trees. Either they both have many nodes, which makes
6976 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00006977 * compression goes fast. But when filling the souldfold word tree
6978 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006979 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006980 if (affixID >= 0)
6981 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006982 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006983
6984 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006985}
6986
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006987/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006988 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6989 * Sets "sps_flags".
6990 */
6991 int
6992spell_check_msm()
6993{
6994 char_u *p = p_msm;
6995 long start = 0;
6996 long inc = 0;
6997 long added = 0;
6998
6999 if (!VIM_ISDIGIT(*p))
7000 return FAIL;
7001 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7002 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7003 if (*p != ',')
7004 return FAIL;
7005 ++p;
7006 if (!VIM_ISDIGIT(*p))
7007 return FAIL;
7008 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7009 if (*p != ',')
7010 return FAIL;
7011 ++p;
7012 if (!VIM_ISDIGIT(*p))
7013 return FAIL;
7014 added = getdigits(&p) * 1024;
7015 if (*p != NUL)
7016 return FAIL;
7017
7018 if (start == 0 || inc == 0 || added == 0 || inc > start)
7019 return FAIL;
7020
7021 compress_start = start;
7022 compress_inc = inc;
7023 compress_added = added;
7024 return OK;
7025}
7026
7027
7028/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007029 * Get a wordnode_T, either from the list of previously freed nodes or
7030 * allocate a new one.
7031 */
7032 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007033get_wordnode(spin)
7034 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007035{
7036 wordnode_T *n;
7037
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007038 if (spin->si_first_free == NULL)
7039 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007040 else
7041 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007042 n = spin->si_first_free;
7043 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007044 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007045 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007046 }
7047#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007048 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007049#endif
7050 return n;
7051}
7052
7053/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007054 * Decrement the reference count on a node (which is the head of a list of
7055 * siblings). If the reference count becomes zero free the node and its
7056 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007057 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007058 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007059 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007060deref_wordnode(spin, node)
7061 spellinfo_T *spin;
7062 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007063{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007064 wordnode_T *np;
7065 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007066
7067 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007068 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007069 for (np = node; np != NULL; np = np->wn_sibling)
7070 {
7071 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007072 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007073 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007074 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007075 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007076 ++cnt; /* length field */
7077 }
7078 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007079}
7080
7081/*
7082 * Free a wordnode_T for re-use later.
7083 * Only the "wn_child" field becomes invalid.
7084 */
7085 static void
7086free_wordnode(spin, n)
7087 spellinfo_T *spin;
7088 wordnode_T *n;
7089{
7090 n->wn_child = spin->si_first_free;
7091 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007092 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007093}
7094
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007095/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007096 * Compress a tree: find tails that are identical and can be shared.
7097 */
7098 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007099wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007100 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007101 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007102{
7103 hashtab_T ht;
7104 int n;
7105 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007106 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007107
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007108 /* Skip the root itself, it's not actually used. The first sibling is the
7109 * start of the tree. */
7110 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007111 {
7112 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007113 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007114
7115#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007116 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007117#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007118 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007119 if (tot > 1000000)
7120 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007121 else if (tot == 0)
7122 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007123 else
7124 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007125 vim_snprintf((char *)IObuff, IOSIZE,
7126 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7127 n, tot, tot - n, perc);
7128 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007129 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007130#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007131 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007132#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007133 hash_clear(&ht);
7134 }
7135}
7136
7137/*
7138 * Compress a node, its siblings and its children, depth first.
7139 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007140 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007141 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007142node_compress(spin, node, ht, tot)
7143 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007144 wordnode_T *node;
7145 hashtab_T *ht;
7146 int *tot; /* total count of nodes before compressing,
7147 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007148{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007149 wordnode_T *np;
7150 wordnode_T *tp;
7151 wordnode_T *child;
7152 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007153 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007154 int len = 0;
7155 unsigned nr, n;
7156 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007157
Bram Moolenaar51485f02005-06-04 21:55:20 +00007158 /*
7159 * Go through the list of siblings. Compress each child and then try
7160 * finding an identical child to replace it.
7161 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007162 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007163 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007164 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007165 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007166 ++len;
7167 if ((child = np->wn_child) != NULL)
7168 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007169 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007170 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007171
7172 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007173 hash = hash_hash(child->wn_u1.hashkey);
7174 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007175 if (!HASHITEM_EMPTY(hi))
7176 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007177 /* There are children we encountered before with a hash value
7178 * identical to the current child. Now check if there is one
7179 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007180 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007181 if (node_equal(child, tp))
7182 {
7183 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007184 * current one. This means the current child and all
7185 * its siblings is unlinked from the tree. */
7186 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007187 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007188 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007189 break;
7190 }
7191 if (tp == NULL)
7192 {
7193 /* No other child with this hash value equals the child of
7194 * the node, add it to the linked list after the first
7195 * item. */
7196 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007197 child->wn_u2.next = tp->wn_u2.next;
7198 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199 }
7200 }
7201 else
7202 /* No other child has this hash value, add it to the
7203 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007204 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007205 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007206 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007207 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007208
7209 /*
7210 * Make a hash key for the node and its siblings, so that we can quickly
7211 * find a lookalike node. This must be done after compressing the sibling
7212 * list, otherwise the hash key would become invalid by the compression.
7213 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007214 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007215 nr = 0;
7216 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007217 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007218 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007219 /* end node: use wn_flags, wn_region and wn_affixID */
7220 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007221 else
7222 /* byte node: use the byte value and the child pointer */
7223 n = np->wn_byte + ((long_u)np->wn_child << 8);
7224 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007225 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007226
7227 /* Avoid NUL bytes, it terminates the hash key. */
7228 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007229 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007230 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007231 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007232 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007233 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007234 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007235 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7236 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007237
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007238 /* Check for CTRL-C pressed now and then. */
7239 fast_breakcheck();
7240
Bram Moolenaar51485f02005-06-04 21:55:20 +00007241 return compressed;
7242}
7243
7244/*
7245 * Return TRUE when two nodes have identical siblings and children.
7246 */
7247 static int
7248node_equal(n1, n2)
7249 wordnode_T *n1;
7250 wordnode_T *n2;
7251{
7252 wordnode_T *p1;
7253 wordnode_T *p2;
7254
7255 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7256 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7257 if (p1->wn_byte != p2->wn_byte
7258 || (p1->wn_byte == NUL
7259 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007260 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007261 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007262 : (p1->wn_child != p2->wn_child)))
7263 break;
7264
7265 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007266}
7267
7268/*
7269 * Write a number to file "fd", MSB first, in "len" bytes.
7270 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007271 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007272put_bytes(fd, nr, len)
7273 FILE *fd;
7274 long_u nr;
7275 int len;
7276{
7277 int i;
7278
7279 for (i = len - 1; i >= 0; --i)
7280 putc((int)(nr >> (i * 8)), fd);
7281}
7282
Bram Moolenaar4770d092006-01-12 23:22:24 +00007283/*
7284 * Write spin->si_sugtime to file "fd".
7285 */
7286 static void
7287put_sugtime(spin, fd)
7288 spellinfo_T *spin;
7289 FILE *fd;
7290{
7291 int c;
7292 int i;
7293
7294 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7295 * can't use put_bytes() here. */
7296 for (i = 7; i >= 0; --i)
7297 if (i + 1 > sizeof(time_t))
7298 /* ">>" doesn't work well when shifting more bits than avail */
7299 putc(0, fd);
7300 else
7301 {
7302 c = (unsigned)spin->si_sugtime >> (i * 8);
7303 putc(c, fd);
7304 }
7305}
7306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007307static int
7308#ifdef __BORLANDC__
7309_RTLENTRYF
7310#endif
7311rep_compare __ARGS((const void *s1, const void *s2));
7312
7313/*
7314 * Function given to qsort() to sort the REP items on "from" string.
7315 */
7316 static int
7317#ifdef __BORLANDC__
7318_RTLENTRYF
7319#endif
7320rep_compare(s1, s2)
7321 const void *s1;
7322 const void *s2;
7323{
7324 fromto_T *p1 = (fromto_T *)s1;
7325 fromto_T *p2 = (fromto_T *)s2;
7326
7327 return STRCMP(p1->ft_from, p2->ft_from);
7328}
7329
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007330/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007331 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007332 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007333 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007334 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007335write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007336 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007337 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007338{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007339 FILE *fd;
7340 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007341 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007342 wordnode_T *tree;
7343 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007344 int i;
7345 int l;
7346 garray_T *gap;
7347 fromto_T *ftp;
7348 char_u *p;
7349 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007350 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007351
Bram Moolenaarb765d632005-06-07 21:00:02 +00007352 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007353 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007354 {
7355 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007356 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007357 }
7358
Bram Moolenaar5195e452005-08-19 20:32:47 +00007359 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007360 /* <fileID> */
7361 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007362 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007363 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007364 retval = FAIL;
7365 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007366 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007367
Bram Moolenaar5195e452005-08-19 20:32:47 +00007368 /*
7369 * <SECTIONS>: <section> ... <sectionend>
7370 */
7371
7372 /* SN_REGION: <regionname> ...
7373 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007374 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007375 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007376 putc(SN_REGION, fd); /* <sectionID> */
7377 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7378 l = spin->si_region_count * 2;
7379 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7380 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7381 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007382 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007383 }
7384 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007385 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007386
Bram Moolenaar5195e452005-08-19 20:32:47 +00007387 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7388 *
7389 * The table with character flags and the table for case folding.
7390 * This makes sure the same characters are recognized as word characters
7391 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007392 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007393 * 'encoding'.
7394 * Also skip this for an .add.spl file, the main spell file must contain
7395 * the table (avoids that it conflicts). File is shorter too.
7396 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007397 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007398 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007399 char_u folchars[128 * 8];
7400 int flags;
7401
Bram Moolenaard12a1322005-08-21 22:08:24 +00007402 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007403 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7404
7405 /* Form the <folchars> string first, we need to know its length. */
7406 l = 0;
7407 for (i = 128; i < 256; ++i)
7408 {
7409#ifdef FEAT_MBYTE
7410 if (has_mbyte)
7411 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7412 else
7413#endif
7414 folchars[l++] = spelltab.st_fold[i];
7415 }
7416 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7417
7418 fputc(128, fd); /* <charflagslen> */
7419 for (i = 128; i < 256; ++i)
7420 {
7421 flags = 0;
7422 if (spelltab.st_isw[i])
7423 flags |= CF_WORD;
7424 if (spelltab.st_isu[i])
7425 flags |= CF_UPPER;
7426 fputc(flags, fd); /* <charflags> */
7427 }
7428
7429 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7430 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007431 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007432
Bram Moolenaar5195e452005-08-19 20:32:47 +00007433 /* SN_MIDWORD: <midword> */
7434 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007435 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007436 putc(SN_MIDWORD, fd); /* <sectionID> */
7437 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7438
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007439 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007440 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007441 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7442 }
7443
Bram Moolenaar5195e452005-08-19 20:32:47 +00007444 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7445 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007446 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007447 putc(SN_PREFCOND, fd); /* <sectionID> */
7448 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7449
7450 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7451 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7452
7453 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007454 }
7455
Bram Moolenaar5195e452005-08-19 20:32:47 +00007456 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007457 * SN_SAL: <salflags> <salcount> <sal> ...
7458 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007459
Bram Moolenaar5195e452005-08-19 20:32:47 +00007460 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007461 * round 2: SN_SAL section (unless SN_SOFO is used)
7462 * round 3: SN_REPSAL section */
7463 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464 {
7465 if (round == 1)
7466 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007467 else if (round == 2)
7468 {
7469 /* Don't write SN_SAL when using a SN_SOFO section */
7470 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7471 continue;
7472 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007474 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007475 gap = &spin->si_repsal;
7476
7477 /* Don't write the section if there are no items. */
7478 if (gap->ga_len == 0)
7479 continue;
7480
7481 /* Sort the REP/REPSAL items. */
7482 if (round != 2)
7483 qsort(gap->ga_data, (size_t)gap->ga_len,
7484 sizeof(fromto_T), rep_compare);
7485
7486 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
7487 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007488
Bram Moolenaar5195e452005-08-19 20:32:47 +00007489 /* This is for making suggestions, section is not required. */
7490 putc(0, fd); /* <sectionflags> */
7491
7492 /* Compute the length of what follows. */
7493 l = 2; /* count <repcount> or <salcount> */
7494 for (i = 0; i < gap->ga_len; ++i)
7495 {
7496 ftp = &((fromto_T *)gap->ga_data)[i];
7497 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7498 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7499 }
7500 if (round == 2)
7501 ++l; /* count <salflags> */
7502 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7503
7504 if (round == 2)
7505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007506 i = 0;
7507 if (spin->si_followup)
7508 i |= SAL_F0LLOWUP;
7509 if (spin->si_collapse)
7510 i |= SAL_COLLAPSE;
7511 if (spin->si_rem_accents)
7512 i |= SAL_REM_ACCENTS;
7513 putc(i, fd); /* <salflags> */
7514 }
7515
7516 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7517 for (i = 0; i < gap->ga_len; ++i)
7518 {
7519 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7520 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7521 ftp = &((fromto_T *)gap->ga_data)[i];
7522 for (rr = 1; rr <= 2; ++rr)
7523 {
7524 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7525 l = STRLEN(p);
7526 putc(l, fd);
7527 fwrite(p, l, (size_t)1, fd);
7528 }
7529 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007531 }
7532
Bram Moolenaar5195e452005-08-19 20:32:47 +00007533 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7534 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007535 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7536 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007537 putc(SN_SOFO, fd); /* <sectionID> */
7538 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007539
7540 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007541 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7542 /* <sectionlen> */
7543
7544 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7545 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007546
7547 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007548 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7549 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007550 }
7551
Bram Moolenaar4770d092006-01-12 23:22:24 +00007552 /* SN_WORDS: <word> ...
7553 * This is for making suggestions, section is not required. */
7554 if (spin->si_commonwords.ht_used > 0)
7555 {
7556 putc(SN_WORDS, fd); /* <sectionID> */
7557 putc(0, fd); /* <sectionflags> */
7558
7559 /* round 1: count the bytes
7560 * round 2: write the bytes */
7561 for (round = 1; round <= 2; ++round)
7562 {
7563 int todo;
7564 int len = 0;
7565 hashitem_T *hi;
7566
7567 todo = spin->si_commonwords.ht_used;
7568 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
7569 if (!HASHITEM_EMPTY(hi))
7570 {
7571 l = STRLEN(hi->hi_key) + 1;
7572 len += l;
7573 if (round == 2) /* <word> */
7574 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
7575 --todo;
7576 }
7577 if (round == 1)
7578 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
7579 }
7580 }
7581
Bram Moolenaar5195e452005-08-19 20:32:47 +00007582 /* SN_MAP: <mapstr>
7583 * This is for making suggestions, section is not required. */
7584 if (spin->si_map.ga_len > 0)
7585 {
7586 putc(SN_MAP, fd); /* <sectionID> */
7587 putc(0, fd); /* <sectionflags> */
7588 l = spin->si_map.ga_len;
7589 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7590 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7591 /* <mapstr> */
7592 }
7593
Bram Moolenaar4770d092006-01-12 23:22:24 +00007594 /* SN_SUGFILE: <timestamp>
7595 * This is used to notify that a .sug file may be available and at the
7596 * same time allows for checking that a .sug file that is found matches
7597 * with this .spl file. That's because the word numbers must be exactly
7598 * right. */
7599 if (!spin->si_nosugfile
7600 && (spin->si_sal.ga_len > 0
7601 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
7602 {
7603 putc(SN_SUGFILE, fd); /* <sectionID> */
7604 putc(0, fd); /* <sectionflags> */
7605 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
7606
7607 /* Set si_sugtime and write it to the file. */
7608 spin->si_sugtime = time(NULL);
7609 put_sugtime(spin, fd); /* <timestamp> */
7610 }
7611
Bram Moolenaar5195e452005-08-19 20:32:47 +00007612 /* SN_COMPOUND: compound info.
7613 * We don't mark it required, when not supported all compound words will
7614 * be bad words. */
7615 if (spin->si_compflags != NULL)
7616 {
7617 putc(SN_COMPOUND, fd); /* <sectionID> */
7618 putc(0, fd); /* <sectionflags> */
7619
7620 l = STRLEN(spin->si_compflags);
7621 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7622 putc(spin->si_compmax, fd); /* <compmax> */
7623 putc(spin->si_compminlen, fd); /* <compminlen> */
7624 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7625 /* <compflags> */
7626 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7627 }
7628
Bram Moolenaar78622822005-08-23 21:00:13 +00007629 /* SN_NOBREAK: NOBREAK flag */
7630 if (spin->si_nobreak)
7631 {
7632 putc(SN_NOBREAK, fd); /* <sectionID> */
7633 putc(0, fd); /* <sectionflags> */
7634
7635 /* It's empty, the precense of the section flags the feature. */
7636 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7637 }
7638
Bram Moolenaar5195e452005-08-19 20:32:47 +00007639 /* SN_SYLLABLE: syllable info.
7640 * We don't mark it required, when not supported syllables will not be
7641 * counted. */
7642 if (spin->si_syllable != NULL)
7643 {
7644 putc(SN_SYLLABLE, fd); /* <sectionID> */
7645 putc(0, fd); /* <sectionflags> */
7646
7647 l = STRLEN(spin->si_syllable);
7648 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7649 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7650 }
7651
7652 /* end of <SECTIONS> */
7653 putc(SN_END, fd); /* <sectionend> */
7654
Bram Moolenaar50cde822005-06-05 21:54:54 +00007655
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007656 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007657 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007658 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007659 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007660 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007661 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007662 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007663 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007664 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007665 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007666 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007667 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007668
Bram Moolenaar0c405862005-06-22 22:26:26 +00007669 /* Clear the index and wnode fields in the tree. */
7670 clear_node(tree);
7671
Bram Moolenaar51485f02005-06-04 21:55:20 +00007672 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007673 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007674 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007675 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007676
Bram Moolenaar51485f02005-06-04 21:55:20 +00007677 /* number of nodes in 4 bytes */
7678 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007679 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007680
Bram Moolenaar51485f02005-06-04 21:55:20 +00007681 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007682 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007683 }
7684
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007685 /* Write another byte to check for errors. */
7686 if (putc(0, fd) == EOF)
7687 retval = FAIL;
7688
7689 if (fclose(fd) == EOF)
7690 retval = FAIL;
7691
7692 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007693}
7694
7695/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007696 * Clear the index and wnode fields of "node", it siblings and its
7697 * children. This is needed because they are a union with other items to save
7698 * space.
7699 */
7700 static void
7701clear_node(node)
7702 wordnode_T *node;
7703{
7704 wordnode_T *np;
7705
7706 if (node != NULL)
7707 for (np = node; np != NULL; np = np->wn_sibling)
7708 {
7709 np->wn_u1.index = 0;
7710 np->wn_u2.wnode = NULL;
7711
7712 if (np->wn_byte != NUL)
7713 clear_node(np->wn_child);
7714 }
7715}
7716
7717
7718/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007719 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007720 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007721 * This first writes the list of possible bytes (siblings). Then for each
7722 * byte recursively write the children.
7723 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00007724 * NOTE: The code here must match the code in read_tree_node(), since
7725 * assumptions are made about the indexes (so that we don't have to write them
7726 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007727 *
7728 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007729 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007730 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007731put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007732 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007733 wordnode_T *node;
7734 int index;
7735 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007736 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007737{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007738 int newindex = index;
7739 int siblingcount = 0;
7740 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007741 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007742
Bram Moolenaar51485f02005-06-04 21:55:20 +00007743 /* If "node" is zero the tree is empty. */
7744 if (node == NULL)
7745 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007746
Bram Moolenaar51485f02005-06-04 21:55:20 +00007747 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007748 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007749
7750 /* Count the number of siblings. */
7751 for (np = node; np != NULL; np = np->wn_sibling)
7752 ++siblingcount;
7753
7754 /* Write the sibling count. */
7755 if (fd != NULL)
7756 putc(siblingcount, fd); /* <siblingcount> */
7757
7758 /* Write each sibling byte and optionally extra info. */
7759 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007760 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007761 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007762 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007763 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007764 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007765 /* For a NUL byte (end of word) write the flags etc. */
7766 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007767 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007768 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007769 * associated condition nr (stored in wn_region). The
7770 * byte value is misused to store the "rare" and "not
7771 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007772 if (np->wn_flags == (short_u)PFX_FLAGS)
7773 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007774 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007775 {
7776 putc(BY_FLAGS, fd); /* <byte> */
7777 putc(np->wn_flags, fd); /* <pflags> */
7778 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007779 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007780 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007781 }
7782 else
7783 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007784 /* For word trees we write the flag/region items. */
7785 flags = np->wn_flags;
7786 if (regionmask != 0 && np->wn_region != regionmask)
7787 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007788 if (np->wn_affixID != 0)
7789 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007790 if (flags == 0)
7791 {
7792 /* word without flags or region */
7793 putc(BY_NOFLAGS, fd); /* <byte> */
7794 }
7795 else
7796 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007797 if (np->wn_flags >= 0x100)
7798 {
7799 putc(BY_FLAGS2, fd); /* <byte> */
7800 putc(flags, fd); /* <flags> */
7801 putc((unsigned)flags >> 8, fd); /* <flags2> */
7802 }
7803 else
7804 {
7805 putc(BY_FLAGS, fd); /* <byte> */
7806 putc(flags, fd); /* <flags> */
7807 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007808 if (flags & WF_REGION)
7809 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007810 if (flags & WF_AFX)
7811 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007812 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007813 }
7814 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007815 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007816 else
7817 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007818 if (np->wn_child->wn_u1.index != 0
7819 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007820 {
7821 /* The child is written elsewhere, write the reference. */
7822 if (fd != NULL)
7823 {
7824 putc(BY_INDEX, fd); /* <byte> */
7825 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007826 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007827 }
7828 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007829 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007830 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007831 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007832
Bram Moolenaar51485f02005-06-04 21:55:20 +00007833 if (fd != NULL)
7834 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7835 {
7836 EMSG(_(e_write));
7837 return 0;
7838 }
7839 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007840 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007841
7842 /* Space used in the array when reading: one for each sibling and one for
7843 * the count. */
7844 newindex += siblingcount + 1;
7845
7846 /* Recursively dump the children of each sibling. */
7847 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007848 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7849 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007850 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007851
7852 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007853}
7854
7855
7856/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007857 * ":mkspell [-ascii] outfile infile ..."
7858 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007859 */
7860 void
7861ex_mkspell(eap)
7862 exarg_T *eap;
7863{
7864 int fcount;
7865 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007866 char_u *arg = eap->arg;
7867 int ascii = FALSE;
7868
7869 if (STRNCMP(arg, "-ascii", 6) == 0)
7870 {
7871 ascii = TRUE;
7872 arg = skipwhite(arg + 6);
7873 }
7874
7875 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7876 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007878 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007879 FreeWild(fcount, fnames);
7880 }
7881}
7882
7883/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00007884 * Create the .sug file.
7885 * Uses the soundfold info in "spin".
7886 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
7887 */
7888 static void
7889spell_make_sugfile(spin, wfname)
7890 spellinfo_T *spin;
7891 char_u *wfname;
7892{
7893 char_u fname[MAXPATHL];
7894 int len;
7895 slang_T *slang;
7896 int free_slang = FALSE;
7897
7898 /*
7899 * Read back the .spl file that was written. This fills the required
7900 * info for soundfolding. This also uses less memory than the
7901 * pointer-linked version of the trie. And it avoids having two versions
7902 * of the code for the soundfolding stuff.
7903 * It might have been done already by spell_reload_one().
7904 */
7905 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
7906 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
7907 break;
7908 if (slang == NULL)
7909 {
7910 spell_message(spin, (char_u *)_("Reading back spell file..."));
7911 slang = spell_load_file(wfname, NULL, NULL, FALSE);
7912 if (slang == NULL)
7913 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007914 free_slang = TRUE;
7915 }
7916
7917 /*
7918 * Clear the info in "spin" that is used.
7919 */
7920 spin->si_blocks = NULL;
7921 spin->si_blocks_cnt = 0;
7922 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
7923 spin->si_free_count = 0;
7924 spin->si_first_free = NULL;
7925 spin->si_foldwcount = 0;
7926
7927 /*
7928 * Go through the trie of good words, soundfold each word and add it to
7929 * the soundfold trie.
7930 */
7931 spell_message(spin, (char_u *)_("Performing soundfolding..."));
7932 if (sug_filltree(spin, slang) == FAIL)
7933 goto theend;
7934
7935 /*
7936 * Create the table which links each soundfold word with a list of the
7937 * good words it may come from. Creates buffer "spin->si_spellbuf".
7938 * This also removes the wordnr from the NUL byte entries to make
7939 * compression possible.
7940 */
7941 if (sug_maketable(spin) == FAIL)
7942 goto theend;
7943
7944 smsg((char_u *)_("Number of words after soundfolding: %ld"),
7945 (long)spin->si_spellbuf->b_ml.ml_line_count);
7946
7947 /*
7948 * Compress the soundfold trie.
7949 */
7950 spell_message(spin, (char_u *)_(msg_compressing));
7951 wordtree_compress(spin, spin->si_foldroot);
7952
7953 /*
7954 * Write the .sug file.
7955 * Make the file name by changing ".spl" to ".sug".
7956 */
7957 STRCPY(fname, wfname);
7958 len = STRLEN(fname);
7959 fname[len - 2] = 'u';
7960 fname[len - 1] = 'g';
7961 sug_write(spin, fname);
7962
7963theend:
7964 if (free_slang)
7965 slang_free(slang);
7966 free_blocks(spin->si_blocks);
7967 close_spellbuf(spin->si_spellbuf);
7968}
7969
7970/*
7971 * Build the soundfold trie for language "slang".
7972 */
7973 static int
7974sug_filltree(spin, slang)
7975 spellinfo_T *spin;
7976 slang_T *slang;
7977{
7978 char_u *byts;
7979 idx_T *idxs;
7980 int depth;
7981 idx_T arridx[MAXWLEN];
7982 int curi[MAXWLEN];
7983 char_u tword[MAXWLEN];
7984 char_u tsalword[MAXWLEN];
7985 int c;
7986 idx_T n;
7987 unsigned words_done = 0;
7988 int wordcount[MAXWLEN];
7989
7990 /* We use si_foldroot for the souldfolded trie. */
7991 spin->si_foldroot = wordtree_alloc(spin);
7992 if (spin->si_foldroot == NULL)
7993 return FAIL;
7994
7995 /* let tree_add_word() know we're adding to the soundfolded tree */
7996 spin->si_sugtree = TRUE;
7997
7998 /*
7999 * Go through the whole case-folded tree, soundfold each word and put it
8000 * in the trie.
8001 */
8002 byts = slang->sl_fbyts;
8003 idxs = slang->sl_fidxs;
8004
8005 arridx[0] = 0;
8006 curi[0] = 1;
8007 wordcount[0] = 0;
8008
8009 depth = 0;
8010 while (depth >= 0 && !got_int)
8011 {
8012 if (curi[depth] > byts[arridx[depth]])
8013 {
8014 /* Done all bytes at this node, go up one level. */
8015 idxs[arridx[depth]] = wordcount[depth];
8016 if (depth > 0)
8017 wordcount[depth - 1] += wordcount[depth];
8018
8019 --depth;
8020 line_breakcheck();
8021 }
8022 else
8023 {
8024
8025 /* Do one more byte at this node. */
8026 n = arridx[depth] + curi[depth];
8027 ++curi[depth];
8028
8029 c = byts[n];
8030 if (c == 0)
8031 {
8032 /* Sound-fold the word. */
8033 tword[depth] = NUL;
8034 spell_soundfold(slang, tword, TRUE, tsalword);
8035
8036 /* We use the "flags" field for the MSB of the wordnr,
8037 * "region" for the LSB of the wordnr. */
8038 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8039 words_done >> 16, words_done & 0xffff,
8040 0) == FAIL)
8041 return FAIL;
8042
8043 ++words_done;
8044 ++wordcount[depth];
8045
8046 /* Reset the block count each time to avoid compression
8047 * kicking in. */
8048 spin->si_blocks_cnt = 0;
8049
8050 /* Skip over any other NUL bytes (same word with different
8051 * flags). */
8052 while (byts[n + 1] == 0)
8053 {
8054 ++n;
8055 ++curi[depth];
8056 }
8057 }
8058 else
8059 {
8060 /* Normal char, go one level deeper. */
8061 tword[depth++] = c;
8062 arridx[depth] = idxs[n];
8063 curi[depth] = 1;
8064 wordcount[depth] = 0;
8065 }
8066 }
8067 }
8068
8069 smsg((char_u *)_("Total number of words: %d"), words_done);
8070
8071 return OK;
8072}
8073
8074/*
8075 * Make the table that links each word in the soundfold trie to the words it
8076 * can be produced from.
8077 * This is not unlike lines in a file, thus use a memfile to be able to access
8078 * the table efficiently.
8079 * Returns FAIL when out of memory.
8080 */
8081 static int
8082sug_maketable(spin)
8083 spellinfo_T *spin;
8084{
8085 garray_T ga;
8086 int res = OK;
8087
8088 /* Allocate a buffer, open a memline for it and create the swap file
8089 * (uses a temp file, not a .swp file). */
8090 spin->si_spellbuf = open_spellbuf();
8091 if (spin->si_spellbuf == NULL)
8092 return FAIL;
8093
8094 /* Use a buffer to store the line info, avoids allocating many small
8095 * pieces of memory. */
8096 ga_init2(&ga, 1, 100);
8097
8098 /* recursively go through the tree */
8099 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8100 res = FAIL;
8101
8102 ga_clear(&ga);
8103 return res;
8104}
8105
8106/*
8107 * Fill the table for one node and its children.
8108 * Returns the wordnr at the start of the node.
8109 * Returns -1 when out of memory.
8110 */
8111 static int
8112sug_filltable(spin, node, startwordnr, gap)
8113 spellinfo_T *spin;
8114 wordnode_T *node;
8115 int startwordnr;
8116 garray_T *gap; /* place to store line of numbers */
8117{
8118 wordnode_T *p, *np;
8119 int wordnr = startwordnr;
8120 int nr;
8121 int prev_nr;
8122
8123 for (p = node; p != NULL; p = p->wn_sibling)
8124 {
8125 if (p->wn_byte == NUL)
8126 {
8127 gap->ga_len = 0;
8128 prev_nr = 0;
8129 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8130 {
8131 if (ga_grow(gap, 10) == FAIL)
8132 return -1;
8133
8134 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8135 /* Compute the offset from the previous nr and store the
8136 * offset in a way that it takes a minimum number of bytes.
8137 * It's a bit like utf-8, but without the need to mark
8138 * following bytes. */
8139 nr -= prev_nr;
8140 prev_nr += nr;
8141 gap->ga_len += offset2bytes(nr,
8142 (char_u *)gap->ga_data + gap->ga_len);
8143 }
8144
8145 /* add the NUL byte */
8146 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8147
8148 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8149 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8150 return -1;
8151 ++wordnr;
8152
8153 /* Remove extra NUL entries, we no longer need them. We don't
8154 * bother freeing the nodes, the won't be reused anyway. */
8155 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8156 p->wn_sibling = p->wn_sibling->wn_sibling;
8157
8158 /* Clear the flags on the remaining NUL node, so that compression
8159 * works a lot better. */
8160 p->wn_flags = 0;
8161 p->wn_region = 0;
8162 }
8163 else
8164 {
8165 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8166 if (wordnr == -1)
8167 return -1;
8168 }
8169 }
8170 return wordnr;
8171}
8172
8173/*
8174 * Convert an offset into a minimal number of bytes.
8175 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8176 * bytes.
8177 */
8178 static int
8179offset2bytes(nr, buf)
8180 int nr;
8181 char_u *buf;
8182{
8183 int rem;
8184 int b1, b2, b3, b4;
8185
8186 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8187 b1 = nr % 255 + 1;
8188 rem = nr / 255;
8189 b2 = rem % 255 + 1;
8190 rem = rem / 255;
8191 b3 = rem % 255 + 1;
8192 b4 = rem / 255 + 1;
8193
8194 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8195 {
8196 buf[0] = 0xe0 + b4;
8197 buf[1] = b3;
8198 buf[2] = b2;
8199 buf[3] = b1;
8200 return 4;
8201 }
8202 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8203 {
8204 buf[0] = 0xc0 + b3;
8205 buf[1] = b2;
8206 buf[2] = b1;
8207 return 3;
8208 }
8209 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8210 {
8211 buf[0] = 0x80 + b2;
8212 buf[1] = b1;
8213 return 2;
8214 }
8215 /* 1 byte */
8216 buf[0] = b1;
8217 return 1;
8218}
8219
8220/*
8221 * Opposite of offset2bytes().
8222 * "pp" points to the bytes and is advanced over it.
8223 * Returns the offset.
8224 */
8225 static int
8226bytes2offset(pp)
8227 char_u **pp;
8228{
8229 char_u *p = *pp;
8230 int nr;
8231 int c;
8232
8233 c = *p++;
8234 if ((c & 0x80) == 0x00) /* 1 byte */
8235 {
8236 nr = c - 1;
8237 }
8238 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8239 {
8240 nr = (c & 0x3f) - 1;
8241 nr = nr * 255 + (*p++ - 1);
8242 }
8243 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8244 {
8245 nr = (c & 0x1f) - 1;
8246 nr = nr * 255 + (*p++ - 1);
8247 nr = nr * 255 + (*p++ - 1);
8248 }
8249 else /* 4 bytes */
8250 {
8251 nr = (c & 0x0f) - 1;
8252 nr = nr * 255 + (*p++ - 1);
8253 nr = nr * 255 + (*p++ - 1);
8254 nr = nr * 255 + (*p++ - 1);
8255 }
8256
8257 *pp = p;
8258 return nr;
8259}
8260
8261/*
8262 * Write the .sug file in "fname".
8263 */
8264 static void
8265sug_write(spin, fname)
8266 spellinfo_T *spin;
8267 char_u *fname;
8268{
8269 FILE *fd;
8270 wordnode_T *tree;
8271 int nodecount;
8272 int wcount;
8273 char_u *line;
8274 linenr_T lnum;
8275 int len;
8276
8277 /* Create the file. Note that an existing file is silently overwritten! */
8278 fd = mch_fopen((char *)fname, "w");
8279 if (fd == NULL)
8280 {
8281 EMSG2(_(e_notopen), fname);
8282 return;
8283 }
8284
8285 vim_snprintf((char *)IObuff, IOSIZE,
8286 _("Writing suggestion file %s ..."), fname);
8287 spell_message(spin, IObuff);
8288
8289 /*
8290 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8291 */
8292 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8293 {
8294 EMSG(_(e_write));
8295 goto theend;
8296 }
8297 putc(VIMSUGVERSION, fd); /* <versionnr> */
8298
8299 /* Write si_sugtime to the file. */
8300 put_sugtime(spin, fd); /* <timestamp> */
8301
8302 /*
8303 * <SUGWORDTREE>
8304 */
8305 spin->si_memtot = 0;
8306 tree = spin->si_foldroot->wn_sibling;
8307
8308 /* Clear the index and wnode fields in the tree. */
8309 clear_node(tree);
8310
8311 /* Count the number of nodes. Needed to be able to allocate the
8312 * memory when reading the nodes. Also fills in index for shared
8313 * nodes. */
8314 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8315
8316 /* number of nodes in 4 bytes */
8317 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8318 spin->si_memtot += nodecount + nodecount * sizeof(int);
8319
8320 /* Write the nodes. */
8321 (void)put_node(fd, tree, 0, 0, FALSE);
8322
8323 /*
8324 * <SUGTABLE>: <sugwcount> <sugline> ...
8325 */
8326 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8327 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8328
8329 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8330 {
8331 /* <sugline>: <sugnr> ... NUL */
8332 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8333 len = STRLEN(line) + 1;
8334 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8335 {
8336 EMSG(_(e_write));
8337 goto theend;
8338 }
8339 spin->si_memtot += len;
8340 }
8341
8342 /* Write another byte to check for errors. */
8343 if (putc(0, fd) == EOF)
8344 EMSG(_(e_write));
8345
8346 vim_snprintf((char *)IObuff, IOSIZE,
8347 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8348 spell_message(spin, IObuff);
8349
8350theend:
8351 /* close the file */
8352 fclose(fd);
8353}
8354
8355/*
8356 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8357 * list and only contains text lines. Can use a swapfile to reduce memory
8358 * use.
8359 * Most other fields are invalid! Esp. watch out for string options being
8360 * NULL and there is no undo info.
8361 * Returns NULL when out of memory.
8362 */
8363 static buf_T *
8364open_spellbuf()
8365{
8366 buf_T *buf;
8367
8368 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8369 if (buf != NULL)
8370 {
8371 buf->b_spell = TRUE;
8372 buf->b_p_swf = TRUE; /* may create a swap file */
8373 ml_open(buf);
8374 ml_open_file(buf); /* create swap file now */
8375 }
8376 return buf;
8377}
8378
8379/*
8380 * Close the buffer used for spell info.
8381 */
8382 static void
8383close_spellbuf(buf)
8384 buf_T *buf;
8385{
8386 if (buf != NULL)
8387 {
8388 ml_close(buf, TRUE);
8389 vim_free(buf);
8390 }
8391}
8392
8393
8394/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008395 * Create a Vim spell file from one or more word lists.
8396 * "fnames[0]" is the output file name.
8397 * "fnames[fcount - 1]" is the last input file name.
8398 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8399 * and ".spl" is appended to make the output file name.
8400 */
8401 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008402mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008403 int fcount;
8404 char_u **fnames;
8405 int ascii; /* -ascii argument given */
8406 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008408{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008409 char_u fname[MAXPATHL];
8410 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008411 char_u **innames;
8412 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008413 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008414 int i;
8415 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008416 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008417 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008418 spellinfo_T spin;
8419
8420 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008421 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008422 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008423 spin.si_followup = TRUE;
8424 spin.si_rem_accents = TRUE;
8425 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008426 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008427 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8428 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008429 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008430 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008431 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008432
Bram Moolenaarb765d632005-06-07 21:00:02 +00008433 /* default: fnames[0] is output file, following are input files */
8434 innames = &fnames[1];
8435 incount = fcount - 1;
8436
8437 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008438 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008439 len = STRLEN(fnames[0]);
8440 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8441 {
8442 /* For ":mkspell path/en.latin1.add" output file is
8443 * "path/en.latin1.add.spl". */
8444 innames = &fnames[0];
8445 incount = 1;
8446 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8447 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008448 else if (fcount == 1)
8449 {
8450 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8451 innames = &fnames[0];
8452 incount = 1;
8453 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8454 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8455 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008456 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8457 {
8458 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008459 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008460 }
8461 else
8462 /* Name should be language, make the file name from it. */
8463 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8464 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8465
8466 /* Check for .ascii.spl. */
8467 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
8468 spin.si_ascii = TRUE;
8469
8470 /* Check for .add.spl. */
8471 if (strstr((char *)gettail(wfname), ".add.") != NULL)
8472 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00008473 }
8474
Bram Moolenaarb765d632005-06-07 21:00:02 +00008475 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008476 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008477 else if (vim_strchr(gettail(wfname), '_') != NULL)
8478 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008479 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008480 EMSG(_("E754: Only up to 8 regions supported"));
8481 else
8482 {
8483 /* Check for overwriting before doing things that may take a lot of
8484 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008485 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008486 {
8487 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008488 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008489 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008490 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008491 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008492 EMSG2(_(e_isadir2), wfname);
8493 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008494 }
8495
8496 /*
8497 * Init the aff and dic pointers.
8498 * Get the region names if there are more than 2 arguments.
8499 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008500 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008501 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008502 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008503
Bram Moolenaar3982c542005-06-08 21:56:31 +00008504 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008505 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008506 len = STRLEN(innames[i]);
8507 if (STRLEN(gettail(innames[i])) < 5
8508 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008509 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008510 EMSG2(_("E755: Invalid region in %s"), innames[i]);
8511 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008512 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008513 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
8514 spin.si_region_name[i * 2 + 1] =
8515 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008516 }
8517 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008518 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008519
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008520 spin.si_foldroot = wordtree_alloc(&spin);
8521 spin.si_keeproot = wordtree_alloc(&spin);
8522 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008523 if (spin.si_foldroot == NULL
8524 || spin.si_keeproot == NULL
8525 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008526 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008527 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008528 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008529 }
8530
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008531 /* When not producing a .add.spl file clear the character table when
8532 * we encounter one in the .aff file. This means we dump the current
8533 * one in the .spl file if the .aff file doesn't define one. That's
8534 * better than guessing the contents, the table will match a
8535 * previously loaded spell file. */
8536 if (!spin.si_add)
8537 spin.si_clear_chartab = TRUE;
8538
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008539 /*
8540 * Read all the .aff and .dic files.
8541 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00008542 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008543 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008544 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008545 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008546 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008547 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008548
Bram Moolenaarb765d632005-06-07 21:00:02 +00008549 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008550 if (mch_stat((char *)fname, &st) >= 0)
8551 {
8552 /* Read the .aff file. Will init "spin->si_conv" based on the
8553 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008554 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008555 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008556 error = TRUE;
8557 else
8558 {
8559 /* Read the .dic file and store the words in the trees. */
8560 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00008561 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008562 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008563 error = TRUE;
8564 }
8565 }
8566 else
8567 {
8568 /* No .aff file, try reading the file as a word list. Store
8569 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008570 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008571 error = TRUE;
8572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008573
Bram Moolenaarb765d632005-06-07 21:00:02 +00008574#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008575 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008576 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008577#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008578 }
8579
Bram Moolenaar78622822005-08-23 21:00:13 +00008580 if (spin.si_compflags != NULL && spin.si_nobreak)
8581 MSG(_("Warning: both compounding and NOBREAK specified"));
8582
Bram Moolenaar4770d092006-01-12 23:22:24 +00008583 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008584 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008585 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008586 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008587 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008588 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008589 wordtree_compress(&spin, spin.si_foldroot);
8590 wordtree_compress(&spin, spin.si_keeproot);
8591 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008592 }
8593
Bram Moolenaar4770d092006-01-12 23:22:24 +00008594 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008595 {
8596 /*
8597 * Write the info in the spell file.
8598 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008599 vim_snprintf((char *)IObuff, IOSIZE,
8600 _("Writing spell file %s ..."), wfname);
8601 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00008602
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008603 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008604
Bram Moolenaar4770d092006-01-12 23:22:24 +00008605 spell_message(&spin, (char_u *)_("Done!"));
8606 vim_snprintf((char *)IObuff, IOSIZE,
8607 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
8608 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008609
Bram Moolenaar4770d092006-01-12 23:22:24 +00008610 /*
8611 * If the file is loaded need to reload it.
8612 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008613 if (!error)
8614 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008615 }
8616
8617 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008619 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 ga_clear(&spin.si_sal);
8621 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008622 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008623 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008624
8625 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008626 for (i = 0; i < incount; ++i)
8627 if (afile[i] != NULL)
8628 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008629
8630 /* Free all the bits and pieces at once. */
8631 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008632
8633 /*
8634 * If there is soundfolding info and no NOSUGFILE item create the
8635 * .sug file with the soundfolded word trie.
8636 */
8637 if (spin.si_sugtime != 0 && !error && !got_int)
8638 spell_make_sugfile(&spin, wfname);
8639
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008640 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008641}
8642
Bram Moolenaar4770d092006-01-12 23:22:24 +00008643/*
8644 * Display a message for spell file processing when 'verbose' is set or using
8645 * ":mkspell". "str" can be IObuff.
8646 */
8647 static void
8648spell_message(spin, str)
8649 spellinfo_T *spin;
8650 char_u *str;
8651{
8652 if (spin->si_verbose || p_verbose > 2)
8653 {
8654 if (!spin->si_verbose)
8655 verbose_enter();
8656 MSG(str);
8657 out_flush();
8658 if (!spin->si_verbose)
8659 verbose_leave();
8660 }
8661}
Bram Moolenaarb765d632005-06-07 21:00:02 +00008662
8663/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008664 * ":[count]spellgood {word}"
8665 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00008666 */
8667 void
8668ex_spell(eap)
8669 exarg_T *eap;
8670{
Bram Moolenaar7887d882005-07-01 22:33:52 +00008671 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008672 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008673}
8674
8675/*
8676 * Add "word[len]" to 'spellfile' as a good or bad word.
8677 */
8678 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008679spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008680 char_u *word;
8681 int len;
8682 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008683 int index; /* "zG" and "zW": zero, otherwise index in
8684 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008685{
8686 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008687 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008688 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008689 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008690 char_u fnamebuf[MAXPATHL];
8691 char_u line[MAXWLEN * 2];
8692 long fpos, fpos_next = 0;
8693 int i;
8694 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008695
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008696 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008697 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008698 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008699 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008700 int_wordlist = vim_tempname('s');
8701 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008702 return;
8703 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008704 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008705 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008706 else
8707 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008708 /* If 'spellfile' isn't set figure out a good default value. */
8709 if (*curbuf->b_p_spf == NUL)
8710 {
8711 init_spellfile();
8712 new_spf = TRUE;
8713 }
8714
8715 if (*curbuf->b_p_spf == NUL)
8716 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008717 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00008718 return;
8719 }
8720
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008721 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
8722 {
8723 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
8724 if (i == index)
8725 break;
8726 if (*spf == NUL)
8727 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00008728 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008729 return;
8730 }
8731 }
8732
Bram Moolenaarb765d632005-06-07 21:00:02 +00008733 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008734 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008735 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
8736 buf = NULL;
8737 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00008738 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008739 EMSG(_(e_bufloaded));
8740 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008741 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008742
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008743 fname = fnamebuf;
8744 }
8745
8746 if (bad)
8747 {
8748 /* When the word also appears as good word we need to remove that one,
8749 * since its flags sort before the one with WF_BANNED. */
8750 fd = mch_fopen((char *)fname, "r");
8751 if (fd != NULL)
8752 {
8753 while (!vim_fgets(line, MAXWLEN * 2, fd))
8754 {
8755 fpos = fpos_next;
8756 fpos_next = ftell(fd);
8757 if (STRNCMP(word, line, len) == 0
8758 && (line[len] == '/' || line[len] < ' '))
8759 {
8760 /* Found duplicate word. Remove it by writing a '#' at
8761 * the start of the line. Mixing reading and writing
8762 * doesn't work for all systems, close the file first. */
8763 fclose(fd);
8764 fd = mch_fopen((char *)fname, "r+");
8765 if (fd == NULL)
8766 break;
8767 if (fseek(fd, fpos, SEEK_SET) == 0)
8768 fputc('#', fd);
8769 fseek(fd, fpos_next, SEEK_SET);
8770 }
8771 }
8772 fclose(fd);
8773 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008774 }
8775
8776 fd = mch_fopen((char *)fname, "a");
8777 if (fd == NULL && new_spf)
8778 {
8779 /* We just initialized the 'spellfile' option and can't open the file.
8780 * We may need to create the "spell" directory first. We already
8781 * checked the runtime directory is writable in init_spellfile(). */
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00008782 if (!dir_of_file_exists(fname))
Bram Moolenaar7887d882005-07-01 22:33:52 +00008783 {
8784 /* The directory doesn't exist. Try creating it and opening the
8785 * file again. */
8786 vim_mkdir(NameBuff, 0755);
8787 fd = mch_fopen((char *)fname, "a");
8788 }
8789 }
8790
8791 if (fd == NULL)
8792 EMSG2(_(e_notopen), fname);
8793 else
8794 {
8795 if (bad)
8796 fprintf(fd, "%.*s/!\n", len, word);
8797 else
8798 fprintf(fd, "%.*s\n", len, word);
8799 fclose(fd);
8800
8801 /* Update the .add.spl file. */
8802 mkspell(1, &fname, FALSE, TRUE, TRUE);
8803
8804 /* If the .add file is edited somewhere, reload it. */
8805 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00008806 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008807
8808 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008809 }
8810}
8811
8812/*
8813 * Initialize 'spellfile' for the current buffer.
8814 */
8815 static void
8816init_spellfile()
8817{
8818 char_u buf[MAXPATHL];
8819 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008820 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008821 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008822 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008823 int aspath = FALSE;
8824 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008825
8826 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
8827 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008828 /* Find the end of the language name. Exclude the region. If there
8829 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008830 for (lend = curbuf->b_p_spl; *lend != NUL
8831 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008832 if (vim_ispathsep(*lend))
8833 {
8834 aspath = TRUE;
8835 lstart = lend + 1;
8836 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008837
8838 /* Loop over all entries in 'runtimepath'. Use the first one where we
8839 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008840 rtp = p_rtp;
8841 while (*rtp != NUL)
8842 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008843 if (aspath)
8844 /* Use directory of an entry with path, e.g., for
8845 * "/dir/lg.utf-8.spl" use "/dir". */
8846 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
8847 else
8848 /* Copy the path from 'runtimepath' to buf[]. */
8849 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00008850 if (filewritable(buf) == 2)
8851 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00008852 /* Use the first language name from 'spelllang' and the
8853 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008854 if (aspath)
8855 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
8856 else
8857 {
8858 l = STRLEN(buf);
8859 vim_snprintf((char *)buf + l, MAXPATHL - l,
8860 "/spell/%.*s", (int)(lend - lstart), lstart);
8861 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008862 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008863 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
8864 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
8865 fname != NULL
8866 && strstr((char *)gettail(fname), ".ascii.") != NULL
8867 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00008868 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
8869 break;
8870 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008871 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008872 }
8873 }
8874}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008875
Bram Moolenaar51485f02005-06-04 21:55:20 +00008876
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008877/*
8878 * Init the chartab used for spelling for ASCII.
8879 * EBCDIC is not supported!
8880 */
8881 static void
8882clear_spell_chartab(sp)
8883 spelltab_T *sp;
8884{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008885 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008886
8887 /* Init everything to FALSE. */
8888 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
8889 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
8890 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008891 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008892 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008893 sp->st_upper[i] = i;
8894 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008895
8896 /* We include digits. A word shouldn't start with a digit, but handling
8897 * that is done separately. */
8898 for (i = '0'; i <= '9'; ++i)
8899 sp->st_isw[i] = TRUE;
8900 for (i = 'A'; i <= 'Z'; ++i)
8901 {
8902 sp->st_isw[i] = TRUE;
8903 sp->st_isu[i] = TRUE;
8904 sp->st_fold[i] = i + 0x20;
8905 }
8906 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008907 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008908 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008909 sp->st_upper[i] = i - 0x20;
8910 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008911}
8912
8913/*
8914 * Init the chartab used for spelling. Only depends on 'encoding'.
8915 * Called once while starting up and when 'encoding' changes.
8916 * The default is to use isalpha(), but the spell file should define the word
8917 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008918 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008919 */
8920 void
8921init_spell_chartab()
8922{
8923 int i;
8924
8925 did_set_spelltab = FALSE;
8926 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008927#ifdef FEAT_MBYTE
8928 if (enc_dbcs)
8929 {
8930 /* DBCS: assume double-wide characters are word characters. */
8931 for (i = 128; i <= 255; ++i)
8932 if (MB_BYTE2LEN(i) == 2)
8933 spelltab.st_isw[i] = TRUE;
8934 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008935 else if (enc_utf8)
8936 {
8937 for (i = 128; i < 256; ++i)
8938 {
8939 spelltab.st_isu[i] = utf_isupper(i);
8940 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
8941 spelltab.st_fold[i] = utf_fold(i);
8942 spelltab.st_upper[i] = utf_toupper(i);
8943 }
8944 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008945 else
8946#endif
8947 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008948 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008949 for (i = 128; i < 256; ++i)
8950 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008951 if (MB_ISUPPER(i))
8952 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008953 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008954 spelltab.st_isu[i] = TRUE;
8955 spelltab.st_fold[i] = MB_TOLOWER(i);
8956 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008957 else if (MB_ISLOWER(i))
8958 {
8959 spelltab.st_isw[i] = TRUE;
8960 spelltab.st_upper[i] = MB_TOUPPER(i);
8961 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008962 }
8963 }
8964}
8965
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008966/*
8967 * Set the spell character tables from strings in the affix file.
8968 */
8969 static int
8970set_spell_chartab(fol, low, upp)
8971 char_u *fol;
8972 char_u *low;
8973 char_u *upp;
8974{
8975 /* We build the new tables here first, so that we can compare with the
8976 * previous one. */
8977 spelltab_T new_st;
8978 char_u *pf = fol, *pl = low, *pu = upp;
8979 int f, l, u;
8980
8981 clear_spell_chartab(&new_st);
8982
8983 while (*pf != NUL)
8984 {
8985 if (*pl == NUL || *pu == NUL)
8986 {
8987 EMSG(_(e_affform));
8988 return FAIL;
8989 }
8990#ifdef FEAT_MBYTE
8991 f = mb_ptr2char_adv(&pf);
8992 l = mb_ptr2char_adv(&pl);
8993 u = mb_ptr2char_adv(&pu);
8994#else
8995 f = *pf++;
8996 l = *pl++;
8997 u = *pu++;
8998#endif
8999 /* Every character that appears is a word character. */
9000 if (f < 256)
9001 new_st.st_isw[f] = TRUE;
9002 if (l < 256)
9003 new_st.st_isw[l] = TRUE;
9004 if (u < 256)
9005 new_st.st_isw[u] = TRUE;
9006
9007 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9008 * case-folding */
9009 if (l < 256 && l != f)
9010 {
9011 if (f >= 256)
9012 {
9013 EMSG(_(e_affrange));
9014 return FAIL;
9015 }
9016 new_st.st_fold[l] = f;
9017 }
9018
9019 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009020 * case-folding, it's upper case and the "UPP" is the upper case of
9021 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009022 if (u < 256 && u != f)
9023 {
9024 if (f >= 256)
9025 {
9026 EMSG(_(e_affrange));
9027 return FAIL;
9028 }
9029 new_st.st_fold[u] = f;
9030 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009031 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009032 }
9033 }
9034
9035 if (*pl != NUL || *pu != NUL)
9036 {
9037 EMSG(_(e_affform));
9038 return FAIL;
9039 }
9040
9041 return set_spell_finish(&new_st);
9042}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009043
9044/*
9045 * Set the spell character tables from strings in the .spl file.
9046 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009047 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009048set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009049 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009050 int cnt; /* length of "flags" */
9051 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009052{
9053 /* We build the new tables here first, so that we can compare with the
9054 * previous one. */
9055 spelltab_T new_st;
9056 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009057 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009058 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009059
9060 clear_spell_chartab(&new_st);
9061
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009062 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009063 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009064 if (i < cnt)
9065 {
9066 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9067 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9068 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009069
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009070 if (*p != NUL)
9071 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009072#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009073 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009074#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009075 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009076#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009077 new_st.st_fold[i + 128] = c;
9078 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9079 new_st.st_upper[c] = i + 128;
9080 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009081 }
9082
Bram Moolenaar5195e452005-08-19 20:32:47 +00009083 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009084}
9085
9086 static int
9087set_spell_finish(new_st)
9088 spelltab_T *new_st;
9089{
9090 int i;
9091
9092 if (did_set_spelltab)
9093 {
9094 /* check that it's the same table */
9095 for (i = 0; i < 256; ++i)
9096 {
9097 if (spelltab.st_isw[i] != new_st->st_isw[i]
9098 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009099 || spelltab.st_fold[i] != new_st->st_fold[i]
9100 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009101 {
9102 EMSG(_("E763: Word characters differ between spell files"));
9103 return FAIL;
9104 }
9105 }
9106 }
9107 else
9108 {
9109 /* copy the new spelltab into the one being used */
9110 spelltab = *new_st;
9111 did_set_spelltab = TRUE;
9112 }
9113
9114 return OK;
9115}
9116
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009117/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009118 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009119 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009120 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009121 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009122 */
9123 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009124spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009125 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009126 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009127{
Bram Moolenaarea408852005-06-25 22:49:46 +00009128#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009129 char_u *s;
9130 int l;
9131 int c;
9132
9133 if (has_mbyte)
9134 {
9135 l = MB_BYTE2LEN(*p);
9136 s = p;
9137 if (l == 1)
9138 {
9139 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009140 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009141 {
9142 s = p + 1; /* skip a mid-word character */
9143 l = MB_BYTE2LEN(*s);
9144 }
9145 }
9146 else
9147 {
9148 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009149 if (c < 256 ? buf->b_spell_ismw[c]
9150 : (buf->b_spell_ismw_mb != NULL
9151 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009152 {
9153 s = p + l;
9154 l = MB_BYTE2LEN(*s);
9155 }
9156 }
9157
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009158 c = mb_ptr2char(s);
9159 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009160 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009161 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009162 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009163#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009164
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009165 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9166}
9167
9168/*
9169 * Return TRUE if "p" points to a word character.
9170 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9171 */
9172 static int
9173spell_iswordp_nmw(p)
9174 char_u *p;
9175{
9176#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009177 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009178
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009179 if (has_mbyte)
9180 {
9181 c = mb_ptr2char(p);
9182 if (c > 255)
9183 return mb_get_class(p) >= 2;
9184 return spelltab.st_isw[c];
9185 }
9186#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009187 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009188}
9189
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009190#ifdef FEAT_MBYTE
9191/*
9192 * Return TRUE if "p" points to a word character.
9193 * Wide version of spell_iswordp().
9194 */
9195 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009196spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009197 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009198 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009199{
9200 int *s;
9201
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009202 if (*p < 256 ? buf->b_spell_ismw[*p]
9203 : (buf->b_spell_ismw_mb != NULL
9204 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009205 s = p + 1;
9206 else
9207 s = p;
9208
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009209 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009210 {
9211 if (enc_utf8)
9212 return utf_class(*s) >= 2;
9213 if (enc_dbcs)
9214 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9215 return 0;
9216 }
9217 return spelltab.st_isw[*s];
9218}
9219#endif
9220
Bram Moolenaarea408852005-06-25 22:49:46 +00009221/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009222 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009223 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009224 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009225 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009226write_spell_prefcond(fd, gap)
9227 FILE *fd;
9228 garray_T *gap;
9229{
9230 int i;
9231 char_u *p;
9232 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009233 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009234
Bram Moolenaar5195e452005-08-19 20:32:47 +00009235 if (fd != NULL)
9236 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9237
9238 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009239
9240 for (i = 0; i < gap->ga_len; ++i)
9241 {
9242 /* <prefcond> : <condlen> <condstr> */
9243 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009244 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009245 {
9246 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009247 if (fd != NULL)
9248 {
9249 fputc(len, fd);
9250 fwrite(p, (size_t)len, (size_t)1, fd);
9251 }
9252 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009253 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009254 else if (fd != NULL)
9255 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009256 }
9257
Bram Moolenaar5195e452005-08-19 20:32:47 +00009258 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009259}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009260
9261/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009262 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9263 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009264 * When using a multi-byte 'encoding' the length may change!
9265 * Returns FAIL when something wrong.
9266 */
9267 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009268spell_casefold(str, len, buf, buflen)
9269 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009270 int len;
9271 char_u *buf;
9272 int buflen;
9273{
9274 int i;
9275
9276 if (len >= buflen)
9277 {
9278 buf[0] = NUL;
9279 return FAIL; /* result will not fit */
9280 }
9281
9282#ifdef FEAT_MBYTE
9283 if (has_mbyte)
9284 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009285 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009286 char_u *p;
9287 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009288
9289 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009290 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009291 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009292 if (outi + MB_MAXBYTES > buflen)
9293 {
9294 buf[outi] = NUL;
9295 return FAIL;
9296 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009297 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009298 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009299 }
9300 buf[outi] = NUL;
9301 }
9302 else
9303#endif
9304 {
9305 /* Be quick for non-multibyte encodings. */
9306 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009307 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009308 buf[i] = NUL;
9309 }
9310
9311 return OK;
9312}
9313
Bram Moolenaar4770d092006-01-12 23:22:24 +00009314/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009315#define SPS_BEST 1
9316#define SPS_FAST 2
9317#define SPS_DOUBLE 4
9318
Bram Moolenaar4770d092006-01-12 23:22:24 +00009319static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9320static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009321
9322/*
9323 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009324 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009325 */
9326 int
9327spell_check_sps()
9328{
9329 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009330 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009331 char_u buf[MAXPATHL];
9332 int f;
9333
9334 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009335 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009336
9337 for (p = p_sps; *p != NUL; )
9338 {
9339 copy_option_part(&p, buf, MAXPATHL, ",");
9340
9341 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009342 if (VIM_ISDIGIT(*buf))
9343 {
9344 s = buf;
9345 sps_limit = getdigits(&s);
9346 if (*s != NUL && !VIM_ISDIGIT(*s))
9347 f = -1;
9348 }
9349 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009350 f = SPS_BEST;
9351 else if (STRCMP(buf, "fast") == 0)
9352 f = SPS_FAST;
9353 else if (STRCMP(buf, "double") == 0)
9354 f = SPS_DOUBLE;
9355 else if (STRNCMP(buf, "expr:", 5) != 0
9356 && STRNCMP(buf, "file:", 5) != 0)
9357 f = -1;
9358
9359 if (f == -1 || (sps_flags != 0 && f != 0))
9360 {
9361 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009362 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009363 return FAIL;
9364 }
9365 if (f != 0)
9366 sps_flags = f;
9367 }
9368
9369 if (sps_flags == 0)
9370 sps_flags = SPS_BEST;
9371
9372 return OK;
9373}
9374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009375/*
9376 * "z?": Find badly spelled word under or after the cursor.
9377 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009378 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009379 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 */
9381 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009382spell_suggest(count)
9383 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384{
9385 char_u *line;
9386 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 char_u wcopy[MAXWLEN + 2];
9388 char_u *p;
9389 int i;
9390 int c;
9391 suginfo_T sug;
9392 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009393 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009394 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009395 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009396 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009397 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009399 if (no_spell_checking(curwin))
9400 return;
9401
9402#ifdef FEAT_VISUAL
9403 if (VIsual_active)
9404 {
9405 /* Use the Visually selected text as the bad word. But reject
9406 * a multi-line selection. */
9407 if (curwin->w_cursor.lnum != VIsual.lnum)
9408 {
9409 vim_beep();
9410 return;
9411 }
9412 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9413 if (badlen < 0)
9414 badlen = -badlen;
9415 else
9416 curwin->w_cursor.col = VIsual.col;
9417 ++badlen;
9418 end_visual_mode();
9419 }
9420 else
9421#endif
9422 /* Find the start of the badly spelled word. */
9423 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009424 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009426 /* No bad word or it starts after the cursor: use the word under the
9427 * cursor. */
9428 curwin->w_cursor = prev_cursor;
9429 line = ml_get_curline();
9430 p = line + curwin->w_cursor.col;
9431 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009432 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009433 mb_ptr_back(line, p);
9434 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009435 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009436 mb_ptr_adv(p);
9437
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009438 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009439 {
9440 beep_flush();
9441 return;
9442 }
9443 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009444 }
9445
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009446 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009448 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009449 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009450
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009451 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009452
Bram Moolenaar5195e452005-08-19 20:32:47 +00009453 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
9454 * 'spellsuggest', whatever is smaller. */
9455 if (sps_limit > (int)Rows - 2)
9456 limit = (int)Rows - 2;
9457 else
9458 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009459 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009460 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461
9462 if (sug.su_ga.ga_len == 0)
9463 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009464 else if (count > 0)
9465 {
9466 if (count > sug.su_ga.ga_len)
9467 smsg((char_u *)_("Sorry, only %ld suggestions"),
9468 (long)sug.su_ga.ga_len);
9469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 else
9471 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009472 vim_free(repl_from);
9473 repl_from = NULL;
9474 vim_free(repl_to);
9475 repl_to = NULL;
9476
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009477#ifdef FEAT_RIGHTLEFT
9478 /* When 'rightleft' is set the list is drawn right-left. */
9479 cmdmsg_rl = curwin->w_p_rl;
9480 if (cmdmsg_rl)
9481 msg_col = Columns - 1;
9482#endif
9483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009484 /* List the suggestions. */
9485 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009486 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
9488 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009489#ifdef FEAT_RIGHTLEFT
9490 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
9491 {
9492 /* And now the rabbit from the high hat: Avoid showing the
9493 * untranslated message rightleft. */
9494 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
9495 sug.su_badlen, sug.su_badptr);
9496 }
9497#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 msg_puts(IObuff);
9499 msg_clr_eos();
9500 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00009501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 msg_scroll = TRUE;
9503 for (i = 0; i < sug.su_ga.ga_len; ++i)
9504 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009505 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506
9507 /* The suggested word may replace only part of the bad word, add
9508 * the not replaced part. */
9509 STRCPY(wcopy, stp->st_word);
9510 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009511 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 sug.su_badptr + stp->st_orglen,
9513 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009514 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
9515#ifdef FEAT_RIGHTLEFT
9516 if (cmdmsg_rl)
9517 rl_mirror(IObuff);
9518#endif
9519 msg_puts(IObuff);
9520
9521 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009522 msg_puts(IObuff);
9523
9524 /* The word may replace more than "su_badlen". */
9525 if (sug.su_badlen < stp->st_orglen)
9526 {
9527 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
9528 stp->st_orglen, sug.su_badptr);
9529 msg_puts(IObuff);
9530 }
9531
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009532 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009533 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009534 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009535 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009536 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009537 stp->st_salscore ? "s " : "",
9538 stp->st_score, stp->st_altscore);
9539 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009540 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00009541 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009542#ifdef FEAT_RIGHTLEFT
9543 if (cmdmsg_rl)
9544 /* Mirror the numbers, but keep the leading space. */
9545 rl_mirror(IObuff + 1);
9546#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00009547 msg_advance(30);
9548 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 msg_putchar('\n');
9551 }
9552
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009553#ifdef FEAT_RIGHTLEFT
9554 cmdmsg_rl = FALSE;
9555 msg_col = 0;
9556#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009558 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009559 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00009560 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 }
9562
Bram Moolenaard12a1322005-08-21 22:08:24 +00009563 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
9564 {
9565 /* Save the from and to text for :spellrepall. */
9566 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00009567 if (sug.su_badlen > stp->st_orglen)
9568 {
9569 /* Replacing less than "su_badlen", append the remainder to
9570 * repl_to. */
9571 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
9572 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
9573 sug.su_badlen - stp->st_orglen,
9574 sug.su_badptr + stp->st_orglen);
9575 repl_to = vim_strsave(IObuff);
9576 }
9577 else
9578 {
9579 /* Replacing su_badlen or more, use the whole word. */
9580 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
9581 repl_to = vim_strsave(stp->st_word);
9582 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009583
9584 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009585 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009586 if (p != NULL)
9587 {
9588 c = sug.su_badptr - line;
9589 mch_memmove(p, line, c);
9590 STRCPY(p + c, stp->st_word);
9591 STRCAT(p, sug.su_badptr + stp->st_orglen);
9592 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9593 curwin->w_cursor.col = c;
9594 changed_bytes(curwin->w_cursor.lnum, c);
9595
9596 /* For redo we use a change-word command. */
9597 ResetRedobuff();
9598 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00009599 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009600 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009601 AppendCharToRedobuff(ESC);
9602 }
9603 }
9604 else
9605 curwin->w_cursor = prev_cursor;
9606
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009607 spell_find_cleanup(&sug);
9608}
9609
9610/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009611 * Check if the word at line "lnum" column "col" is required to start with a
9612 * capital. This uses 'spellcapcheck' of the current buffer.
9613 */
9614 static int
9615check_need_cap(lnum, col)
9616 linenr_T lnum;
9617 colnr_T col;
9618{
9619 int need_cap = FALSE;
9620 char_u *line;
9621 char_u *line_copy = NULL;
9622 char_u *p;
9623 colnr_T endcol;
9624 regmatch_T regmatch;
9625
9626 if (curbuf->b_cap_prog == NULL)
9627 return FALSE;
9628
9629 line = ml_get_curline();
9630 endcol = 0;
9631 if ((int)(skipwhite(line) - line) >= (int)col)
9632 {
9633 /* At start of line, check if previous line is empty or sentence
9634 * ends there. */
9635 if (lnum == 1)
9636 need_cap = TRUE;
9637 else
9638 {
9639 line = ml_get(lnum - 1);
9640 if (*skipwhite(line) == NUL)
9641 need_cap = TRUE;
9642 else
9643 {
9644 /* Append a space in place of the line break. */
9645 line_copy = concat_str(line, (char_u *)" ");
9646 line = line_copy;
9647 endcol = STRLEN(line);
9648 }
9649 }
9650 }
9651 else
9652 endcol = col;
9653
9654 if (endcol > 0)
9655 {
9656 /* Check if sentence ends before the bad word. */
9657 regmatch.regprog = curbuf->b_cap_prog;
9658 regmatch.rm_ic = FALSE;
9659 p = line + endcol;
9660 for (;;)
9661 {
9662 mb_ptr_back(line, p);
9663 if (p == line || spell_iswordp_nmw(p))
9664 break;
9665 if (vim_regexec(&regmatch, p, 0)
9666 && regmatch.endp[0] == line + endcol)
9667 {
9668 need_cap = TRUE;
9669 break;
9670 }
9671 }
9672 }
9673
9674 vim_free(line_copy);
9675
9676 return need_cap;
9677}
9678
9679
9680/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009681 * ":spellrepall"
9682 */
9683/*ARGSUSED*/
9684 void
9685ex_spellrepall(eap)
9686 exarg_T *eap;
9687{
9688 pos_T pos = curwin->w_cursor;
9689 char_u *frompat;
9690 int addlen;
9691 char_u *line;
9692 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009693 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009694 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009695
9696 if (repl_from == NULL || repl_to == NULL)
9697 {
9698 EMSG(_("E752: No previous spell replacement"));
9699 return;
9700 }
9701 addlen = STRLEN(repl_to) - STRLEN(repl_from);
9702
9703 frompat = alloc(STRLEN(repl_from) + 7);
9704 if (frompat == NULL)
9705 return;
9706 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
9707 p_ws = FALSE;
9708
Bram Moolenaar5195e452005-08-19 20:32:47 +00009709 sub_nsubs = 0;
9710 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009711 curwin->w_cursor.lnum = 0;
9712 while (!got_int)
9713 {
9714 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
9715 || u_save_cursor() == FAIL)
9716 break;
9717
9718 /* Only replace when the right word isn't there yet. This happens
9719 * when changing "etc" to "etc.". */
9720 line = ml_get_curline();
9721 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
9722 repl_to, STRLEN(repl_to)) != 0)
9723 {
9724 p = alloc(STRLEN(line) + addlen + 1);
9725 if (p == NULL)
9726 break;
9727 mch_memmove(p, line, curwin->w_cursor.col);
9728 STRCPY(p + curwin->w_cursor.col, repl_to);
9729 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
9730 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9731 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009732
9733 if (curwin->w_cursor.lnum != prev_lnum)
9734 {
9735 ++sub_nlines;
9736 prev_lnum = curwin->w_cursor.lnum;
9737 }
9738 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009739 }
9740 curwin->w_cursor.col += STRLEN(repl_to);
9741 }
9742
9743 p_ws = save_ws;
9744 curwin->w_cursor = pos;
9745 vim_free(frompat);
9746
Bram Moolenaar5195e452005-08-19 20:32:47 +00009747 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009748 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009749 else
9750 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009751}
9752
9753/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009754 * Find spell suggestions for "word". Return them in the growarray "*gap" as
9755 * a list of allocated strings.
9756 */
9757 void
Bram Moolenaar4770d092006-01-12 23:22:24 +00009758spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009759 garray_T *gap;
9760 char_u *word;
9761 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009762 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009763 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009764{
9765 suginfo_T sug;
9766 int i;
9767 suggest_T *stp;
9768 char_u *wcopy;
9769
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009770 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009771
9772 /* Make room in "gap". */
9773 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009774 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009775 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009776 for (i = 0; i < sug.su_ga.ga_len; ++i)
9777 {
9778 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009779
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009780 /* The suggested word may replace only part of "word", add the not
9781 * replaced part. */
9782 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009783 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009784 if (wcopy == NULL)
9785 break;
9786 STRCPY(wcopy, stp->st_word);
9787 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
9788 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
9789 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009790 }
9791
9792 spell_find_cleanup(&sug);
9793}
9794
9795/*
9796 * Find spell suggestions for the word at the start of "badptr".
9797 * Return the suggestions in "su->su_ga".
9798 * The maximum number of suggestions is "maxcount".
9799 * Note: does use info for the current window.
9800 * This is based on the mechanisms of Aspell, but completely reimplemented.
9801 */
9802 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009803spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009804 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009805 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009806 suginfo_T *su;
9807 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00009808 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009809 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009810 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009811{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009812 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009813 char_u buf[MAXPATHL];
9814 char_u *p;
9815 int do_combine = FALSE;
9816 char_u *sps_copy;
9817#ifdef FEAT_EVAL
9818 static int expr_busy = FALSE;
9819#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009820 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009821 int i;
9822 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009823
9824 /*
9825 * Set the info in "*su".
9826 */
9827 vim_memset(su, 0, sizeof(suginfo_T));
9828 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
9829 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009830 if (*badptr == NUL)
9831 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009832 hash_init(&su->su_banned);
9833
9834 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009835 if (badlen != 0)
9836 su->su_badlen = badlen;
9837 else
9838 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009839 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009840 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009841
9842 if (su->su_badlen >= MAXWLEN)
9843 su->su_badlen = MAXWLEN - 1; /* just in case */
9844 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
9845 (void)spell_casefold(su->su_badptr, su->su_badlen,
9846 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009847 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009848 su->su_badflags = badword_captype(su->su_badptr,
9849 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009850 if (need_cap)
9851 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009852
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009853 /* Find the default language for sound folding. We simply use the first
9854 * one in 'spelllang' that supports sound folding. That's good for when
9855 * using multiple files for one language, it's not that bad when mixing
9856 * languages (e.g., "pl,en"). */
9857 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
9858 {
9859 lp = LANGP_ENTRY(curbuf->b_langp, i);
9860 if (lp->lp_sallang != NULL)
9861 {
9862 su->su_sallang = lp->lp_sallang;
9863 break;
9864 }
9865 }
9866
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009867 /* Soundfold the bad word with the default sound folding, so that we don't
9868 * have to do this many times. */
9869 if (su->su_sallang != NULL)
9870 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
9871 su->su_sal_badword);
9872
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009873 /* If the word is not capitalised and spell_check() doesn't consider the
9874 * word to be bad then it might need to be capitalised. Add a suggestion
9875 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009876 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009877 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009878 {
9879 make_case_word(su->su_badword, buf, WF_ONECAP);
9880 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009881 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009882 }
9883
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009884 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009885 if (banbadword)
9886 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009887
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009888 /* Make a copy of 'spellsuggest', because the expression may change it. */
9889 sps_copy = vim_strsave(p_sps);
9890 if (sps_copy == NULL)
9891 return;
9892
9893 /* Loop over the items in 'spellsuggest'. */
9894 for (p = sps_copy; *p != NUL; )
9895 {
9896 copy_option_part(&p, buf, MAXPATHL, ",");
9897
9898 if (STRNCMP(buf, "expr:", 5) == 0)
9899 {
9900#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009901 /* Evaluate an expression. Skip this when called recursively,
9902 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009903 if (!expr_busy)
9904 {
9905 expr_busy = TRUE;
9906 spell_suggest_expr(su, buf + 5);
9907 expr_busy = FALSE;
9908 }
9909#endif
9910 }
9911 else if (STRNCMP(buf, "file:", 5) == 0)
9912 /* Use list of suggestions in a file. */
9913 spell_suggest_file(su, buf + 5);
9914 else
9915 {
9916 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009917 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009918 if (sps_flags & SPS_DOUBLE)
9919 do_combine = TRUE;
9920 }
9921 }
9922
9923 vim_free(sps_copy);
9924
9925 if (do_combine)
9926 /* Combine the two list of suggestions. This must be done last,
9927 * because sorting changes the order again. */
9928 score_combine(su);
9929}
9930
9931#ifdef FEAT_EVAL
9932/*
9933 * Find suggestions by evaluating expression "expr".
9934 */
9935 static void
9936spell_suggest_expr(su, expr)
9937 suginfo_T *su;
9938 char_u *expr;
9939{
9940 list_T *list;
9941 listitem_T *li;
9942 int score;
9943 char_u *p;
9944
9945 /* The work is split up in a few parts to avoid having to export
9946 * suginfo_T.
9947 * First evaluate the expression and get the resulting list. */
9948 list = eval_spell_expr(su->su_badword, expr);
9949 if (list != NULL)
9950 {
9951 /* Loop over the items in the list. */
9952 for (li = list->lv_first; li != NULL; li = li->li_next)
9953 if (li->li_tv.v_type == VAR_LIST)
9954 {
9955 /* Get the word and the score from the items. */
9956 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009957 if (score >= 0 && score <= su->su_maxscore)
9958 add_suggestion(su, &su->su_ga, p, su->su_badlen,
9959 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009960 }
9961 list_unref(list);
9962 }
9963
Bram Moolenaar4770d092006-01-12 23:22:24 +00009964 /* Remove bogus suggestions, sort and truncate at "maxcount". */
9965 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009966 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9967}
9968#endif
9969
9970/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009971 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009972 */
9973 static void
9974spell_suggest_file(su, fname)
9975 suginfo_T *su;
9976 char_u *fname;
9977{
9978 FILE *fd;
9979 char_u line[MAXWLEN * 2];
9980 char_u *p;
9981 int len;
9982 char_u cword[MAXWLEN];
9983
9984 /* Open the file. */
9985 fd = mch_fopen((char *)fname, "r");
9986 if (fd == NULL)
9987 {
9988 EMSG2(_(e_notopen), fname);
9989 return;
9990 }
9991
9992 /* Read it line by line. */
9993 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
9994 {
9995 line_breakcheck();
9996
9997 p = vim_strchr(line, '/');
9998 if (p == NULL)
9999 continue; /* No Tab found, just skip the line. */
10000 *p++ = NUL;
10001 if (STRICMP(su->su_badword, line) == 0)
10002 {
10003 /* Match! Isolate the good word, until CR or NL. */
10004 for (len = 0; p[len] >= ' '; ++len)
10005 ;
10006 p[len] = NUL;
10007
10008 /* If the suggestion doesn't have specific case duplicate the case
10009 * of the bad word. */
10010 if (captype(p, NULL) == 0)
10011 {
10012 make_case_word(p, cword, su->su_badflags);
10013 p = cword;
10014 }
10015
10016 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010017 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010018 }
10019 }
10020
10021 fclose(fd);
10022
Bram Moolenaar4770d092006-01-12 23:22:24 +000010023 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10024 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010025 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10026}
10027
10028/*
10029 * Find suggestions for the internal method indicated by "sps_flags".
10030 */
10031 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010032spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010033 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010034 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010035{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010036 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010037 * Load the .sug file(s) that are available and not done yet.
10038 */
10039 suggest_load_files();
10040
10041 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010042 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010043 *
10044 * Set a maximum score to limit the combination of operations that is
10045 * tried.
10046 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010047 suggest_try_special(su);
10048
10049 /*
10050 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10051 * from the .aff file and inserting a space (split the word).
10052 */
10053 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010054
10055 /* For the resulting top-scorers compute the sound-a-like score. */
10056 if (sps_flags & SPS_DOUBLE)
10057 score_comp_sal(su);
10058
10059 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010060 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010061 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010062 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010063 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010064 if (sps_flags & SPS_BEST)
10065 /* Adjust the word score for the suggestions found so far for how
10066 * they sounds like. */
10067 rescore_suggestions(su);
10068
10069 /*
10070 * While going throught the soundfold tree "su_maxscore" is the score
10071 * for the soundfold word, limits the changes that are being tried,
10072 * and "su_sfmaxscore" the rescored score, which is set by
10073 * cleanup_suggestions().
10074 * First find words with a small edit distance, because this is much
10075 * faster and often already finds the top-N suggestions. If we didn't
10076 * find many suggestions try again with a higher edit distance.
10077 * "sl_sounddone" is used to avoid doing the same word twice.
10078 */
10079 suggest_try_soundalike_prep();
10080 su->su_maxscore = SCORE_SFMAX1;
10081 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010082 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010083 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10084 {
10085 /* We didn't find enough matches, try again, allowing more
10086 * changes to the soundfold word. */
10087 su->su_maxscore = SCORE_SFMAX2;
10088 suggest_try_soundalike(su);
10089 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10090 {
10091 /* Still didn't find enough matches, try again, allowing even
10092 * more changes to the soundfold word. */
10093 su->su_maxscore = SCORE_SFMAX3;
10094 suggest_try_soundalike(su);
10095 }
10096 }
10097 su->su_maxscore = su->su_sfmaxscore;
10098 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010099 }
10100
Bram Moolenaar4770d092006-01-12 23:22:24 +000010101 /* When CTRL-C was hit while searching do show the results. Only clear
10102 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010103 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010104 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010105 {
10106 (void)vgetc();
10107 got_int = FALSE;
10108 }
10109
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010110 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010111 {
10112 if (sps_flags & SPS_BEST)
10113 /* Adjust the word score for how it sounds like. */
10114 rescore_suggestions(su);
10115
Bram Moolenaar4770d092006-01-12 23:22:24 +000010116 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10117 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010118 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010119 }
10120}
10121
10122/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010123 * Load the .sug files for languages that have one and weren't loaded yet.
10124 */
10125 static void
10126suggest_load_files()
10127{
10128 langp_T *lp;
10129 int lpi;
10130 slang_T *slang;
10131 char_u *dotp;
10132 FILE *fd;
10133 char_u buf[MAXWLEN];
10134 int i;
10135 time_t timestamp;
10136 int wcount;
10137 int wordnr;
10138 garray_T ga;
10139 int c;
10140
10141 /* Do this for all languages that support sound folding. */
10142 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10143 {
10144 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10145 slang = lp->lp_slang;
10146 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10147 {
10148 /* Change ".spl" to ".sug" and open the file. When the file isn't
10149 * found silently skip it. Do set "sl_sugloaded" so that we
10150 * don't try again and again. */
10151 slang->sl_sugloaded = TRUE;
10152
10153 dotp = vim_strrchr(slang->sl_fname, '.');
10154 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10155 continue;
10156 STRCPY(dotp, ".sug");
10157 fd = fopen((char *)slang->sl_fname, "r");
10158 if (fd == NULL)
10159 goto nextone;
10160
10161 /*
10162 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10163 */
10164 for (i = 0; i < VIMSUGMAGICL; ++i)
10165 buf[i] = getc(fd); /* <fileID> */
10166 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10167 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010168 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010169 slang->sl_fname);
10170 goto nextone;
10171 }
10172 c = getc(fd); /* <versionnr> */
10173 if (c < VIMSUGVERSION)
10174 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010175 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010176 slang->sl_fname);
10177 goto nextone;
10178 }
10179 else if (c > VIMSUGVERSION)
10180 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010181 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010182 slang->sl_fname);
10183 goto nextone;
10184 }
10185
10186 /* Check the timestamp, it must be exactly the same as the one in
10187 * the .spl file. Otherwise the word numbers won't match. */
10188 timestamp = 0;
10189 for (i = 7; i >= 0; --i) /* <timestamp> */
10190 timestamp += getc(fd) << (i * 8);
10191 if (timestamp != slang->sl_sugtime)
10192 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010193 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010194 slang->sl_fname);
10195 goto nextone;
10196 }
10197
10198 /*
10199 * <SUGWORDTREE>: <wordtree>
10200 * Read the trie with the soundfolded words.
10201 */
10202 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10203 FALSE, 0) != 0)
10204 {
10205someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010206 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010207 slang->sl_fname);
10208 slang_clear_sug(slang);
10209 goto nextone;
10210 }
10211
10212 /*
10213 * <SUGTABLE>: <sugwcount> <sugline> ...
10214 *
10215 * Read the table with word numbers. We use a file buffer for
10216 * this, because it's so much like a file with lines. Makes it
10217 * possible to swap the info and save on memory use.
10218 */
10219 slang->sl_sugbuf = open_spellbuf();
10220 if (slang->sl_sugbuf == NULL)
10221 goto someerror;
10222 /* <sugwcount> */
10223 wcount = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8)
10224 + getc(fd);
10225 if (wcount < 0)
10226 goto someerror;
10227
10228 /* Read all the wordnr lists into the buffer, one NUL terminated
10229 * list per line. */
10230 ga_init2(&ga, 1, 100);
10231 for (wordnr = 0; wordnr < wcount; ++wordnr)
10232 {
10233 ga.ga_len = 0;
10234 for (;;)
10235 {
10236 c = getc(fd); /* <sugline> */
10237 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10238 goto someerror;
10239 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10240 if (c == NUL)
10241 break;
10242 }
10243 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10244 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10245 goto someerror;
10246 }
10247 ga_clear(&ga);
10248
10249 /*
10250 * Need to put word counts in the word tries, so that we can find
10251 * a word by its number.
10252 */
10253 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10254 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10255
10256nextone:
10257 if (fd != NULL)
10258 fclose(fd);
10259 STRCPY(dotp, ".spl");
10260 }
10261 }
10262}
10263
10264
10265/*
10266 * Fill in the wordcount fields for a trie.
10267 * Returns the total number of words.
10268 */
10269 static void
10270tree_count_words(byts, idxs)
10271 char_u *byts;
10272 idx_T *idxs;
10273{
10274 int depth;
10275 idx_T arridx[MAXWLEN];
10276 int curi[MAXWLEN];
10277 int c;
10278 idx_T n;
10279 int wordcount[MAXWLEN];
10280
10281 arridx[0] = 0;
10282 curi[0] = 1;
10283 wordcount[0] = 0;
10284 depth = 0;
10285 while (depth >= 0 && !got_int)
10286 {
10287 if (curi[depth] > byts[arridx[depth]])
10288 {
10289 /* Done all bytes at this node, go up one level. */
10290 idxs[arridx[depth]] = wordcount[depth];
10291 if (depth > 0)
10292 wordcount[depth - 1] += wordcount[depth];
10293
10294 --depth;
10295 fast_breakcheck();
10296 }
10297 else
10298 {
10299 /* Do one more byte at this node. */
10300 n = arridx[depth] + curi[depth];
10301 ++curi[depth];
10302
10303 c = byts[n];
10304 if (c == 0)
10305 {
10306 /* End of word, count it. */
10307 ++wordcount[depth];
10308
10309 /* Skip over any other NUL bytes (same word with different
10310 * flags). */
10311 while (byts[n + 1] == 0)
10312 {
10313 ++n;
10314 ++curi[depth];
10315 }
10316 }
10317 else
10318 {
10319 /* Normal char, go one level deeper to count the words. */
10320 ++depth;
10321 arridx[depth] = idxs[n];
10322 curi[depth] = 1;
10323 wordcount[depth] = 0;
10324 }
10325 }
10326 }
10327}
10328
10329/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010330 * Free the info put in "*su" by spell_find_suggest().
10331 */
10332 static void
10333spell_find_cleanup(su)
10334 suginfo_T *su;
10335{
10336 int i;
10337
10338 /* Free the suggestions. */
10339 for (i = 0; i < su->su_ga.ga_len; ++i)
10340 vim_free(SUG(su->su_ga, i).st_word);
10341 ga_clear(&su->su_ga);
10342 for (i = 0; i < su->su_sga.ga_len; ++i)
10343 vim_free(SUG(su->su_sga, i).st_word);
10344 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345
10346 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010347 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010348}
10349
10350/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010351 * Make a copy of "word", with the first letter upper or lower cased, to
10352 * "wcopy[MAXWLEN]". "word" must not be empty.
10353 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010354 */
10355 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010356onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010357 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010358 char_u *wcopy;
10359 int upper; /* TRUE: first letter made upper case */
10360{
10361 char_u *p;
10362 int c;
10363 int l;
10364
10365 p = word;
10366#ifdef FEAT_MBYTE
10367 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010368 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010369 else
10370#endif
10371 c = *p++;
10372 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010373 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010374 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010375 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376#ifdef FEAT_MBYTE
10377 if (has_mbyte)
10378 l = mb_char2bytes(c, wcopy);
10379 else
10380#endif
10381 {
10382 l = 1;
10383 wcopy[0] = c;
10384 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010385 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386}
10387
10388/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010389 * Make a copy of "word" with all the letters upper cased into
10390 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010391 */
10392 static void
10393allcap_copy(word, wcopy)
10394 char_u *word;
10395 char_u *wcopy;
10396{
10397 char_u *s;
10398 char_u *d;
10399 int c;
10400
10401 d = wcopy;
10402 for (s = word; *s != NUL; )
10403 {
10404#ifdef FEAT_MBYTE
10405 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010406 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 else
10408#endif
10409 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010410
10411#ifdef FEAT_MBYTE
10412 /* We only change ß to SS when we are certain latin1 is used. It
10413 * would cause weird errors in other 8-bit encodings. */
10414 if (enc_latin1like && c == 0xdf)
10415 {
10416 c = 'S';
10417 if (d - wcopy >= MAXWLEN - 1)
10418 break;
10419 *d++ = c;
10420 }
10421 else
10422#endif
10423 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424
10425#ifdef FEAT_MBYTE
10426 if (has_mbyte)
10427 {
10428 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10429 break;
10430 d += mb_char2bytes(c, d);
10431 }
10432 else
10433#endif
10434 {
10435 if (d - wcopy >= MAXWLEN - 1)
10436 break;
10437 *d++ = c;
10438 }
10439 }
10440 *d = NUL;
10441}
10442
10443/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010444 * Try finding suggestions by recognizing specific situations.
10445 */
10446 static void
10447suggest_try_special(su)
10448 suginfo_T *su;
10449{
10450 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010451 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010452 int c;
10453 char_u word[MAXWLEN];
10454
10455 /*
10456 * Recognize a word that is repeated: "the the".
10457 */
10458 p = skiptowhite(su->su_fbadword);
10459 len = p - su->su_fbadword;
10460 p = skipwhite(p);
10461 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10462 {
10463 /* Include badflags: if the badword is onecap or allcap
10464 * use that for the goodword too: "The the" -> "The". */
10465 c = su->su_fbadword[len];
10466 su->su_fbadword[len] = NUL;
10467 make_case_word(su->su_fbadword, word, su->su_badflags);
10468 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010469
10470 /* Give a soundalike score of 0, compute the score as if deleting one
10471 * character. */
10472 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010473 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010474 }
10475}
10476
10477/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 * Try finding suggestions by adding/removing/swapping letters.
10479 */
10480 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010481suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 suginfo_T *su;
10483{
10484 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010485 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010487 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010488 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489
10490 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000010491 * to find matches (esp. REP items). Append some more text, changing
10492 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010493 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010494 n = STRLEN(fword);
10495 p = su->su_badptr + su->su_badlen;
10496 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010498 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010500 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010501
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010502 /* If reloading a spell file fails it's still in the list but
10503 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010504 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010505 continue;
10506
Bram Moolenaar4770d092006-01-12 23:22:24 +000010507 /* Try it for this language. Will add possible suggestions. */
10508 suggest_trie_walk(su, lp, fword, FALSE);
10509 }
10510}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511
Bram Moolenaar4770d092006-01-12 23:22:24 +000010512/* Check the maximum score, if we go over it we won't try this change. */
10513#define TRY_DEEPER(su, stack, depth, add) \
10514 (stack[depth].ts_score + (add) < su->su_maxscore)
10515
10516/*
10517 * Try finding suggestions by adding/removing/swapping letters.
10518 *
10519 * This uses a state machine. At each node in the tree we try various
10520 * operations. When trying if an operation works "depth" is increased and the
10521 * stack[] is used to store info. This allows combinations, thus insert one
10522 * character, replace one and delete another. The number of changes is
10523 * limited by su->su_maxscore.
10524 *
10525 * After implementing this I noticed an article by Kemal Oflazer that
10526 * describes something similar: "Error-tolerant Finite State Recognition with
10527 * Applications to Morphological Analysis and Spelling Correction" (1996).
10528 * The implementation in the article is simplified and requires a stack of
10529 * unknown depth. The implementation here only needs a stack depth equal to
10530 * the length of the word.
10531 *
10532 * This is also used for the sound-folded word, "soundfold" is TRUE then.
10533 * The mechanism is the same, but we find a match with a sound-folded word
10534 * that comes from one or more original words. Each of these words may be
10535 * added, this is done by add_sound_suggest().
10536 * Don't use:
10537 * the prefix tree or the keep-case tree
10538 * "su->su_badlen"
10539 * anything to do with upper and lower case
10540 * anything to do with word or non-word characters ("spell_iswordp()")
10541 * banned words
10542 * word flags (rare, region, compounding)
10543 * word splitting for now
10544 * "similar_chars()"
10545 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
10546 */
10547 static void
10548suggest_trie_walk(su, lp, fword, soundfold)
10549 suginfo_T *su;
10550 langp_T *lp;
10551 char_u *fword;
10552 int soundfold;
10553{
10554 char_u tword[MAXWLEN]; /* good word collected so far */
10555 trystate_T stack[MAXWLEN];
10556 char_u preword[MAXWLEN * 3]; /* word found with proper case;
10557 * concatanation of prefix compound
10558 * words and split word. NUL terminated
10559 * when going deeper but not when coming
10560 * back. */
10561 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
10562 trystate_T *sp;
10563 int newscore;
10564 int score;
10565 char_u *byts, *fbyts, *pbyts;
10566 idx_T *idxs, *fidxs, *pidxs;
10567 int depth;
10568 int c, c2, c3;
10569 int n = 0;
10570 int flags;
10571 garray_T *gap;
10572 idx_T arridx;
10573 int len;
10574 char_u *p;
10575 fromto_T *ftp;
10576 int fl = 0, tl;
10577 int repextra = 0; /* extra bytes in fword[] from REP item */
10578 slang_T *slang = lp->lp_slang;
10579 int fword_ends;
10580 int goodword_ends;
10581#ifdef DEBUG_TRIEWALK
10582 /* Stores the name of the change made at each level. */
10583 char_u changename[MAXWLEN][80];
10584#endif
10585 int breakcheckcount = 1000;
10586 int compound_ok;
10587
10588 /*
10589 * Go through the whole case-fold tree, try changes at each node.
10590 * "tword[]" contains the word collected from nodes in the tree.
10591 * "fword[]" the word we are trying to match with (initially the bad
10592 * word).
10593 */
10594 depth = 0;
10595 sp = &stack[0];
10596 vim_memset(sp, 0, sizeof(trystate_T));
10597 sp->ts_curi = 1;
10598
10599 if (soundfold)
10600 {
10601 /* Going through the soundfold tree. */
10602 byts = fbyts = slang->sl_sbyts;
10603 idxs = fidxs = slang->sl_sidxs;
10604 pbyts = NULL;
10605 pidxs = NULL;
10606 sp->ts_prefixdepth = PFD_NOPREFIX;
10607 sp->ts_state = STATE_START;
10608 }
10609 else
10610 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010611 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010612 * When there are postponed prefixes we need to use these first. At
10613 * the end of the prefix we continue in the case-fold tree.
10614 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010615 fbyts = slang->sl_fbyts;
10616 fidxs = slang->sl_fidxs;
10617 pbyts = slang->sl_pbyts;
10618 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010619 if (pbyts != NULL)
10620 {
10621 byts = pbyts;
10622 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010623 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010624 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
10625 }
10626 else
10627 {
10628 byts = fbyts;
10629 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010630 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010631 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010632 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010633 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010634
Bram Moolenaar4770d092006-01-12 23:22:24 +000010635 /*
10636 * Loop to find all suggestions. At each round we either:
10637 * - For the current state try one operation, advance "ts_curi",
10638 * increase "depth".
10639 * - When a state is done go to the next, set "ts_state".
10640 * - When all states are tried decrease "depth".
10641 */
10642 while (depth >= 0 && !got_int)
10643 {
10644 sp = &stack[depth];
10645 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010647 case STATE_START:
10648 case STATE_NOPREFIX:
10649 /*
10650 * Start of node: Deal with NUL bytes, which means
10651 * tword[] may end here.
10652 */
10653 arridx = sp->ts_arridx; /* current node in the tree */
10654 len = byts[arridx]; /* bytes in this node */
10655 arridx += sp->ts_curi; /* index of current byte */
10656
10657 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010659 /* Skip over the NUL bytes, we use them later. */
10660 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
10661 ;
10662 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663
Bram Moolenaar4770d092006-01-12 23:22:24 +000010664 /* Always past NUL bytes now. */
10665 n = (int)sp->ts_state;
10666 sp->ts_state = STATE_ENDNUL;
10667 sp->ts_save_badflags = su->su_badflags;
10668
10669 /* At end of a prefix or at start of prefixtree: check for
10670 * following word. */
10671 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010672 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010673 /* Set su->su_badflags to the caps type at this position.
10674 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010675#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010676 if (has_mbyte)
10677 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
10678 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000010679#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010680 n = sp->ts_fidx;
10681 flags = badword_captype(su->su_badptr, su->su_badptr + n);
10682 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010683 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010684#ifdef DEBUG_TRIEWALK
10685 sprintf(changename[depth], "prefix");
10686#endif
10687 go_deeper(stack, depth, 0);
10688 ++depth;
10689 sp = &stack[depth];
10690 sp->ts_prefixdepth = depth - 1;
10691 byts = fbyts;
10692 idxs = fidxs;
10693 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010694
Bram Moolenaar4770d092006-01-12 23:22:24 +000010695 /* Move the prefix to preword[] with the right case
10696 * and make find_keepcap_word() works. */
10697 tword[sp->ts_twordlen] = NUL;
10698 make_case_word(tword + sp->ts_splitoff,
10699 preword + sp->ts_prewordlen, flags);
10700 sp->ts_prewordlen = STRLEN(preword);
10701 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010702 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010703 break;
10704 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010705
Bram Moolenaar4770d092006-01-12 23:22:24 +000010706 if (sp->ts_curi > len || byts[arridx] != 0)
10707 {
10708 /* Past bytes in node and/or past NUL bytes. */
10709 sp->ts_state = STATE_ENDNUL;
10710 sp->ts_save_badflags = su->su_badflags;
10711 break;
10712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713
Bram Moolenaar4770d092006-01-12 23:22:24 +000010714 /*
10715 * End of word in tree.
10716 */
10717 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718
Bram Moolenaar4770d092006-01-12 23:22:24 +000010719 flags = (int)idxs[arridx];
10720 fword_ends = (fword[sp->ts_fidx] == NUL
10721 || (soundfold
10722 ? vim_iswhite(fword[sp->ts_fidx])
10723 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
10724 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725
Bram Moolenaar4770d092006-01-12 23:22:24 +000010726 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000010727 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010728 {
10729 /* There was a prefix before the word. Check that the prefix
10730 * can be used with this word. */
10731 /* Count the length of the NULs in the prefix. If there are
10732 * none this must be the first try without a prefix. */
10733 n = stack[sp->ts_prefixdepth].ts_arridx;
10734 len = pbyts[n++];
10735 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
10736 ;
10737 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010738 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010739 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010740 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010741 if (c == 0)
10742 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010743
Bram Moolenaar4770d092006-01-12 23:22:24 +000010744 /* Use the WF_RARE flag for a rare prefix. */
10745 if (c & WF_RAREPFX)
10746 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010747
Bram Moolenaar4770d092006-01-12 23:22:24 +000010748 /* Tricky: when checking for both prefix and compounding
10749 * we run into the prefix flag first.
10750 * Remember that it's OK, so that we accept the prefix
10751 * when arriving at a compound flag. */
10752 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010753 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010754 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010755
Bram Moolenaar4770d092006-01-12 23:22:24 +000010756 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
10757 * appending another compound word below. */
10758 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010759 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000010760 goodword_ends = FALSE;
10761 else
10762 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010763
Bram Moolenaar4770d092006-01-12 23:22:24 +000010764 p = NULL;
10765 compound_ok = TRUE;
10766 if (sp->ts_complen > sp->ts_compsplit)
10767 {
10768 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010769 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010770 /* There was a word before this word. When there was no
10771 * change in this word (it was correct) add the first word
10772 * as a suggestion. If this word was corrected too, we
10773 * need to check if a correct word follows. */
10774 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000010775 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000010776 && STRNCMP(fword + sp->ts_splitfidx,
10777 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000010778 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010779 {
10780 preword[sp->ts_prewordlen] = NUL;
10781 newscore = score_wordcount_adj(slang, sp->ts_score,
10782 preword + sp->ts_prewordlen,
10783 sp->ts_prewordlen > 0);
10784 /* Add the suggestion if the score isn't too bad. */
10785 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000010786 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010787 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010788 newscore, 0, FALSE,
10789 lp->lp_sallang, FALSE);
10790 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000010791 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010792 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000010793 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000010794 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010795 /* There was a compound word before this word. If this
10796 * word does not support compounding then give up
10797 * (splitting is tried for the word without compound
10798 * flag). */
10799 if (((unsigned)flags >> 24) == 0
10800 || sp->ts_twordlen - sp->ts_splitoff
10801 < slang->sl_compminlen)
10802 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010803#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010804 /* For multi-byte chars check character length against
10805 * COMPOUNDMIN. */
10806 if (has_mbyte
10807 && slang->sl_compminlen > 0
10808 && mb_charlen(tword + sp->ts_splitoff)
10809 < slang->sl_compminlen)
10810 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010811#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000010812
Bram Moolenaar4770d092006-01-12 23:22:24 +000010813 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
10814 compflags[sp->ts_complen + 1] = NUL;
10815 vim_strncpy(preword + sp->ts_prewordlen,
10816 tword + sp->ts_splitoff,
10817 sp->ts_twordlen - sp->ts_splitoff);
10818 p = preword;
10819 while (*skiptowhite(p) != NUL)
10820 p = skipwhite(skiptowhite(p));
10821 if (fword_ends && !can_compound(slang, p,
10822 compflags + sp->ts_compsplit))
10823 /* Compound is not allowed. But it may still be
10824 * possible if we add another (short) word. */
10825 compound_ok = FALSE;
10826
10827 /* Get pointer to last char of previous word. */
10828 p = preword + sp->ts_prewordlen;
10829 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010830 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010831 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832
Bram Moolenaar4770d092006-01-12 23:22:24 +000010833 /*
10834 * Form the word with proper case in preword.
10835 * If there is a word from a previous split, append.
10836 * For the soundfold tree don't change the case, simply append.
10837 */
10838 if (soundfold)
10839 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
10840 else if (flags & WF_KEEPCAP)
10841 /* Must find the word in the keep-case tree. */
10842 find_keepcap_word(slang, tword + sp->ts_splitoff,
10843 preword + sp->ts_prewordlen);
10844 else
10845 {
10846 /* Include badflags: If the badword is onecap or allcap
10847 * use that for the goodword too. But if the badword is
10848 * allcap and it's only one char long use onecap. */
10849 c = su->su_badflags;
10850 if ((c & WF_ALLCAP)
10851#ifdef FEAT_MBYTE
10852 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
10853#else
10854 && su->su_badlen == 1
10855#endif
10856 )
10857 c = WF_ONECAP;
10858 c |= flags;
10859
10860 /* When appending a compound word after a word character don't
10861 * use Onecap. */
10862 if (p != NULL && spell_iswordp_nmw(p))
10863 c &= ~WF_ONECAP;
10864 make_case_word(tword + sp->ts_splitoff,
10865 preword + sp->ts_prewordlen, c);
10866 }
10867
10868 if (!soundfold)
10869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 /* Don't use a banned word. It may appear again as a good
10871 * word, thus remember it. */
10872 if (flags & WF_BANNED)
10873 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000010874 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 break;
10876 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010877 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000010878 && WAS_BANNED(su, preword + sp->ts_prewordlen))
10879 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010880 {
10881 if (slang->sl_compprog == NULL)
10882 break;
10883 /* the word so far was banned but we may try compounding */
10884 goodword_ends = FALSE;
10885 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887
Bram Moolenaar4770d092006-01-12 23:22:24 +000010888 newscore = 0;
10889 if (!soundfold) /* soundfold words don't have flags */
10890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010892 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 newscore += SCORE_REGION;
10894 if (flags & WF_RARE)
10895 newscore += SCORE_RARE;
10896
Bram Moolenaar0c405862005-06-22 22:26:26 +000010897 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010898 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010900 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901
Bram Moolenaar4770d092006-01-12 23:22:24 +000010902 /* TODO: how about splitting in the soundfold tree? */
10903 if (fword_ends
10904 && goodword_ends
10905 && sp->ts_fidx >= sp->ts_fidxtry
10906 && compound_ok)
10907 {
10908 /* The badword also ends: add suggestions. */
10909#ifdef DEBUG_TRIEWALK
10910 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010912 int j;
10913
10914 /* print the stack of changes that brought us here */
10915 smsg("------ %s -------", fword);
10916 for (j = 0; j < depth; ++j)
10917 smsg("%s", changename[j]);
10918 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010919#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010920 if (soundfold)
10921 {
10922 /* For soundfolded words we need to find the original
10923 * words, the edit distrance and then add them. */
10924 add_sound_suggest(su, preword, sp->ts_score, lp);
10925 }
10926 else
10927 {
10928 /* Give a penalty when changing non-word char to word
10929 * char, e.g., "thes," -> "these". */
10930 p = fword + sp->ts_fidx;
10931 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010932 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010933 {
10934 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010935 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010936 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010937 newscore += SCORE_NONWORD;
10938 }
10939
Bram Moolenaar4770d092006-01-12 23:22:24 +000010940 /* Give a bonus to words seen before. */
10941 score = score_wordcount_adj(slang,
10942 sp->ts_score + newscore,
10943 preword + sp->ts_prewordlen,
10944 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010945
Bram Moolenaar4770d092006-01-12 23:22:24 +000010946 /* Add the suggestion if the score isn't too bad. */
10947 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000010948 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010949 add_suggestion(su, &su->su_ga, preword,
10950 sp->ts_fidx - repextra,
10951 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000010952
10953 if (su->su_badflags & WF_MIXCAP)
10954 {
10955 /* We really don't know if the word should be
10956 * upper or lower case, add both. */
10957 c = captype(preword, NULL);
10958 if (c == 0 || c == WF_ALLCAP)
10959 {
10960 make_case_word(tword + sp->ts_splitoff,
10961 preword + sp->ts_prewordlen,
10962 c == 0 ? WF_ALLCAP : 0);
10963
10964 add_suggestion(su, &su->su_ga, preword,
10965 sp->ts_fidx - repextra,
10966 score + SCORE_ICASE, 0, FALSE,
10967 lp->lp_sallang, FALSE);
10968 }
10969 }
10970 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010972 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010973
Bram Moolenaar4770d092006-01-12 23:22:24 +000010974 /*
10975 * Try word split and/or compounding.
10976 */
10977 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000010978#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010979 /* Don't split halfway a character. */
10980 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000010981#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010982 )
10983 {
10984 int try_compound;
10985 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010986
Bram Moolenaar4770d092006-01-12 23:22:24 +000010987 /* If past the end of the bad word don't try a split.
10988 * Otherwise try changing the next word. E.g., find
10989 * suggestions for "the the" where the second "the" is
10990 * different. It's done like a split.
10991 * TODO: word split for soundfold words */
10992 try_split = (sp->ts_fidx - repextra < su->su_badlen)
10993 && !soundfold;
10994
10995 /* Get here in several situations:
10996 * 1. The word in the tree ends:
10997 * If the word allows compounding try that. Otherwise try
10998 * a split by inserting a space. For both check that a
10999 * valid words starts at fword[sp->ts_fidx].
11000 * For NOBREAK do like compounding to be able to check if
11001 * the next word is valid.
11002 * 2. The badword does end, but it was due to a change (e.g.,
11003 * a swap). No need to split, but do check that the
11004 * following word is valid.
11005 * 3. The badword and the word in the tree end. It may still
11006 * be possible to compound another (short) word.
11007 */
11008 try_compound = FALSE;
11009 if (!soundfold
11010 && slang->sl_compprog != NULL
11011 && ((unsigned)flags >> 24) != 0
11012 && sp->ts_twordlen - sp->ts_splitoff
11013 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011014#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011015 && (!has_mbyte
11016 || slang->sl_compminlen == 0
11017 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011018 >= slang->sl_compminlen)
11019#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011020 && (slang->sl_compsylmax < MAXWLEN
11021 || sp->ts_complen + 1 - sp->ts_compsplit
11022 < slang->sl_compmax)
11023 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11024 ? slang->sl_compstartflags
11025 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011026 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011027 {
11028 try_compound = TRUE;
11029 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11030 compflags[sp->ts_complen + 1] = NUL;
11031 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011032
Bram Moolenaar4770d092006-01-12 23:22:24 +000011033 /* For NOBREAK we never try splitting, it won't make any word
11034 * valid. */
11035 if (slang->sl_nobreak)
11036 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011037
Bram Moolenaar4770d092006-01-12 23:22:24 +000011038 /* If we could add a compound word, and it's also possible to
11039 * split at this point, do the split first and set
11040 * TSF_DIDSPLIT to avoid doing it again. */
11041 else if (!fword_ends
11042 && try_compound
11043 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11044 {
11045 try_compound = FALSE;
11046 sp->ts_flags |= TSF_DIDSPLIT;
11047 --sp->ts_curi; /* do the same NUL again */
11048 compflags[sp->ts_complen] = NUL;
11049 }
11050 else
11051 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011052
Bram Moolenaar4770d092006-01-12 23:22:24 +000011053 if (try_split || try_compound)
11054 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011055 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011056 {
11057 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011058 * words so far are valid for compounding. If there
11059 * is only one word it must not have the NEEDCOMPOUND
11060 * flag. */
11061 if (sp->ts_complen == sp->ts_compsplit
11062 && (flags & WF_NEEDCOMP))
11063 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011064 p = preword;
11065 while (*skiptowhite(p) != NUL)
11066 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011067 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011068 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011069 compflags + sp->ts_compsplit))
11070 break;
11071 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011072
11073 /* Give a bonus to words seen before. */
11074 newscore = score_wordcount_adj(slang, newscore,
11075 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011076 }
11077
Bram Moolenaar4770d092006-01-12 23:22:24 +000011078 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011080 go_deeper(stack, depth, newscore);
11081#ifdef DEBUG_TRIEWALK
11082 if (!try_compound && !fword_ends)
11083 sprintf(changename[depth], "%.*s-%s: split",
11084 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11085 else
11086 sprintf(changename[depth], "%.*s-%s: compound",
11087 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11088#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011090 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011091 sp->ts_state = STATE_SPLITUNDO;
11092
11093 ++depth;
11094 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011096 /* Append a space to preword when splitting. */
11097 if (!try_compound && !fword_ends)
11098 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011099 sp->ts_prewordlen = STRLEN(preword);
11100 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011101 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011102
11103 /* If the badword has a non-word character at this
11104 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011105 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011106 * character when the word ends. But only when the
11107 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011108 if (((!try_compound && !spell_iswordp_nmw(fword
11109 + sp->ts_fidx))
11110 || fword_ends)
11111 && fword[sp->ts_fidx] != NUL
11112 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011113 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011114 int l;
11115
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011116#ifdef FEAT_MBYTE
11117 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011118 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011119 else
11120#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011121 l = 1;
11122 if (fword_ends)
11123 {
11124 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011125 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011126 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011127 sp->ts_prewordlen += l;
11128 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011129 }
11130 else
11131 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11132 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011133 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011134
Bram Moolenaard12a1322005-08-21 22:08:24 +000011135 /* When compounding include compound flag in
11136 * compflags[] (already set above). When splitting we
11137 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011138 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011139 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011140 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011141 sp->ts_compsplit = sp->ts_complen;
11142 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011143
Bram Moolenaar53805d12005-08-01 07:08:33 +000011144 /* set su->su_badflags to the caps type at this
11145 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011146#ifdef FEAT_MBYTE
11147 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011148 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011149 else
11150#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011151 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011152 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011153 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011156 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011157
11158 /* If there are postponed prefixes, try these too. */
11159 if (pbyts != NULL)
11160 {
11161 byts = pbyts;
11162 idxs = pidxs;
11163 sp->ts_prefixdepth = PFD_PREFIXTREE;
11164 sp->ts_state = STATE_NOPREFIX;
11165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 }
11167 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011168 }
11169 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170
Bram Moolenaar4770d092006-01-12 23:22:24 +000011171 case STATE_SPLITUNDO:
11172 /* Undo the changes done for word split or compound word. */
11173 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174
Bram Moolenaar4770d092006-01-12 23:22:24 +000011175 /* Continue looking for NUL bytes. */
11176 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011177
Bram Moolenaar4770d092006-01-12 23:22:24 +000011178 /* In case we went into the prefix tree. */
11179 byts = fbyts;
11180 idxs = fidxs;
11181 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182
Bram Moolenaar4770d092006-01-12 23:22:24 +000011183 case STATE_ENDNUL:
11184 /* Past the NUL bytes in the node. */
11185 su->su_badflags = sp->ts_save_badflags;
11186 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011187#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011188 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011189#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011190 )
11191 {
11192 /* The badword ends, can't use STATE_PLAIN. */
11193 sp->ts_state = STATE_DEL;
11194 break;
11195 }
11196 sp->ts_state = STATE_PLAIN;
11197 /*FALLTHROUGH*/
11198
11199 case STATE_PLAIN:
11200 /*
11201 * Go over all possible bytes at this node, add each to tword[]
11202 * and use child node. "ts_curi" is the index.
11203 */
11204 arridx = sp->ts_arridx;
11205 if (sp->ts_curi > byts[arridx])
11206 {
11207 /* Done all bytes at this node, do next state. When still at
11208 * already changed bytes skip the other tricks. */
11209 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011212 sp->ts_state = STATE_FINAL;
11213 }
11214 else
11215 {
11216 arridx += sp->ts_curi++;
11217 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218
Bram Moolenaar4770d092006-01-12 23:22:24 +000011219 /* Normal byte, go one level deeper. If it's not equal to the
11220 * byte in the bad word adjust the score. But don't even try
11221 * when the byte was already changed. And don't try when we
11222 * just deleted this byte, accepting it is always cheaper then
11223 * delete + substitute. */
11224 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011225#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011226 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011227#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011228 )
11229 newscore = 0;
11230 else
11231 newscore = SCORE_SUBST;
11232 if ((newscore == 0
11233 || (sp->ts_fidx >= sp->ts_fidxtry
11234 && ((sp->ts_flags & TSF_DIDDEL) == 0
11235 || c != fword[sp->ts_delidx])))
11236 && TRY_DEEPER(su, stack, depth, newscore))
11237 {
11238 go_deeper(stack, depth, newscore);
11239#ifdef DEBUG_TRIEWALK
11240 if (newscore > 0)
11241 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11242 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11243 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011245 sprintf(changename[depth], "%.*s-%s: accept %c",
11246 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11247 fword[sp->ts_fidx]);
11248#endif
11249 ++depth;
11250 sp = &stack[depth];
11251 ++sp->ts_fidx;
11252 tword[sp->ts_twordlen++] = c;
11253 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011254#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011255 if (newscore == SCORE_SUBST)
11256 sp->ts_isdiff = DIFF_YES;
11257 if (has_mbyte)
11258 {
11259 /* Multi-byte characters are a bit complicated to
11260 * handle: They differ when any of the bytes differ
11261 * and then their length may also differ. */
11262 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011263 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011264 /* First byte. */
11265 sp->ts_tcharidx = 0;
11266 sp->ts_tcharlen = MB_BYTE2LEN(c);
11267 sp->ts_fcharstart = sp->ts_fidx - 1;
11268 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011269 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011270 }
11271 else if (sp->ts_isdiff == DIFF_INSERT)
11272 /* When inserting trail bytes don't advance in the
11273 * bad word. */
11274 --sp->ts_fidx;
11275 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11276 {
11277 /* Last byte of character. */
11278 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011279 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011280 /* Correct ts_fidx for the byte length of the
11281 * character (we didn't check that before). */
11282 sp->ts_fidx = sp->ts_fcharstart
11283 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011284 fword[sp->ts_fcharstart]);
11285
Bram Moolenaar4770d092006-01-12 23:22:24 +000011286 /* For changing a composing character adjust
11287 * the score from SCORE_SUBST to
11288 * SCORE_SUBCOMP. */
11289 if (enc_utf8
11290 && utf_iscomposing(
11291 mb_ptr2char(tword
11292 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011293 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011294 && utf_iscomposing(
11295 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011296 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011297 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011298 SCORE_SUBST - SCORE_SUBCOMP;
11299
Bram Moolenaar4770d092006-01-12 23:22:24 +000011300 /* For a similar character adjust score from
11301 * SCORE_SUBST to SCORE_SIMILAR. */
11302 else if (!soundfold
11303 && slang->sl_has_map
11304 && similar_chars(slang,
11305 mb_ptr2char(tword
11306 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011307 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011308 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011309 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011310 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011311 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011312 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313 else if (sp->ts_isdiff == DIFF_INSERT
11314 && sp->ts_twordlen > sp->ts_tcharlen)
11315 {
11316 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11317 c = mb_ptr2char(p);
11318 if (enc_utf8 && utf_iscomposing(c))
11319 {
11320 /* Inserting a composing char doesn't
11321 * count that much. */
11322 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11323 }
11324 else
11325 {
11326 /* If the previous character was the same,
11327 * thus doubling a character, give a bonus
11328 * to the score. Also for the soundfold
11329 * tree (might seem illogical but does
11330 * give better scores). */
11331 mb_ptr_back(tword, p);
11332 if (c == mb_ptr2char(p))
11333 sp->ts_score -= SCORE_INS
11334 - SCORE_INSDUP;
11335 }
11336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337
Bram Moolenaar4770d092006-01-12 23:22:24 +000011338 /* Starting a new char, reset the length. */
11339 sp->ts_tcharlen = 0;
11340 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011341 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011342 else
11343#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011344 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011345 /* If we found a similar char adjust the score.
11346 * We do this after calling go_deeper() because
11347 * it's slow. */
11348 if (newscore != 0
11349 && !soundfold
11350 && slang->sl_has_map
11351 && similar_chars(slang,
11352 c, fword[sp->ts_fidx - 1]))
11353 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011356 }
11357 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358
Bram Moolenaar4770d092006-01-12 23:22:24 +000011359 case STATE_DEL:
11360#ifdef FEAT_MBYTE
11361 /* When past the first byte of a multi-byte char don't try
11362 * delete/insert/swap a character. */
11363 if (has_mbyte && sp->ts_tcharlen > 0)
11364 {
11365 sp->ts_state = STATE_FINAL;
11366 break;
11367 }
11368#endif
11369 /*
11370 * Try skipping one character in the bad word (delete it).
11371 */
11372 sp->ts_state = STATE_INS_PREP;
11373 sp->ts_curi = 1;
11374 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11375 /* Deleting a vowel at the start of a word counts less, see
11376 * soundalike_score(). */
11377 newscore = 2 * SCORE_DEL / 3;
11378 else
11379 newscore = SCORE_DEL;
11380 if (fword[sp->ts_fidx] != NUL
11381 && TRY_DEEPER(su, stack, depth, newscore))
11382 {
11383 go_deeper(stack, depth, newscore);
11384#ifdef DEBUG_TRIEWALK
11385 sprintf(changename[depth], "%.*s-%s: delete %c",
11386 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11387 fword[sp->ts_fidx]);
11388#endif
11389 ++depth;
11390
11391 /* Remember what character we deleted, so that we can avoid
11392 * inserting it again. */
11393 stack[depth].ts_flags |= TSF_DIDDEL;
11394 stack[depth].ts_delidx = sp->ts_fidx;
11395
11396 /* Advance over the character in fword[]. Give a bonus to the
11397 * score if the same character is following "nn" -> "n". It's
11398 * a bit illogical for soundfold tree but it does give better
11399 * results. */
11400#ifdef FEAT_MBYTE
11401 if (has_mbyte)
11402 {
11403 c = mb_ptr2char(fword + sp->ts_fidx);
11404 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11405 if (enc_utf8 && utf_iscomposing(c))
11406 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11407 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11408 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11409 }
11410 else
11411#endif
11412 {
11413 ++stack[depth].ts_fidx;
11414 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11415 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11416 }
11417 break;
11418 }
11419 /*FALLTHROUGH*/
11420
11421 case STATE_INS_PREP:
11422 if (sp->ts_flags & TSF_DIDDEL)
11423 {
11424 /* If we just deleted a byte then inserting won't make sense,
11425 * a substitute is always cheaper. */
11426 sp->ts_state = STATE_SWAP;
11427 break;
11428 }
11429
11430 /* skip over NUL bytes */
11431 n = sp->ts_arridx;
11432 for (;;)
11433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011434 if (sp->ts_curi > byts[n])
11435 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011436 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011438 break;
11439 }
11440 if (byts[n + sp->ts_curi] != NUL)
11441 {
11442 /* Found a byte to insert. */
11443 sp->ts_state = STATE_INS;
11444 break;
11445 }
11446 ++sp->ts_curi;
11447 }
11448 break;
11449
11450 /*FALLTHROUGH*/
11451
11452 case STATE_INS:
11453 /* Insert one byte. Repeat this for each possible byte at this
11454 * node. */
11455 n = sp->ts_arridx;
11456 if (sp->ts_curi > byts[n])
11457 {
11458 /* Done all bytes at this node, go to next state. */
11459 sp->ts_state = STATE_SWAP;
11460 break;
11461 }
11462
11463 /* Do one more byte at this node, but:
11464 * - Skip NUL bytes.
11465 * - Skip the byte if it's equal to the byte in the word,
11466 * accepting that byte is always better.
11467 */
11468 n += sp->ts_curi++;
11469 c = byts[n];
11470 if (soundfold && sp->ts_twordlen == 0 && c == '*')
11471 /* Inserting a vowel at the start of a word counts less,
11472 * see soundalike_score(). */
11473 newscore = 2 * SCORE_INS / 3;
11474 else
11475 newscore = SCORE_INS;
11476 if (c != fword[sp->ts_fidx]
11477 && TRY_DEEPER(su, stack, depth, newscore))
11478 {
11479 go_deeper(stack, depth, newscore);
11480#ifdef DEBUG_TRIEWALK
11481 sprintf(changename[depth], "%.*s-%s: insert %c",
11482 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11483 c);
11484#endif
11485 ++depth;
11486 sp = &stack[depth];
11487 tword[sp->ts_twordlen++] = c;
11488 sp->ts_arridx = idxs[n];
11489#ifdef FEAT_MBYTE
11490 if (has_mbyte)
11491 {
11492 fl = MB_BYTE2LEN(c);
11493 if (fl > 1)
11494 {
11495 /* There are following bytes for the same character.
11496 * We must find all bytes before trying
11497 * delete/insert/swap/etc. */
11498 sp->ts_tcharlen = fl;
11499 sp->ts_tcharidx = 1;
11500 sp->ts_isdiff = DIFF_INSERT;
11501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502 }
11503 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 fl = 1;
11505 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000011506#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011507 {
11508 /* If the previous character was the same, thus doubling a
11509 * character, give a bonus to the score. Also for
11510 * soundfold words (illogical but does give a better
11511 * score). */
11512 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000011513 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011514 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011516 }
11517 break;
11518
11519 case STATE_SWAP:
11520 /*
11521 * Swap two bytes in the bad word: "12" -> "21".
11522 * We change "fword" here, it's changed back afterwards at
11523 * STATE_UNSWAP.
11524 */
11525 p = fword + sp->ts_fidx;
11526 c = *p;
11527 if (c == NUL)
11528 {
11529 /* End of word, can't swap or replace. */
11530 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533
Bram Moolenaar4770d092006-01-12 23:22:24 +000011534 /* Don't swap if the first character is not a word character.
11535 * SWAP3 etc. also don't make sense then. */
11536 if (!soundfold && !spell_iswordp(p, curbuf))
11537 {
11538 sp->ts_state = STATE_REP_INI;
11539 break;
11540 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011541
Bram Moolenaar4770d092006-01-12 23:22:24 +000011542#ifdef FEAT_MBYTE
11543 if (has_mbyte)
11544 {
11545 n = mb_cptr2len(p);
11546 c = mb_ptr2char(p);
11547 if (!soundfold && !spell_iswordp(p + n, curbuf))
11548 c2 = c; /* don't swap non-word char */
11549 else
11550 c2 = mb_ptr2char(p + n);
11551 }
11552 else
11553#endif
11554 {
11555 if (!soundfold && !spell_iswordp(p + 1, curbuf))
11556 c2 = c; /* don't swap non-word char */
11557 else
11558 c2 = p[1];
11559 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011560
Bram Moolenaar4770d092006-01-12 23:22:24 +000011561 /* When characters are identical, swap won't do anything.
11562 * Also get here if the second char is not a word character. */
11563 if (c == c2)
11564 {
11565 sp->ts_state = STATE_SWAP3;
11566 break;
11567 }
11568 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
11569 {
11570 go_deeper(stack, depth, SCORE_SWAP);
11571#ifdef DEBUG_TRIEWALK
11572 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
11573 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11574 c, c2);
11575#endif
11576 sp->ts_state = STATE_UNSWAP;
11577 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011578#ifdef FEAT_MBYTE
11579 if (has_mbyte)
11580 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011581 fl = mb_char2len(c2);
11582 mch_memmove(p, p + n, fl);
11583 mb_char2bytes(c, p + fl);
11584 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011585 }
11586 else
11587#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011588 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011589 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011590 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011592 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011593 }
11594 else
11595 /* If this swap doesn't work then SWAP3 won't either. */
11596 sp->ts_state = STATE_REP_INI;
11597 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000011598
Bram Moolenaar4770d092006-01-12 23:22:24 +000011599 case STATE_UNSWAP:
11600 /* Undo the STATE_SWAP swap: "21" -> "12". */
11601 p = fword + sp->ts_fidx;
11602#ifdef FEAT_MBYTE
11603 if (has_mbyte)
11604 {
11605 n = MB_BYTE2LEN(*p);
11606 c = mb_ptr2char(p + n);
11607 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
11608 mb_char2bytes(c, p);
11609 }
11610 else
11611#endif
11612 {
11613 c = *p;
11614 *p = p[1];
11615 p[1] = c;
11616 }
11617 /*FALLTHROUGH*/
11618
11619 case STATE_SWAP3:
11620 /* Swap two bytes, skipping one: "123" -> "321". We change
11621 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
11622 p = fword + sp->ts_fidx;
11623#ifdef FEAT_MBYTE
11624 if (has_mbyte)
11625 {
11626 n = mb_cptr2len(p);
11627 c = mb_ptr2char(p);
11628 fl = mb_cptr2len(p + n);
11629 c2 = mb_ptr2char(p + n);
11630 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
11631 c3 = c; /* don't swap non-word char */
11632 else
11633 c3 = mb_ptr2char(p + n + fl);
11634 }
11635 else
11636#endif
11637 {
11638 c = *p;
11639 c2 = p[1];
11640 if (!soundfold && !spell_iswordp(p + 2, curbuf))
11641 c3 = c; /* don't swap non-word char */
11642 else
11643 c3 = p[2];
11644 }
11645
11646 /* When characters are identical: "121" then SWAP3 result is
11647 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
11648 * same as SWAP on next char: "112". Thus skip all swapping.
11649 * Also skip when c3 is NUL.
11650 * Also get here when the third character is not a word character.
11651 * Second character may any char: "a.b" -> "b.a" */
11652 if (c == c3 || c3 == NUL)
11653 {
11654 sp->ts_state = STATE_REP_INI;
11655 break;
11656 }
11657 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11658 {
11659 go_deeper(stack, depth, SCORE_SWAP3);
11660#ifdef DEBUG_TRIEWALK
11661 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
11662 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11663 c, c3);
11664#endif
11665 sp->ts_state = STATE_UNSWAP3;
11666 ++depth;
11667#ifdef FEAT_MBYTE
11668 if (has_mbyte)
11669 {
11670 tl = mb_char2len(c3);
11671 mch_memmove(p, p + n + fl, tl);
11672 mb_char2bytes(c2, p + tl);
11673 mb_char2bytes(c, p + fl + tl);
11674 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
11675 }
11676 else
11677#endif
11678 {
11679 p[0] = p[2];
11680 p[2] = c;
11681 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11682 }
11683 }
11684 else
11685 sp->ts_state = STATE_REP_INI;
11686 break;
11687
11688 case STATE_UNSWAP3:
11689 /* Undo STATE_SWAP3: "321" -> "123" */
11690 p = fword + sp->ts_fidx;
11691#ifdef FEAT_MBYTE
11692 if (has_mbyte)
11693 {
11694 n = MB_BYTE2LEN(*p);
11695 c2 = mb_ptr2char(p + n);
11696 fl = MB_BYTE2LEN(p[n]);
11697 c = mb_ptr2char(p + n + fl);
11698 tl = MB_BYTE2LEN(p[n + fl]);
11699 mch_memmove(p + fl + tl, p, n);
11700 mb_char2bytes(c, p);
11701 mb_char2bytes(c2, p + tl);
11702 p = p + tl;
11703 }
11704 else
11705#endif
11706 {
11707 c = *p;
11708 *p = p[2];
11709 p[2] = c;
11710 ++p;
11711 }
11712
11713 if (!soundfold && !spell_iswordp(p, curbuf))
11714 {
11715 /* Middle char is not a word char, skip the rotate. First and
11716 * third char were already checked at swap and swap3. */
11717 sp->ts_state = STATE_REP_INI;
11718 break;
11719 }
11720
11721 /* Rotate three characters left: "123" -> "231". We change
11722 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
11723 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11724 {
11725 go_deeper(stack, depth, SCORE_SWAP3);
11726#ifdef DEBUG_TRIEWALK
11727 p = fword + sp->ts_fidx;
11728 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
11729 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11730 p[0], p[1], p[2]);
11731#endif
11732 sp->ts_state = STATE_UNROT3L;
11733 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011734 p = fword + sp->ts_fidx;
11735#ifdef FEAT_MBYTE
11736 if (has_mbyte)
11737 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011738 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011739 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011740 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011741 fl += mb_cptr2len(p + n + fl);
11742 mch_memmove(p, p + n, fl);
11743 mb_char2bytes(c, p + fl);
11744 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011745 }
11746 else
11747#endif
11748 {
11749 c = *p;
11750 *p = p[1];
11751 p[1] = p[2];
11752 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011753 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000011754 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011755 }
11756 else
11757 sp->ts_state = STATE_REP_INI;
11758 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759
Bram Moolenaar4770d092006-01-12 23:22:24 +000011760 case STATE_UNROT3L:
11761 /* Undo ROT3L: "231" -> "123" */
11762 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000011763#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011764 if (has_mbyte)
11765 {
11766 n = MB_BYTE2LEN(*p);
11767 n += MB_BYTE2LEN(p[n]);
11768 c = mb_ptr2char(p + n);
11769 tl = MB_BYTE2LEN(p[n]);
11770 mch_memmove(p + tl, p, n);
11771 mb_char2bytes(c, p);
11772 }
11773 else
Bram Moolenaarea424162005-06-16 21:51:00 +000011774#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011775 {
11776 c = p[2];
11777 p[2] = p[1];
11778 p[1] = *p;
11779 *p = c;
11780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011781
Bram Moolenaar4770d092006-01-12 23:22:24 +000011782 /* Rotate three bytes right: "123" -> "312". We change "fword"
11783 * here, it's changed back afterwards at STATE_UNROT3R. */
11784 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11785 {
11786 go_deeper(stack, depth, SCORE_SWAP3);
11787#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011789 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
11790 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11791 p[0], p[1], p[2]);
11792#endif
11793 sp->ts_state = STATE_UNROT3R;
11794 ++depth;
11795 p = fword + sp->ts_fidx;
11796#ifdef FEAT_MBYTE
11797 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011798 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011799 n = mb_cptr2len(p);
11800 n += mb_cptr2len(p + n);
11801 c = mb_ptr2char(p + n);
11802 tl = mb_cptr2len(p + n);
11803 mch_memmove(p + tl, p, n);
11804 mb_char2bytes(c, p);
11805 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011806 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011807 else
11808#endif
11809 {
11810 c = p[2];
11811 p[2] = p[1];
11812 p[1] = *p;
11813 *p = c;
11814 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11815 }
11816 }
11817 else
11818 sp->ts_state = STATE_REP_INI;
11819 break;
11820
11821 case STATE_UNROT3R:
11822 /* Undo ROT3R: "312" -> "123" */
11823 p = fword + sp->ts_fidx;
11824#ifdef FEAT_MBYTE
11825 if (has_mbyte)
11826 {
11827 c = mb_ptr2char(p);
11828 tl = MB_BYTE2LEN(*p);
11829 n = MB_BYTE2LEN(p[tl]);
11830 n += MB_BYTE2LEN(p[tl + n]);
11831 mch_memmove(p, p + tl, n);
11832 mb_char2bytes(c, p + n);
11833 }
11834 else
11835#endif
11836 {
11837 c = *p;
11838 *p = p[1];
11839 p[1] = p[2];
11840 p[2] = c;
11841 }
11842 /*FALLTHROUGH*/
11843
11844 case STATE_REP_INI:
11845 /* Check if matching with REP items from the .aff file would work.
11846 * Quickly skip if:
11847 * - there are no REP items and we are not in the soundfold trie
11848 * - the score is going to be too high anyway
11849 * - already applied a REP item or swapped here */
11850 if ((lp->lp_replang == NULL && !soundfold)
11851 || sp->ts_score + SCORE_REP >= su->su_maxscore
11852 || sp->ts_fidx < sp->ts_fidxtry)
11853 {
11854 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011856 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011857
Bram Moolenaar4770d092006-01-12 23:22:24 +000011858 /* Use the first byte to quickly find the first entry that may
11859 * match. If the index is -1 there is none. */
11860 if (soundfold)
11861 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
11862 else
11863 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864
Bram Moolenaar4770d092006-01-12 23:22:24 +000011865 if (sp->ts_curi < 0)
11866 {
11867 sp->ts_state = STATE_FINAL;
11868 break;
11869 }
11870
11871 sp->ts_state = STATE_REP;
11872 /*FALLTHROUGH*/
11873
11874 case STATE_REP:
11875 /* Try matching with REP items from the .aff file. For each match
11876 * replace the characters and check if the resulting word is
11877 * valid. */
11878 p = fword + sp->ts_fidx;
11879
11880 if (soundfold)
11881 gap = &slang->sl_repsal;
11882 else
11883 gap = &lp->lp_replang->sl_rep;
11884 while (sp->ts_curi < gap->ga_len)
11885 {
11886 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
11887 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011888 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011889 /* past possible matching entries */
11890 sp->ts_curi = gap->ga_len;
11891 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011892 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011893 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
11894 && TRY_DEEPER(su, stack, depth, SCORE_REP))
11895 {
11896 go_deeper(stack, depth, SCORE_REP);
11897#ifdef DEBUG_TRIEWALK
11898 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
11899 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11900 ftp->ft_from, ftp->ft_to);
11901#endif
11902 /* Need to undo this afterwards. */
11903 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011904
Bram Moolenaar4770d092006-01-12 23:22:24 +000011905 /* Change the "from" to the "to" string. */
11906 ++depth;
11907 fl = STRLEN(ftp->ft_from);
11908 tl = STRLEN(ftp->ft_to);
11909 if (fl != tl)
11910 {
11911 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
11912 repextra += tl - fl;
11913 }
11914 mch_memmove(p, ftp->ft_to, tl);
11915 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
11916#ifdef FEAT_MBYTE
11917 stack[depth].ts_tcharlen = 0;
11918#endif
11919 break;
11920 }
11921 }
11922
11923 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
11924 /* No (more) matches. */
11925 sp->ts_state = STATE_FINAL;
11926
11927 break;
11928
11929 case STATE_REP_UNDO:
11930 /* Undo a REP replacement and continue with the next one. */
11931 if (soundfold)
11932 gap = &slang->sl_repsal;
11933 else
11934 gap = &lp->lp_replang->sl_rep;
11935 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
11936 fl = STRLEN(ftp->ft_from);
11937 tl = STRLEN(ftp->ft_to);
11938 p = fword + sp->ts_fidx;
11939 if (fl != tl)
11940 {
11941 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
11942 repextra -= tl - fl;
11943 }
11944 mch_memmove(p, ftp->ft_from, fl);
11945 sp->ts_state = STATE_REP;
11946 break;
11947
11948 default:
11949 /* Did all possible states at this level, go up one level. */
11950 --depth;
11951
11952 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
11953 {
11954 /* Continue in or go back to the prefix tree. */
11955 byts = pbyts;
11956 idxs = pidxs;
11957 }
11958
11959 /* Don't check for CTRL-C too often, it takes time. */
11960 if (--breakcheckcount == 0)
11961 {
11962 ui_breakcheck();
11963 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011964 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 }
11966 }
11967}
11968
Bram Moolenaar4770d092006-01-12 23:22:24 +000011969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000011971 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011972 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011973 static void
11974go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 trystate_T *stack;
11976 int depth;
11977 int score_add;
11978{
Bram Moolenaarea424162005-06-16 21:51:00 +000011979 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011981 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011982 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000011983 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984}
11985
Bram Moolenaar53805d12005-08-01 07:08:33 +000011986#ifdef FEAT_MBYTE
11987/*
11988 * Case-folding may change the number of bytes: Count nr of chars in
11989 * fword[flen] and return the byte length of that many chars in "word".
11990 */
11991 static int
11992nofold_len(fword, flen, word)
11993 char_u *fword;
11994 int flen;
11995 char_u *word;
11996{
11997 char_u *p;
11998 int i = 0;
11999
12000 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12001 ++i;
12002 for (p = word; i > 0; mb_ptr_adv(p))
12003 --i;
12004 return (int)(p - word);
12005}
12006#endif
12007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008/*
12009 * "fword" is a good word with case folded. Find the matching keep-case
12010 * words and put it in "kword".
12011 * Theoretically there could be several keep-case words that result in the
12012 * same case-folded word, but we only find one...
12013 */
12014 static void
12015find_keepcap_word(slang, fword, kword)
12016 slang_T *slang;
12017 char_u *fword;
12018 char_u *kword;
12019{
12020 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12021 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012022 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023
12024 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012025 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 int round[MAXWLEN];
12027 int fwordidx[MAXWLEN];
12028 int uwordidx[MAXWLEN];
12029 int kwordlen[MAXWLEN];
12030
12031 int flen, ulen;
12032 int l;
12033 int len;
12034 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012035 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 char_u *p;
12037 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012038 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039
12040 if (byts == NULL)
12041 {
12042 /* array is empty: "cannot happen" */
12043 *kword = NUL;
12044 return;
12045 }
12046
12047 /* Make an all-cap version of "fword". */
12048 allcap_copy(fword, uword);
12049
12050 /*
12051 * Each character needs to be tried both case-folded and upper-case.
12052 * All this gets very complicated if we keep in mind that changing case
12053 * may change the byte length of a multi-byte character...
12054 */
12055 depth = 0;
12056 arridx[0] = 0;
12057 round[0] = 0;
12058 fwordidx[0] = 0;
12059 uwordidx[0] = 0;
12060 kwordlen[0] = 0;
12061 while (depth >= 0)
12062 {
12063 if (fword[fwordidx[depth]] == NUL)
12064 {
12065 /* We are at the end of "fword". If the tree allows a word to end
12066 * here we have found a match. */
12067 if (byts[arridx[depth] + 1] == 0)
12068 {
12069 kword[kwordlen[depth]] = NUL;
12070 return;
12071 }
12072
12073 /* kword is getting too long, continue one level up */
12074 --depth;
12075 }
12076 else if (++round[depth] > 2)
12077 {
12078 /* tried both fold-case and upper-case character, continue one
12079 * level up */
12080 --depth;
12081 }
12082 else
12083 {
12084 /*
12085 * round[depth] == 1: Try using the folded-case character.
12086 * round[depth] == 2: Try using the upper-case character.
12087 */
12088#ifdef FEAT_MBYTE
12089 if (has_mbyte)
12090 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012091 flen = mb_cptr2len(fword + fwordidx[depth]);
12092 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 }
12094 else
12095#endif
12096 ulen = flen = 1;
12097 if (round[depth] == 1)
12098 {
12099 p = fword + fwordidx[depth];
12100 l = flen;
12101 }
12102 else
12103 {
12104 p = uword + uwordidx[depth];
12105 l = ulen;
12106 }
12107
12108 for (tryidx = arridx[depth]; l > 0; --l)
12109 {
12110 /* Perform a binary search in the list of accepted bytes. */
12111 len = byts[tryidx++];
12112 c = *p++;
12113 lo = tryidx;
12114 hi = tryidx + len - 1;
12115 while (lo < hi)
12116 {
12117 m = (lo + hi) / 2;
12118 if (byts[m] > c)
12119 hi = m - 1;
12120 else if (byts[m] < c)
12121 lo = m + 1;
12122 else
12123 {
12124 lo = hi = m;
12125 break;
12126 }
12127 }
12128
12129 /* Stop if there is no matching byte. */
12130 if (hi < lo || byts[lo] != c)
12131 break;
12132
12133 /* Continue at the child (if there is one). */
12134 tryidx = idxs[lo];
12135 }
12136
12137 if (l == 0)
12138 {
12139 /*
12140 * Found the matching char. Copy it to "kword" and go a
12141 * level deeper.
12142 */
12143 if (round[depth] == 1)
12144 {
12145 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12146 flen);
12147 kwordlen[depth + 1] = kwordlen[depth] + flen;
12148 }
12149 else
12150 {
12151 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12152 ulen);
12153 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12154 }
12155 fwordidx[depth + 1] = fwordidx[depth] + flen;
12156 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12157
12158 ++depth;
12159 arridx[depth] = tryidx;
12160 round[depth] = 0;
12161 }
12162 }
12163 }
12164
12165 /* Didn't find it: "cannot happen". */
12166 *kword = NUL;
12167}
12168
12169/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012170 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12171 * su->su_sga.
12172 */
12173 static void
12174score_comp_sal(su)
12175 suginfo_T *su;
12176{
12177 langp_T *lp;
12178 char_u badsound[MAXWLEN];
12179 int i;
12180 suggest_T *stp;
12181 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012182 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012183 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012184
12185 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12186 return;
12187
12188 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012189 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012190 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012191 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012192 if (lp->lp_slang->sl_sal.ga_len > 0)
12193 {
12194 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012195 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012196
12197 for (i = 0; i < su->su_ga.ga_len; ++i)
12198 {
12199 stp = &SUG(su->su_ga, i);
12200
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012201 /* Case-fold the suggested word, sound-fold it and compute the
12202 * sound-a-like score. */
12203 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012204 if (score < SCORE_MAXMAX)
12205 {
12206 /* Add the suggestion. */
12207 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12208 sstp->st_word = vim_strsave(stp->st_word);
12209 if (sstp->st_word != NULL)
12210 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012211 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012212 sstp->st_score = score;
12213 sstp->st_altscore = 0;
12214 sstp->st_orglen = stp->st_orglen;
12215 ++su->su_sga.ga_len;
12216 }
12217 }
12218 }
12219 break;
12220 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012221 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012222}
12223
12224/*
12225 * Combine the list of suggestions in su->su_ga and su->su_sga.
12226 * They are intwined.
12227 */
12228 static void
12229score_combine(su)
12230 suginfo_T *su;
12231{
12232 int i;
12233 int j;
12234 garray_T ga;
12235 garray_T *gap;
12236 langp_T *lp;
12237 suggest_T *stp;
12238 char_u *p;
12239 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012240 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012241 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012242 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012243
12244 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012245 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012246 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012247 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012248 if (lp->lp_slang->sl_sal.ga_len > 0)
12249 {
12250 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012251 slang = lp->lp_slang;
12252 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012253
12254 for (i = 0; i < su->su_ga.ga_len; ++i)
12255 {
12256 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012257 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012258 if (stp->st_altscore == SCORE_MAXMAX)
12259 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12260 else
12261 stp->st_score = (stp->st_score * 3
12262 + stp->st_altscore) / 4;
12263 stp->st_salscore = FALSE;
12264 }
12265 break;
12266 }
12267 }
12268
Bram Moolenaar4770d092006-01-12 23:22:24 +000012269 if (slang == NULL) /* just in case */
12270 return;
12271
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012272 /* Add the alternate score to su_sga. */
12273 for (i = 0; i < su->su_sga.ga_len; ++i)
12274 {
12275 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012276 stp->st_altscore = spell_edit_score(slang,
12277 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012278 if (stp->st_score == SCORE_MAXMAX)
12279 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12280 else
12281 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12282 stp->st_salscore = TRUE;
12283 }
12284
Bram Moolenaar4770d092006-01-12 23:22:24 +000012285 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12286 * for both lists. */
12287 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012288 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012289 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012290 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12291
12292 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12293 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12294 return;
12295
12296 stp = &SUG(ga, 0);
12297 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12298 {
12299 /* round 1: get a suggestion from su_ga
12300 * round 2: get a suggestion from su_sga */
12301 for (round = 1; round <= 2; ++round)
12302 {
12303 gap = round == 1 ? &su->su_ga : &su->su_sga;
12304 if (i < gap->ga_len)
12305 {
12306 /* Don't add a word if it's already there. */
12307 p = SUG(*gap, i).st_word;
12308 for (j = 0; j < ga.ga_len; ++j)
12309 if (STRCMP(stp[j].st_word, p) == 0)
12310 break;
12311 if (j == ga.ga_len)
12312 stp[ga.ga_len++] = SUG(*gap, i);
12313 else
12314 vim_free(p);
12315 }
12316 }
12317 }
12318
12319 ga_clear(&su->su_ga);
12320 ga_clear(&su->su_sga);
12321
12322 /* Truncate the list to the number of suggestions that will be displayed. */
12323 if (ga.ga_len > su->su_maxcount)
12324 {
12325 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12326 vim_free(stp[i].st_word);
12327 ga.ga_len = su->su_maxcount;
12328 }
12329
12330 su->su_ga = ga;
12331}
12332
12333/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012334 * For the goodword in "stp" compute the soundalike score compared to the
12335 * badword.
12336 */
12337 static int
12338stp_sal_score(stp, su, slang, badsound)
12339 suggest_T *stp;
12340 suginfo_T *su;
12341 slang_T *slang;
12342 char_u *badsound; /* sound-folded badword */
12343{
12344 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012345 char_u *pbad;
12346 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012347 char_u badsound2[MAXWLEN];
12348 char_u fword[MAXWLEN];
12349 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012350 char_u goodword[MAXWLEN];
12351 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012352
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012353 lendiff = (int)(su->su_badlen - stp->st_orglen);
12354 if (lendiff >= 0)
12355 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012356 else
12357 {
12358 /* soundfold the bad word with more characters following */
12359 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12360
12361 /* When joining two words the sound often changes a lot. E.g., "t he"
12362 * sounds like "t h" while "the" sounds like "@". Avoid that by
12363 * removing the space. Don't do it when the good word also contains a
12364 * space. */
12365 if (vim_iswhite(su->su_badptr[su->su_badlen])
12366 && *skiptowhite(stp->st_word) == NUL)
12367 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12368 mch_memmove(p, p + 1, STRLEN(p));
12369
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012370 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012371 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012372 }
12373
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012374 if (lendiff > 0)
12375 {
12376 /* Add part of the bad word to the good word, so that we soundfold
12377 * what replaces the bad word. */
12378 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012379 vim_strncpy(goodword + stp->st_wordlen,
12380 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012381 pgood = goodword;
12382 }
12383 else
12384 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012385
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012386 /* Sound-fold the word and compute the score for the difference. */
12387 spell_soundfold(slang, pgood, FALSE, goodsound);
12388
12389 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012390}
12391
Bram Moolenaar4770d092006-01-12 23:22:24 +000012392/* structure used to store soundfolded words that add_sound_suggest() has
12393 * handled already. */
12394typedef struct
12395{
12396 short sft_score; /* lowest score used */
12397 char_u sft_word[1]; /* soundfolded word, actually longer */
12398} sftword_T;
12399
12400static sftword_T dumsft;
12401#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12402#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12403
12404/*
12405 * Prepare for calling suggest_try_soundalike().
12406 */
12407 static void
12408suggest_try_soundalike_prep()
12409{
12410 langp_T *lp;
12411 int lpi;
12412 slang_T *slang;
12413
12414 /* Do this for all languages that support sound folding and for which a
12415 * .sug file has been loaded. */
12416 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12417 {
12418 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12419 slang = lp->lp_slang;
12420 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12421 /* prepare the hashtable used by add_sound_suggest() */
12422 hash_init(&slang->sl_sounddone);
12423 }
12424}
12425
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012426/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012427 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012428 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012429 */
12430 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012431suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012432 suginfo_T *su;
12433{
12434 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012436 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012437 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438
Bram Moolenaar4770d092006-01-12 23:22:24 +000012439 /* Do this for all languages that support sound folding and for which a
12440 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012441 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012442 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012443 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12444 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012445 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012446 {
12447 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012448 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012449
Bram Moolenaar4770d092006-01-12 23:22:24 +000012450 /* try all kinds of inserts/deletes/swaps/etc. */
12451 /* TODO: also soundfold the next words, so that we can try joining
12452 * and splitting */
12453 suggest_trie_walk(su, lp, salword, TRUE);
12454 }
12455 }
12456}
12457
12458/*
12459 * Finish up after calling suggest_try_soundalike().
12460 */
12461 static void
12462suggest_try_soundalike_finish()
12463{
12464 langp_T *lp;
12465 int lpi;
12466 slang_T *slang;
12467 int todo;
12468 hashitem_T *hi;
12469
12470 /* Do this for all languages that support sound folding and for which a
12471 * .sug file has been loaded. */
12472 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12473 {
12474 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12475 slang = lp->lp_slang;
12476 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12477 {
12478 /* Free the info about handled words. */
12479 todo = slang->sl_sounddone.ht_used;
12480 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
12481 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012482 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012483 vim_free(HI2SFT(hi));
12484 --todo;
12485 }
12486 hash_clear(&slang->sl_sounddone);
12487 }
12488 }
12489}
12490
12491/*
12492 * A match with a soundfolded word is found. Add the good word(s) that
12493 * produce this soundfolded word.
12494 */
12495 static void
12496add_sound_suggest(su, goodword, score, lp)
12497 suginfo_T *su;
12498 char_u *goodword;
12499 int score; /* soundfold score */
12500 langp_T *lp;
12501{
12502 slang_T *slang = lp->lp_slang; /* language for sound folding */
12503 int sfwordnr;
12504 char_u *nrline;
12505 int orgnr;
12506 char_u theword[MAXWLEN];
12507 int i;
12508 int wlen;
12509 char_u *byts;
12510 idx_T *idxs;
12511 int n;
12512 int wordcount;
12513 int wc;
12514 int goodscore;
12515 hash_T hash;
12516 hashitem_T *hi;
12517 sftword_T *sft;
12518 int bc, gc;
12519 int limit;
12520
12521 /*
12522 * It's very well possible that the same soundfold word is found several
12523 * times with different scores. Since the following is quite slow only do
12524 * the words that have a better score than before. Use a hashtable to
12525 * remember the words that have been done.
12526 */
12527 hash = hash_hash(goodword);
12528 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
12529 if (HASHITEM_EMPTY(hi))
12530 {
12531 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
12532 if (sft != NULL)
12533 {
12534 sft->sft_score = score;
12535 STRCPY(sft->sft_word, goodword);
12536 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
12537 }
12538 }
12539 else
12540 {
12541 sft = HI2SFT(hi);
12542 if (score >= sft->sft_score)
12543 return;
12544 sft->sft_score = score;
12545 }
12546
12547 /*
12548 * Find the word nr in the soundfold tree.
12549 */
12550 sfwordnr = soundfold_find(slang, goodword);
12551 if (sfwordnr < 0)
12552 {
12553 EMSG2(_(e_intern2), "add_sound_suggest()");
12554 return;
12555 }
12556
12557 /*
12558 * go over the list of good words that produce this soundfold word
12559 */
12560 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
12561 orgnr = 0;
12562 while (*nrline != NUL)
12563 {
12564 /* The wordnr was stored in a minimal nr of bytes as an offset to the
12565 * previous wordnr. */
12566 orgnr += bytes2offset(&nrline);
12567
12568 byts = slang->sl_fbyts;
12569 idxs = slang->sl_fidxs;
12570
12571 /* Lookup the word "orgnr" one of the two tries. */
12572 n = 0;
12573 wlen = 0;
12574 wordcount = 0;
12575 for (;;)
12576 {
12577 i = 1;
12578 if (wordcount == orgnr && byts[n + 1] == NUL)
12579 break; /* found end of word */
12580
12581 if (byts[n + 1] == NUL)
12582 ++wordcount;
12583
12584 /* skip over the NUL bytes */
12585 for ( ; byts[n + i] == NUL; ++i)
12586 if (i > byts[n]) /* safety check */
12587 {
12588 STRCPY(theword + wlen, "BAD");
12589 goto badword;
12590 }
12591
12592 /* One of the siblings must have the word. */
12593 for ( ; i < byts[n]; ++i)
12594 {
12595 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
12596 if (wordcount + wc > orgnr)
12597 break;
12598 wordcount += wc;
12599 }
12600
12601 theword[wlen++] = byts[n + i];
12602 n = idxs[n + i];
12603 }
12604badword:
12605 theword[wlen] = NUL;
12606
12607 /* Go over the possible flags and regions. */
12608 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
12609 {
12610 char_u cword[MAXWLEN];
12611 char_u *p;
12612 int flags = (int)idxs[n + i];
12613
12614 if (flags & WF_KEEPCAP)
12615 {
12616 /* Must find the word in the keep-case tree. */
12617 find_keepcap_word(slang, theword, cword);
12618 p = cword;
12619 }
12620 else
12621 {
12622 flags |= su->su_badflags;
12623 if ((flags & WF_CAPMASK) != 0)
12624 {
12625 /* Need to fix case according to "flags". */
12626 make_case_word(theword, cword, flags);
12627 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012628 }
12629 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012630 p = theword;
12631 }
12632
12633 /* Add the suggestion. */
12634 if (sps_flags & SPS_DOUBLE)
12635 {
12636 /* Add the suggestion if the score isn't too bad. */
12637 if (score <= su->su_maxscore)
12638 add_suggestion(su, &su->su_sga, p, su->su_badlen,
12639 score, 0, FALSE, slang, FALSE);
12640 }
12641 else
12642 {
12643 /* Add a penalty for words in another region. */
12644 if ((flags & WF_REGION)
12645 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
12646 goodscore = SCORE_REGION;
12647 else
12648 goodscore = 0;
12649
12650 /* Add a small penalty for changing the first letter from
12651 * lower to upper case. Helps for "tath" -> "Kath", which is
12652 * less common thatn "tath" -> "path". Don't do it when the
12653 * letter is the same, that has already been counted. */
12654 gc = PTR2CHAR(p);
12655 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012657 bc = PTR2CHAR(su->su_badword);
12658 if (!SPELL_ISUPPER(bc)
12659 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
12660 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661 }
12662
Bram Moolenaar4770d092006-01-12 23:22:24 +000012663 /* Compute the score for the good word. This only does letter
12664 * insert/delete/swap/replace. REP items are not considered,
12665 * which may make the score a bit higher.
12666 * Use a limit for the score to make it work faster. Use
12667 * MAXSCORE(), because RESCORE() will change the score.
12668 * If the limit is very high then the iterative method is
12669 * inefficient, using an array is quicker. */
12670 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
12671 if (limit > SCORE_LIMITMAX)
12672 goodscore += spell_edit_score(slang, su->su_badword, p);
12673 else
12674 goodscore += spell_edit_score_limit(slang, su->su_badword,
12675 p, limit);
12676
12677 /* When going over the limit don't bother to do the rest. */
12678 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012680 /* Give a bonus to words seen before. */
12681 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682
Bram Moolenaar4770d092006-01-12 23:22:24 +000012683 /* Add the suggestion if the score isn't too bad. */
12684 goodscore = RESCORE(goodscore, score);
12685 if (goodscore <= su->su_sfmaxscore)
12686 add_suggestion(su, &su->su_ga, p, su->su_badlen,
12687 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 }
12690 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012691 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 }
12693}
12694
12695/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012696 * Find word "word" in fold-case tree for "slang" and return the word number.
12697 */
12698 static int
12699soundfold_find(slang, word)
12700 slang_T *slang;
12701 char_u *word;
12702{
12703 idx_T arridx = 0;
12704 int len;
12705 int wlen = 0;
12706 int c;
12707 char_u *ptr = word;
12708 char_u *byts;
12709 idx_T *idxs;
12710 int wordnr = 0;
12711
12712 byts = slang->sl_sbyts;
12713 idxs = slang->sl_sidxs;
12714
12715 for (;;)
12716 {
12717 /* First byte is the number of possible bytes. */
12718 len = byts[arridx++];
12719
12720 /* If the first possible byte is a zero the word could end here.
12721 * If the word ends we found the word. If not skip the NUL bytes. */
12722 c = ptr[wlen];
12723 if (byts[arridx] == NUL)
12724 {
12725 if (c == NUL)
12726 break;
12727
12728 /* Skip over the zeros, there can be several. */
12729 while (len > 0 && byts[arridx] == NUL)
12730 {
12731 ++arridx;
12732 --len;
12733 }
12734 if (len == 0)
12735 return -1; /* no children, word should have ended here */
12736 ++wordnr;
12737 }
12738
12739 /* If the word ends we didn't find it. */
12740 if (c == NUL)
12741 return -1;
12742
12743 /* Perform a binary search in the list of accepted bytes. */
12744 if (c == TAB) /* <Tab> is handled like <Space> */
12745 c = ' ';
12746 while (byts[arridx] < c)
12747 {
12748 /* The word count is in the first idxs[] entry of the child. */
12749 wordnr += idxs[idxs[arridx]];
12750 ++arridx;
12751 if (--len == 0) /* end of the bytes, didn't find it */
12752 return -1;
12753 }
12754 if (byts[arridx] != c) /* didn't find the byte */
12755 return -1;
12756
12757 /* Continue at the child (if there is one). */
12758 arridx = idxs[arridx];
12759 ++wlen;
12760
12761 /* One space in the good word may stand for several spaces in the
12762 * checked word. */
12763 if (c == ' ')
12764 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
12765 ++wlen;
12766 }
12767
12768 return wordnr;
12769}
12770
12771/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012772 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012773 */
12774 static void
12775make_case_word(fword, cword, flags)
12776 char_u *fword;
12777 char_u *cword;
12778 int flags;
12779{
12780 if (flags & WF_ALLCAP)
12781 /* Make it all upper-case */
12782 allcap_copy(fword, cword);
12783 else if (flags & WF_ONECAP)
12784 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012785 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 else
12787 /* Use goodword as-is. */
12788 STRCPY(cword, fword);
12789}
12790
Bram Moolenaarea424162005-06-16 21:51:00 +000012791/*
12792 * Use map string "map" for languages "lp".
12793 */
12794 static void
12795set_map_str(lp, map)
12796 slang_T *lp;
12797 char_u *map;
12798{
12799 char_u *p;
12800 int headc = 0;
12801 int c;
12802 int i;
12803
12804 if (*map == NUL)
12805 {
12806 lp->sl_has_map = FALSE;
12807 return;
12808 }
12809 lp->sl_has_map = TRUE;
12810
Bram Moolenaar4770d092006-01-12 23:22:24 +000012811 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000012812 for (i = 0; i < 256; ++i)
12813 lp->sl_map_array[i] = 0;
12814#ifdef FEAT_MBYTE
12815 hash_init(&lp->sl_map_hash);
12816#endif
12817
12818 /*
12819 * The similar characters are stored separated with slashes:
12820 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
12821 * before the same slash. For characters above 255 sl_map_hash is used.
12822 */
12823 for (p = map; *p != NUL; )
12824 {
12825#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012826 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012827#else
12828 c = *p++;
12829#endif
12830 if (c == '/')
12831 headc = 0;
12832 else
12833 {
12834 if (headc == 0)
12835 headc = c;
12836
12837#ifdef FEAT_MBYTE
12838 /* Characters above 255 don't fit in sl_map_array[], put them in
12839 * the hash table. Each entry is the char, a NUL the headchar and
12840 * a NUL. */
12841 if (c >= 256)
12842 {
12843 int cl = mb_char2len(c);
12844 int headcl = mb_char2len(headc);
12845 char_u *b;
12846 hash_T hash;
12847 hashitem_T *hi;
12848
12849 b = alloc((unsigned)(cl + headcl + 2));
12850 if (b == NULL)
12851 return;
12852 mb_char2bytes(c, b);
12853 b[cl] = NUL;
12854 mb_char2bytes(headc, b + cl + 1);
12855 b[cl + 1 + headcl] = NUL;
12856 hash = hash_hash(b);
12857 hi = hash_lookup(&lp->sl_map_hash, b, hash);
12858 if (HASHITEM_EMPTY(hi))
12859 hash_add_item(&lp->sl_map_hash, hi, b, hash);
12860 else
12861 {
12862 /* This should have been checked when generating the .spl
12863 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000012864 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000012865 vim_free(b);
12866 }
12867 }
12868 else
12869#endif
12870 lp->sl_map_array[c] = headc;
12871 }
12872 }
12873}
12874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012875/*
12876 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
12877 * lines in the .aff file.
12878 */
12879 static int
12880similar_chars(slang, c1, c2)
12881 slang_T *slang;
12882 int c1;
12883 int c2;
12884{
Bram Moolenaarea424162005-06-16 21:51:00 +000012885 int m1, m2;
12886#ifdef FEAT_MBYTE
12887 char_u buf[MB_MAXBYTES];
12888 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012889
Bram Moolenaarea424162005-06-16 21:51:00 +000012890 if (c1 >= 256)
12891 {
12892 buf[mb_char2bytes(c1, buf)] = 0;
12893 hi = hash_find(&slang->sl_map_hash, buf);
12894 if (HASHITEM_EMPTY(hi))
12895 m1 = 0;
12896 else
12897 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
12898 }
12899 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012900#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000012901 m1 = slang->sl_map_array[c1];
12902 if (m1 == 0)
12903 return FALSE;
12904
12905
12906#ifdef FEAT_MBYTE
12907 if (c2 >= 256)
12908 {
12909 buf[mb_char2bytes(c2, buf)] = 0;
12910 hi = hash_find(&slang->sl_map_hash, buf);
12911 if (HASHITEM_EMPTY(hi))
12912 m2 = 0;
12913 else
12914 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
12915 }
12916 else
12917#endif
12918 m2 = slang->sl_map_array[c2];
12919
12920 return m1 == m2;
12921}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012922
12923/*
12924 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000012925 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012926 */
12927 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000012928add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
12929 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012930 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012931 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012932 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012933 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012934 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012935 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012936 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012937 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012938 int maxsf; /* su_maxscore applies to soundfold score,
12939 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012940{
Bram Moolenaar4770d092006-01-12 23:22:24 +000012941 int goodlen; /* len of goodword changed */
12942 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012943 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012944 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012945 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012946 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012947
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012948 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
12949 * "thee the" is added next to changing the first "the" the "thee". */
12950 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012951 pbad = su->su_badptr + badlenarg;
12952 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012953 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012954 goodlen = pgood - goodword;
12955 badlen = pbad - su->su_badptr;
12956 if (goodlen <= 0 || badlen <= 0)
12957 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012958 mb_ptr_back(goodword, pgood);
12959 mb_ptr_back(su->su_badptr, pbad);
12960#ifdef FEAT_MBYTE
12961 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012962 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012963 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
12964 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012965 }
12966 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012967#endif
12968 if (*pgood != *pbad)
12969 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012970 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012971
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012972 if (badlen == 0 && goodlen == 0)
12973 /* goodword doesn't change anything; may happen for "the the" changing
12974 * the first "the" to itself. */
12975 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012976
Bram Moolenaar4770d092006-01-12 23:22:24 +000012977 /* Check if the word is already there. Also check the length that is
12978 * being replaced "thes," -> "these" is a different suggestion from
12979 * "thes" -> "these". */
12980 stp = &SUG(*gap, 0);
12981 for (i = gap->ga_len; --i >= 0; ++stp)
12982 if (stp->st_wordlen == goodlen
12983 && stp->st_orglen == badlen
12984 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
12985 {
12986 /*
12987 * Found it. Remember the word with the lowest score.
12988 */
12989 if (stp->st_slang == NULL)
12990 stp->st_slang = slang;
12991
12992 new_sug.st_score = score;
12993 new_sug.st_altscore = altscore;
12994 new_sug.st_had_bonus = had_bonus;
12995
12996 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012997 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012998 /* Only one of the two had the soundalike score computed.
12999 * Need to do that for the other one now, otherwise the
13000 * scores can't be compared. This happens because
13001 * suggest_try_change() doesn't compute the soundalike
13002 * word to keep it fast, while some special methods set
13003 * the soundalike score to zero. */
13004 if (had_bonus)
13005 rescore_one(su, stp);
13006 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013007 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013008 new_sug.st_word = stp->st_word;
13009 new_sug.st_wordlen = stp->st_wordlen;
13010 new_sug.st_slang = stp->st_slang;
13011 new_sug.st_orglen = badlen;
13012 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 }
13015
Bram Moolenaar4770d092006-01-12 23:22:24 +000013016 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013018 stp->st_score = new_sug.st_score;
13019 stp->st_altscore = new_sug.st_altscore;
13020 stp->st_had_bonus = new_sug.st_had_bonus;
13021 }
13022 break;
13023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024
Bram Moolenaar4770d092006-01-12 23:22:24 +000013025 if (i < 0 && ga_grow(gap, 1) == OK)
13026 {
13027 /* Add a suggestion. */
13028 stp = &SUG(*gap, gap->ga_len);
13029 stp->st_word = vim_strnsave(goodword, goodlen);
13030 if (stp->st_word != NULL)
13031 {
13032 stp->st_wordlen = goodlen;
13033 stp->st_score = score;
13034 stp->st_altscore = altscore;
13035 stp->st_had_bonus = had_bonus;
13036 stp->st_orglen = badlen;
13037 stp->st_slang = slang;
13038 ++gap->ga_len;
13039
13040 /* If we have too many suggestions now, sort the list and keep
13041 * the best suggestions. */
13042 if (gap->ga_len > SUG_MAX_COUNT(su))
13043 {
13044 if (maxsf)
13045 su->su_sfmaxscore = cleanup_suggestions(gap,
13046 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13047 else
13048 {
13049 i = su->su_maxscore;
13050 su->su_maxscore = cleanup_suggestions(gap,
13051 su->su_maxscore, SUG_CLEAN_COUNT(su));
13052 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013053 }
13054 }
13055 }
13056}
13057
13058/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013059 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13060 * for split words, such as "the the". Remove these from the list here.
13061 */
13062 static void
13063check_suggestions(su, gap)
13064 suginfo_T *su;
13065 garray_T *gap; /* either su_ga or su_sga */
13066{
13067 suggest_T *stp;
13068 int i;
13069 char_u longword[MAXWLEN + 1];
13070 int len;
13071 hlf_T attr;
13072
13073 stp = &SUG(*gap, 0);
13074 for (i = gap->ga_len - 1; i >= 0; --i)
13075 {
13076 /* Need to append what follows to check for "the the". */
13077 STRCPY(longword, stp[i].st_word);
13078 len = stp[i].st_wordlen;
13079 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13080 MAXWLEN - len);
13081 attr = HLF_COUNT;
13082 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13083 if (attr != HLF_COUNT)
13084 {
13085 /* Remove this entry. */
13086 vim_free(stp[i].st_word);
13087 --gap->ga_len;
13088 if (i < gap->ga_len)
13089 mch_memmove(stp + i, stp + i + 1,
13090 sizeof(suggest_T) * (gap->ga_len - i));
13091 }
13092 }
13093}
13094
13095
13096/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 * Add a word to be banned.
13098 */
13099 static void
13100add_banned(su, word)
13101 suginfo_T *su;
13102 char_u *word;
13103{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013104 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105 hash_T hash;
13106 hashitem_T *hi;
13107
Bram Moolenaar4770d092006-01-12 23:22:24 +000013108 hash = hash_hash(word);
13109 hi = hash_lookup(&su->su_banned, word, hash);
13110 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013111 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013112 s = vim_strsave(word);
13113 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013114 hash_add_item(&su->su_banned, hi, s, hash);
13115 }
13116}
13117
13118/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013119 * Recompute the score for all suggestions if sound-folding is possible. This
13120 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013121 */
13122 static void
13123rescore_suggestions(su)
13124 suginfo_T *su;
13125{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013126 int i;
13127
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013128 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013129 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013130 rescore_one(su, &SUG(su->su_ga, i));
13131}
13132
13133/*
13134 * Recompute the score for one suggestion if sound-folding is possible.
13135 */
13136 static void
13137rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013138 suginfo_T *su;
13139 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013140{
13141 slang_T *slang = stp->st_slang;
13142 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013143 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013144
13145 /* Only rescore suggestions that have no sal score yet and do have a
13146 * language. */
13147 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13148 {
13149 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013150 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013151 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013152 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013153 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013154 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013155 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013156
13157 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013158 if (stp->st_altscore == SCORE_MAXMAX)
13159 stp->st_altscore = SCORE_BIG;
13160 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13161 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013162 }
13163}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165static int
13166#ifdef __BORLANDC__
13167_RTLENTRYF
13168#endif
13169sug_compare __ARGS((const void *s1, const void *s2));
13170
13171/*
13172 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013173 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013174 */
13175 static int
13176#ifdef __BORLANDC__
13177_RTLENTRYF
13178#endif
13179sug_compare(s1, s2)
13180 const void *s1;
13181 const void *s2;
13182{
13183 suggest_T *p1 = (suggest_T *)s1;
13184 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013185 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013186
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013187 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013188 {
13189 n = p1->st_altscore - p2->st_altscore;
13190 if (n == 0)
13191 n = STRICMP(p1->st_word, p2->st_word);
13192 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013193 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194}
13195
13196/*
13197 * Cleanup the suggestions:
13198 * - Sort on score.
13199 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013200 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013202 static int
13203cleanup_suggestions(gap, maxscore, keep)
13204 garray_T *gap;
13205 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013206 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013208 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013209 int i;
13210
13211 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013212 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013213
13214 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013215 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013217 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013219 gap->ga_len = keep;
13220 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013222 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223}
13224
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013225#if defined(FEAT_EVAL) || defined(PROTO)
13226/*
13227 * Soundfold a string, for soundfold().
13228 * Result is in allocated memory, NULL for an error.
13229 */
13230 char_u *
13231eval_soundfold(word)
13232 char_u *word;
13233{
13234 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013235 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013236 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013237
13238 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13239 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013240 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013241 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013242 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013243 if (lp->lp_slang->sl_sal.ga_len > 0)
13244 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013245 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013246 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013247 return vim_strsave(sound);
13248 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013249 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013250
13251 /* No language with sound folding, return word as-is. */
13252 return vim_strsave(word);
13253}
13254#endif
13255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013256/*
13257 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013258 *
13259 * There are many ways to turn a word into a sound-a-like representation. The
13260 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13261 * swedish name matching - survey and test of different algorithms" by Klas
13262 * Erikson.
13263 *
13264 * We support two methods:
13265 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13266 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 */
13268 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013269spell_soundfold(slang, inword, folded, res)
13270 slang_T *slang;
13271 char_u *inword;
13272 int folded; /* "inword" is already case-folded */
13273 char_u *res;
13274{
13275 char_u fword[MAXWLEN];
13276 char_u *word;
13277
13278 if (slang->sl_sofo)
13279 /* SOFOFROM and SOFOTO used */
13280 spell_soundfold_sofo(slang, inword, res);
13281 else
13282 {
13283 /* SAL items used. Requires the word to be case-folded. */
13284 if (folded)
13285 word = inword;
13286 else
13287 {
13288 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13289 word = fword;
13290 }
13291
13292#ifdef FEAT_MBYTE
13293 if (has_mbyte)
13294 spell_soundfold_wsal(slang, word, res);
13295 else
13296#endif
13297 spell_soundfold_sal(slang, word, res);
13298 }
13299}
13300
13301/*
13302 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13303 * SOFOTO lines.
13304 */
13305 static void
13306spell_soundfold_sofo(slang, inword, res)
13307 slang_T *slang;
13308 char_u *inword;
13309 char_u *res;
13310{
13311 char_u *s;
13312 int ri = 0;
13313 int c;
13314
13315#ifdef FEAT_MBYTE
13316 if (has_mbyte)
13317 {
13318 int prevc = 0;
13319 int *ip;
13320
13321 /* The sl_sal_first[] table contains the translation for chars up to
13322 * 255, sl_sal the rest. */
13323 for (s = inword; *s != NUL; )
13324 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013325 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013326 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13327 c = ' ';
13328 else if (c < 256)
13329 c = slang->sl_sal_first[c];
13330 else
13331 {
13332 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13333 if (ip == NULL) /* empty list, can't match */
13334 c = NUL;
13335 else
13336 for (;;) /* find "c" in the list */
13337 {
13338 if (*ip == 0) /* not found */
13339 {
13340 c = NUL;
13341 break;
13342 }
13343 if (*ip == c) /* match! */
13344 {
13345 c = ip[1];
13346 break;
13347 }
13348 ip += 2;
13349 }
13350 }
13351
13352 if (c != NUL && c != prevc)
13353 {
13354 ri += mb_char2bytes(c, res + ri);
13355 if (ri + MB_MAXBYTES > MAXWLEN)
13356 break;
13357 prevc = c;
13358 }
13359 }
13360 }
13361 else
13362#endif
13363 {
13364 /* The sl_sal_first[] table contains the translation. */
13365 for (s = inword; (c = *s) != NUL; ++s)
13366 {
13367 if (vim_iswhite(c))
13368 c = ' ';
13369 else
13370 c = slang->sl_sal_first[c];
13371 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13372 res[ri++] = c;
13373 }
13374 }
13375
13376 res[ri] = NUL;
13377}
13378
13379 static void
13380spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 slang_T *slang;
13382 char_u *inword;
13383 char_u *res;
13384{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013385 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013387 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013389 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013390 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013391 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 int n, k = 0;
13393 int z0;
13394 int k0;
13395 int n0;
13396 int c;
13397 int pri;
13398 int p0 = -333;
13399 int c0;
13400
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013401 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013402 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 if (slang->sl_rem_accents)
13404 {
13405 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013406 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013408 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013409 {
13410 *t++ = ' ';
13411 s = skipwhite(s);
13412 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013413 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013415 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013416 *t++ = *s;
13417 ++s;
13418 }
13419 }
13420 *t = NUL;
13421 }
13422 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013423 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013424
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013425 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426
13427 /*
13428 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013429 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013431 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013432 while ((c = word[i]) != NUL)
13433 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013434 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 n = slang->sl_sal_first[c];
13436 z0 = 0;
13437
13438 if (n >= 0)
13439 {
13440 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013441 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013443 /* Quickly skip entries that don't match the word. Most
13444 * entries are less then three chars, optimize for that. */
13445 k = smp[n].sm_leadlen;
13446 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013448 if (word[i + 1] != s[1])
13449 continue;
13450 if (k > 2)
13451 {
13452 for (j = 2; j < k; ++j)
13453 if (word[i + j] != s[j])
13454 break;
13455 if (j < k)
13456 continue;
13457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458 }
13459
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013460 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013462 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013463 while (*pf != NUL && *pf != word[i + k])
13464 ++pf;
13465 if (*pf == NUL)
13466 continue;
13467 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013469 s = smp[n].sm_rules;
13470 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471
13472 p0 = *s;
13473 k0 = k;
13474 while (*s == '-' && k > 1)
13475 {
13476 k--;
13477 s++;
13478 }
13479 if (*s == '<')
13480 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013481 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 {
13483 /* determine priority */
13484 pri = *s - '0';
13485 s++;
13486 }
13487 if (*s == '^' && *(s + 1) == '^')
13488 s++;
13489
13490 if (*s == NUL
13491 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013492 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013493 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013495 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013497 && spell_iswordp(word + i - 1, curbuf)
13498 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 {
13500 /* search for followup rules, if: */
13501 /* followup and k > 1 and NO '-' in searchstring */
13502 c0 = word[i + k - 1];
13503 n0 = slang->sl_sal_first[c0];
13504
13505 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013506 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013507 {
13508 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013509 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013511 /* Quickly skip entries that don't match the word.
13512 * */
13513 k0 = smp[n0].sm_leadlen;
13514 if (k0 > 1)
13515 {
13516 if (word[i + k] != s[1])
13517 continue;
13518 if (k0 > 2)
13519 {
13520 pf = word + i + k + 1;
13521 for (j = 2; j < k0; ++j)
13522 if (*pf++ != s[j])
13523 break;
13524 if (j < k0)
13525 continue;
13526 }
13527 }
13528 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013529
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013530 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013531 {
13532 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013533 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013534 while (*pf != NUL && *pf != word[i + k0])
13535 ++pf;
13536 if (*pf == NUL)
13537 continue;
13538 ++k0;
13539 }
13540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013541 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013542 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013543 while (*s == '-')
13544 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013545 /* "k0" gets NOT reduced because
13546 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013547 s++;
13548 }
13549 if (*s == '<')
13550 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013551 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013552 {
13553 p0 = *s - '0';
13554 s++;
13555 }
13556
13557 if (*s == NUL
13558 /* *s == '^' cuts */
13559 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013560 && !spell_iswordp(word + i + k0,
13561 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013562 {
13563 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013565 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566
13567 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 /* rule fits; stop search */
13571 break;
13572 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 }
13574
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013575 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013576 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013577 }
13578
13579 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013580 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013581 if (s == NULL)
13582 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013583 pf = smp[n].sm_rules;
13584 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 if (p0 == 1 && z == 0)
13586 {
13587 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013588 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
13589 || res[reslen - 1] == *s))
13590 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591 z0 = 1;
13592 z = 1;
13593 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013594 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 {
13596 word[i + k0] = *s;
13597 k0++;
13598 s++;
13599 }
13600 if (k > k0)
13601 mch_memmove(word + i + k0, word + i + k,
13602 STRLEN(word + i + k) + 1);
13603
13604 /* new "actual letter" */
13605 c = word[i];
13606 }
13607 else
13608 {
13609 /* no '<' rule used */
13610 i += k - 1;
13611 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013612 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013614 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013615 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 s++;
13617 }
13618 /* new "actual letter" */
13619 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013620 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 {
13622 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013623 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 mch_memmove(word, word + i + 1,
13625 STRLEN(word + i + 1) + 1);
13626 i = 0;
13627 z0 = 1;
13628 }
13629 }
13630 break;
13631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 }
13633 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013634 else if (vim_iswhite(c))
13635 {
13636 c = ' ';
13637 k = 1;
13638 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013639
13640 if (z0 == 0)
13641 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013642 if (k && !p0 && reslen < MAXWLEN && c != NUL
13643 && (!slang->sl_collapse || reslen == 0
13644 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013646 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013647
13648 i++;
13649 z = 0;
13650 k = 0;
13651 }
13652 }
13653
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013654 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655}
13656
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013657#ifdef FEAT_MBYTE
13658/*
13659 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
13660 * Multi-byte version of spell_soundfold().
13661 */
13662 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013663spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013664 slang_T *slang;
13665 char_u *inword;
13666 char_u *res;
13667{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013668 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013669 int word[MAXWLEN];
13670 int wres[MAXWLEN];
13671 int l;
13672 char_u *s;
13673 int *ws;
13674 char_u *t;
13675 int *pf;
13676 int i, j, z;
13677 int reslen;
13678 int n, k = 0;
13679 int z0;
13680 int k0;
13681 int n0;
13682 int c;
13683 int pri;
13684 int p0 = -333;
13685 int c0;
13686 int did_white = FALSE;
13687
13688 /*
13689 * Convert the multi-byte string to a wide-character string.
13690 * Remove accents, if wanted. We actually remove all non-word characters.
13691 * But keep white space.
13692 */
13693 n = 0;
13694 for (s = inword; *s != NUL; )
13695 {
13696 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013697 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013698 if (slang->sl_rem_accents)
13699 {
13700 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13701 {
13702 if (did_white)
13703 continue;
13704 c = ' ';
13705 did_white = TRUE;
13706 }
13707 else
13708 {
13709 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013710 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013711 continue;
13712 }
13713 }
13714 word[n++] = c;
13715 }
13716 word[n] = NUL;
13717
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013718 /*
13719 * This comes from Aspell phonet.cpp.
13720 * Converted from C++ to C. Added support for multi-byte chars.
13721 * Changed to keep spaces.
13722 */
13723 i = reslen = z = 0;
13724 while ((c = word[i]) != NUL)
13725 {
13726 /* Start with the first rule that has the character in the word. */
13727 n = slang->sl_sal_first[c & 0xff];
13728 z0 = 0;
13729
13730 if (n >= 0)
13731 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013732 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013733 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
13734 {
13735 /* Quickly skip entries that don't match the word. Most
13736 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013737 if (c != ws[0])
13738 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013739 k = smp[n].sm_leadlen;
13740 if (k > 1)
13741 {
13742 if (word[i + 1] != ws[1])
13743 continue;
13744 if (k > 2)
13745 {
13746 for (j = 2; j < k; ++j)
13747 if (word[i + j] != ws[j])
13748 break;
13749 if (j < k)
13750 continue;
13751 }
13752 }
13753
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013754 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013755 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013756 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013757 while (*pf != NUL && *pf != word[i + k])
13758 ++pf;
13759 if (*pf == NUL)
13760 continue;
13761 ++k;
13762 }
13763 s = smp[n].sm_rules;
13764 pri = 5; /* default priority */
13765
13766 p0 = *s;
13767 k0 = k;
13768 while (*s == '-' && k > 1)
13769 {
13770 k--;
13771 s++;
13772 }
13773 if (*s == '<')
13774 s++;
13775 if (VIM_ISDIGIT(*s))
13776 {
13777 /* determine priority */
13778 pri = *s - '0';
13779 s++;
13780 }
13781 if (*s == '^' && *(s + 1) == '^')
13782 s++;
13783
13784 if (*s == NUL
13785 || (*s == '^'
13786 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013787 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013788 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013789 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013790 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013791 && spell_iswordp_w(word + i - 1, curbuf)
13792 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013793 {
13794 /* search for followup rules, if: */
13795 /* followup and k > 1 and NO '-' in searchstring */
13796 c0 = word[i + k - 1];
13797 n0 = slang->sl_sal_first[c0 & 0xff];
13798
13799 if (slang->sl_followup && k > 1 && n0 >= 0
13800 && p0 != '-' && word[i + k] != NUL)
13801 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013802 /* Test follow-up rule for "word[i + k]"; loop over
13803 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013804 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
13805 == (c0 & 0xff); ++n0)
13806 {
13807 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013808 */
13809 if (c0 != ws[0])
13810 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013811 k0 = smp[n0].sm_leadlen;
13812 if (k0 > 1)
13813 {
13814 if (word[i + k] != ws[1])
13815 continue;
13816 if (k0 > 2)
13817 {
13818 pf = word + i + k + 1;
13819 for (j = 2; j < k0; ++j)
13820 if (*pf++ != ws[j])
13821 break;
13822 if (j < k0)
13823 continue;
13824 }
13825 }
13826 k0 += k - 1;
13827
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013828 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013829 {
13830 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013831 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013832 while (*pf != NUL && *pf != word[i + k0])
13833 ++pf;
13834 if (*pf == NUL)
13835 continue;
13836 ++k0;
13837 }
13838
13839 p0 = 5;
13840 s = smp[n0].sm_rules;
13841 while (*s == '-')
13842 {
13843 /* "k0" gets NOT reduced because
13844 * "if (k0 == k)" */
13845 s++;
13846 }
13847 if (*s == '<')
13848 s++;
13849 if (VIM_ISDIGIT(*s))
13850 {
13851 p0 = *s - '0';
13852 s++;
13853 }
13854
13855 if (*s == NUL
13856 /* *s == '^' cuts */
13857 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013858 && !spell_iswordp_w(word + i + k0,
13859 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013860 {
13861 if (k0 == k)
13862 /* this is just a piece of the string */
13863 continue;
13864
13865 if (p0 < pri)
13866 /* priority too low */
13867 continue;
13868 /* rule fits; stop search */
13869 break;
13870 }
13871 }
13872
13873 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
13874 == (c0 & 0xff))
13875 continue;
13876 }
13877
13878 /* replace string */
13879 ws = smp[n].sm_to_w;
13880 s = smp[n].sm_rules;
13881 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
13882 if (p0 == 1 && z == 0)
13883 {
13884 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013885 if (reslen > 0 && ws != NULL && *ws != NUL
13886 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013887 || wres[reslen - 1] == *ws))
13888 reslen--;
13889 z0 = 1;
13890 z = 1;
13891 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013892 if (ws != NULL)
13893 while (*ws != NUL && word[i + k0] != NUL)
13894 {
13895 word[i + k0] = *ws;
13896 k0++;
13897 ws++;
13898 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013899 if (k > k0)
13900 mch_memmove(word + i + k0, word + i + k,
13901 sizeof(int) * (STRLEN(word + i + k) + 1));
13902
13903 /* new "actual letter" */
13904 c = word[i];
13905 }
13906 else
13907 {
13908 /* no '<' rule used */
13909 i += k - 1;
13910 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013911 if (ws != NULL)
13912 while (*ws != NUL && ws[1] != NUL
13913 && reslen < MAXWLEN)
13914 {
13915 if (reslen == 0 || wres[reslen - 1] != *ws)
13916 wres[reslen++] = *ws;
13917 ws++;
13918 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013919 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013920 if (ws == NULL)
13921 c = NUL;
13922 else
13923 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013924 if (strstr((char *)s, "^^") != NULL)
13925 {
13926 if (c != NUL)
13927 wres[reslen++] = c;
13928 mch_memmove(word, word + i + 1,
13929 sizeof(int) * (STRLEN(word + i + 1) + 1));
13930 i = 0;
13931 z0 = 1;
13932 }
13933 }
13934 break;
13935 }
13936 }
13937 }
13938 else if (vim_iswhite(c))
13939 {
13940 c = ' ';
13941 k = 1;
13942 }
13943
13944 if (z0 == 0)
13945 {
13946 if (k && !p0 && reslen < MAXWLEN && c != NUL
13947 && (!slang->sl_collapse || reslen == 0
13948 || wres[reslen - 1] != c))
13949 /* condense only double letters */
13950 wres[reslen++] = c;
13951
13952 i++;
13953 z = 0;
13954 k = 0;
13955 }
13956 }
13957
13958 /* Convert wide characters in "wres" to a multi-byte string in "res". */
13959 l = 0;
13960 for (n = 0; n < reslen; ++n)
13961 {
13962 l += mb_char2bytes(wres[n], res + l);
13963 if (l + MB_MAXBYTES > MAXWLEN)
13964 break;
13965 }
13966 res[l] = NUL;
13967}
13968#endif
13969
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013970/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013971 * Compute a score for two sound-a-like words.
13972 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
13973 * Instead of a generic loop we write out the code. That keeps it fast by
13974 * avoiding checks that will not be possible.
13975 */
13976 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013977soundalike_score(goodstart, badstart)
13978 char_u *goodstart; /* sound-folded good word */
13979 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013980{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013981 char_u *goodsound = goodstart;
13982 char_u *badsound = badstart;
13983 int goodlen;
13984 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013985 int n;
13986 char_u *pl, *ps;
13987 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013988 int score = 0;
13989
13990 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
13991 * counted so much, vowels halfway the word aren't counted at all. */
13992 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
13993 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013994 if (badsound[1] == goodsound[1]
13995 || (badsound[1] != NUL
13996 && goodsound[1] != NUL
13997 && badsound[2] == goodsound[2]))
13998 {
13999 /* handle like a substitute */
14000 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014001 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014002 {
14003 score = 2 * SCORE_DEL / 3;
14004 if (*badsound == '*')
14005 ++badsound;
14006 else
14007 ++goodsound;
14008 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014009 }
14010
14011 goodlen = STRLEN(goodsound);
14012 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014013
14014 /* Return quickly if the lenghts are too different to be fixed by two
14015 * changes. */
14016 n = goodlen - badlen;
14017 if (n < -2 || n > 2)
14018 return SCORE_MAXMAX;
14019
14020 if (n > 0)
14021 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014022 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014023 ps = badsound;
14024 }
14025 else
14026 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014027 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014028 ps = goodsound;
14029 }
14030
14031 /* Skip over the identical part. */
14032 while (*pl == *ps && *pl != NUL)
14033 {
14034 ++pl;
14035 ++ps;
14036 }
14037
14038 switch (n)
14039 {
14040 case -2:
14041 case 2:
14042 /*
14043 * Must delete two characters from "pl".
14044 */
14045 ++pl; /* first delete */
14046 while (*pl == *ps)
14047 {
14048 ++pl;
14049 ++ps;
14050 }
14051 /* strings must be equal after second delete */
14052 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014053 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014054
14055 /* Failed to compare. */
14056 break;
14057
14058 case -1:
14059 case 1:
14060 /*
14061 * Minimal one delete from "pl" required.
14062 */
14063
14064 /* 1: delete */
14065 pl2 = pl + 1;
14066 ps2 = ps;
14067 while (*pl2 == *ps2)
14068 {
14069 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014070 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014071 ++pl2;
14072 ++ps2;
14073 }
14074
14075 /* 2: delete then swap, then rest must be equal */
14076 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14077 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014078 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014079
14080 /* 3: delete then substitute, then the rest must be equal */
14081 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014082 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014083
14084 /* 4: first swap then delete */
14085 if (pl[0] == ps[1] && pl[1] == ps[0])
14086 {
14087 pl2 = pl + 2; /* swap, skip two chars */
14088 ps2 = ps + 2;
14089 while (*pl2 == *ps2)
14090 {
14091 ++pl2;
14092 ++ps2;
14093 }
14094 /* delete a char and then strings must be equal */
14095 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014096 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014097 }
14098
14099 /* 5: first substitute then delete */
14100 pl2 = pl + 1; /* substitute, skip one char */
14101 ps2 = ps + 1;
14102 while (*pl2 == *ps2)
14103 {
14104 ++pl2;
14105 ++ps2;
14106 }
14107 /* delete a char and then strings must be equal */
14108 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014109 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014110
14111 /* Failed to compare. */
14112 break;
14113
14114 case 0:
14115 /*
14116 * Lenghts are equal, thus changes must result in same length: An
14117 * insert is only possible in combination with a delete.
14118 * 1: check if for identical strings
14119 */
14120 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014121 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014122
14123 /* 2: swap */
14124 if (pl[0] == ps[1] && pl[1] == ps[0])
14125 {
14126 pl2 = pl + 2; /* swap, skip two chars */
14127 ps2 = ps + 2;
14128 while (*pl2 == *ps2)
14129 {
14130 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014131 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014132 ++pl2;
14133 ++ps2;
14134 }
14135 /* 3: swap and swap again */
14136 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14137 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014138 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014139
14140 /* 4: swap and substitute */
14141 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014142 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014143 }
14144
14145 /* 5: substitute */
14146 pl2 = pl + 1;
14147 ps2 = ps + 1;
14148 while (*pl2 == *ps2)
14149 {
14150 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014151 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014152 ++pl2;
14153 ++ps2;
14154 }
14155
14156 /* 6: substitute and swap */
14157 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14158 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014159 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014160
14161 /* 7: substitute and substitute */
14162 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014163 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014164
14165 /* 8: insert then delete */
14166 pl2 = pl;
14167 ps2 = ps + 1;
14168 while (*pl2 == *ps2)
14169 {
14170 ++pl2;
14171 ++ps2;
14172 }
14173 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014174 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014175
14176 /* 9: delete then insert */
14177 pl2 = pl + 1;
14178 ps2 = ps;
14179 while (*pl2 == *ps2)
14180 {
14181 ++pl2;
14182 ++ps2;
14183 }
14184 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014185 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014186
14187 /* Failed to compare. */
14188 break;
14189 }
14190
14191 return SCORE_MAXMAX;
14192}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194/*
14195 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014196 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014197 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014198 * The algorithm is described by Du and Chang, 1992.
14199 * The implementation of the algorithm comes from Aspell editdist.cpp,
14200 * edit_distance(). It has been converted from C++ to C and modified to
14201 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 */
14203 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014204spell_edit_score(slang, badword, goodword)
14205 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 char_u *badword;
14207 char_u *goodword;
14208{
14209 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014210 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 int j, i;
14212 int t;
14213 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014214 int pbc, pgc;
14215#ifdef FEAT_MBYTE
14216 char_u *p;
14217 int wbadword[MAXWLEN];
14218 int wgoodword[MAXWLEN];
14219
14220 if (has_mbyte)
14221 {
14222 /* Get the characters from the multi-byte strings and put them in an
14223 * int array for easy access. */
14224 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014225 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014226 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014227 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014228 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014229 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014230 }
14231 else
14232#endif
14233 {
14234 badlen = STRLEN(badword) + 1;
14235 goodlen = STRLEN(goodword) + 1;
14236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237
14238 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14239#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14241 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014242 if (cnt == NULL)
14243 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244
14245 CNT(0, 0) = 0;
14246 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014247 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248
14249 for (i = 1; i <= badlen; ++i)
14250 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014251 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 for (j = 1; j <= goodlen; ++j)
14253 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014254#ifdef FEAT_MBYTE
14255 if (has_mbyte)
14256 {
14257 bc = wbadword[i - 1];
14258 gc = wgoodword[j - 1];
14259 }
14260 else
14261#endif
14262 {
14263 bc = badword[i - 1];
14264 gc = goodword[j - 1];
14265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 if (bc == gc)
14267 CNT(i, j) = CNT(i - 1, j - 1);
14268 else
14269 {
14270 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014271 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14273 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014274 {
14275 /* For a similar character use SCORE_SIMILAR. */
14276 if (slang != NULL
14277 && slang->sl_has_map
14278 && similar_chars(slang, gc, bc))
14279 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14280 else
14281 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014284 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014286#ifdef FEAT_MBYTE
14287 if (has_mbyte)
14288 {
14289 pbc = wbadword[i - 2];
14290 pgc = wgoodword[j - 2];
14291 }
14292 else
14293#endif
14294 {
14295 pbc = badword[i - 2];
14296 pgc = goodword[j - 2];
14297 }
14298 if (bc == pgc && pbc == gc)
14299 {
14300 t = SCORE_SWAP + CNT(i - 2, j - 2);
14301 if (t < CNT(i, j))
14302 CNT(i, j) = t;
14303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 }
14305 t = SCORE_DEL + CNT(i - 1, j);
14306 if (t < CNT(i, j))
14307 CNT(i, j) = t;
14308 t = SCORE_INS + CNT(i, j - 1);
14309 if (t < CNT(i, j))
14310 CNT(i, j) = t;
14311 }
14312 }
14313 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014314
14315 i = CNT(badlen - 1, goodlen - 1);
14316 vim_free(cnt);
14317 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014319
Bram Moolenaar4770d092006-01-12 23:22:24 +000014320typedef struct
14321{
14322 int badi;
14323 int goodi;
14324 int score;
14325} limitscore_T;
14326
14327/*
14328 * Like spell_edit_score(), but with a limit on the score to make it faster.
14329 * May return SCORE_MAXMAX when the score is higher than "limit".
14330 *
14331 * This uses a stack for the edits still to be tried.
14332 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14333 * for multi-byte characters.
14334 */
14335 static int
14336spell_edit_score_limit(slang, badword, goodword, limit)
14337 slang_T *slang;
14338 char_u *badword;
14339 char_u *goodword;
14340 int limit;
14341{
14342 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14343 int stackidx;
14344 int bi, gi;
14345 int bi2, gi2;
14346 int bc, gc;
14347 int score;
14348 int score_off;
14349 int minscore;
14350 int round;
14351
14352#ifdef FEAT_MBYTE
14353 /* Multi-byte characters require a bit more work, use a different function
14354 * to avoid testing "has_mbyte" quite often. */
14355 if (has_mbyte)
14356 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14357#endif
14358
14359 /*
14360 * The idea is to go from start to end over the words. So long as
14361 * characters are equal just continue, this always gives the lowest score.
14362 * When there is a difference try several alternatives. Each alternative
14363 * increases "score" for the edit distance. Some of the alternatives are
14364 * pushed unto a stack and tried later, some are tried right away. At the
14365 * end of the word the score for one alternative is known. The lowest
14366 * possible score is stored in "minscore".
14367 */
14368 stackidx = 0;
14369 bi = 0;
14370 gi = 0;
14371 score = 0;
14372 minscore = limit + 1;
14373
14374 for (;;)
14375 {
14376 /* Skip over an equal part, score remains the same. */
14377 for (;;)
14378 {
14379 bc = badword[bi];
14380 gc = goodword[gi];
14381 if (bc != gc) /* stop at a char that's different */
14382 break;
14383 if (bc == NUL) /* both words end */
14384 {
14385 if (score < minscore)
14386 minscore = score;
14387 goto pop; /* do next alternative */
14388 }
14389 ++bi;
14390 ++gi;
14391 }
14392
14393 if (gc == NUL) /* goodword ends, delete badword chars */
14394 {
14395 do
14396 {
14397 if ((score += SCORE_DEL) >= minscore)
14398 goto pop; /* do next alternative */
14399 } while (badword[++bi] != NUL);
14400 minscore = score;
14401 }
14402 else if (bc == NUL) /* badword ends, insert badword chars */
14403 {
14404 do
14405 {
14406 if ((score += SCORE_INS) >= minscore)
14407 goto pop; /* do next alternative */
14408 } while (goodword[++gi] != NUL);
14409 minscore = score;
14410 }
14411 else /* both words continue */
14412 {
14413 /* If not close to the limit, perform a change. Only try changes
14414 * that may lead to a lower score than "minscore".
14415 * round 0: try deleting a char from badword
14416 * round 1: try inserting a char in badword */
14417 for (round = 0; round <= 1; ++round)
14418 {
14419 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14420 if (score_off < minscore)
14421 {
14422 if (score_off + SCORE_EDIT_MIN >= minscore)
14423 {
14424 /* Near the limit, rest of the words must match. We
14425 * can check that right now, no need to push an item
14426 * onto the stack. */
14427 bi2 = bi + 1 - round;
14428 gi2 = gi + round;
14429 while (goodword[gi2] == badword[bi2])
14430 {
14431 if (goodword[gi2] == NUL)
14432 {
14433 minscore = score_off;
14434 break;
14435 }
14436 ++bi2;
14437 ++gi2;
14438 }
14439 }
14440 else
14441 {
14442 /* try deleting/inserting a character later */
14443 stack[stackidx].badi = bi + 1 - round;
14444 stack[stackidx].goodi = gi + round;
14445 stack[stackidx].score = score_off;
14446 ++stackidx;
14447 }
14448 }
14449 }
14450
14451 if (score + SCORE_SWAP < minscore)
14452 {
14453 /* If swapping two characters makes a match then the
14454 * substitution is more expensive, thus there is no need to
14455 * try both. */
14456 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14457 {
14458 /* Swap two characters, that is: skip them. */
14459 gi += 2;
14460 bi += 2;
14461 score += SCORE_SWAP;
14462 continue;
14463 }
14464 }
14465
14466 /* Substitute one character for another which is the same
14467 * thing as deleting a character from both goodword and badword.
14468 * Use a better score when there is only a case difference. */
14469 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14470 score += SCORE_ICASE;
14471 else
14472 {
14473 /* For a similar character use SCORE_SIMILAR. */
14474 if (slang != NULL
14475 && slang->sl_has_map
14476 && similar_chars(slang, gc, bc))
14477 score += SCORE_SIMILAR;
14478 else
14479 score += SCORE_SUBST;
14480 }
14481
14482 if (score < minscore)
14483 {
14484 /* Do the substitution. */
14485 ++gi;
14486 ++bi;
14487 continue;
14488 }
14489 }
14490pop:
14491 /*
14492 * Get here to try the next alternative, pop it from the stack.
14493 */
14494 if (stackidx == 0) /* stack is empty, finished */
14495 break;
14496
14497 /* pop an item from the stack */
14498 --stackidx;
14499 gi = stack[stackidx].goodi;
14500 bi = stack[stackidx].badi;
14501 score = stack[stackidx].score;
14502 }
14503
14504 /* When the score goes over "limit" it may actually be much higher.
14505 * Return a very large number to avoid going below the limit when giving a
14506 * bonus. */
14507 if (minscore > limit)
14508 return SCORE_MAXMAX;
14509 return minscore;
14510}
14511
14512#ifdef FEAT_MBYTE
14513/*
14514 * Multi-byte version of spell_edit_score_limit().
14515 * Keep it in sync with the above!
14516 */
14517 static int
14518spell_edit_score_limit_w(slang, badword, goodword, limit)
14519 slang_T *slang;
14520 char_u *badword;
14521 char_u *goodword;
14522 int limit;
14523{
14524 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14525 int stackidx;
14526 int bi, gi;
14527 int bi2, gi2;
14528 int bc, gc;
14529 int score;
14530 int score_off;
14531 int minscore;
14532 int round;
14533 char_u *p;
14534 int wbadword[MAXWLEN];
14535 int wgoodword[MAXWLEN];
14536
14537 /* Get the characters from the multi-byte strings and put them in an
14538 * int array for easy access. */
14539 bi = 0;
14540 for (p = badword; *p != NUL; )
14541 wbadword[bi++] = mb_cptr2char_adv(&p);
14542 wbadword[bi++] = 0;
14543 gi = 0;
14544 for (p = goodword; *p != NUL; )
14545 wgoodword[gi++] = mb_cptr2char_adv(&p);
14546 wgoodword[gi++] = 0;
14547
14548 /*
14549 * The idea is to go from start to end over the words. So long as
14550 * characters are equal just continue, this always gives the lowest score.
14551 * When there is a difference try several alternatives. Each alternative
14552 * increases "score" for the edit distance. Some of the alternatives are
14553 * pushed unto a stack and tried later, some are tried right away. At the
14554 * end of the word the score for one alternative is known. The lowest
14555 * possible score is stored in "minscore".
14556 */
14557 stackidx = 0;
14558 bi = 0;
14559 gi = 0;
14560 score = 0;
14561 minscore = limit + 1;
14562
14563 for (;;)
14564 {
14565 /* Skip over an equal part, score remains the same. */
14566 for (;;)
14567 {
14568 bc = wbadword[bi];
14569 gc = wgoodword[gi];
14570
14571 if (bc != gc) /* stop at a char that's different */
14572 break;
14573 if (bc == NUL) /* both words end */
14574 {
14575 if (score < minscore)
14576 minscore = score;
14577 goto pop; /* do next alternative */
14578 }
14579 ++bi;
14580 ++gi;
14581 }
14582
14583 if (gc == NUL) /* goodword ends, delete badword chars */
14584 {
14585 do
14586 {
14587 if ((score += SCORE_DEL) >= minscore)
14588 goto pop; /* do next alternative */
14589 } while (wbadword[++bi] != NUL);
14590 minscore = score;
14591 }
14592 else if (bc == NUL) /* badword ends, insert badword chars */
14593 {
14594 do
14595 {
14596 if ((score += SCORE_INS) >= minscore)
14597 goto pop; /* do next alternative */
14598 } while (wgoodword[++gi] != NUL);
14599 minscore = score;
14600 }
14601 else /* both words continue */
14602 {
14603 /* If not close to the limit, perform a change. Only try changes
14604 * that may lead to a lower score than "minscore".
14605 * round 0: try deleting a char from badword
14606 * round 1: try inserting a char in badword */
14607 for (round = 0; round <= 1; ++round)
14608 {
14609 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14610 if (score_off < minscore)
14611 {
14612 if (score_off + SCORE_EDIT_MIN >= minscore)
14613 {
14614 /* Near the limit, rest of the words must match. We
14615 * can check that right now, no need to push an item
14616 * onto the stack. */
14617 bi2 = bi + 1 - round;
14618 gi2 = gi + round;
14619 while (wgoodword[gi2] == wbadword[bi2])
14620 {
14621 if (wgoodword[gi2] == NUL)
14622 {
14623 minscore = score_off;
14624 break;
14625 }
14626 ++bi2;
14627 ++gi2;
14628 }
14629 }
14630 else
14631 {
14632 /* try deleting a character from badword later */
14633 stack[stackidx].badi = bi + 1 - round;
14634 stack[stackidx].goodi = gi + round;
14635 stack[stackidx].score = score_off;
14636 ++stackidx;
14637 }
14638 }
14639 }
14640
14641 if (score + SCORE_SWAP < minscore)
14642 {
14643 /* If swapping two characters makes a match then the
14644 * substitution is more expensive, thus there is no need to
14645 * try both. */
14646 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
14647 {
14648 /* Swap two characters, that is: skip them. */
14649 gi += 2;
14650 bi += 2;
14651 score += SCORE_SWAP;
14652 continue;
14653 }
14654 }
14655
14656 /* Substitute one character for another which is the same
14657 * thing as deleting a character from both goodword and badword.
14658 * Use a better score when there is only a case difference. */
14659 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14660 score += SCORE_ICASE;
14661 else
14662 {
14663 /* For a similar character use SCORE_SIMILAR. */
14664 if (slang != NULL
14665 && slang->sl_has_map
14666 && similar_chars(slang, gc, bc))
14667 score += SCORE_SIMILAR;
14668 else
14669 score += SCORE_SUBST;
14670 }
14671
14672 if (score < minscore)
14673 {
14674 /* Do the substitution. */
14675 ++gi;
14676 ++bi;
14677 continue;
14678 }
14679 }
14680pop:
14681 /*
14682 * Get here to try the next alternative, pop it from the stack.
14683 */
14684 if (stackidx == 0) /* stack is empty, finished */
14685 break;
14686
14687 /* pop an item from the stack */
14688 --stackidx;
14689 gi = stack[stackidx].goodi;
14690 bi = stack[stackidx].badi;
14691 score = stack[stackidx].score;
14692 }
14693
14694 /* When the score goes over "limit" it may actually be much higher.
14695 * Return a very large number to avoid going below the limit when giving a
14696 * bonus. */
14697 if (minscore > limit)
14698 return SCORE_MAXMAX;
14699 return minscore;
14700}
14701#endif
14702
14703#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
14704#define DUMPFLAG_COUNT 2 /* include word count */
14705
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014706/*
14707 * ":spelldump"
14708 */
14709/*ARGSUSED*/
14710 void
14711ex_spelldump(eap)
14712 exarg_T *eap;
14713{
14714 buf_T *buf = curbuf;
14715 langp_T *lp;
14716 slang_T *slang;
14717 idx_T arridx[MAXWLEN];
14718 int curi[MAXWLEN];
14719 char_u word[MAXWLEN];
14720 int c;
14721 char_u *byts;
14722 idx_T *idxs;
14723 linenr_T lnum = 0;
14724 int round;
14725 int depth;
14726 int n;
14727 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000014728 char_u *region_names = NULL; /* region names being used */
14729 int do_region = TRUE; /* dump region names and numbers */
14730 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014731 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014732 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014733
Bram Moolenaar95529562005-08-25 21:21:38 +000014734 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014735 return;
14736
14737 /* Create a new empty buffer by splitting the window. */
14738 do_cmdline_cmd((char_u *)"new");
14739 if (!bufempty() || !buf_valid(buf))
14740 return;
14741
Bram Moolenaar7887d882005-07-01 22:33:52 +000014742 /* Find out if we can support regions: All languages must support the same
14743 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014744 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000014745 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014746 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000014747 p = lp->lp_slang->sl_regions;
14748 if (p[0] != 0)
14749 {
14750 if (region_names == NULL) /* first language with regions */
14751 region_names = p;
14752 else if (STRCMP(region_names, p) != 0)
14753 {
14754 do_region = FALSE; /* region names are different */
14755 break;
14756 }
14757 }
14758 }
14759
14760 if (do_region && region_names != NULL)
14761 {
14762 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
14763 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
14764 }
14765 else
14766 do_region = FALSE;
14767
14768 /*
14769 * Loop over all files loaded for the entries in 'spelllang'.
14770 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014771 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014772 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014773 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014774 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014775 if (slang->sl_fbyts == NULL) /* reloading failed */
14776 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014777
14778 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
14779 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
14780
14781 /* round 1: case-folded tree
14782 * round 2: keep-case tree */
14783 for (round = 1; round <= 2; ++round)
14784 {
14785 if (round == 1)
14786 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014787 dumpflags = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014788 byts = slang->sl_fbyts;
14789 idxs = slang->sl_fidxs;
14790 }
14791 else
14792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014793 dumpflags = DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014794 byts = slang->sl_kbyts;
14795 idxs = slang->sl_kidxs;
14796 }
14797 if (byts == NULL)
14798 continue; /* array is empty */
14799
Bram Moolenaar4770d092006-01-12 23:22:24 +000014800 if (eap->forceit)
14801 dumpflags |= DUMPFLAG_COUNT;
14802
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014803 depth = 0;
14804 arridx[0] = 0;
14805 curi[0] = 1;
14806 while (depth >= 0 && !got_int)
14807 {
14808 if (curi[depth] > byts[arridx[depth]])
14809 {
14810 /* Done all bytes at this node, go up one level. */
14811 --depth;
14812 line_breakcheck();
14813 }
14814 else
14815 {
14816 /* Do one more byte at this node. */
14817 n = arridx[depth] + curi[depth];
14818 ++curi[depth];
14819 c = byts[n];
14820 if (c == 0)
14821 {
14822 /* End of word, deal with the word.
14823 * Don't use keep-case words in the fold-case tree,
14824 * they will appear in the keep-case tree.
14825 * Only use the word when the region matches. */
14826 flags = (int)idxs[n];
14827 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014828 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000014829 && (do_region
14830 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014831 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014832 & lp->lp_region) != 0))
14833 {
14834 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000014835 if (!do_region)
14836 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014837
14838 /* Dump the basic word if there is no prefix or
14839 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014840 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014841 if (c == 0 || curi[depth] == 2)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014842 dump_word(slang, word, dumpflags,
14843 flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014844
14845 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014846 if (c != 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014847 lnum = dump_prefixes(slang, word, dumpflags,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014848 flags, lnum);
14849 }
14850 }
14851 else
14852 {
14853 /* Normal char, go one level deeper. */
14854 word[depth++] = c;
14855 arridx[depth] = idxs[n];
14856 curi[depth] = 1;
14857 }
14858 }
14859 }
14860 }
14861 }
14862
14863 /* Delete the empty line that we started with. */
14864 if (curbuf->b_ml.ml_line_count > 1)
14865 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
14866
14867 redraw_later(NOT_VALID);
14868}
14869
14870/*
14871 * Dump one word: apply case modifications and append a line to the buffer.
14872 */
14873 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000014874dump_word(slang, word, dumpflags, flags, lnum)
14875 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014876 char_u *word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014877 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014878 int flags;
14879 linenr_T lnum;
14880{
14881 int keepcap = FALSE;
14882 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014883 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014884 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000014885 char_u badword[MAXWLEN + 10];
14886 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014887
Bram Moolenaar4770d092006-01-12 23:22:24 +000014888 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014889 {
14890 /* Need to fix case according to "flags". */
14891 make_case_word(word, cword, flags);
14892 p = cword;
14893 }
14894 else
14895 {
14896 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014897 if ((dumpflags & DUMPFLAG_KEEPCASE)
14898 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014899 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014900 keepcap = TRUE;
14901 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000014902 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014903
Bram Moolenaar7887d882005-07-01 22:33:52 +000014904 /* Add flags and regions after a slash. */
14905 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014906 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000014907 STRCPY(badword, p);
14908 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014909 if (keepcap)
14910 STRCAT(badword, "=");
14911 if (flags & WF_BANNED)
14912 STRCAT(badword, "!");
14913 else if (flags & WF_RARE)
14914 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000014915 if (flags & WF_REGION)
14916 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014917 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000014918 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014919 p = badword;
14920 }
14921
Bram Moolenaar4770d092006-01-12 23:22:24 +000014922 if (dumpflags & DUMPFLAG_COUNT)
14923 {
14924 hashitem_T *hi;
14925
14926 /* Include the word count for ":spelldump!". */
14927 hi = hash_find(&slang->sl_wordcount, tw);
14928 if (!HASHITEM_EMPTY(hi))
14929 {
14930 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
14931 tw, HI2WC(hi)->wc_count);
14932 p = IObuff;
14933 }
14934 }
14935
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014936 ml_append(lnum, p, (colnr_T)0, FALSE);
14937}
14938
14939/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014940 * For ":spelldump": Find matching prefixes for "word". Prepend each to
14941 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014942 * Return the updated line number.
14943 */
14944 static linenr_T
Bram Moolenaar4770d092006-01-12 23:22:24 +000014945dump_prefixes(slang, word, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014946 slang_T *slang;
14947 char_u *word; /* case-folded word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000014948 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014949 int flags; /* flags with prefix ID */
14950 linenr_T startlnum;
14951{
14952 idx_T arridx[MAXWLEN];
14953 int curi[MAXWLEN];
14954 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000014955 char_u word_up[MAXWLEN];
14956 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014957 int c;
14958 char_u *byts;
14959 idx_T *idxs;
14960 linenr_T lnum = startlnum;
14961 int depth;
14962 int n;
14963 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014964 int i;
14965
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000014966 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000014967 * upper-case letter in word_up[]. */
14968 c = PTR2CHAR(word);
14969 if (SPELL_TOUPPER(c) != c)
14970 {
14971 onecap_copy(word, word_up, TRUE);
14972 has_word_up = TRUE;
14973 }
14974
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014975 byts = slang->sl_pbyts;
14976 idxs = slang->sl_pidxs;
14977 if (byts != NULL) /* array not is empty */
14978 {
14979 /*
14980 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014981 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014982 */
14983 depth = 0;
14984 arridx[0] = 0;
14985 curi[0] = 1;
14986 while (depth >= 0 && !got_int)
14987 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014988 n = arridx[depth];
14989 len = byts[n];
14990 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014991 {
14992 /* Done all bytes at this node, go up one level. */
14993 --depth;
14994 line_breakcheck();
14995 }
14996 else
14997 {
14998 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014999 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015000 ++curi[depth];
15001 c = byts[n];
15002 if (c == 0)
15003 {
15004 /* End of prefix, find out how many IDs there are. */
15005 for (i = 1; i < len; ++i)
15006 if (byts[n + i] != 0)
15007 break;
15008 curi[depth] += i - 1;
15009
Bram Moolenaar53805d12005-08-01 07:08:33 +000015010 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15011 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015012 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015013 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +000015014 dump_word(slang, prefix, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015015 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000015016 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015017 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015018
15019 /* Check for prefix that matches the word when the
15020 * first letter is upper-case, but only if the prefix has
15021 * a condition. */
15022 if (has_word_up)
15023 {
15024 c = valid_word_prefix(i, n, flags, word_up, slang,
15025 TRUE);
15026 if (c != 0)
15027 {
15028 vim_strncpy(prefix + depth, word_up,
15029 MAXWLEN - depth - 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +000015030 dump_word(slang, prefix, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015031 (c & WF_RAREPFX) ? (flags | WF_RARE)
15032 : flags, lnum++);
15033 }
15034 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015035 }
15036 else
15037 {
15038 /* Normal char, go one level deeper. */
15039 prefix[depth++] = c;
15040 arridx[depth] = idxs[n];
15041 curi[depth] = 1;
15042 }
15043 }
15044 }
15045 }
15046
15047 return lnum;
15048}
15049
Bram Moolenaar95529562005-08-25 21:21:38 +000015050/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015051 * Move "p" to the end of word "start".
15052 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015053 */
15054 char_u *
15055spell_to_word_end(start, buf)
15056 char_u *start;
15057 buf_T *buf;
15058{
15059 char_u *p = start;
15060
15061 while (*p != NUL && spell_iswordp(p, buf))
15062 mb_ptr_adv(p);
15063 return p;
15064}
15065
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015066#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015067/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015068 * For Insert mode completion CTRL-X s:
15069 * Find start of the word in front of column "startcol".
15070 * We don't check if it is badly spelled, with completion we can only change
15071 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015072 * Returns the column number of the word.
15073 */
15074 int
15075spell_word_start(startcol)
15076 int startcol;
15077{
15078 char_u *line;
15079 char_u *p;
15080 int col = 0;
15081
Bram Moolenaar95529562005-08-25 21:21:38 +000015082 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015083 return startcol;
15084
15085 /* Find a word character before "startcol". */
15086 line = ml_get_curline();
15087 for (p = line + startcol; p > line; )
15088 {
15089 mb_ptr_back(line, p);
15090 if (spell_iswordp_nmw(p))
15091 break;
15092 }
15093
15094 /* Go back to start of the word. */
15095 while (p > line)
15096 {
15097 col = p - line;
15098 mb_ptr_back(line, p);
15099 if (!spell_iswordp(p, curbuf))
15100 break;
15101 col = 0;
15102 }
15103
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015104 return col;
15105}
15106
15107/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015108 * Need to check for 'spellcapcheck' now, the word is removed before
15109 * expand_spelling() is called. Therefore the ugly global variable.
15110 */
15111static int spell_expand_need_cap;
15112
15113 void
15114spell_expand_check_cap(col)
15115 colnr_T col;
15116{
15117 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15118}
15119
15120/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015121 * Get list of spelling suggestions.
15122 * Used for Insert mode completion CTRL-X ?.
15123 * Returns the number of matches. The matches are in "matchp[]", array of
15124 * allocated strings.
15125 */
15126/*ARGSUSED*/
15127 int
15128expand_spelling(lnum, col, pat, matchp)
15129 linenr_T lnum;
15130 int col;
15131 char_u *pat;
15132 char_u ***matchp;
15133{
15134 garray_T ga;
15135
Bram Moolenaar4770d092006-01-12 23:22:24 +000015136 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015137 *matchp = ga.ga_data;
15138 return ga.ga_len;
15139}
15140#endif
15141
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000015142#endif /* FEAT_SYN_HL */