blob: ce887f7493ed1e431158600883dd40eaca649e2f [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 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaare19defe2005-03-21 08:23:33 +0000308#endif
309
310#include "vim.h"
311
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000312#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000313
314#ifdef HAVE_FCNTL_H
315# include <fcntl.h>
316#endif
317
Bram Moolenaar4770d092006-01-12 23:22:24 +0000318#ifndef UNIX /* it's in os_unix.h for Unix */
319# include <time.h> /* for time_t */
320#endif
321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000322#define MAXWLEN 250 /* Assume max. word len is this many bytes.
323 Some places assume a word length fits in a
324 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000325
Bram Moolenaare52325c2005-08-22 22:54:29 +0000326/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000327 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000328#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000329typedef int idx_T;
330#else
331typedef long idx_T;
332#endif
333
334/* Flags used for a word. Only the lowest byte can be used, the region byte
335 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336#define WF_REGION 0x01 /* region byte follows */
337#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
338#define WF_ALLCAP 0x04 /* word must be all capitals */
339#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000340#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000341#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000342#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000345/* for <flags2>, shifted up one byte to be used in wn_flags */
346#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000347#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000348#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000349#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000350#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
351#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000352
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000353/* only used for su_badflags */
354#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
355
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000356#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000357
Bram Moolenaar53805d12005-08-01 07:08:33 +0000358/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000359#define WFP_RARE 0x01 /* rare prefix */
360#define WFP_NC 0x02 /* prefix is not combining */
361#define WFP_UP 0x04 /* to-upper prefix */
362#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
363#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000364
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000365/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
366 * byte) and prefcondnr (two bytes). */
367#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
368#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
369#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
370#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
371 * COMPOUNDPERMITFLAG */
372#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
373 * COMPOUNDFORBIDFLAG */
374
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000375
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000376/* flags for <compoptions> */
377#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
378#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
379#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
380#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
381
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000382/* Special byte values for <byte>. Some are only used in the tree for
383 * postponed prefixes, some only in the other trees. This is a bit messy... */
384#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000385 * postponed prefix: no <pflags> */
386#define BY_INDEX 1 /* child is shared, index follows */
387#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
388 * postponed prefix: <pflags> follows */
389#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
390 * follow; never used in prefix tree */
391#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000392
Bram Moolenaar4770d092006-01-12 23:22:24 +0000393/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
394 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 * One replacement: from "ft_from" to "ft_to". */
396typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398 char_u *ft_from;
399 char_u *ft_to;
400} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000401
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000402/* Info from "SAL" entries in ".aff" file used in sl_sal.
403 * The info is split for quick processing by spell_soundfold().
404 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
405typedef struct salitem_S
406{
407 char_u *sm_lead; /* leading letters */
408 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000409 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000410 char_u *sm_rules; /* rules like ^, $, priority */
411 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000412#ifdef FEAT_MBYTE
413 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000414 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000415 int *sm_to_w; /* wide character copy of "sm_to" */
416#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000417} salitem_T;
418
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000419#ifdef FEAT_MBYTE
420typedef int salfirst_T;
421#else
422typedef short salfirst_T;
423#endif
424
Bram Moolenaar5195e452005-08-19 20:32:47 +0000425/* Values for SP_*ERROR are negative, positive values are used by
426 * read_cnt_string(). */
427#define SP_TRUNCERROR -1 /* spell file truncated error */
428#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000429#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000430
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000432 * Structure used to store words and other info for one language, loaded from
433 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000434 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
435 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
436 *
437 * The "byts" array stores the possible bytes in each tree node, preceded by
438 * the number of possible bytes, sorted on byte value:
439 * <len> <byte1> <byte2> ...
440 * The "idxs" array stores the index of the child node corresponding to the
441 * byte in "byts".
442 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000443 * the flags, region mask and affixID for the word. There may be several
444 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000445 */
446typedef struct slang_S slang_T;
447struct slang_S
448{
449 slang_T *sl_next; /* next language */
450 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000451 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000452 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000453
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000455 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000458 char_u *sl_pbyts; /* prefix tree word bytes */
459 idx_T *sl_pidxs; /* prefix tree word indexes */
460
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000461 char_u *sl_info; /* infotext string or NULL */
462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000463 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000465 char_u *sl_midword; /* MIDWORD string or NULL */
466
Bram Moolenaar4770d092006-01-12 23:22:24 +0000467 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
468
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000469 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000470 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000472 int sl_compoptions; /* COMP_* flags */
473 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000475 * (NULL when no compounding) */
476 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000477 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000478 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
480 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000481
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000482 int sl_prefixcnt; /* number of items in "sl_prefprog" */
483 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485 garray_T sl_rep; /* list of fromto_T entries from REP lines */
486 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
487 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000489 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490 there is none */
491 int sl_followup; /* SAL followup */
492 int sl_collapse; /* SAL collapse_result */
493 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000494 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
495 * "sl_sal_first" maps chars, when has_mbyte
496 * "sl_sal" is a list of wide char lists. */
497 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
498 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000499 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000500
501 /* Info from the .sug file. Loaded on demand. */
502 time_t sl_sugtime; /* timestamp for .sug file */
503 char_u *sl_sbyts; /* soundfolded word bytes */
504 idx_T *sl_sidxs; /* soundfolded word indexes */
505 buf_T *sl_sugbuf; /* buffer with word number table */
506 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
507 load */
508
Bram Moolenaarea424162005-06-16 21:51:00 +0000509 int sl_has_map; /* TRUE if there is a MAP line */
510#ifdef FEAT_MBYTE
511 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
512 int sl_map_array[256]; /* MAP for first 256 chars */
513#else
514 char_u sl_map_array[256]; /* MAP for first 256 chars */
515#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516 hashtab_T sl_sounddone; /* table with soundfolded words that have
517 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000518};
519
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000520/* First language that is loaded, start of the linked list of loaded
521 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000522static slang_T *first_lang = NULL;
523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000524/* Flags used in .spl file for soundsalike flags. */
525#define SAL_F0LLOWUP 1
526#define SAL_COLLAPSE 2
527#define SAL_REM_ACCENTS 4
528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529/*
530 * Structure used in "b_langp", filled from 'spelllang'.
531 */
532typedef struct langp_S
533{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000534 slang_T *lp_slang; /* info for this language */
535 slang_T *lp_sallang; /* language used for sound folding or NULL */
536 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 int lp_region; /* bitmask for region or REGION_ALL */
538} langp_T;
539
540#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
541
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542#define REGION_ALL 0xff /* word valid in all regions */
543
Bram Moolenaar5195e452005-08-19 20:32:47 +0000544#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
545#define VIMSPELLMAGICL 8
546#define VIMSPELLVERSION 50
547
Bram Moolenaar4770d092006-01-12 23:22:24 +0000548#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
549#define VIMSUGMAGICL 6
550#define VIMSUGVERSION 1
551
Bram Moolenaar5195e452005-08-19 20:32:47 +0000552/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
553#define SN_REGION 0 /* <regionname> section */
554#define SN_CHARFLAGS 1 /* charflags section */
555#define SN_MIDWORD 2 /* <midword> section */
556#define SN_PREFCOND 3 /* <prefcond> section */
557#define SN_REP 4 /* REP items section */
558#define SN_SAL 5 /* SAL items section */
559#define SN_SOFO 6 /* soundfolding section */
560#define SN_MAP 7 /* MAP items section */
561#define SN_COMPOUND 8 /* compound words section */
562#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000563#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000564#define SN_SUGFILE 11 /* timestamp for .sug file */
565#define SN_REPSAL 12 /* REPSAL items section */
566#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000567#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000568#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000569#define SN_END 255 /* end of sections */
570
571#define SNF_REQUIRED 1 /* <sectionflags>: required section */
572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573/* Result values. Lower number is accepted over higher one. */
574#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000575#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576#define SP_RARE 1
577#define SP_LOCAL 2
578#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000579
Bram Moolenaar7887d882005-07-01 22:33:52 +0000580/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000581static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000582
Bram Moolenaar4770d092006-01-12 23:22:24 +0000583typedef struct wordcount_S
584{
585 short_u wc_count; /* nr of times word was seen */
586 char_u wc_word[1]; /* word, actually longer */
587} wordcount_T;
588
589static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000590#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000591#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
592#define MAXWORDCOUNT 0xffff
593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000594/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000595 * Information used when looking for suggestions.
596 */
597typedef struct suginfo_S
598{
599 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000602 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 char_u *su_badptr; /* start of bad word in line */
605 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000606 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
608 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000609 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000611 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000612} suginfo_T;
613
614/* One word suggestion. Used in "si_ga". */
615typedef struct suggest_S
616{
617 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000618 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619 int st_orglen; /* length of replaced text */
620 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621 int st_altscore; /* used when st_score compares equal */
622 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000624 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625} suggest_T;
626
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628
Bram Moolenaar4770d092006-01-12 23:22:24 +0000629/* TRUE if a word appears in the list of banned words. */
630#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
631
632/* Number of suggestions kept when cleaning up. we need to keep more than
633 * what is displayed, because when rescore_suggestions() is called the score
634 * may change and wrong suggestions may be removed later. */
635#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000636
637/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
638 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000639#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640
641/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000642#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000643#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000645#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000647#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000649#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650#define SCORE_SUBST 93 /* substitute a character */
651#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000652#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000653#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000654#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000658#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000659#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000661#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
663 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000664
Bram Moolenaar4770d092006-01-12 23:22:24 +0000665#define SCORE_COMMON1 30 /* subtracted for words seen before */
666#define SCORE_COMMON2 40 /* subtracted for words often seen */
667#define SCORE_COMMON3 50 /* subtracted for words very often seen */
668#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
669#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
670
671/* When trying changed soundfold words it becomes slow when trying more than
672 * two changes. With less then two changes it's slightly faster but we miss a
673 * few good suggestions. In rare cases we need to try three of four changes.
674 */
675#define SCORE_SFMAX1 200 /* maximum score for first try */
676#define SCORE_SFMAX2 300 /* maximum score for second try */
677#define SCORE_SFMAX3 400 /* maximum score for third try */
678
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000679#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000680#define SCORE_MAXMAX 999999 /* accept any score */
681#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
682
683/* for spell_edit_score_limit() we need to know the minimum value of
684 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
685#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686
687/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000688 * Structure to store info for word matching.
689 */
690typedef struct matchinf_S
691{
692 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000693
694 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000696 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000698 char_u *mi_cend; /* char after what was used for
699 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
701 /* case-folded text */
702 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000704
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000705 /* for when checking word after a prefix */
706 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000707 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 int mi_prefcnt; /* number of entries at mi_prefarridx */
709 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000710#ifdef FEAT_MBYTE
711 int mi_cprefixlen; /* byte length of prefix in original
712 case */
713#else
714# define mi_cprefixlen mi_prefixlen /* it's the same value */
715#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000716
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000717 /* for when checking a compound word */
718 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000719 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
720 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000721 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000722
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000723 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000724 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000725 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000726 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000727
728 /* for NOBREAK */
729 int mi_result2; /* "mi_resul" without following word */
730 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731} matchinf_T;
732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000733/*
734 * The tables used for recognizing word characters according to spelling.
735 * These are only used for the first 256 characters of 'encoding'.
736 */
737typedef struct spelltab_S
738{
739 char_u st_isw[256]; /* flags: is word char */
740 char_u st_isu[256]; /* flags: is uppercase char */
741 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000742 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000743} spelltab_T;
744
745static spelltab_T spelltab;
746static int did_set_spelltab;
747
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000748#define CF_WORD 0x01
749#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000750
751static void clear_spell_chartab __ARGS((spelltab_T *sp));
752static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000753static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
754static int spell_iswordp_nmw __ARGS((char_u *p));
755#ifdef FEAT_MBYTE
756static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
757#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000758static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000759
760/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000762 */
763typedef enum
764{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 STATE_START = 0, /* At start of node check for NUL bytes (goodword
766 * ends); if badword ends there is a match, otherwise
767 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000768 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
771 STATE_PLAIN, /* Use each byte of the node. */
772 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000773 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_INS, /* Insert a byte in the bad word. */
775 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNSWAP, /* Undo swap two characters. */
777 STATE_SWAP3, /* Swap two characters over three. */
778 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000779 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000781 STATE_REP_INI, /* Prepare for using REP items. */
782 STATE_REP, /* Use matching REP items from the .aff file. */
783 STATE_REP_UNDO, /* Undo a REP item replacement. */
784 STATE_FINAL /* End of this node. */
785} state_T;
786
787/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000788 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789 */
790typedef struct trystate_S
791{
Bram Moolenaarea424162005-06-16 21:51:00 +0000792 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000795 short ts_curi; /* index in list of child nodes */
796 char_u ts_fidx; /* index in fword[], case-folded bad word */
797 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
798 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000799 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000800 * PFD_PREFIXTREE or PFD_NOPREFIX */
801 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000802#ifdef FEAT_MBYTE
803 char_u ts_tcharlen; /* number of bytes in tword character */
804 char_u ts_tcharidx; /* current byte index in tword character */
805 char_u ts_isdiff; /* DIFF_ values */
806 char_u ts_fcharstart; /* index in fword where badword char started */
807#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000808 char_u ts_prewordlen; /* length of word in "preword[]" */
809 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000810 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000811 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000812 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000813 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000814 char_u ts_delidx; /* index in fword for char that was deleted,
815 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816} trystate_T;
817
Bram Moolenaarea424162005-06-16 21:51:00 +0000818/* values for ts_isdiff */
819#define DIFF_NONE 0 /* no different byte (yet) */
820#define DIFF_YES 1 /* different byte found */
821#define DIFF_INSERT 2 /* inserting character */
822
Bram Moolenaard12a1322005-08-21 22:08:24 +0000823/* values for ts_flags */
824#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
825#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000826#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000828/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000829#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000830#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000831#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000834#define FIND_FOLDWORD 0 /* find word case-folded */
835#define FIND_KEEPWORD 1 /* find keep-case word */
836#define FIND_PREFIX 2 /* find word after prefix */
837#define FIND_COMPOUND 3 /* find case-folded compound word */
838#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000839
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840static slang_T *slang_alloc __ARGS((char_u *lang));
841static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000842static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000843static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000845static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000846static 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 +0000847static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000849static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000850static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000851static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000852static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000853static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000854static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000855static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000856static int get2c __ARGS((FILE *fd));
857static int get3c __ARGS((FILE *fd));
858static int get4c __ARGS((FILE *fd));
859static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000860static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000861static char_u *read_string __ARGS((FILE *fd, int cnt));
862static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
863static int read_charflags_section __ARGS((FILE *fd));
864static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000865static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
868static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
869static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
871static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000872static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000873static int init_syl_tab __ARGS((slang_T *slang));
874static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000875static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
876static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000877#ifdef FEAT_MBYTE
878static int *mb_str2wide __ARGS((char_u *s));
879#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000880static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
881static 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 +0000882static void clear_midword __ARGS((buf_T *buf));
883static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000884static int find_region __ARGS((char_u *rp, char_u *region));
885static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000886static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000887static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000891static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000892static 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 +0000893#ifdef FEAT_EVAL
894static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
895#endif
896static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000897static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
898static void suggest_load_files __ARGS((void));
899static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000903static void suggest_try_special __ARGS((suginfo_T *su));
904static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000905static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
906static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000907#ifdef FEAT_MBYTE
908static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000911static void score_comp_sal __ARGS((suginfo_T *su));
912static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000913static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000914static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000915static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_finish __ARGS((void));
917static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
918static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000919static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000920static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922static 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));
923static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000924static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000925static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000926static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000927static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
929static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
930static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000931#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000932static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000934static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000935static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
936static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
937#ifdef FEAT_MBYTE
938static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000940static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
941static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000942static buf_T *open_spellbuf __ARGS((void));
943static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000944
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000945/*
946 * Use our own character-case definitions, because the current locale may
947 * differ from what the .spl file uses.
948 * These must not be called with negative number!
949 */
950#ifndef FEAT_MBYTE
951/* Non-multi-byte implementation. */
952# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
953# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
954# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
955#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000956# if defined(HAVE_WCHAR_H)
957# include <wchar.h> /* for towupper() and towlower() */
958# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000959/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
960 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
961 * the "w" library function for characters above 255 if available. */
962# ifdef HAVE_TOWLOWER
963# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
964 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
965# else
966# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
967 : (c) < 256 ? spelltab.st_fold[c] : (c))
968# endif
969
970# ifdef HAVE_TOWUPPER
971# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
972 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
973# else
974# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
975 : (c) < 256 ? spelltab.st_upper[c] : (c))
976# endif
977
978# ifdef HAVE_ISWUPPER
979# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
980 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
981# else
982# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000983 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000984# endif
985#endif
986
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000987
988static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000989static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000990static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000991static char *e_affname = N_("Affix name too long in %s line %d: %s");
992static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
993static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000994static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000996/* Remember what "z?" replaced. */
997static char_u *repl_from = NULL;
998static char_u *repl_to = NULL;
999
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001000/*
1001 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001002 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001003 * "*attrp" is set to the highlight index for a badly spelled word. For a
1004 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001007 * "capcol" is used to check for a Capitalised word after the end of a
1008 * sentence. If it's zero then perform the check. Return the column where to
1009 * check next, or -1 when no sentence end was found. If it's NULL then don't
1010 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001011 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 * Returns the length of the word in bytes, also when it's OK, so that the
1013 * caller can skip over the word.
1014 */
1015 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001016spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001019 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001020 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001021 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022{
1023 matchinf_T mi; /* Most things are put in "mi" so that it can
1024 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001025 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001026 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001027 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001028 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001029 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001030
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001031 /* A word never starts at a space or a control character. Return quickly
1032 * then, skipping over the character. */
1033 if (*ptr <= ' ')
1034 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001035
1036 /* Return here when loading language files failed. */
1037 if (wp->w_buffer->b_langp.ga_len == 0)
1038 return 1;
1039
Bram Moolenaar5195e452005-08-19 20:32:47 +00001040 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001042 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001043 * 0X99FF. But always do check spelling to find "3GPP" and "11
1044 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001045 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001046 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001047 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1048 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 else
1050 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001051 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001053
Bram Moolenaar0c405862005-06-22 22:26:26 +00001054 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001056 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001057 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001058 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001059 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001061 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001062 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001063
1064 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1065 {
1066 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001067 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001069 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 if (capcol != NULL)
1073 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* We always use the characters up to the next non-word character,
1076 * also for bad words. */
1077 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001078
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001079 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001080 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001081
Bram Moolenaar5195e452005-08-19 20:32:47 +00001082 /* case-fold the word with one non-word character, so that we can check
1083 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001084 if (*mi.mi_fend != NUL)
1085 mb_ptr_adv(mi.mi_fend);
1086
1087 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1088 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001089 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090
1091 /* The word is bad unless we recognize it. */
1092 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001093 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /*
1096 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001097 * We check them all, because a word may be matched longer in another
1098 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001099 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001100 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001102 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1103
1104 /* If reloading fails the language is still in the list but everything
1105 * has been cleared. */
1106 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1107 continue;
1108
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001109 /* Check for a matching word in case-folded words. */
1110 find_word(&mi, FIND_FOLDWORD);
1111
1112 /* Check for a matching word in keep-case words. */
1113 find_word(&mi, FIND_KEEPWORD);
1114
1115 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001116 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001117
1118 /* For a NOBREAK language, may want to use a word without a following
1119 * word as a backup. */
1120 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1121 && mi.mi_result2 != SP_BAD)
1122 {
1123 mi.mi_result = mi.mi_result2;
1124 mi.mi_end = mi.mi_end2;
1125 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001126
1127 /* Count the word in the first language where it's found to be OK. */
1128 if (count_word && mi.mi_result == SP_OK)
1129 {
1130 count_common_word(mi.mi_lp->lp_slang, ptr,
1131 (int)(mi.mi_end - ptr), 1);
1132 count_word = FALSE;
1133 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001134 }
1135
1136 if (mi.mi_result != SP_OK)
1137 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001138 /* If we found a number skip over it. Allows for "42nd". Do flag
1139 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001140 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001141 {
1142 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1143 return nrlen;
1144 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001145
1146 /* When we are at a non-word character there is no error, just
1147 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001148 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001149 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001150 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1151 {
1152 regmatch_T regmatch;
1153
1154 /* Check for end of sentence. */
1155 regmatch.regprog = wp->w_buffer->b_cap_prog;
1156 regmatch.rm_ic = FALSE;
1157 if (vim_regexec(&regmatch, ptr, 0))
1158 *capcol = (int)(regmatch.endp[0] - ptr);
1159 }
1160
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001161#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001163 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001165 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001166 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001167 else if (mi.mi_end == ptr)
1168 /* Always include at least one character. Required for when there
1169 * is a mixup in "midword". */
1170 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 else if (mi.mi_result == SP_BAD
1172 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1173 {
1174 char_u *p, *fp;
1175 int save_result = mi.mi_result;
1176
1177 /* First language in 'spelllang' is NOBREAK. Find first position
1178 * at which any word would be valid. */
1179 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001180 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001182 p = mi.mi_word;
1183 fp = mi.mi_fword;
1184 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001186 mb_ptr_adv(p);
1187 mb_ptr_adv(fp);
1188 if (p >= mi.mi_end)
1189 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001190 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001191 find_word(&mi, FIND_COMPOUND);
1192 if (mi.mi_result != SP_BAD)
1193 {
1194 mi.mi_end = p;
1195 break;
1196 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001198 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001199 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001201
1202 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001204 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001205 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001206 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001207 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001208 }
1209
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1211 {
1212 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001213 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001214 return wrongcaplen;
1215 }
1216
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217 return (int)(mi.mi_end - ptr);
1218}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001219
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220/*
1221 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001222 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1223 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1224 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1225 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 *
1227 * For a match mip->mi_result is updated.
1228 */
1229 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001234 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001236 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 int endidxcnt = 0;
1238 int len;
1239 int wlen = 0;
1240 int flen;
1241 int c;
1242 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001243 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244#ifdef FEAT_MBYTE
1245 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001246#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001247 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001248 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 slang_T *slang = mip->mi_lp->lp_slang;
1250 unsigned flags;
1251 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001252 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001253 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001254 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001255 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 /* Check for word with matching case in keep-case tree. */
1260 ptr = mip->mi_word;
1261 flen = 9999; /* no case folding, always enough bytes */
1262 byts = slang->sl_kbyts;
1263 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001264
1265 if (mode == FIND_KEEPCOMPOUND)
1266 /* Skip over the previously found word(s). */
1267 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001268 }
1269 else
1270 {
1271 /* Check for case-folded in case-folded tree. */
1272 ptr = mip->mi_fword;
1273 flen = mip->mi_fwordlen; /* available case-folded bytes */
1274 byts = slang->sl_fbyts;
1275 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001276
1277 if (mode == FIND_PREFIX)
1278 {
1279 /* Skip over the prefix. */
1280 wlen = mip->mi_prefixlen;
1281 flen -= mip->mi_prefixlen;
1282 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001283 else if (mode == FIND_COMPOUND)
1284 {
1285 /* Skip over the previously found word(s). */
1286 wlen = mip->mi_compoff;
1287 flen -= mip->mi_compoff;
1288 }
1289
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290 }
1291
Bram Moolenaar51485f02005-06-04 21:55:20 +00001292 if (byts == NULL)
1293 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294
Bram Moolenaar51485f02005-06-04 21:55:20 +00001295 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001296 * Repeat advancing in the tree until:
1297 * - there is a byte that doesn't match,
1298 * - we reach the end of the tree,
1299 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001300 */
1301 for (;;)
1302 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001303 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001304 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001305
1306 len = byts[arridx++];
1307
1308 /* If the first possible byte is a zero the word could end here.
1309 * Remember this index, we first check for the longest word. */
1310 if (byts[arridx] == 0)
1311 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001312 if (endidxcnt == MAXWLEN)
1313 {
1314 /* Must be a corrupted spell file. */
1315 EMSG(_(e_format));
1316 return;
1317 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 endlen[endidxcnt] = wlen;
1319 endidx[endidxcnt++] = arridx++;
1320 --len;
1321
1322 /* Skip over the zeros, there can be several flag/region
1323 * combinations. */
1324 while (len > 0 && byts[arridx] == 0)
1325 {
1326 ++arridx;
1327 --len;
1328 }
1329 if (len == 0)
1330 break; /* no children, word must end here */
1331 }
1332
1333 /* Stop looking at end of the line. */
1334 if (ptr[wlen] == NUL)
1335 break;
1336
1337 /* Perform a binary search in the list of accepted bytes. */
1338 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001339 if (c == TAB) /* <Tab> is handled like <Space> */
1340 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001341 lo = arridx;
1342 hi = arridx + len - 1;
1343 while (lo < hi)
1344 {
1345 m = (lo + hi) / 2;
1346 if (byts[m] > c)
1347 hi = m - 1;
1348 else if (byts[m] < c)
1349 lo = m + 1;
1350 else
1351 {
1352 lo = hi = m;
1353 break;
1354 }
1355 }
1356
1357 /* Stop if there is no matching byte. */
1358 if (hi < lo || byts[lo] != c)
1359 break;
1360
1361 /* Continue at the child (if there is one). */
1362 arridx = idxs[lo];
1363 ++wlen;
1364 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001365
1366 /* One space in the good word may stand for several spaces in the
1367 * checked word. */
1368 if (c == ' ')
1369 {
1370 for (;;)
1371 {
1372 if (flen <= 0 && *mip->mi_fend != NUL)
1373 flen = fold_more(mip);
1374 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1375 break;
1376 ++wlen;
1377 --flen;
1378 }
1379 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380 }
1381
1382 /*
1383 * Verify that one of the possible endings is valid. Try the longest
1384 * first.
1385 */
1386 while (endidxcnt > 0)
1387 {
1388 --endidxcnt;
1389 arridx = endidx[endidxcnt];
1390 wlen = endlen[endidxcnt];
1391
1392#ifdef FEAT_MBYTE
1393 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1394 continue; /* not at first byte of character */
1395#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001396 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001399 continue; /* next char is a word character */
1400 word_ends = FALSE;
1401 }
1402 else
1403 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001404 /* The prefix flag is before compound flags. Once a valid prefix flag
1405 * has been found we try compound flags. */
1406 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407
1408#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 {
1411 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 * when folding case. This can be slow, take a shortcut when the
1413 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001415 if (STRNCMP(ptr, p, wlen) != 0)
1416 {
1417 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1418 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001419 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001421 }
1422#endif
1423
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 /* Check flags and region. For FIND_PREFIX check the condition and
1425 * prefix ID.
1426 * Repeat this if there are more flags/region alternatives until there
1427 * is a match. */
1428 res = SP_BAD;
1429 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1430 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 {
1432 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001433
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001434 /* For the fold-case tree check that the case of the checked word
1435 * matches with what the word in the tree requires.
1436 * For keep-case tree the case is always right. For prefixes we
1437 * don't bother to check. */
1438 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 if (mip->mi_cend != mip->mi_word + wlen)
1441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 /* mi_capflags was set for a different word length, need
1443 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 }
1447
Bram Moolenaar0c405862005-06-22 22:26:26 +00001448 if (mip->mi_capflags == WF_KEEPCAP
1449 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001450 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001451 }
1452
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001453 /* When mode is FIND_PREFIX the word must support the prefix:
1454 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001455 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001456 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001458 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001459 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001460 mip->mi_word + mip->mi_cprefixlen, slang,
1461 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001463 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001464
1465 /* Use the WF_RARE flag for a rare prefix. */
1466 if (c & WF_RAREPFX)
1467 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001468 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469 }
1470
Bram Moolenaar78622822005-08-23 21:00:13 +00001471 if (slang->sl_nobreak)
1472 {
1473 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1474 && (flags & WF_BANNED) == 0)
1475 {
1476 /* NOBREAK: found a valid following word. That's all we
1477 * need to know, so return. */
1478 mip->mi_result = SP_OK;
1479 break;
1480 }
1481 }
1482
1483 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1484 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001485 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001486 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001487 * COMPOUNDMIN reject it quickly.
1488 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 * that's too short... Myspell compatibility requires this
1490 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001491 if (((unsigned)flags >> 24) == 0
1492 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001494#ifdef FEAT_MBYTE
1495 /* For multi-byte chars check character length against
1496 * COMPOUNDMIN. */
1497 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001498 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001499 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1500 wlen - mip->mi_compoff) < slang->sl_compminlen)
1501 continue;
1502#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001503
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001504 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001505 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001506 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1507 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001508 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001509 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001510
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001511 /* Don't allow compounding on a side where an affix was added,
1512 * unless COMPOUNDPERMITFLAG was used. */
1513 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1514 continue;
1515 if (!word_ends && (flags & WF_NOCOMPAFT))
1516 continue;
1517
Bram Moolenaard12a1322005-08-21 22:08:24 +00001518 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001519 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001520 ? slang->sl_compstartflags
1521 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001522 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001523 continue;
1524
Bram Moolenaare52325c2005-08-22 22:54:29 +00001525 if (mode == FIND_COMPOUND)
1526 {
1527 int capflags;
1528
1529 /* Need to check the caps type of the appended compound
1530 * word. */
1531#ifdef FEAT_MBYTE
1532 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1533 mip->mi_compoff) != 0)
1534 {
1535 /* case folding may have changed the length */
1536 p = mip->mi_word;
1537 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1538 mb_ptr_adv(p);
1539 }
1540 else
1541#endif
1542 p = mip->mi_word + mip->mi_compoff;
1543 capflags = captype(p, mip->mi_word + wlen);
1544 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1545 && (flags & WF_FIXCAP) != 0))
1546 continue;
1547
1548 if (capflags != WF_ALLCAP)
1549 {
1550 /* When the character before the word is a word
1551 * character we do not accept a Onecap word. We do
1552 * accept a no-caps word, even when the dictionary
1553 * word specifies ONECAP. */
1554 mb_ptr_back(mip->mi_word, p);
1555 if (spell_iswordp_nmw(p)
1556 ? capflags == WF_ONECAP
1557 : (flags & WF_ONECAP) != 0
1558 && capflags != WF_ONECAP)
1559 continue;
1560 }
1561 }
1562
Bram Moolenaar5195e452005-08-19 20:32:47 +00001563 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001564 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001565 * the number of syllables must not be too large. */
1566 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1567 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1568 if (word_ends)
1569 {
1570 char_u fword[MAXWLEN];
1571
1572 if (slang->sl_compsylmax < MAXWLEN)
1573 {
1574 /* "fword" is only needed for checking syllables. */
1575 if (ptr == mip->mi_word)
1576 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1577 else
1578 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1579 }
1580 if (!can_compound(slang, fword, mip->mi_compflags))
1581 continue;
1582 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001583 }
1584
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001585 /* Check NEEDCOMPOUND: can't use word without compounding. */
1586 else if (flags & WF_NEEDCOMP)
1587 continue;
1588
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 nobreak_result = SP_OK;
1590
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001591 if (!word_ends)
1592 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001593 int save_result = mip->mi_result;
1594 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001595 langp_T *save_lp = mip->mi_lp;
1596 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001597
1598 /* Check that a valid word follows. If there is one and we
1599 * are compounding, it will set "mi_result", thus we are
1600 * always finished here. For NOBREAK we only check that a
1601 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001602 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 if (slang->sl_nobreak)
1604 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001605
1606 /* Find following word in case-folded tree. */
1607 mip->mi_compoff = endlen[endidxcnt];
1608#ifdef FEAT_MBYTE
1609 if (has_mbyte && mode == FIND_KEEPWORD)
1610 {
1611 /* Compute byte length in case-folded word from "wlen":
1612 * byte length in keep-case word. Length may change when
1613 * folding case. This can be slow, take a shortcut when
1614 * the case-folded word is equal to the keep-case word. */
1615 p = mip->mi_fword;
1616 if (STRNCMP(ptr, p, wlen) != 0)
1617 {
1618 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1619 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001620 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001621 }
1622 }
1623#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001624 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001625 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001626 if (flags & WF_COMPROOT)
1627 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001628
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001629 /* For NOBREAK we need to try all NOBREAK languages, at least
1630 * to find the ".add" file(s). */
1631 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001632 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001633 if (slang->sl_nobreak)
1634 {
1635 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1636 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1637 || !mip->mi_lp->lp_slang->sl_nobreak)
1638 continue;
1639 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001640
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001641 find_word(mip, FIND_COMPOUND);
1642
1643 /* When NOBREAK any word that matches is OK. Otherwise we
1644 * need to find the longest match, thus try with keep-case
1645 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001646 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1647 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001648 /* Find following word in keep-case tree. */
1649 mip->mi_compoff = wlen;
1650 find_word(mip, FIND_KEEPCOMPOUND);
1651
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001652#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1653 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1654 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1656 {
1657 /* Check for following word with prefix. */
1658 mip->mi_compoff = c;
1659 find_prefix(mip, FIND_COMPOUND);
1660 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001661#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663
1664 if (!slang->sl_nobreak)
1665 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001666 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001667 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001668 if (flags & WF_COMPROOT)
1669 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001670 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001671
Bram Moolenaar78622822005-08-23 21:00:13 +00001672 if (slang->sl_nobreak)
1673 {
1674 nobreak_result = mip->mi_result;
1675 mip->mi_result = save_result;
1676 mip->mi_end = save_end;
1677 }
1678 else
1679 {
1680 if (mip->mi_result == SP_OK)
1681 break;
1682 continue;
1683 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001684 }
1685
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001686 if (flags & WF_BANNED)
1687 res = SP_BANNED;
1688 else if (flags & WF_REGION)
1689 {
1690 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001691 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001692 res = SP_OK;
1693 else
1694 res = SP_LOCAL;
1695 }
1696 else if (flags & WF_RARE)
1697 res = SP_RARE;
1698 else
1699 res = SP_OK;
1700
Bram Moolenaar78622822005-08-23 21:00:13 +00001701 /* Always use the longest match and the best result. For NOBREAK
1702 * we separately keep the longest match without a following good
1703 * word as a fall-back. */
1704 if (nobreak_result == SP_BAD)
1705 {
1706 if (mip->mi_result2 > res)
1707 {
1708 mip->mi_result2 = res;
1709 mip->mi_end2 = mip->mi_word + wlen;
1710 }
1711 else if (mip->mi_result2 == res
1712 && mip->mi_end2 < mip->mi_word + wlen)
1713 mip->mi_end2 = mip->mi_word + wlen;
1714 }
1715 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716 {
1717 mip->mi_result = res;
1718 mip->mi_end = mip->mi_word + wlen;
1719 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001720 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001721 mip->mi_end = mip->mi_word + wlen;
1722
Bram Moolenaar78622822005-08-23 21:00:13 +00001723 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 }
1726
Bram Moolenaar78622822005-08-23 21:00:13 +00001727 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001728 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001729 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001730}
1731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001733 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1734 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001735 */
1736 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001737can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001738 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001739 char_u *word;
1740 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001741{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001742 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001743#ifdef FEAT_MBYTE
1744 char_u uflags[MAXWLEN * 2];
1745 int i;
1746#endif
1747 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001748
1749 if (slang->sl_compprog == NULL)
1750 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001751#ifdef FEAT_MBYTE
1752 if (enc_utf8)
1753 {
1754 /* Need to convert the single byte flags to utf8 characters. */
1755 p = uflags;
1756 for (i = 0; flags[i] != NUL; ++i)
1757 p += mb_char2bytes(flags[i], p);
1758 *p = NUL;
1759 p = uflags;
1760 }
1761 else
1762#endif
1763 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001764 regmatch.regprog = slang->sl_compprog;
1765 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001766 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001767 return FALSE;
1768
Bram Moolenaare52325c2005-08-22 22:54:29 +00001769 /* Count the number of syllables. This may be slow, do it last. If there
1770 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001771 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001772 if (slang->sl_compsylmax < MAXWLEN
1773 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001774 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001775 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001776}
1777
1778/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001779 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1780 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001781 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001782 */
1783 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001784valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001785 int totprefcnt; /* nr of prefix IDs */
1786 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001787 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001788 char_u *word;
1789 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001790 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791{
1792 int prefcnt;
1793 int pidx;
1794 regprog_T *rp;
1795 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001796 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001797
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001798 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1800 {
1801 pidx = slang->sl_pidxs[arridx + prefcnt];
1802
1803 /* Check the prefix ID. */
1804 if (prefid != (pidx & 0xff))
1805 continue;
1806
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001807 /* Check if the prefix doesn't combine and the word already has a
1808 * suffix. */
1809 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1810 continue;
1811
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001812 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001813 * stored in the two bytes above the prefix ID byte. */
1814 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001815 if (rp != NULL)
1816 {
1817 regmatch.regprog = rp;
1818 regmatch.rm_ic = FALSE;
1819 if (!vim_regexec(&regmatch, word, 0))
1820 continue;
1821 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001822 else if (cond_req)
1823 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001824
Bram Moolenaar53805d12005-08-01 07:08:33 +00001825 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001826 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001827 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001828 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001829}
1830
1831/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001832 * Check if the word at "mip->mi_word" has a matching prefix.
1833 * If it does, then check the following word.
1834 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001835 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1836 * prefix in a compound word.
1837 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838 * For a match mip->mi_result is updated.
1839 */
1840 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001841find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001842 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001843 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001844{
1845 idx_T arridx = 0;
1846 int len;
1847 int wlen = 0;
1848 int flen;
1849 int c;
1850 char_u *ptr;
1851 idx_T lo, hi, m;
1852 slang_T *slang = mip->mi_lp->lp_slang;
1853 char_u *byts;
1854 idx_T *idxs;
1855
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001856 byts = slang->sl_pbyts;
1857 if (byts == NULL)
1858 return; /* array is empty */
1859
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001860 /* We use the case-folded word here, since prefixes are always
1861 * case-folded. */
1862 ptr = mip->mi_fword;
1863 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001864 if (mode == FIND_COMPOUND)
1865 {
1866 /* Skip over the previously found word(s). */
1867 ptr += mip->mi_compoff;
1868 flen -= mip->mi_compoff;
1869 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001870 idxs = slang->sl_pidxs;
1871
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001872 /*
1873 * Repeat advancing in the tree until:
1874 * - there is a byte that doesn't match,
1875 * - we reach the end of the tree,
1876 * - or we reach the end of the line.
1877 */
1878 for (;;)
1879 {
1880 if (flen == 0 && *mip->mi_fend != NUL)
1881 flen = fold_more(mip);
1882
1883 len = byts[arridx++];
1884
1885 /* If the first possible byte is a zero the prefix could end here.
1886 * Check if the following word matches and supports the prefix. */
1887 if (byts[arridx] == 0)
1888 {
1889 /* There can be several prefixes with different conditions. We
1890 * try them all, since we don't know which one will give the
1891 * longest match. The word is the same each time, pass the list
1892 * of possible prefixes to find_word(). */
1893 mip->mi_prefarridx = arridx;
1894 mip->mi_prefcnt = len;
1895 while (len > 0 && byts[arridx] == 0)
1896 {
1897 ++arridx;
1898 --len;
1899 }
1900 mip->mi_prefcnt -= len;
1901
1902 /* Find the word that comes after the prefix. */
1903 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001904 if (mode == FIND_COMPOUND)
1905 /* Skip over the previously found word(s). */
1906 mip->mi_prefixlen += mip->mi_compoff;
1907
Bram Moolenaar53805d12005-08-01 07:08:33 +00001908#ifdef FEAT_MBYTE
1909 if (has_mbyte)
1910 {
1911 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001912 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1913 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001914 }
1915 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001916 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001917#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001918 find_word(mip, FIND_PREFIX);
1919
1920
1921 if (len == 0)
1922 break; /* no children, word must end here */
1923 }
1924
1925 /* Stop looking at end of the line. */
1926 if (ptr[wlen] == NUL)
1927 break;
1928
1929 /* Perform a binary search in the list of accepted bytes. */
1930 c = ptr[wlen];
1931 lo = arridx;
1932 hi = arridx + len - 1;
1933 while (lo < hi)
1934 {
1935 m = (lo + hi) / 2;
1936 if (byts[m] > c)
1937 hi = m - 1;
1938 else if (byts[m] < c)
1939 lo = m + 1;
1940 else
1941 {
1942 lo = hi = m;
1943 break;
1944 }
1945 }
1946
1947 /* Stop if there is no matching byte. */
1948 if (hi < lo || byts[lo] != c)
1949 break;
1950
1951 /* Continue at the child (if there is one). */
1952 arridx = idxs[lo];
1953 ++wlen;
1954 --flen;
1955 }
1956}
1957
1958/*
1959 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001960 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001961 * Return the length of the folded chars in bytes.
1962 */
1963 static int
1964fold_more(mip)
1965 matchinf_T *mip;
1966{
1967 int flen;
1968 char_u *p;
1969
1970 p = mip->mi_fend;
1971 do
1972 {
1973 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001974 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001976 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 if (*mip->mi_fend != NUL)
1978 mb_ptr_adv(mip->mi_fend);
1979
1980 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1981 mip->mi_fword + mip->mi_fwordlen,
1982 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001984 mip->mi_fwordlen += flen;
1985 return flen;
1986}
1987
1988/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 * Check case flags for a word. Return TRUE if the word has the requested
1990 * case.
1991 */
1992 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001993spell_valid_case(wordflags, treeflags)
1994 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 int treeflags; /* flags for the word in the spell tree */
1996{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001997 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001999 && ((treeflags & WF_ONECAP) == 0
2000 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001}
2002
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002003/*
2004 * Return TRUE if spell checking is not enabled.
2005 */
2006 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002007no_spell_checking(wp)
2008 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002009{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002010 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2011 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002012 {
2013 EMSG(_("E756: Spell checking is not enabled"));
2014 return TRUE;
2015 }
2016 return FALSE;
2017}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019/*
2020 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002021 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2022 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002023 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2024 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002025 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002026 */
2027 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002028spell_move_to(wp, dir, allwords, curline, attrp)
2029 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002030 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002031 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002033 hlf_T *attrp; /* return: attributes of bad word or NULL
2034 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002036 linenr_T lnum;
2037 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002038 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 char_u *line;
2040 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002041 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002042 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002043 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002044# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002045 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002046# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002047 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002048 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002049 char_u *buf = NULL;
2050 int buflen = 0;
2051 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002052 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002053 int found_one = FALSE;
2054 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaar95529562005-08-25 21:21:38 +00002056 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002057 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002058
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002059 /*
2060 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002061 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002062 *
2063 * When searching backwards, we continue in the line to find the last
2064 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002065 *
2066 * We concatenate the start of the next line, so that wrapped words work
2067 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2068 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002069 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002070 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002071 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072
2073 while (!got_int)
2074 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002075 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002078 if (buflen < len + MAXWLEN + 2)
2079 {
2080 vim_free(buf);
2081 buflen = len + MAXWLEN + 2;
2082 buf = alloc(buflen);
2083 if (buf == NULL)
2084 break;
2085 }
2086
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002087 /* In first line check first word for Capital. */
2088 if (lnum == 1)
2089 capcol = 0;
2090
2091 /* For checking first word with a capital skip white space. */
2092 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 capcol = (int)(skipwhite(line) - line);
2094 else if (curline && wp == curwin)
2095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002097 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002098 if (check_need_cap(lnum, col))
2099 capcol = col;
2100
2101 /* Need to get the line again, may have looked at the previous
2102 * one. */
2103 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2104 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002105
Bram Moolenaar0c405862005-06-22 22:26:26 +00002106 /* Copy the line into "buf" and append the start of the next line if
2107 * possible. */
2108 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002109 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002110 spell_cat_line(buf + STRLEN(buf),
2111 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002112
2113 p = buf + skip;
2114 endp = buf + len;
2115 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002117 /* When searching backward don't search after the cursor. Unless
2118 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002119 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002120 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002121 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002122 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002123 break;
2124
2125 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002126 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002127 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002128
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002129 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002132 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002133 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002134 /* When searching forward only accept a bad word after
2135 * the cursor. */
2136 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002137 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002138 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002139 && (wrapped
2140 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002141 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002142 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002143 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002144# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002145 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002147 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002148 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002149 FALSE, &can_spell);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002150 if (!can_spell)
2151 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 }
2153 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002154#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002155 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 if (can_spell)
2158 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002159 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002160 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002161 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002162#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002163 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002164#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165 if (dir == FORWARD)
2166 {
2167 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002168 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002169 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002170 if (attrp != NULL)
2171 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002172 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002173 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002174 else if (curline)
2175 /* Insert mode completion: put cursor after
2176 * the bad word. */
2177 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002178 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002179 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002181 else
2182 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 }
2185
Bram Moolenaar51485f02005-06-04 21:55:20 +00002186 /* advance to character after the word */
2187 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002188 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002189 }
2190
Bram Moolenaar5195e452005-08-19 20:32:47 +00002191 if (dir == BACKWARD && found_pos.lnum != 0)
2192 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002193 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002194 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002195 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002197 }
2198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002199 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002200 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002202 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002203 if (dir == BACKWARD)
2204 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002205 /* If we are back at the starting line and searched it again there
2206 * is no match, give up. */
2207 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002208 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002209
2210 if (lnum > 1)
2211 --lnum;
2212 else if (!p_ws)
2213 break; /* at first line and 'nowrapscan' */
2214 else
2215 {
2216 /* Wrap around to the end of the buffer. May search the
2217 * starting line again and accept the last match. */
2218 lnum = wp->w_buffer->b_ml.ml_line_count;
2219 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 if (!shortmess(SHM_SEARCH))
2221 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002222 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002223 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002224 }
2225 else
2226 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002227 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2228 ++lnum;
2229 else if (!p_ws)
2230 break; /* at first line and 'nowrapscan' */
2231 else
2232 {
2233 /* Wrap around to the start of the buffer. May search the
2234 * starting line again and accept the first match. */
2235 lnum = 1;
2236 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 if (!shortmess(SHM_SEARCH))
2238 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002239 }
2240
2241 /* If we are back at the starting line and there is no match then
2242 * give up. */
2243 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002244 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245
2246 /* Skip the characters at the start of the next line that were
2247 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002248 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002249 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002250 else
2251 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002253 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002254 --capcol;
2255
2256 /* But after empty line check first word in next line */
2257 if (*skipwhite(line) == NUL)
2258 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002260
2261 line_breakcheck();
2262 }
2263
Bram Moolenaar0c405862005-06-22 22:26:26 +00002264 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002265 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002266}
2267
2268/*
2269 * For spell checking: concatenate the start of the following line "line" into
2270 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2271 */
2272 void
2273spell_cat_line(buf, line, maxlen)
2274 char_u *buf;
2275 char_u *line;
2276 int maxlen;
2277{
2278 char_u *p;
2279 int n;
2280
2281 p = skipwhite(line);
2282 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2283 p = skipwhite(p + 1);
2284
2285 if (*p != NUL)
2286 {
2287 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002288 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002289 n = (int)(p - line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002290 if (n >= maxlen)
2291 n = maxlen - 1;
2292 vim_memset(buf + 1, ' ', n);
2293 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002294}
2295
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002296/*
2297 * Structure used for the cookie argument of do_in_runtimepath().
2298 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002299typedef struct spelload_S
2300{
2301 char_u sl_lang[MAXWLEN + 1]; /* language name */
2302 slang_T *sl_slang; /* resulting slang_T struct */
2303 int sl_nobreak; /* NOBREAK language found */
2304} spelload_T;
2305
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002306/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002307 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002308 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002309 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002310 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002311spell_load_lang(lang)
2312 char_u *lang;
2313{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002314 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002316 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002317#ifdef FEAT_AUTOCMD
2318 int round;
2319#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320
Bram Moolenaarb765d632005-06-07 21:00:02 +00002321 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002322 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002323 STRCPY(sl.sl_lang, lang);
2324 sl.sl_slang = NULL;
2325 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002326
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002327#ifdef FEAT_AUTOCMD
2328 /* We may retry when no spell file is found for the language, an
2329 * autocommand may load it then. */
2330 for (round = 1; round <= 2; ++round)
2331#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002332 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002333 /*
2334 * Find the first spell file for "lang" in 'runtimepath' and load it.
2335 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002336 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002337 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002338 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002339
2340 if (r == FAIL && *sl.sl_lang != NUL)
2341 {
2342 /* Try loading the ASCII version. */
2343 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2344 "spell/%s.ascii.spl", lang);
2345 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2346
2347#ifdef FEAT_AUTOCMD
2348 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2349 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2350 curbuf->b_fname, FALSE, curbuf))
2351 continue;
2352 break;
2353#endif
2354 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355#ifdef FEAT_AUTOCMD
2356 break;
2357#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002358 }
2359
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002360 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002361 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002362 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2363 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002364 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002365 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002366 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002367 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002368 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002369 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002370 }
2371}
2372
2373/*
2374 * Return the encoding used for spell checking: Use 'encoding', except that we
2375 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2376 */
2377 static char_u *
2378spell_enc()
2379{
2380
2381#ifdef FEAT_MBYTE
2382 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2383 return p_enc;
2384#endif
2385 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002386}
2387
2388/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002389 * Get the name of the .spl file for the internal wordlist into
2390 * "fname[MAXPATHL]".
2391 */
2392 static void
2393int_wordlist_spl(fname)
2394 char_u *fname;
2395{
2396 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2397 int_wordlist, spell_enc());
2398}
2399
2400/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002401 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002402 * Caller must fill "sl_next".
2403 */
2404 static slang_T *
2405slang_alloc(lang)
2406 char_u *lang;
2407{
2408 slang_T *lp;
2409
Bram Moolenaar51485f02005-06-04 21:55:20 +00002410 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002411 if (lp != NULL)
2412 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002413 if (lang != NULL)
2414 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002416 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002417 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002418 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002419 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002420 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002421
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002422 return lp;
2423}
2424
2425/*
2426 * Free the contents of an slang_T and the structure itself.
2427 */
2428 static void
2429slang_free(lp)
2430 slang_T *lp;
2431{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002432 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002433 vim_free(lp->sl_fname);
2434 slang_clear(lp);
2435 vim_free(lp);
2436}
2437
2438/*
2439 * Clear an slang_T so that the file can be reloaded.
2440 */
2441 static void
2442slang_clear(lp)
2443 slang_T *lp;
2444{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002445 garray_T *gap;
2446 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002447 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002448 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002449 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450
Bram Moolenaar51485f02005-06-04 21:55:20 +00002451 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002452 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002453 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002454 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002455 vim_free(lp->sl_pbyts);
2456 lp->sl_pbyts = NULL;
2457
Bram Moolenaar51485f02005-06-04 21:55:20 +00002458 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002459 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002460 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002461 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002462 vim_free(lp->sl_pidxs);
2463 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464
Bram Moolenaar4770d092006-01-12 23:22:24 +00002465 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002467 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2468 while (gap->ga_len > 0)
2469 {
2470 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2471 vim_free(ftp->ft_from);
2472 vim_free(ftp->ft_to);
2473 }
2474 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002476
2477 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002478 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002479 {
2480 /* "ga_len" is set to 1 without adding an item for latin1 */
2481 if (gap->ga_data != NULL)
2482 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2483 for (i = 0; i < gap->ga_len; ++i)
2484 vim_free(((int **)gap->ga_data)[i]);
2485 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002486 else
2487 /* SAL items: free salitem_T items */
2488 while (gap->ga_len > 0)
2489 {
2490 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2491 vim_free(smp->sm_lead);
2492 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2493 vim_free(smp->sm_to);
2494#ifdef FEAT_MBYTE
2495 vim_free(smp->sm_lead_w);
2496 vim_free(smp->sm_oneof_w);
2497 vim_free(smp->sm_to_w);
2498#endif
2499 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002500 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002502 for (i = 0; i < lp->sl_prefixcnt; ++i)
2503 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002504 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002505 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002506 lp->sl_prefprog = NULL;
2507
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002508 vim_free(lp->sl_info);
2509 lp->sl_info = NULL;
2510
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002511 vim_free(lp->sl_midword);
2512 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002513
Bram Moolenaar5195e452005-08-19 20:32:47 +00002514 vim_free(lp->sl_compprog);
2515 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002516 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002517 lp->sl_compprog = NULL;
2518 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002519 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002520
2521 vim_free(lp->sl_syllable);
2522 lp->sl_syllable = NULL;
2523 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002524
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002525 ga_clear_strings(&lp->sl_comppat);
2526
Bram Moolenaar4770d092006-01-12 23:22:24 +00002527 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2528 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002529
Bram Moolenaar4770d092006-01-12 23:22:24 +00002530#ifdef FEAT_MBYTE
2531 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002532#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002533
Bram Moolenaar4770d092006-01-12 23:22:24 +00002534 /* Clear info from .sug file. */
2535 slang_clear_sug(lp);
2536
Bram Moolenaar5195e452005-08-19 20:32:47 +00002537 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002538 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002539 lp->sl_compsylmax = MAXWLEN;
2540 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541}
2542
2543/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002544 * Clear the info from the .sug file in "lp".
2545 */
2546 static void
2547slang_clear_sug(lp)
2548 slang_T *lp;
2549{
2550 vim_free(lp->sl_sbyts);
2551 lp->sl_sbyts = NULL;
2552 vim_free(lp->sl_sidxs);
2553 lp->sl_sidxs = NULL;
2554 close_spellbuf(lp->sl_sugbuf);
2555 lp->sl_sugbuf = NULL;
2556 lp->sl_sugloaded = FALSE;
2557 lp->sl_sugtime = 0;
2558}
2559
2560/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002561 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002562 * Invoked through do_in_runtimepath().
2563 */
2564 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002565spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002566 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002567 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002568{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002569 spelload_T *slp = (spelload_T *)cookie;
2570 slang_T *slang;
2571
2572 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2573 if (slang != NULL)
2574 {
2575 /* When a previously loaded file has NOBREAK also use it for the
2576 * ".add" files. */
2577 if (slp->sl_nobreak && slang->sl_add)
2578 slang->sl_nobreak = TRUE;
2579 else if (slang->sl_nobreak)
2580 slp->sl_nobreak = TRUE;
2581
2582 slp->sl_slang = slang;
2583 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002584}
2585
2586/*
2587 * Load one spell file and store the info into a slang_T.
2588 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002589 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002590 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2591 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2592 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2593 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002594 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002595 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2596 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002597 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002598 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002599 static slang_T *
2600spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002601 char_u *fname;
2602 char_u *lang;
2603 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002604 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002605{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002606 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002607 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002608 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002610 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002612 char_u *save_sourcing_name = sourcing_name;
2613 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002614 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002615 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002616 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002617
Bram Moolenaarb765d632005-06-07 21:00:02 +00002618 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002619 if (fd == NULL)
2620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 if (!silent)
2622 EMSG2(_(e_notopen), fname);
2623 else if (p_verbose > 2)
2624 {
2625 verbose_enter();
2626 smsg((char_u *)e_notopen, fname);
2627 verbose_leave();
2628 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002629 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002630 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002631 if (p_verbose > 2)
2632 {
2633 verbose_enter();
2634 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2635 verbose_leave();
2636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002637
Bram Moolenaarb765d632005-06-07 21:00:02 +00002638 if (old_lp == NULL)
2639 {
2640 lp = slang_alloc(lang);
2641 if (lp == NULL)
2642 goto endFAIL;
2643
2644 /* Remember the file name, used to reload the file when it's updated. */
2645 lp->sl_fname = vim_strsave(fname);
2646 if (lp->sl_fname == NULL)
2647 goto endFAIL;
2648
2649 /* Check for .add.spl. */
2650 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2651 }
2652 else
2653 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002654
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002655 /* Set sourcing_name, so that error messages mention the file name. */
2656 sourcing_name = fname;
2657 sourcing_lnum = 0;
2658
Bram Moolenaar4770d092006-01-12 23:22:24 +00002659 /*
2660 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002661 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002662 for (i = 0; i < VIMSPELLMAGICL; ++i)
2663 buf[i] = getc(fd); /* <fileID> */
2664 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2665 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002666 EMSG(_("E757: This does not look like a spell file"));
2667 goto endFAIL;
2668 }
2669 c = getc(fd); /* <versionnr> */
2670 if (c < VIMSPELLVERSION)
2671 {
2672 EMSG(_("E771: Old spell file, needs to be updated"));
2673 goto endFAIL;
2674 }
2675 else if (c > VIMSPELLVERSION)
2676 {
2677 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002678 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002679 }
2680
Bram Moolenaar5195e452005-08-19 20:32:47 +00002681
2682 /*
2683 * <SECTIONS>: <section> ... <sectionend>
2684 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2685 */
2686 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002687 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002688 n = getc(fd); /* <sectionID> or <sectionend> */
2689 if (n == SN_END)
2690 break;
2691 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002692 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693 if (len < 0)
2694 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002695
Bram Moolenaar5195e452005-08-19 20:32:47 +00002696 res = 0;
2697 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002698 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 case SN_INFO:
2700 lp->sl_info = read_string(fd, len); /* <infotext> */
2701 if (lp->sl_info == NULL)
2702 goto endFAIL;
2703 break;
2704
Bram Moolenaar5195e452005-08-19 20:32:47 +00002705 case SN_REGION:
2706 res = read_region_section(fd, lp, len);
2707 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002708
Bram Moolenaar5195e452005-08-19 20:32:47 +00002709 case SN_CHARFLAGS:
2710 res = read_charflags_section(fd);
2711 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002712
Bram Moolenaar5195e452005-08-19 20:32:47 +00002713 case SN_MIDWORD:
2714 lp->sl_midword = read_string(fd, len); /* <midword> */
2715 if (lp->sl_midword == NULL)
2716 goto endFAIL;
2717 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002718
Bram Moolenaar5195e452005-08-19 20:32:47 +00002719 case SN_PREFCOND:
2720 res = read_prefcond_section(fd, lp);
2721 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002722
Bram Moolenaar5195e452005-08-19 20:32:47 +00002723 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002724 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2725 break;
2726
2727 case SN_REPSAL:
2728 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002729 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002730
Bram Moolenaar5195e452005-08-19 20:32:47 +00002731 case SN_SAL:
2732 res = read_sal_section(fd, lp);
2733 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002734
Bram Moolenaar5195e452005-08-19 20:32:47 +00002735 case SN_SOFO:
2736 res = read_sofo_section(fd, lp);
2737 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002738
Bram Moolenaar5195e452005-08-19 20:32:47 +00002739 case SN_MAP:
2740 p = read_string(fd, len); /* <mapstr> */
2741 if (p == NULL)
2742 goto endFAIL;
2743 set_map_str(lp, p);
2744 vim_free(p);
2745 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002746
Bram Moolenaar4770d092006-01-12 23:22:24 +00002747 case SN_WORDS:
2748 res = read_words_section(fd, lp, len);
2749 break;
2750
2751 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002752 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002753 break;
2754
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002755 case SN_NOSPLITSUGS:
2756 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2757 break;
2758
Bram Moolenaar5195e452005-08-19 20:32:47 +00002759 case SN_COMPOUND:
2760 res = read_compound(fd, lp, len);
2761 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002762
Bram Moolenaar78622822005-08-23 21:00:13 +00002763 case SN_NOBREAK:
2764 lp->sl_nobreak = TRUE;
2765 break;
2766
Bram Moolenaar5195e452005-08-19 20:32:47 +00002767 case SN_SYLLABLE:
2768 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2769 if (lp->sl_syllable == NULL)
2770 goto endFAIL;
2771 if (init_syl_tab(lp) == FAIL)
2772 goto endFAIL;
2773 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002774
Bram Moolenaar5195e452005-08-19 20:32:47 +00002775 default:
2776 /* Unsupported section. When it's required give an error
2777 * message. When it's not required skip the contents. */
2778 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002779 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002780 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002781 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002782 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002783 while (--len >= 0)
2784 if (getc(fd) < 0)
2785 goto truncerr;
2786 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002787 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002788someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002789 if (res == SP_FORMERROR)
2790 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002791 EMSG(_(e_format));
2792 goto endFAIL;
2793 }
2794 if (res == SP_TRUNCERROR)
2795 {
2796truncerr:
2797 EMSG(_(e_spell_trunc));
2798 goto endFAIL;
2799 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002800 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002801 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002802 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803
Bram Moolenaar4770d092006-01-12 23:22:24 +00002804 /* <LWORDTREE> */
2805 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2806 if (res != 0)
2807 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002808
Bram Moolenaar4770d092006-01-12 23:22:24 +00002809 /* <KWORDTREE> */
2810 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2811 if (res != 0)
2812 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002813
Bram Moolenaar4770d092006-01-12 23:22:24 +00002814 /* <PREFIXTREE> */
2815 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2816 lp->sl_prefixcnt);
2817 if (res != 0)
2818 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002819
Bram Moolenaarb765d632005-06-07 21:00:02 +00002820 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002821 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002822 {
2823 lp->sl_next = first_lang;
2824 first_lang = lp;
2825 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002826
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002827 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002828
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002829endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002830 if (lang != NULL)
2831 /* truncating the name signals the error to spell_load_lang() */
2832 *lang = NUL;
2833 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002834 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002835 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002836
2837endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002838 if (fd != NULL)
2839 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002840 sourcing_name = save_sourcing_name;
2841 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842
2843 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002844}
2845
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002846/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002847 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2848 */
2849 static int
2850get2c(fd)
2851 FILE *fd;
2852{
2853 long n;
2854
2855 n = getc(fd);
2856 n = (n << 8) + getc(fd);
2857 return n;
2858}
2859
2860/*
2861 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2862 */
2863 static int
2864get3c(fd)
2865 FILE *fd;
2866{
2867 long n;
2868
2869 n = getc(fd);
2870 n = (n << 8) + getc(fd);
2871 n = (n << 8) + getc(fd);
2872 return n;
2873}
2874
2875/*
2876 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2877 */
2878 static int
2879get4c(fd)
2880 FILE *fd;
2881{
2882 long n;
2883
2884 n = getc(fd);
2885 n = (n << 8) + getc(fd);
2886 n = (n << 8) + getc(fd);
2887 n = (n << 8) + getc(fd);
2888 return n;
2889}
2890
2891/*
2892 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2893 */
2894 static time_t
2895get8c(fd)
2896 FILE *fd;
2897{
2898 time_t n = 0;
2899 int i;
2900
2901 for (i = 0; i < 8; ++i)
2902 n = (n << 8) + getc(fd);
2903 return n;
2904}
2905
2906/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002907 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002908 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002909 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002910 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2911 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002912 */
2913 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002914read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002915 FILE *fd;
2916 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002917 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002918{
2919 int cnt = 0;
2920 int i;
2921 char_u *str;
2922
2923 /* read the length bytes, MSB first */
2924 for (i = 0; i < cnt_bytes; ++i)
2925 cnt = (cnt << 8) + getc(fd);
2926 if (cnt < 0)
2927 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002928 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002929 return NULL;
2930 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002931 *cntp = cnt;
2932 if (cnt == 0)
2933 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002934
Bram Moolenaar5195e452005-08-19 20:32:47 +00002935 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002936 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002937 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002938 return str;
2939}
2940
Bram Moolenaar7887d882005-07-01 22:33:52 +00002941/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002942 * Read a string of length "cnt" from "fd" into allocated memory.
2943 * Returns NULL when out of memory.
2944 */
2945 static char_u *
2946read_string(fd, cnt)
2947 FILE *fd;
2948 int cnt;
2949{
2950 char_u *str;
2951 int i;
2952
2953 /* allocate memory */
2954 str = alloc((unsigned)cnt + 1);
2955 if (str != NULL)
2956 {
2957 /* Read the string. Doesn't check for truncated file. */
2958 for (i = 0; i < cnt; ++i)
2959 str[i] = getc(fd);
2960 str[i] = NUL;
2961 }
2962 return str;
2963}
2964
2965/*
2966 * Read SN_REGION: <regionname> ...
2967 * Return SP_*ERROR flags.
2968 */
2969 static int
2970read_region_section(fd, lp, len)
2971 FILE *fd;
2972 slang_T *lp;
2973 int len;
2974{
2975 int i;
2976
2977 if (len > 16)
2978 return SP_FORMERROR;
2979 for (i = 0; i < len; ++i)
2980 lp->sl_regions[i] = getc(fd); /* <regionname> */
2981 lp->sl_regions[len] = NUL;
2982 return 0;
2983}
2984
2985/*
2986 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2987 * <folcharslen> <folchars>
2988 * Return SP_*ERROR flags.
2989 */
2990 static int
2991read_charflags_section(fd)
2992 FILE *fd;
2993{
2994 char_u *flags;
2995 char_u *fol;
2996 int flagslen, follen;
2997
2998 /* <charflagslen> <charflags> */
2999 flags = read_cnt_string(fd, 1, &flagslen);
3000 if (flagslen < 0)
3001 return flagslen;
3002
3003 /* <folcharslen> <folchars> */
3004 fol = read_cnt_string(fd, 2, &follen);
3005 if (follen < 0)
3006 {
3007 vim_free(flags);
3008 return follen;
3009 }
3010
3011 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3012 if (flags != NULL && fol != NULL)
3013 set_spell_charflags(flags, flagslen, fol);
3014
3015 vim_free(flags);
3016 vim_free(fol);
3017
3018 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3019 if ((flags == NULL) != (fol == NULL))
3020 return SP_FORMERROR;
3021 return 0;
3022}
3023
3024/*
3025 * Read SN_PREFCOND section.
3026 * Return SP_*ERROR flags.
3027 */
3028 static int
3029read_prefcond_section(fd, lp)
3030 FILE *fd;
3031 slang_T *lp;
3032{
3033 int cnt;
3034 int i;
3035 int n;
3036 char_u *p;
3037 char_u buf[MAXWLEN + 1];
3038
3039 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003040 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003041 if (cnt <= 0)
3042 return SP_FORMERROR;
3043
3044 lp->sl_prefprog = (regprog_T **)alloc_clear(
3045 (unsigned)sizeof(regprog_T *) * cnt);
3046 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003047 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003048 lp->sl_prefixcnt = cnt;
3049
3050 for (i = 0; i < cnt; ++i)
3051 {
3052 /* <prefcond> : <condlen> <condstr> */
3053 n = getc(fd); /* <condlen> */
3054 if (n < 0 || n >= MAXWLEN)
3055 return SP_FORMERROR;
3056
3057 /* When <condlen> is zero we have an empty condition. Otherwise
3058 * compile the regexp program used to check for the condition. */
3059 if (n > 0)
3060 {
3061 buf[0] = '^'; /* always match at one position only */
3062 p = buf + 1;
3063 while (n-- > 0)
3064 *p++ = getc(fd); /* <condstr> */
3065 *p = NUL;
3066 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3067 }
3068 }
3069 return 0;
3070}
3071
3072/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003073 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003074 * Return SP_*ERROR flags.
3075 */
3076 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003077read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003078 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003079 garray_T *gap;
3080 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003081{
3082 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003083 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003084 int i;
3085
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003086 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003087 if (cnt < 0)
3088 return SP_TRUNCERROR;
3089
Bram Moolenaar5195e452005-08-19 20:32:47 +00003090 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003091 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003092
3093 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3094 for (; gap->ga_len < cnt; ++gap->ga_len)
3095 {
3096 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3097 ftp->ft_from = read_cnt_string(fd, 1, &i);
3098 if (i < 0)
3099 return i;
3100 if (i == 0)
3101 return SP_FORMERROR;
3102 ftp->ft_to = read_cnt_string(fd, 1, &i);
3103 if (i <= 0)
3104 {
3105 vim_free(ftp->ft_from);
3106 if (i < 0)
3107 return i;
3108 return SP_FORMERROR;
3109 }
3110 }
3111
3112 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003113 for (i = 0; i < 256; ++i)
3114 first[i] = -1;
3115 for (i = 0; i < gap->ga_len; ++i)
3116 {
3117 ftp = &((fromto_T *)gap->ga_data)[i];
3118 if (first[*ftp->ft_from] == -1)
3119 first[*ftp->ft_from] = i;
3120 }
3121 return 0;
3122}
3123
3124/*
3125 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3126 * Return SP_*ERROR flags.
3127 */
3128 static int
3129read_sal_section(fd, slang)
3130 FILE *fd;
3131 slang_T *slang;
3132{
3133 int i;
3134 int cnt;
3135 garray_T *gap;
3136 salitem_T *smp;
3137 int ccnt;
3138 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003139 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003140
3141 slang->sl_sofo = FALSE;
3142
3143 i = getc(fd); /* <salflags> */
3144 if (i & SAL_F0LLOWUP)
3145 slang->sl_followup = TRUE;
3146 if (i & SAL_COLLAPSE)
3147 slang->sl_collapse = TRUE;
3148 if (i & SAL_REM_ACCENTS)
3149 slang->sl_rem_accents = TRUE;
3150
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003151 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003152 if (cnt < 0)
3153 return SP_TRUNCERROR;
3154
3155 gap = &slang->sl_sal;
3156 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003157 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003158 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159
3160 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3161 for (; gap->ga_len < cnt; ++gap->ga_len)
3162 {
3163 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3164 ccnt = getc(fd); /* <salfromlen> */
3165 if (ccnt < 0)
3166 return SP_TRUNCERROR;
3167 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003168 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003169 smp->sm_lead = p;
3170
3171 /* Read up to the first special char into sm_lead. */
3172 for (i = 0; i < ccnt; ++i)
3173 {
3174 c = getc(fd); /* <salfrom> */
3175 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3176 break;
3177 *p++ = c;
3178 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003179 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003180 *p++ = NUL;
3181
3182 /* Put (abc) chars in sm_oneof, if any. */
3183 if (c == '(')
3184 {
3185 smp->sm_oneof = p;
3186 for (++i; i < ccnt; ++i)
3187 {
3188 c = getc(fd); /* <salfrom> */
3189 if (c == ')')
3190 break;
3191 *p++ = c;
3192 }
3193 *p++ = NUL;
3194 if (++i < ccnt)
3195 c = getc(fd);
3196 }
3197 else
3198 smp->sm_oneof = NULL;
3199
3200 /* Any following chars go in sm_rules. */
3201 smp->sm_rules = p;
3202 if (i < ccnt)
3203 /* store the char we got while checking for end of sm_lead */
3204 *p++ = c;
3205 for (++i; i < ccnt; ++i)
3206 *p++ = getc(fd); /* <salfrom> */
3207 *p++ = NUL;
3208
3209 /* <saltolen> <salto> */
3210 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3211 if (ccnt < 0)
3212 {
3213 vim_free(smp->sm_lead);
3214 return ccnt;
3215 }
3216
3217#ifdef FEAT_MBYTE
3218 if (has_mbyte)
3219 {
3220 /* convert the multi-byte strings to wide char strings */
3221 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3222 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3223 if (smp->sm_oneof == NULL)
3224 smp->sm_oneof_w = NULL;
3225 else
3226 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3227 if (smp->sm_to == NULL)
3228 smp->sm_to_w = NULL;
3229 else
3230 smp->sm_to_w = mb_str2wide(smp->sm_to);
3231 if (smp->sm_lead_w == NULL
3232 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3233 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3234 {
3235 vim_free(smp->sm_lead);
3236 vim_free(smp->sm_to);
3237 vim_free(smp->sm_lead_w);
3238 vim_free(smp->sm_oneof_w);
3239 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003240 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003241 }
3242 }
3243#endif
3244 }
3245
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003246 if (gap->ga_len > 0)
3247 {
3248 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3249 * that we need to check the index every time. */
3250 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3251 if ((p = alloc(1)) == NULL)
3252 return SP_OTHERERROR;
3253 p[0] = NUL;
3254 smp->sm_lead = p;
3255 smp->sm_leadlen = 0;
3256 smp->sm_oneof = NULL;
3257 smp->sm_rules = p;
3258 smp->sm_to = NULL;
3259#ifdef FEAT_MBYTE
3260 if (has_mbyte)
3261 {
3262 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3263 smp->sm_leadlen = 0;
3264 smp->sm_oneof_w = NULL;
3265 smp->sm_to_w = NULL;
3266 }
3267#endif
3268 ++gap->ga_len;
3269 }
3270
Bram Moolenaar5195e452005-08-19 20:32:47 +00003271 /* Fill the first-index table. */
3272 set_sal_first(slang);
3273
3274 return 0;
3275}
3276
3277/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003278 * Read SN_WORDS: <word> ...
3279 * Return SP_*ERROR flags.
3280 */
3281 static int
3282read_words_section(fd, lp, len)
3283 FILE *fd;
3284 slang_T *lp;
3285 int len;
3286{
3287 int done = 0;
3288 int i;
3289 char_u word[MAXWLEN];
3290
3291 while (done < len)
3292 {
3293 /* Read one word at a time. */
3294 for (i = 0; ; ++i)
3295 {
3296 word[i] = getc(fd);
3297 if (word[i] == NUL)
3298 break;
3299 if (i == MAXWLEN - 1)
3300 return SP_FORMERROR;
3301 }
3302
3303 /* Init the count to 10. */
3304 count_common_word(lp, word, -1, 10);
3305 done += i + 1;
3306 }
3307 return 0;
3308}
3309
3310/*
3311 * Add a word to the hashtable of common words.
3312 * If it's already there then the counter is increased.
3313 */
3314 static void
3315count_common_word(lp, word, len, count)
3316 slang_T *lp;
3317 char_u *word;
3318 int len; /* word length, -1 for upto NUL */
3319 int count; /* 1 to count once, 10 to init */
3320{
3321 hash_T hash;
3322 hashitem_T *hi;
3323 wordcount_T *wc;
3324 char_u buf[MAXWLEN];
3325 char_u *p;
3326
3327 if (len == -1)
3328 p = word;
3329 else
3330 {
3331 vim_strncpy(buf, word, len);
3332 p = buf;
3333 }
3334
3335 hash = hash_hash(p);
3336 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3337 if (HASHITEM_EMPTY(hi))
3338 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003339 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003340 if (wc == NULL)
3341 return;
3342 STRCPY(wc->wc_word, p);
3343 wc->wc_count = count;
3344 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3345 }
3346 else
3347 {
3348 wc = HI2WC(hi);
3349 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3350 wc->wc_count = MAXWORDCOUNT;
3351 }
3352}
3353
3354/*
3355 * Adjust the score of common words.
3356 */
3357 static int
3358score_wordcount_adj(slang, score, word, split)
3359 slang_T *slang;
3360 int score;
3361 char_u *word;
3362 int split; /* word was split, less bonus */
3363{
3364 hashitem_T *hi;
3365 wordcount_T *wc;
3366 int bonus;
3367 int newscore;
3368
3369 hi = hash_find(&slang->sl_wordcount, word);
3370 if (!HASHITEM_EMPTY(hi))
3371 {
3372 wc = HI2WC(hi);
3373 if (wc->wc_count < SCORE_THRES2)
3374 bonus = SCORE_COMMON1;
3375 else if (wc->wc_count < SCORE_THRES3)
3376 bonus = SCORE_COMMON2;
3377 else
3378 bonus = SCORE_COMMON3;
3379 if (split)
3380 newscore = score - bonus / 2;
3381 else
3382 newscore = score - bonus;
3383 if (newscore < 0)
3384 return 0;
3385 return newscore;
3386 }
3387 return score;
3388}
3389
3390/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003391 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3392 * Return SP_*ERROR flags.
3393 */
3394 static int
3395read_sofo_section(fd, slang)
3396 FILE *fd;
3397 slang_T *slang;
3398{
3399 int cnt;
3400 char_u *from, *to;
3401 int res;
3402
3403 slang->sl_sofo = TRUE;
3404
3405 /* <sofofromlen> <sofofrom> */
3406 from = read_cnt_string(fd, 2, &cnt);
3407 if (cnt < 0)
3408 return cnt;
3409
3410 /* <sofotolen> <sofoto> */
3411 to = read_cnt_string(fd, 2, &cnt);
3412 if (cnt < 0)
3413 {
3414 vim_free(from);
3415 return cnt;
3416 }
3417
3418 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3419 if (from != NULL && to != NULL)
3420 res = set_sofo(slang, from, to);
3421 else if (from != NULL || to != NULL)
3422 res = SP_FORMERROR; /* only one of two strings is an error */
3423 else
3424 res = 0;
3425
3426 vim_free(from);
3427 vim_free(to);
3428 return res;
3429}
3430
3431/*
3432 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003433 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003434 * Returns SP_*ERROR flags.
3435 */
3436 static int
3437read_compound(fd, slang, len)
3438 FILE *fd;
3439 slang_T *slang;
3440 int len;
3441{
3442 int todo = len;
3443 int c;
3444 int atstart;
3445 char_u *pat;
3446 char_u *pp;
3447 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003448 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003449 int cnt;
3450 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003451
3452 if (todo < 2)
3453 return SP_FORMERROR; /* need at least two bytes */
3454
3455 --todo;
3456 c = getc(fd); /* <compmax> */
3457 if (c < 2)
3458 c = MAXWLEN;
3459 slang->sl_compmax = c;
3460
3461 --todo;
3462 c = getc(fd); /* <compminlen> */
3463 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003464 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003465 slang->sl_compminlen = c;
3466
3467 --todo;
3468 c = getc(fd); /* <compsylmax> */
3469 if (c < 1)
3470 c = MAXWLEN;
3471 slang->sl_compsylmax = c;
3472
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003473 c = getc(fd); /* <compoptions> */
3474 if (c != 0)
3475 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3476 else
3477 {
3478 --todo;
3479 c = getc(fd); /* only use the lower byte for now */
3480 --todo;
3481 slang->sl_compoptions = c;
3482
3483 gap = &slang->sl_comppat;
3484 c = get2c(fd); /* <comppatcount> */
3485 todo -= 2;
3486 ga_init2(gap, sizeof(char_u *), c);
3487 if (ga_grow(gap, c) == OK)
3488 while (--c >= 0)
3489 {
3490 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3491 read_cnt_string(fd, 1, &cnt);
3492 /* <comppatlen> <comppattext> */
3493 if (cnt < 0)
3494 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003495 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003496 }
3497 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003498 if (todo < 0)
3499 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003500
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003502 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003503 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3504 * Conversion to utf-8 may double the size. */
3505 c = todo * 2 + 7;
3506#ifdef FEAT_MBYTE
3507 if (enc_utf8)
3508 c += todo * 2;
3509#endif
3510 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003511 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003512 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003513
Bram Moolenaard12a1322005-08-21 22:08:24 +00003514 /* We also need a list of all flags that can appear at the start and one
3515 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003516 cp = alloc(todo + 1);
3517 if (cp == NULL)
3518 {
3519 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003520 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003521 }
3522 slang->sl_compstartflags = cp;
3523 *cp = NUL;
3524
Bram Moolenaard12a1322005-08-21 22:08:24 +00003525 ap = alloc(todo + 1);
3526 if (ap == NULL)
3527 {
3528 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003529 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003530 }
3531 slang->sl_compallflags = ap;
3532 *ap = NUL;
3533
Bram Moolenaar5195e452005-08-19 20:32:47 +00003534 pp = pat;
3535 *pp++ = '^';
3536 *pp++ = '\\';
3537 *pp++ = '(';
3538
3539 atstart = 1;
3540 while (todo-- > 0)
3541 {
3542 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003543
3544 /* Add all flags to "sl_compallflags". */
3545 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003546 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003547 {
3548 *ap++ = c;
3549 *ap = NUL;
3550 }
3551
Bram Moolenaar5195e452005-08-19 20:32:47 +00003552 if (atstart != 0)
3553 {
3554 /* At start of item: copy flags to "sl_compstartflags". For a
3555 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3556 if (c == '[')
3557 atstart = 2;
3558 else if (c == ']')
3559 atstart = 0;
3560 else
3561 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003562 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003563 {
3564 *cp++ = c;
3565 *cp = NUL;
3566 }
3567 if (atstart == 1)
3568 atstart = 0;
3569 }
3570 }
3571 if (c == '/') /* slash separates two items */
3572 {
3573 *pp++ = '\\';
3574 *pp++ = '|';
3575 atstart = 1;
3576 }
3577 else /* normal char, "[abc]" and '*' are copied as-is */
3578 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003579 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003580 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003581#ifdef FEAT_MBYTE
3582 if (enc_utf8)
3583 pp += mb_char2bytes(c, pp);
3584 else
3585#endif
3586 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003587 }
3588 }
3589
3590 *pp++ = '\\';
3591 *pp++ = ')';
3592 *pp++ = '$';
3593 *pp = NUL;
3594
3595 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3596 vim_free(pat);
3597 if (slang->sl_compprog == NULL)
3598 return SP_FORMERROR;
3599
3600 return 0;
3601}
3602
Bram Moolenaar6de68532005-08-24 22:08:48 +00003603/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003604 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003605 * Like strchr() but independent of locale.
3606 */
3607 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003608byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003609 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003610 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003611{
3612 char_u *p;
3613
3614 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003615 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003616 return TRUE;
3617 return FALSE;
3618}
3619
Bram Moolenaar5195e452005-08-19 20:32:47 +00003620#define SY_MAXLEN 30
3621typedef struct syl_item_S
3622{
3623 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3624 int sy_len;
3625} syl_item_T;
3626
3627/*
3628 * Truncate "slang->sl_syllable" at the first slash and put the following items
3629 * in "slang->sl_syl_items".
3630 */
3631 static int
3632init_syl_tab(slang)
3633 slang_T *slang;
3634{
3635 char_u *p;
3636 char_u *s;
3637 int l;
3638 syl_item_T *syl;
3639
3640 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3641 p = vim_strchr(slang->sl_syllable, '/');
3642 while (p != NULL)
3643 {
3644 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003645 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003646 break;
3647 s = p;
3648 p = vim_strchr(p, '/');
3649 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003650 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003651 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003652 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003653 if (l >= SY_MAXLEN)
3654 return SP_FORMERROR;
3655 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003656 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003657 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3658 + slang->sl_syl_items.ga_len++;
3659 vim_strncpy(syl->sy_chars, s, l);
3660 syl->sy_len = l;
3661 }
3662 return OK;
3663}
3664
3665/*
3666 * Count the number of syllables in "word".
3667 * When "word" contains spaces the syllables after the last space are counted.
3668 * Returns zero if syllables are not defines.
3669 */
3670 static int
3671count_syllables(slang, word)
3672 slang_T *slang;
3673 char_u *word;
3674{
3675 int cnt = 0;
3676 int skip = FALSE;
3677 char_u *p;
3678 int len;
3679 int i;
3680 syl_item_T *syl;
3681 int c;
3682
3683 if (slang->sl_syllable == NULL)
3684 return 0;
3685
3686 for (p = word; *p != NUL; p += len)
3687 {
3688 /* When running into a space reset counter. */
3689 if (*p == ' ')
3690 {
3691 len = 1;
3692 cnt = 0;
3693 continue;
3694 }
3695
3696 /* Find longest match of syllable items. */
3697 len = 0;
3698 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3699 {
3700 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3701 if (syl->sy_len > len
3702 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3703 len = syl->sy_len;
3704 }
3705 if (len != 0) /* found a match, count syllable */
3706 {
3707 ++cnt;
3708 skip = FALSE;
3709 }
3710 else
3711 {
3712 /* No recognized syllable item, at least a syllable char then? */
3713#ifdef FEAT_MBYTE
3714 c = mb_ptr2char(p);
3715 len = (*mb_ptr2len)(p);
3716#else
3717 c = *p;
3718 len = 1;
3719#endif
3720 if (vim_strchr(slang->sl_syllable, c) == NULL)
3721 skip = FALSE; /* No, search for next syllable */
3722 else if (!skip)
3723 {
3724 ++cnt; /* Yes, count it */
3725 skip = TRUE; /* don't count following syllable chars */
3726 }
3727 }
3728 }
3729 return cnt;
3730}
3731
3732/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003733 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003734 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003735 */
3736 static int
3737set_sofo(lp, from, to)
3738 slang_T *lp;
3739 char_u *from;
3740 char_u *to;
3741{
3742 int i;
3743
3744#ifdef FEAT_MBYTE
3745 garray_T *gap;
3746 char_u *s;
3747 char_u *p;
3748 int c;
3749 int *inp;
3750
3751 if (has_mbyte)
3752 {
3753 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3754 * characters. The index is the low byte of the character.
3755 * The list contains from-to pairs with a terminating NUL.
3756 * sl_sal_first[] is used for latin1 "from" characters. */
3757 gap = &lp->sl_sal;
3758 ga_init2(gap, sizeof(int *), 1);
3759 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003760 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003761 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3762 gap->ga_len = 256;
3763
3764 /* First count the number of items for each list. Temporarily use
3765 * sl_sal_first[] for this. */
3766 for (p = from, s = to; *p != NUL && *s != NUL; )
3767 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003768 c = mb_cptr2char_adv(&p);
3769 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003770 if (c >= 256)
3771 ++lp->sl_sal_first[c & 0xff];
3772 }
3773 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003774 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003775
3776 /* Allocate the lists. */
3777 for (i = 0; i < 256; ++i)
3778 if (lp->sl_sal_first[i] > 0)
3779 {
3780 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3781 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003782 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003783 ((int **)gap->ga_data)[i] = (int *)p;
3784 *(int *)p = 0;
3785 }
3786
3787 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3788 * list. */
3789 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3790 for (p = from, s = to; *p != NUL && *s != NUL; )
3791 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003792 c = mb_cptr2char_adv(&p);
3793 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003794 if (c >= 256)
3795 {
3796 /* Append the from-to chars at the end of the list with
3797 * the low byte. */
3798 inp = ((int **)gap->ga_data)[c & 0xff];
3799 while (*inp != 0)
3800 ++inp;
3801 *inp++ = c; /* from char */
3802 *inp++ = i; /* to char */
3803 *inp++ = NUL; /* NUL at the end */
3804 }
3805 else
3806 /* mapping byte to char is done in sl_sal_first[] */
3807 lp->sl_sal_first[c] = i;
3808 }
3809 }
3810 else
3811#endif
3812 {
3813 /* mapping bytes to bytes is done in sl_sal_first[] */
3814 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003815 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003816
3817 for (i = 0; to[i] != NUL; ++i)
3818 lp->sl_sal_first[from[i]] = to[i];
3819 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3820 }
3821
Bram Moolenaar5195e452005-08-19 20:32:47 +00003822 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003823}
3824
3825/*
3826 * Fill the first-index table for "lp".
3827 */
3828 static void
3829set_sal_first(lp)
3830 slang_T *lp;
3831{
3832 salfirst_T *sfirst;
3833 int i;
3834 salitem_T *smp;
3835 int c;
3836 garray_T *gap = &lp->sl_sal;
3837
3838 sfirst = lp->sl_sal_first;
3839 for (i = 0; i < 256; ++i)
3840 sfirst[i] = -1;
3841 smp = (salitem_T *)gap->ga_data;
3842 for (i = 0; i < gap->ga_len; ++i)
3843 {
3844#ifdef FEAT_MBYTE
3845 if (has_mbyte)
3846 /* Use the lowest byte of the first character. For latin1 it's
3847 * the character, for other encodings it should differ for most
3848 * characters. */
3849 c = *smp[i].sm_lead_w & 0xff;
3850 else
3851#endif
3852 c = *smp[i].sm_lead;
3853 if (sfirst[c] == -1)
3854 {
3855 sfirst[c] = i;
3856#ifdef FEAT_MBYTE
3857 if (has_mbyte)
3858 {
3859 int n;
3860
3861 /* Make sure all entries with this byte are following each
3862 * other. Move the ones that are in the wrong position. Do
3863 * keep the same ordering! */
3864 while (i + 1 < gap->ga_len
3865 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3866 /* Skip over entry with same index byte. */
3867 ++i;
3868
3869 for (n = 1; i + n < gap->ga_len; ++n)
3870 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3871 {
3872 salitem_T tsal;
3873
3874 /* Move entry with same index byte after the entries
3875 * we already found. */
3876 ++i;
3877 --n;
3878 tsal = smp[i + n];
3879 mch_memmove(smp + i + 1, smp + i,
3880 sizeof(salitem_T) * n);
3881 smp[i] = tsal;
3882 }
3883 }
3884#endif
3885 }
3886 }
3887}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003888
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003889#ifdef FEAT_MBYTE
3890/*
3891 * Turn a multi-byte string into a wide character string.
3892 * Return it in allocated memory (NULL for out-of-memory)
3893 */
3894 static int *
3895mb_str2wide(s)
3896 char_u *s;
3897{
3898 int *res;
3899 char_u *p;
3900 int i = 0;
3901
3902 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3903 if (res != NULL)
3904 {
3905 for (p = s; *p != NUL; )
3906 res[i++] = mb_ptr2char_adv(&p);
3907 res[i] = NUL;
3908 }
3909 return res;
3910}
3911#endif
3912
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003914 * Read a tree from the .spl or .sug file.
3915 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3916 * This is skipped when the tree has zero length.
3917 * Returns zero when OK, SP_ value for an error.
3918 */
3919 static int
3920spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3921 FILE *fd;
3922 char_u **bytsp;
3923 idx_T **idxsp;
3924 int prefixtree; /* TRUE for the prefix tree */
3925 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3926{
3927 int len;
3928 int idx;
3929 char_u *bp;
3930 idx_T *ip;
3931
3932 /* The tree size was computed when writing the file, so that we can
3933 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003934 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003935 if (len < 0)
3936 return SP_TRUNCERROR;
3937 if (len > 0)
3938 {
3939 /* Allocate the byte array. */
3940 bp = lalloc((long_u)len, TRUE);
3941 if (bp == NULL)
3942 return SP_OTHERERROR;
3943 *bytsp = bp;
3944
3945 /* Allocate the index array. */
3946 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3947 if (ip == NULL)
3948 return SP_OTHERERROR;
3949 *idxsp = ip;
3950
3951 /* Recursively read the tree and store it in the array. */
3952 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3953 if (idx < 0)
3954 return idx;
3955 }
3956 return 0;
3957}
3958
3959/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003960 * Read one row of siblings from the spell file and store it in the byte array
3961 * "byts" and index array "idxs". Recursively read the children.
3962 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003963 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003964 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003965 * Returns the index (>= 0) following the siblings.
3966 * Returns SP_TRUNCERROR if the file is shorter than expected.
3967 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003968 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003969 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003970read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003971 FILE *fd;
3972 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003973 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003974 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003975 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003976 int prefixtree; /* TRUE for reading PREFIXTREE */
3977 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003978{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003979 int len;
3980 int i;
3981 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003982 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003984 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003985#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003986
Bram Moolenaar51485f02005-06-04 21:55:20 +00003987 len = getc(fd); /* <siblingcount> */
3988 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003989 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003990
3991 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003992 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003993 byts[idx++] = len;
3994
3995 /* Read the byte values, flag/region bytes and shared indexes. */
3996 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003997 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003998 c = getc(fd); /* <byte> */
3999 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004000 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004001 if (c <= BY_SPECIAL)
4002 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004003 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004004 {
4005 /* No flags, all regions. */
4006 idxs[idx] = 0;
4007 c = 0;
4008 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004009 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004010 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004011 if (prefixtree)
4012 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004013 /* Read the optional pflags byte, the prefix ID and the
4014 * condition nr. In idxs[] store the prefix ID in the low
4015 * byte, the condition index shifted up 8 bits, the flags
4016 * shifted up 24 bits. */
4017 if (c == BY_FLAGS)
4018 c = getc(fd) << 24; /* <pflags> */
4019 else
4020 c = 0;
4021
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004022 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004023
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004024 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004025 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004026 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004027 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004028 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004029 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004030 {
4031 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004032 * idxs[] the flags go in the low two bytes, region above
4033 * that and prefix ID above the region. */
4034 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004036 if (c2 == BY_FLAGS2)
4037 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004038 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004039 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004040 if (c & WF_AFX)
4041 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004042 }
4043
Bram Moolenaar51485f02005-06-04 21:55:20 +00004044 idxs[idx] = c;
4045 c = 0;
4046 }
4047 else /* c == BY_INDEX */
4048 {
4049 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004050 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004051 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004052 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004053 idxs[idx] = n + SHARED_MASK;
4054 c = getc(fd); /* <xbyte> */
4055 }
4056 }
4057 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058 }
4059
Bram Moolenaar51485f02005-06-04 21:55:20 +00004060 /* Recursively read the children for non-shared siblings.
4061 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4062 * remove SHARED_MASK) */
4063 for (i = 1; i <= len; ++i)
4064 if (byts[startidx + i] != 0)
4065 {
4066 if (idxs[startidx + i] & SHARED_MASK)
4067 idxs[startidx + i] &= ~SHARED_MASK;
4068 else
4069 {
4070 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004071 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004072 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 if (idx < 0)
4074 break;
4075 }
4076 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077
Bram Moolenaar51485f02005-06-04 21:55:20 +00004078 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079}
4080
4081/*
4082 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004083 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084 */
4085 char_u *
4086did_set_spelllang(buf)
4087 buf_T *buf;
4088{
4089 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004090 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004091 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004092 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004093 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004094 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004095 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004097 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004099 int len;
4100 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004101 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004102 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004103 char_u *use_region = NULL;
4104 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004105 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004106 int i, j;
4107 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004108 static int recursive = FALSE;
4109 char_u *ret_msg = NULL;
4110 char_u *spl_copy;
4111
4112 /* We don't want to do this recursively. May happen when a language is
4113 * not available and the SpellFileMissing autocommand opens a new buffer
4114 * in which 'spell' is set. */
4115 if (recursive)
4116 return NULL;
4117 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004118
4119 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004120 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004121
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004122 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4123 * it under our fingers. */
4124 spl_copy = vim_strsave(buf->b_p_spl);
4125 if (spl_copy == NULL)
4126 goto theend;
4127
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004128 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004129 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004130 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004131 /* Get one language name. */
4132 copy_option_part(&splp, lang, MAXWLEN, ",");
4133
Bram Moolenaar5482f332005-04-17 20:18:43 +00004134 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004135 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004136
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004137 /* If the name ends in ".spl" use it as the name of the spell file.
4138 * If there is a region name let "region" point to it and remove it
4139 * from the name. */
4140 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4141 {
4142 filename = TRUE;
4143
Bram Moolenaarb6356332005-07-18 21:40:44 +00004144 /* Locate a region and remove it from the file name. */
4145 p = vim_strchr(gettail(lang), '_');
4146 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4147 && !ASCII_ISALPHA(p[3]))
4148 {
4149 vim_strncpy(region_cp, p + 1, 2);
4150 mch_memmove(p, p + 3, len - (p - lang) - 2);
4151 len -= 3;
4152 region = region_cp;
4153 }
4154 else
4155 dont_use_region = TRUE;
4156
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004157 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004158 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4159 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004160 break;
4161 }
4162 else
4163 {
4164 filename = FALSE;
4165 if (len > 3 && lang[len - 3] == '_')
4166 {
4167 region = lang + len - 2;
4168 len -= 3;
4169 lang[len] = NUL;
4170 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004171 else
4172 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004173
4174 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004175 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4176 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004177 break;
4178 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004179
Bram Moolenaarb6356332005-07-18 21:40:44 +00004180 if (region != NULL)
4181 {
4182 /* If the region differs from what was used before then don't
4183 * use it for 'spellfile'. */
4184 if (use_region != NULL && STRCMP(region, use_region) != 0)
4185 dont_use_region = TRUE;
4186 use_region = region;
4187 }
4188
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004189 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004190 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004191 {
4192 if (filename)
4193 (void)spell_load_file(lang, lang, NULL, FALSE);
4194 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004195 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004196 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004197#ifdef FEAT_AUTOCMD
4198 /* SpellFileMissing autocommands may do anything, including
4199 * destroying the buffer we are using... */
4200 if (!buf_valid(buf))
4201 {
4202 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4203 goto theend;
4204 }
4205#endif
4206 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004207 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004208
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004209 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004210 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004211 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004212 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4213 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4214 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004215 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004216 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004217 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004218 {
4219 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004220 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004221 if (c == REGION_ALL)
4222 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004223 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004224 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004225 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004226 /* This addition file is for other regions. */
4227 region_mask = 0;
4228 }
4229 else
4230 /* This is probably an error. Give a warning and
4231 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004232 smsg((char_u *)
4233 _("Warning: region %s not supported"),
4234 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004235 }
4236 else
4237 region_mask = 1 << c;
4238 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004239
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004240 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004241 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004242 if (ga_grow(&ga, 1) == FAIL)
4243 {
4244 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004245 ret_msg = e_outofmem;
4246 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004247 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004248 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004249 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4250 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004251 use_midword(slang, buf);
4252 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004253 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004254 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004255 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004256 }
4257
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004258 /* round 0: load int_wordlist, if possible.
4259 * round 1: load first name in 'spellfile'.
4260 * round 2: load second name in 'spellfile.
4261 * etc. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004262 spf = buf->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004263 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004265 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004266 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004267 /* Internal wordlist, if there is one. */
4268 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004269 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004270 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004271 }
4272 else
4273 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004274 /* One entry in 'spellfile'. */
4275 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4276 STRCAT(spf_name, ".spl");
4277
4278 /* If it was already found above then skip it. */
4279 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004280 {
4281 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4282 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004283 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004284 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004285 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004286 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004287 }
4288
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004289 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004290 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4291 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004295 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004296 * region name, the region is ignored otherwise. for int_wordlist
4297 * use an arbitrary name. */
4298 if (round == 0)
4299 STRCPY(lang, "internal wordlist");
4300 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004301 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004302 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004303 p = vim_strchr(lang, '.');
4304 if (p != NULL)
4305 *p = NUL; /* truncate at ".encoding.add" */
4306 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004308
4309 /* If one of the languages has NOBREAK we assume the addition
4310 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004311 if (slang != NULL && nobreak)
4312 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004314 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004316 region_mask = REGION_ALL;
4317 if (use_region != NULL && !dont_use_region)
4318 {
4319 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004320 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004321 if (c != REGION_ALL)
4322 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004323 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004324 /* This spell file is for other regions. */
4325 region_mask = 0;
4326 }
4327
4328 if (region_mask != 0)
4329 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004330 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4331 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4332 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004333 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4334 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 }
4338 }
4339
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004340 /* Everything is fine, store the new b_langp value. */
4341 ga_clear(&buf->b_langp);
4342 buf->b_langp = ga;
4343
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004344 /* For each language figure out what language to use for sound folding and
4345 * REP items. If the language doesn't support it itself use another one
4346 * with the same name. E.g. for "en-math" use "en". */
4347 for (i = 0; i < ga.ga_len; ++i)
4348 {
4349 lp = LANGP_ENTRY(ga, i);
4350
4351 /* sound folding */
4352 if (lp->lp_slang->sl_sal.ga_len > 0)
4353 /* language does sound folding itself */
4354 lp->lp_sallang = lp->lp_slang;
4355 else
4356 /* find first similar language that does sound folding */
4357 for (j = 0; j < ga.ga_len; ++j)
4358 {
4359 lp2 = LANGP_ENTRY(ga, j);
4360 if (lp2->lp_slang->sl_sal.ga_len > 0
4361 && STRNCMP(lp->lp_slang->sl_name,
4362 lp2->lp_slang->sl_name, 2) == 0)
4363 {
4364 lp->lp_sallang = lp2->lp_slang;
4365 break;
4366 }
4367 }
4368
4369 /* REP items */
4370 if (lp->lp_slang->sl_rep.ga_len > 0)
4371 /* language has REP items itself */
4372 lp->lp_replang = lp->lp_slang;
4373 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004374 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004375 for (j = 0; j < ga.ga_len; ++j)
4376 {
4377 lp2 = LANGP_ENTRY(ga, j);
4378 if (lp2->lp_slang->sl_rep.ga_len > 0
4379 && STRNCMP(lp->lp_slang->sl_name,
4380 lp2->lp_slang->sl_name, 2) == 0)
4381 {
4382 lp->lp_replang = lp2->lp_slang;
4383 break;
4384 }
4385 }
4386 }
4387
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004388theend:
4389 vim_free(spl_copy);
4390 recursive = FALSE;
4391 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392}
4393
4394/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004395 * Clear the midword characters for buffer "buf".
4396 */
4397 static void
4398clear_midword(buf)
4399 buf_T *buf;
4400{
4401 vim_memset(buf->b_spell_ismw, 0, 256);
4402#ifdef FEAT_MBYTE
4403 vim_free(buf->b_spell_ismw_mb);
4404 buf->b_spell_ismw_mb = NULL;
4405#endif
4406}
4407
4408/*
4409 * Use the "sl_midword" field of language "lp" for buffer "buf".
4410 * They add up to any currently used midword characters.
4411 */
4412 static void
4413use_midword(lp, buf)
4414 slang_T *lp;
4415 buf_T *buf;
4416{
4417 char_u *p;
4418
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004419 if (lp->sl_midword == NULL) /* there aren't any */
4420 return;
4421
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004422 for (p = lp->sl_midword; *p != NUL; )
4423#ifdef FEAT_MBYTE
4424 if (has_mbyte)
4425 {
4426 int c, l, n;
4427 char_u *bp;
4428
4429 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004430 l = (*mb_ptr2len)(p);
4431 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004432 buf->b_spell_ismw[c] = TRUE;
4433 else if (buf->b_spell_ismw_mb == NULL)
4434 /* First multi-byte char in "b_spell_ismw_mb". */
4435 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4436 else
4437 {
4438 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004439 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004440 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4441 if (bp != NULL)
4442 {
4443 vim_free(buf->b_spell_ismw_mb);
4444 buf->b_spell_ismw_mb = bp;
4445 vim_strncpy(bp + n, p, l);
4446 }
4447 }
4448 p += l;
4449 }
4450 else
4451#endif
4452 buf->b_spell_ismw[*p++] = TRUE;
4453}
4454
4455/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004456 * Find the region "region[2]" in "rp" (points to "sl_regions").
4457 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004458 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004459 */
4460 static int
4461find_region(rp, region)
4462 char_u *rp;
4463 char_u *region;
4464{
4465 int i;
4466
4467 for (i = 0; ; i += 2)
4468 {
4469 if (rp[i] == NUL)
4470 return REGION_ALL;
4471 if (rp[i] == region[0] && rp[i + 1] == region[1])
4472 break;
4473 }
4474 return i / 2;
4475}
4476
4477/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004478 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004480 * Word WF_ONECAP
4481 * W WORD WF_ALLCAP
4482 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483 */
4484 static int
4485captype(word, end)
4486 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004487 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004488{
4489 char_u *p;
4490 int c;
4491 int firstcap;
4492 int allcap;
4493 int past_second = FALSE; /* past second word char */
4494
4495 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004496 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004498 return 0; /* only non-word characters, illegal word */
4499#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004500 if (has_mbyte)
4501 c = mb_ptr2char_adv(&p);
4502 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004503#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004504 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004505 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004506
4507 /*
4508 * Need to check all letters to find a word with mixed upper/lower.
4509 * But a word with an upper char only at start is a ONECAP.
4510 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004511 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004512 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004513 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004514 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004515 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004516 {
4517 /* UUl -> KEEPCAP */
4518 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004519 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004520 allcap = FALSE;
4521 }
4522 else if (!allcap)
4523 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004524 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525 past_second = TRUE;
4526 }
4527
4528 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004529 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004530 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004531 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004532 return 0;
4533}
4534
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004535/*
4536 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4537 * capital. So that make_case_word() can turn WOrd into Word.
4538 * Add ALLCAP for "WOrD".
4539 */
4540 static int
4541badword_captype(word, end)
4542 char_u *word;
4543 char_u *end;
4544{
4545 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004546 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004547 int l, u;
4548 int first;
4549 char_u *p;
4550
4551 if (flags & WF_KEEPCAP)
4552 {
4553 /* Count the number of UPPER and lower case letters. */
4554 l = u = 0;
4555 first = FALSE;
4556 for (p = word; p < end; mb_ptr_adv(p))
4557 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004558 c = PTR2CHAR(p);
4559 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004560 {
4561 ++u;
4562 if (p == word)
4563 first = TRUE;
4564 }
4565 else
4566 ++l;
4567 }
4568
4569 /* If there are more UPPER than lower case letters suggest an
4570 * ALLCAP word. Otherwise, if the first letter is UPPER then
4571 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4572 * require three upper case letters. */
4573 if (u > l && u > 2)
4574 flags |= WF_ALLCAP;
4575 else if (first)
4576 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004577
4578 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4579 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004580 }
4581 return flags;
4582}
4583
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004584# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4585/*
4586 * Free all languages.
4587 */
4588 void
4589spell_free_all()
4590{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004591 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004592 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004593 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004594
4595 /* Go through all buffers and handle 'spelllang'. */
4596 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4597 ga_clear(&buf->b_langp);
4598
4599 while (first_lang != NULL)
4600 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004601 slang = first_lang;
4602 first_lang = slang->sl_next;
4603 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004604 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004605
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004606 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004607 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004608 /* Delete the internal wordlist and its .spl file */
4609 mch_remove(int_wordlist);
4610 int_wordlist_spl(fname);
4611 mch_remove(fname);
4612 vim_free(int_wordlist);
4613 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004614 }
4615
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004616 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004617
4618 vim_free(repl_to);
4619 repl_to = NULL;
4620 vim_free(repl_from);
4621 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004622}
4623# endif
4624
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004625# if defined(FEAT_MBYTE) || defined(PROTO)
4626/*
4627 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004628 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004629 */
4630 void
4631spell_reload()
4632{
4633 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004634 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004635
Bram Moolenaarea408852005-06-25 22:49:46 +00004636 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 init_spell_chartab();
4638
4639 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004640 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641
4642 /* Go through all buffers and handle 'spelllang'. */
4643 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4644 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004645 /* Only load the wordlists when 'spelllang' is set and there is a
4646 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004647 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004648 {
4649 FOR_ALL_WINDOWS(wp)
4650 if (wp->w_buffer == buf && wp->w_p_spell)
4651 {
4652 (void)did_set_spelllang(buf);
4653# ifdef FEAT_WINDOWS
4654 break;
4655# endif
4656 }
4657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004658 }
4659}
4660# endif
4661
Bram Moolenaarb765d632005-06-07 21:00:02 +00004662/*
4663 * Reload the spell file "fname" if it's loaded.
4664 */
4665 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004667 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004669{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004670 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004672
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004673 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004674 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004675 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004676 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004677 slang_clear(slang);
4678 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004679 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004680 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004681 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004683 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685
4686 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004687 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 if (added_word && !didit)
4689 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004690}
4691
4692
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004693/*
4694 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004695 */
4696
Bram Moolenaar51485f02005-06-04 21:55:20 +00004697#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004698 and .dic file. */
4699/*
4700 * Main structure to store the contents of a ".aff" file.
4701 */
4702typedef struct afffile_S
4703{
4704 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004705 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004706 unsigned af_rare; /* RARE ID for rare word */
4707 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004708 unsigned af_bad; /* BAD ID for banned word */
4709 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004710 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004711 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004712 unsigned af_comproot; /* COMPOUNDROOT ID */
4713 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4714 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004715 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004716 int af_pfxpostpone; /* postpone prefixes without chop string and
4717 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004718 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4719 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004720 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004721} afffile_T;
4722
Bram Moolenaar6de68532005-08-24 22:08:48 +00004723#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004724#define AFT_LONG 1 /* flags are two characters */
4725#define AFT_CAPLONG 2 /* flags are one or two characters */
4726#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004727
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004728typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004729/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4730struct affentry_S
4731{
4732 affentry_T *ae_next; /* next affix with same name/number */
4733 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4734 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004735 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004736 char_u *ae_cond; /* condition (NULL for ".") */
4737 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004738 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4739 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004740};
4741
Bram Moolenaar6de68532005-08-24 22:08:48 +00004742#ifdef FEAT_MBYTE
4743# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4744#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004745# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004746#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004747
Bram Moolenaar51485f02005-06-04 21:55:20 +00004748/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4749typedef struct affheader_S
4750{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004751 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4752 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4753 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004754 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004755 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004756 affentry_T *ah_first; /* first affix entry */
4757} affheader_T;
4758
4759#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4760
Bram Moolenaar6de68532005-08-24 22:08:48 +00004761/* Flag used in compound items. */
4762typedef struct compitem_S
4763{
4764 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4765 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4766 int ci_newID; /* affix ID after renumbering. */
4767} compitem_T;
4768
4769#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4770
Bram Moolenaar51485f02005-06-04 21:55:20 +00004771/*
4772 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004773 * the need to keep track of each allocated thing, everything is freed all at
4774 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004775 */
4776#define SBLOCKSIZE 16000 /* size of sb_data */
4777typedef struct sblock_S sblock_T;
4778struct sblock_S
4779{
4780 sblock_T *sb_next; /* next block in list */
4781 int sb_used; /* nr of bytes already in use */
4782 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004783};
4784
4785/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004786 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004787 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004788typedef struct wordnode_S wordnode_T;
4789struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004790{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004791 union /* shared to save space */
4792 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004793 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004794 int index; /* index in written nodes (valid after first
4795 round) */
4796 } wn_u1;
4797 union /* shared to save space */
4798 {
4799 wordnode_T *next; /* next node with same hash key */
4800 wordnode_T *wnode; /* parent node that will write this node */
4801 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004802 wordnode_T *wn_child; /* child (next byte in word) */
4803 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4804 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004805 int wn_refs; /* Nr. of references to this node. Only
4806 relevant for first node in a list of
4807 siblings, in following siblings it is
4808 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004810
4811 /* Info for when "wn_byte" is NUL.
4812 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4813 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4814 * "wn_region" the LSW of the wordnr. */
4815 char_u wn_affixID; /* supported/required prefix ID or 0 */
4816 short_u wn_flags; /* WF_ flags */
4817 short wn_region; /* region mask */
4818
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004819#ifdef SPELL_PRINTTREE
4820 int wn_nr; /* sequence nr for printing */
4821#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004822};
4823
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004824#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4825
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004827
Bram Moolenaar51485f02005-06-04 21:55:20 +00004828/*
4829 * Info used while reading the spell files.
4830 */
4831typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004832{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004833 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004834 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004835
Bram Moolenaar51485f02005-06-04 21:55:20 +00004836 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004837 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004838
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004839 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004840
Bram Moolenaar4770d092006-01-12 23:22:24 +00004841 long si_sugtree; /* creating the soundfolding trie */
4842
Bram Moolenaar51485f02005-06-04 21:55:20 +00004843 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004844 long si_blocks_cnt; /* memory blocks allocated */
4845 long si_compress_cnt; /* words to add before lowering
4846 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004847 wordnode_T *si_first_free; /* List of nodes that have been freed during
4848 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004849 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004850#ifdef SPELL_PRINTTREE
4851 int si_wordnode_nr; /* sequence nr for nodes */
4852#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004853 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004854
Bram Moolenaar51485f02005-06-04 21:55:20 +00004855 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004856 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004857 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004858 int si_region; /* region mask */
4859 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004860 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004861 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004862 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004863 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004864 int si_region_count; /* number of regions supported (1 when there
4865 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004866 char_u si_region_name[16]; /* region names; used only if
4867 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004868
4869 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004870 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004872 char_u *si_sofofr; /* SOFOFROM text */
4873 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004874 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004875 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 int si_followup; /* soundsalike: ? */
4877 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004878 hashtab_T si_commonwords; /* hashtable for common words */
4879 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 int si_rem_accents; /* soundsalike: remove accents */
4881 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004882 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004883 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004884 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004885 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004886 int si_compoptions; /* COMP_ flags */
4887 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4888 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004889 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004890 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004891 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004892 garray_T si_prefcond; /* table with conditions for postponed
4893 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004894 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004895 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004896} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004898static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004899static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004900static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004901static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4902static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4903static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004904static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004905static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4906static void aff_check_number __ARGS((int spinval, int affval, char *name));
4907static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004908static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004909static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4910static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004911static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004913static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004914static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004915static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004916static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004917static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004918static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4919static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4920static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004922static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004923static 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 +00004924static 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 +00004925static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004926static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004927static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4928static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4929static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004930static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004931static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004932static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004933static void clear_node __ARGS((wordnode_T *node));
4934static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004935static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4936static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4937static int sug_maketable __ARGS((spellinfo_T *spin));
4938static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4939static int offset2bytes __ARGS((int nr, char_u *buf));
4940static int bytes2offset __ARGS((char_u **pp));
4941static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004942static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004943static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004944static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004945
Bram Moolenaar53805d12005-08-01 07:08:33 +00004946/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4947 * but it must be negative to indicate the prefix tree to tree_add_word().
4948 * Use a negative number with the lower 8 bits zero. */
4949#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004950
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004951/* flags for "condit" argument of store_aff_word() */
4952#define CONDIT_COMB 1 /* affix must combine */
4953#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4954#define CONDIT_SUF 4 /* add a suffix for matching flags */
4955#define CONDIT_AFF 8 /* word already has an affix */
4956
Bram Moolenaar5195e452005-08-19 20:32:47 +00004957/*
4958 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4959 */
4960static long compress_start = 30000; /* memory / SBLOCKSIZE */
4961static long compress_inc = 100; /* memory / SBLOCKSIZE */
4962static long compress_added = 500000; /* word count */
4963
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004964#ifdef SPELL_PRINTTREE
4965/*
4966 * For debugging the tree code: print the current tree in a (more or less)
4967 * readable format, so that we can see what happens when adding a word and/or
4968 * compressing the tree.
4969 * Based on code from Olaf Seibert.
4970 */
4971#define PRINTLINESIZE 1000
4972#define PRINTWIDTH 6
4973
4974#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4975 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4976
4977static char line1[PRINTLINESIZE];
4978static char line2[PRINTLINESIZE];
4979static char line3[PRINTLINESIZE];
4980
4981 static void
4982spell_clear_flags(wordnode_T *node)
4983{
4984 wordnode_T *np;
4985
4986 for (np = node; np != NULL; np = np->wn_sibling)
4987 {
4988 np->wn_u1.index = FALSE;
4989 spell_clear_flags(np->wn_child);
4990 }
4991}
4992
4993 static void
4994spell_print_node(wordnode_T *node, int depth)
4995{
4996 if (node->wn_u1.index)
4997 {
4998 /* Done this node before, print the reference. */
4999 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5000 PRINTSOME(line2, depth, " ", 0, 0);
5001 PRINTSOME(line3, depth, " ", 0, 0);
5002 msg(line1);
5003 msg(line2);
5004 msg(line3);
5005 }
5006 else
5007 {
5008 node->wn_u1.index = TRUE;
5009
5010 if (node->wn_byte != NUL)
5011 {
5012 if (node->wn_child != NULL)
5013 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5014 else
5015 /* Cannot happen? */
5016 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5017 }
5018 else
5019 PRINTSOME(line1, depth, " $ ", 0, 0);
5020
5021 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5022
5023 if (node->wn_sibling != NULL)
5024 PRINTSOME(line3, depth, " | ", 0, 0);
5025 else
5026 PRINTSOME(line3, depth, " ", 0, 0);
5027
5028 if (node->wn_byte == NUL)
5029 {
5030 msg(line1);
5031 msg(line2);
5032 msg(line3);
5033 }
5034
5035 /* do the children */
5036 if (node->wn_byte != NUL && node->wn_child != NULL)
5037 spell_print_node(node->wn_child, depth + 1);
5038
5039 /* do the siblings */
5040 if (node->wn_sibling != NULL)
5041 {
5042 /* get rid of all parent details except | */
5043 STRCPY(line1, line3);
5044 STRCPY(line2, line3);
5045 spell_print_node(node->wn_sibling, depth);
5046 }
5047 }
5048}
5049
5050 static void
5051spell_print_tree(wordnode_T *root)
5052{
5053 if (root != NULL)
5054 {
5055 /* Clear the "wn_u1.index" fields, used to remember what has been
5056 * done. */
5057 spell_clear_flags(root);
5058
5059 /* Recursively print the tree. */
5060 spell_print_node(root, 0);
5061 }
5062}
5063#endif /* SPELL_PRINTTREE */
5064
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005065/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005066 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005067 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068 */
5069 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005070spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005071 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005072 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005073{
5074 FILE *fd;
5075 afffile_T *aff;
5076 char_u rline[MAXLINELEN];
5077 char_u *line;
5078 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005079#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005080 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005081 int itemcnt;
5082 char_u *p;
5083 int lnum = 0;
5084 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005085 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005086 int aff_todo = 0;
5087 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005088 char_u *low = NULL;
5089 char_u *fol = NULL;
5090 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005091 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005092 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005093 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005094 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005095 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005096 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005097 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005098 int compminlen = 0; /* COMPOUNDMIN value */
5099 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005100 int compoptions = 0; /* COMP_ flags */
5101 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005102 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005103 concatenated */
5104 char_u *midword = NULL; /* MIDWORD value */
5105 char_u *syllable = NULL; /* SYLLABLE value */
5106 char_u *sofofrom = NULL; /* SOFOFROM value */
5107 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005108
Bram Moolenaar51485f02005-06-04 21:55:20 +00005109 /*
5110 * Open the file.
5111 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005112 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005113 if (fd == NULL)
5114 {
5115 EMSG2(_(e_notopen), fname);
5116 return NULL;
5117 }
5118
Bram Moolenaar4770d092006-01-12 23:22:24 +00005119 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5120 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 /* Only do REP lines when not done in another .aff file already. */
5123 do_rep = spin->si_rep.ga_len == 0;
5124
Bram Moolenaar4770d092006-01-12 23:22:24 +00005125 /* Only do REPSAL lines when not done in another .aff file already. */
5126 do_repsal = spin->si_repsal.ga_len == 0;
5127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128 /* Only do SAL lines when not done in another .aff file already. */
5129 do_sal = spin->si_sal.ga_len == 0;
5130
5131 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005132 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133
Bram Moolenaar51485f02005-06-04 21:55:20 +00005134 /*
5135 * Allocate and init the afffile_T structure.
5136 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005137 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005138 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005139 {
5140 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005142 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005143 hash_init(&aff->af_pref);
5144 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005145 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005146
5147 /*
5148 * Read all the lines in the file one by one.
5149 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005150 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005151 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005152 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005153 ++lnum;
5154
5155 /* Skip comment lines. */
5156 if (*rline == '#')
5157 continue;
5158
5159 /* Convert from "SET" to 'encoding' when needed. */
5160 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005161#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005162 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005163 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005164 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005165 if (pc == NULL)
5166 {
5167 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5168 fname, lnum, rline);
5169 continue;
5170 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005171 line = pc;
5172 }
5173 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005174#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005175 {
5176 pc = NULL;
5177 line = rline;
5178 }
5179
5180 /* Split the line up in white separated items. Put a NUL after each
5181 * item. */
5182 itemcnt = 0;
5183 for (p = line; ; )
5184 {
5185 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5186 ++p;
5187 if (*p == NUL)
5188 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005189 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005190 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005191 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005192 /* A few items have arbitrary text argument, don't split them. */
5193 if (itemcnt == 2 && spell_info_item(items[0]))
5194 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5195 ++p;
5196 else
5197 while (*p > ' ') /* skip until white space or CR/NL */
5198 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005199 if (*p == NUL)
5200 break;
5201 *p++ = NUL;
5202 }
5203
5204 /* Handle non-empty lines. */
5205 if (itemcnt > 0)
5206 {
5207 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5208 && aff->af_enc == NULL)
5209 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005210#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005211 /* Setup for conversion from "ENC" to 'encoding'. */
5212 aff->af_enc = enc_canonize(items[1]);
5213 if (aff->af_enc != NULL && !spin->si_ascii
5214 && convert_setup(&spin->si_conv, aff->af_enc,
5215 p_enc) == FAIL)
5216 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5217 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005218 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005219#else
5220 smsg((char_u *)_("Conversion in %s not supported"), fname);
5221#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005222 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005223 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5224 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005225 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005226 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005227 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005228 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005229 aff->af_flagtype = AFT_NUM;
5230 else if (STRCMP(items[1], "caplong") == 0)
5231 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005232 else
5233 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5234 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005235 if (aff->af_rare != 0
5236 || aff->af_keepcase != 0
5237 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005238 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005239 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005240 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005241 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005242 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005243 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005244 || aff->af_suff.ht_used > 0
5245 || aff->af_pref.ht_used > 0)
5246 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5247 fname, lnum, items[1]);
5248 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005249 else if (spell_info_item(items[0]))
5250 {
5251 p = (char_u *)getroom(spin,
5252 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5253 + STRLEN(items[0])
5254 + STRLEN(items[1]) + 3, FALSE);
5255 if (p != NULL)
5256 {
5257 if (spin->si_info != NULL)
5258 {
5259 STRCPY(p, spin->si_info);
5260 STRCAT(p, "\n");
5261 }
5262 STRCAT(p, items[0]);
5263 STRCAT(p, " ");
5264 STRCAT(p, items[1]);
5265 spin->si_info = p;
5266 }
5267 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005268 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5269 && midword == NULL)
5270 {
5271 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005272 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005276 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005277 /* TODO: remove "RAR" later */
5278 else if ((STRCMP(items[0], "RAR") == 0
5279 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5280 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005281 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005282 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005283 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005284 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005285 /* TODO: remove "KEP" later */
5286 else if ((STRCMP(items[0], "KEP") == 0
5287 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5288 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005289 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005290 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005291 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005292 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005293 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5294 && aff->af_bad == 0)
5295 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005296 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5297 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005298 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005299 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5300 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005301 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005302 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5303 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005304 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005305 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5306 && aff->af_circumfix == 0)
5307 {
5308 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5309 fname, lnum);
5310 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005311 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5312 && aff->af_nosuggest == 0)
5313 {
5314 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5315 fname, lnum);
5316 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005317 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5318 && aff->af_needcomp == 0)
5319 {
5320 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5321 fname, lnum);
5322 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005323 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5324 && aff->af_comproot == 0)
5325 {
5326 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5327 fname, lnum);
5328 }
5329 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5330 && itemcnt == 2 && aff->af_compforbid == 0)
5331 {
5332 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5333 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005334 if (aff->af_pref.ht_used > 0)
5335 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5336 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005337 }
5338 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5339 && itemcnt == 2 && aff->af_comppermit == 0)
5340 {
5341 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5342 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005343 if (aff->af_pref.ht_used > 0)
5344 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5345 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005346 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005347 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005348 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005349 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005350 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005351 * "Na" into "Na+", "1234" into "1234+". */
5352 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005353 if (p != NULL)
5354 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005355 STRCPY(p, items[1]);
5356 STRCAT(p, "+");
5357 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005358 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005359 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005360 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005361 {
5362 /* Concatenate this string to previously defined ones, using a
5363 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005364 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005365 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005366 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005367 p = getroom(spin, l, FALSE);
5368 if (p != NULL)
5369 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005370 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005371 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005372 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005373 STRCAT(p, "/");
5374 }
5375 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005376 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005377 }
5378 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005379 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005380 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005381 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005382 compmax = atoi((char *)items[1]);
5383 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005384 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005385 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005386 }
5387 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005388 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005389 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005390 compminlen = atoi((char *)items[1]);
5391 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005392 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5393 fname, lnum, items[1]);
5394 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005395 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005396 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005397 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005398 compsylmax = atoi((char *)items[1]);
5399 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005400 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5401 fname, lnum, items[1]);
5402 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005403 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5404 {
5405 compoptions |= COMP_CHECKDUP;
5406 }
5407 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5408 {
5409 compoptions |= COMP_CHECKREP;
5410 }
5411 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5412 {
5413 compoptions |= COMP_CHECKCASE;
5414 }
5415 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5416 && itemcnt == 1)
5417 {
5418 compoptions |= COMP_CHECKTRIPLE;
5419 }
5420 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5421 && itemcnt == 2)
5422 {
5423 if (atoi((char *)items[1]) == 0)
5424 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5425 fname, lnum, items[1]);
5426 }
5427 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5428 && itemcnt == 3)
5429 {
5430 garray_T *gap = &spin->si_comppat;
5431 int i;
5432
5433 /* Only add the couple if it isn't already there. */
5434 for (i = 0; i < gap->ga_len - 1; i += 2)
5435 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5436 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5437 items[2]) == 0)
5438 break;
5439 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5440 {
5441 ((char_u **)(gap->ga_data))[gap->ga_len++]
5442 = getroom_save(spin, items[1]);
5443 ((char_u **)(gap->ga_data))[gap->ga_len++]
5444 = getroom_save(spin, items[2]);
5445 }
5446 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005447 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005448 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005449 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005450 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005451 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005452 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5453 {
5454 spin->si_nobreak = TRUE;
5455 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005456 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5457 {
5458 spin->si_nosplitsugs = TRUE;
5459 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005460 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5461 {
5462 spin->si_nosugfile = TRUE;
5463 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005464 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5465 {
5466 aff->af_pfxpostpone = TRUE;
5467 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005468 else if ((STRCMP(items[0], "PFX") == 0
5469 || STRCMP(items[0], "SFX") == 0)
5470 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005471 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005472 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005473 int lasti = 4;
5474 char_u key[AH_KEY_LEN];
5475
5476 if (*items[0] == 'P')
5477 tp = &aff->af_pref;
5478 else
5479 tp = &aff->af_suff;
5480
5481 /* Myspell allows the same affix name to be used multiple
5482 * times. The affix files that do this have an undocumented
5483 * "S" flag on all but the last block, thus we check for that
5484 * and store it in ah_follows. */
5485 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5486 hi = hash_find(tp, key);
5487 if (!HASHITEM_EMPTY(hi))
5488 {
5489 cur_aff = HI2AH(hi);
5490 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5491 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5492 fname, lnum, items[1]);
5493 if (!cur_aff->ah_follows)
5494 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5495 fname, lnum, items[1]);
5496 }
5497 else
5498 {
5499 /* New affix letter. */
5500 cur_aff = (affheader_T *)getroom(spin,
5501 sizeof(affheader_T), TRUE);
5502 if (cur_aff == NULL)
5503 break;
5504 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5505 fname, lnum);
5506 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5507 break;
5508 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005509 || cur_aff->ah_flag == aff->af_rare
5510 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005511 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005512 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005513 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005514 || cur_aff->ah_flag == aff->af_needcomp
5515 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005516 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00005517 fname, lnum, items[1]);
5518 STRCPY(cur_aff->ah_key, items[1]);
5519 hash_add(tp, cur_aff->ah_key);
5520
5521 cur_aff->ah_combine = (*items[2] == 'Y');
5522 }
5523
5524 /* Check for the "S" flag, which apparently means that another
5525 * block with the same affix name is following. */
5526 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5527 {
5528 ++lasti;
5529 cur_aff->ah_follows = TRUE;
5530 }
5531 else
5532 cur_aff->ah_follows = FALSE;
5533
Bram Moolenaar8db73182005-06-17 21:51:16 +00005534 /* Myspell allows extra text after the item, but that might
5535 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005536 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005537 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005538
Bram Moolenaar95529562005-08-25 21:21:38 +00005539 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005540 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5541 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005542
Bram Moolenaar95529562005-08-25 21:21:38 +00005543 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005544 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005545 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005546 {
5547 /* Use a new number in the .spl file later, to be able
5548 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005549 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005550 cur_aff->ah_newID = ++spin->si_newprefID;
5551
5552 /* We only really use ah_newID if the prefix is
5553 * postponed. We know that only after handling all
5554 * the items. */
5555 did_postpone_prefix = FALSE;
5556 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005557 else
5558 /* Did use the ID in a previous block. */
5559 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005560 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005561
Bram Moolenaar51485f02005-06-04 21:55:20 +00005562 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005563 }
5564 else if ((STRCMP(items[0], "PFX") == 0
5565 || STRCMP(items[0], "SFX") == 0)
5566 && aff_todo > 0
5567 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005568 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005569 {
5570 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005571 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005572 int lasti = 5;
5573
Bram Moolenaar8db73182005-06-17 21:51:16 +00005574 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005575 * mean mistakes go unnoticed. Require a comment-starter.
5576 * Hunspell uses a "-" item. */
5577 if (itemcnt > lasti && *items[lasti] != '#'
5578 && (STRCMP(items[lasti], "-") != 0
5579 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005580 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005581
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005582 /* New item for an affix letter. */
5583 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005584 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005585 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005586 if (aff_entry == NULL)
5587 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005588
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005589 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005590 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005591 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005592 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005593 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005594
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005595 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005596 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5597 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005598 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005599 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005600 aff_process_flags(aff, aff_entry);
5601 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005602 }
5603
Bram Moolenaar51485f02005-06-04 21:55:20 +00005604 /* Don't use an affix entry with non-ASCII characters when
5605 * "spin->si_ascii" is TRUE. */
5606 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005607 || has_non_ascii(aff_entry->ae_add)))
5608 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005609 aff_entry->ae_next = cur_aff->ah_first;
5610 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005611
5612 if (STRCMP(items[4], ".") != 0)
5613 {
5614 char_u buf[MAXLINELEN];
5615
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005616 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005617 if (*items[0] == 'P')
5618 sprintf((char *)buf, "^%s", items[4]);
5619 else
5620 sprintf((char *)buf, "%s$", items[4]);
5621 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005622 RE_MAGIC + RE_STRING + RE_STRICT);
5623 if (aff_entry->ae_prog == NULL)
5624 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5625 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005626 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005627
5628 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005629 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005630 * Can't be done for an affix with flags, ignoring
5631 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005632 if (*items[0] == 'P' && aff->af_pfxpostpone
5633 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005634 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005635 /* When the chop string is one lower-case letter and
5636 * the add string ends in the upper-case letter we set
5637 * the "upper" flag, clear "ae_chop" and remove the
5638 * letters from "ae_add". The condition must either
5639 * be empty or start with the same letter. */
5640 if (aff_entry->ae_chop != NULL
5641 && aff_entry->ae_add != NULL
5642#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005643 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005644 aff_entry->ae_chop)] == NUL
5645#else
5646 && aff_entry->ae_chop[1] == NUL
5647#endif
5648 )
5649 {
5650 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005651
Bram Moolenaar53805d12005-08-01 07:08:33 +00005652 c = PTR2CHAR(aff_entry->ae_chop);
5653 c_up = SPELL_TOUPPER(c);
5654 if (c_up != c
5655 && (aff_entry->ae_cond == NULL
5656 || PTR2CHAR(aff_entry->ae_cond) == c))
5657 {
5658 p = aff_entry->ae_add
5659 + STRLEN(aff_entry->ae_add);
5660 mb_ptr_back(aff_entry->ae_add, p);
5661 if (PTR2CHAR(p) == c_up)
5662 {
5663 upper = TRUE;
5664 aff_entry->ae_chop = NULL;
5665 *p = NUL;
5666
5667 /* The condition is matched with the
5668 * actual word, thus must check for the
5669 * upper-case letter. */
5670 if (aff_entry->ae_cond != NULL)
5671 {
5672 char_u buf[MAXLINELEN];
5673#ifdef FEAT_MBYTE
5674 if (has_mbyte)
5675 {
5676 onecap_copy(items[4], buf, TRUE);
5677 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005678 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005679 }
5680 else
5681#endif
5682 *aff_entry->ae_cond = c_up;
5683 if (aff_entry->ae_cond != NULL)
5684 {
5685 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005686 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005687 vim_free(aff_entry->ae_prog);
5688 aff_entry->ae_prog = vim_regcomp(
5689 buf, RE_MAGIC + RE_STRING);
5690 }
5691 }
5692 }
5693 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005694 }
5695
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005696 if (aff_entry->ae_chop == NULL
5697 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005698 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005699 int idx;
5700 char_u **pp;
5701 int n;
5702
Bram Moolenaar6de68532005-08-24 22:08:48 +00005703 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005704 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5705 --idx)
5706 {
5707 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5708 if (str_equal(p, aff_entry->ae_cond))
5709 break;
5710 }
5711 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5712 {
5713 /* Not found, add a new condition. */
5714 idx = spin->si_prefcond.ga_len++;
5715 pp = ((char_u **)spin->si_prefcond.ga_data)
5716 + idx;
5717 if (aff_entry->ae_cond == NULL)
5718 *pp = NULL;
5719 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005720 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005721 aff_entry->ae_cond);
5722 }
5723
5724 /* Add the prefix to the prefix tree. */
5725 if (aff_entry->ae_add == NULL)
5726 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005727 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005728 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005729
Bram Moolenaar53805d12005-08-01 07:08:33 +00005730 /* PFX_FLAGS is a negative number, so that
5731 * tree_add_word() knows this is the prefix tree. */
5732 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005733 if (!cur_aff->ah_combine)
5734 n |= WFP_NC;
5735 if (upper)
5736 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005737 if (aff_entry->ae_comppermit)
5738 n |= WFP_COMPPERMIT;
5739 if (aff_entry->ae_compforbid)
5740 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005741 tree_add_word(spin, p, spin->si_prefroot, n,
5742 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005743 did_postpone_prefix = TRUE;
5744 }
5745
5746 /* Didn't actually use ah_newID, backup si_newprefID. */
5747 if (aff_todo == 0 && !did_postpone_prefix)
5748 {
5749 --spin->si_newprefID;
5750 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005751 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005752 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005753 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005754 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005755 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5756 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005757 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005758 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005759 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005760 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5761 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005762 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005763 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005764 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005765 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5766 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005767 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005768 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005769 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005770 else if ((STRCMP(items[0], "REP") == 0
5771 || STRCMP(items[0], "REPSAL") == 0)
5772 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005773 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005774 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005775 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005776 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005777 fname, lnum);
5778 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005779 else if ((STRCMP(items[0], "REP") == 0
5780 || STRCMP(items[0], "REPSAL") == 0)
5781 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005782 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005783 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005784 /* Myspell ignores extra arguments, we require it starts with
5785 * # to detect mistakes. */
5786 if (itemcnt > 3 && items[3][0] != '#')
5787 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005788 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005789 {
5790 /* Replace underscore with space (can't include a space
5791 * directly). */
5792 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5793 if (*p == '_')
5794 *p = ' ';
5795 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5796 if (*p == '_')
5797 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005798 add_fromto(spin, items[0][3] == 'S'
5799 ? &spin->si_repsal
5800 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005802 }
5803 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5804 {
5805 /* MAP item or count */
5806 if (!found_map)
5807 {
5808 /* First line contains the count. */
5809 found_map = TRUE;
5810 if (!isdigit(*items[1]))
5811 smsg((char_u *)_("Expected MAP count in %s line %d"),
5812 fname, lnum);
5813 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005814 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005815 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005816 int c;
5817
5818 /* Check that every character appears only once. */
5819 for (p = items[1]; *p != NUL; )
5820 {
5821#ifdef FEAT_MBYTE
5822 c = mb_ptr2char_adv(&p);
5823#else
5824 c = *p++;
5825#endif
5826 if ((spin->si_map.ga_len > 0
5827 && vim_strchr(spin->si_map.ga_data, c)
5828 != NULL)
5829 || vim_strchr(p, c) != NULL)
5830 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5831 fname, lnum);
5832 }
5833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005834 /* We simply concatenate all the MAP strings, separated by
5835 * slashes. */
5836 ga_concat(&spin->si_map, items[1]);
5837 ga_append(&spin->si_map, '/');
5838 }
5839 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005840 /* Accept "SAL from to" and "SAL from to # comment". */
5841 else if (STRCMP(items[0], "SAL") == 0
5842 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005843 {
5844 if (do_sal)
5845 {
5846 /* SAL item (sounds-a-like)
5847 * Either one of the known keys or a from-to pair. */
5848 if (STRCMP(items[1], "followup") == 0)
5849 spin->si_followup = sal_to_bool(items[2]);
5850 else if (STRCMP(items[1], "collapse_result") == 0)
5851 spin->si_collapse = sal_to_bool(items[2]);
5852 else if (STRCMP(items[1], "remove_accents") == 0)
5853 spin->si_rem_accents = sal_to_bool(items[2]);
5854 else
5855 /* when "to" is "_" it means empty */
5856 add_fromto(spin, &spin->si_sal, items[1],
5857 STRCMP(items[2], "_") == 0 ? (char_u *)""
5858 : items[2]);
5859 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005860 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005861 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005862 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005863 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005864 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005865 }
5866 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005867 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005868 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005870 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005871 else if (STRCMP(items[0], "COMMON") == 0)
5872 {
5873 int i;
5874
5875 for (i = 1; i < itemcnt; ++i)
5876 {
5877 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5878 items[i])))
5879 {
5880 p = vim_strsave(items[i]);
5881 if (p == NULL)
5882 break;
5883 hash_add(&spin->si_commonwords, p);
5884 }
5885 }
5886 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005887 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005888 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005889 fname, lnum, items[0]);
5890 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005891 }
5892
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005893 if (fol != NULL || low != NULL || upp != NULL)
5894 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005895 if (spin->si_clear_chartab)
5896 {
5897 /* Clear the char type tables, don't want to use any of the
5898 * currently used spell properties. */
5899 init_spell_chartab();
5900 spin->si_clear_chartab = FALSE;
5901 }
5902
Bram Moolenaar3982c542005-06-08 21:56:31 +00005903 /*
5904 * Don't write a word table for an ASCII file, so that we don't check
5905 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005906 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005907 * mb_get_class(), the list of chars in the file will be incomplete.
5908 */
5909 if (!spin->si_ascii
5910#ifdef FEAT_MBYTE
5911 && !enc_utf8
5912#endif
5913 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005914 {
5915 if (fol == NULL || low == NULL || upp == NULL)
5916 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5917 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005918 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005919 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005920
5921 vim_free(fol);
5922 vim_free(low);
5923 vim_free(upp);
5924 }
5925
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005926 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005927 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005928 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005929 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005930 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005931 }
5932
Bram Moolenaar6de68532005-08-24 22:08:48 +00005933 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005934 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005935 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5936 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005937 }
5938
Bram Moolenaar6de68532005-08-24 22:08:48 +00005939 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005940 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005941 if (syllable == NULL)
5942 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5943 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5944 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005945 }
5946
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005947 if (compoptions != 0)
5948 {
5949 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5950 spin->si_compoptions |= compoptions;
5951 }
5952
Bram Moolenaar6de68532005-08-24 22:08:48 +00005953 if (compflags != NULL)
5954 process_compflags(spin, aff, compflags);
5955
5956 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005957 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005958 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005959 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005960 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005961 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005962 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005963 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005964 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005965 }
5966
Bram Moolenaar6de68532005-08-24 22:08:48 +00005967 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005968 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005969 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5970 spin->si_syllable = syllable;
5971 }
5972
5973 if (sofofrom != NULL || sofoto != NULL)
5974 {
5975 if (sofofrom == NULL || sofoto == NULL)
5976 smsg((char_u *)_("Missing SOFO%s line in %s"),
5977 sofofrom == NULL ? "FROM" : "TO", fname);
5978 else if (spin->si_sal.ga_len > 0)
5979 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005980 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005981 {
5982 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5983 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5984 spin->si_sofofr = sofofrom;
5985 spin->si_sofoto = sofoto;
5986 }
5987 }
5988
5989 if (midword != NULL)
5990 {
5991 aff_check_string(spin->si_midword, midword, "MIDWORD");
5992 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005993 }
5994
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005995 vim_free(pc);
5996 fclose(fd);
5997 return aff;
5998}
5999
6000/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006001 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6002 * ae_flags to ae_comppermit and ae_compforbid.
6003 */
6004 static void
6005aff_process_flags(affile, entry)
6006 afffile_T *affile;
6007 affentry_T *entry;
6008{
6009 char_u *p;
6010 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006011 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006012
6013 if (entry->ae_flags != NULL
6014 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6015 {
6016 for (p = entry->ae_flags; *p != NUL; )
6017 {
6018 prevp = p;
6019 flag = get_affitem(affile->af_flagtype, &p);
6020 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6021 {
6022 mch_memmove(prevp, p, STRLEN(p) + 1);
6023 p = prevp;
6024 if (flag == affile->af_comppermit)
6025 entry->ae_comppermit = TRUE;
6026 else
6027 entry->ae_compforbid = TRUE;
6028 }
6029 if (affile->af_flagtype == AFT_NUM && *p == ',')
6030 ++p;
6031 }
6032 if (*entry->ae_flags == NUL)
6033 entry->ae_flags = NULL; /* nothing left */
6034 }
6035}
6036
6037/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038 * Return TRUE if "s" is the name of an info item in the affix file.
6039 */
6040 static int
6041spell_info_item(s)
6042 char_u *s;
6043{
6044 return STRCMP(s, "NAME") == 0
6045 || STRCMP(s, "HOME") == 0
6046 || STRCMP(s, "VERSION") == 0
6047 || STRCMP(s, "AUTHOR") == 0
6048 || STRCMP(s, "EMAIL") == 0
6049 || STRCMP(s, "COPYRIGHT") == 0;
6050}
6051
6052/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006053 * Turn an affix flag name into a number, according to the FLAG type.
6054 * returns zero for failure.
6055 */
6056 static unsigned
6057affitem2flag(flagtype, item, fname, lnum)
6058 int flagtype;
6059 char_u *item;
6060 char_u *fname;
6061 int lnum;
6062{
6063 unsigned res;
6064 char_u *p = item;
6065
6066 res = get_affitem(flagtype, &p);
6067 if (res == 0)
6068 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006069 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006070 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6071 fname, lnum, item);
6072 else
6073 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6074 fname, lnum, item);
6075 }
6076 if (*p != NUL)
6077 {
6078 smsg((char_u *)_(e_affname), fname, lnum, item);
6079 return 0;
6080 }
6081
6082 return res;
6083}
6084
6085/*
6086 * Get one affix name from "*pp" and advance the pointer.
6087 * Returns zero for an error, still advances the pointer then.
6088 */
6089 static unsigned
6090get_affitem(flagtype, pp)
6091 int flagtype;
6092 char_u **pp;
6093{
6094 int res;
6095
Bram Moolenaar95529562005-08-25 21:21:38 +00006096 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006097 {
6098 if (!VIM_ISDIGIT(**pp))
6099 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006100 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006101 return 0;
6102 }
6103 res = getdigits(pp);
6104 }
6105 else
6106 {
6107#ifdef FEAT_MBYTE
6108 res = mb_ptr2char_adv(pp);
6109#else
6110 res = *(*pp)++;
6111#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006112 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006113 && res >= 'A' && res <= 'Z'))
6114 {
6115 if (**pp == NUL)
6116 return 0;
6117#ifdef FEAT_MBYTE
6118 res = mb_ptr2char_adv(pp) + (res << 16);
6119#else
6120 res = *(*pp)++ + (res << 16);
6121#endif
6122 }
6123 }
6124 return res;
6125}
6126
6127/*
6128 * Process the "compflags" string used in an affix file and append it to
6129 * spin->si_compflags.
6130 * The processing involves changing the affix names to ID numbers, so that
6131 * they fit in one byte.
6132 */
6133 static void
6134process_compflags(spin, aff, compflags)
6135 spellinfo_T *spin;
6136 afffile_T *aff;
6137 char_u *compflags;
6138{
6139 char_u *p;
6140 char_u *prevp;
6141 unsigned flag;
6142 compitem_T *ci;
6143 int id;
6144 int len;
6145 char_u *tp;
6146 char_u key[AH_KEY_LEN];
6147 hashitem_T *hi;
6148
6149 /* Make room for the old and the new compflags, concatenated with a / in
6150 * between. Processing it makes it shorter, but we don't know by how
6151 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006153 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006154 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006155 p = getroom(spin, len, FALSE);
6156 if (p == NULL)
6157 return;
6158 if (spin->si_compflags != NULL)
6159 {
6160 STRCPY(p, spin->si_compflags);
6161 STRCAT(p, "/");
6162 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006163 spin->si_compflags = p;
6164 tp = p + STRLEN(p);
6165
6166 for (p = compflags; *p != NUL; )
6167 {
6168 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6169 /* Copy non-flag characters directly. */
6170 *tp++ = *p++;
6171 else
6172 {
6173 /* First get the flag number, also checks validity. */
6174 prevp = p;
6175 flag = get_affitem(aff->af_flagtype, &p);
6176 if (flag != 0)
6177 {
6178 /* Find the flag in the hashtable. If it was used before, use
6179 * the existing ID. Otherwise add a new entry. */
6180 vim_strncpy(key, prevp, p - prevp);
6181 hi = hash_find(&aff->af_comp, key);
6182 if (!HASHITEM_EMPTY(hi))
6183 id = HI2CI(hi)->ci_newID;
6184 else
6185 {
6186 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6187 if (ci == NULL)
6188 break;
6189 STRCPY(ci->ci_key, key);
6190 ci->ci_flag = flag;
6191 /* Avoid using a flag ID that has a special meaning in a
6192 * regexp (also inside []). */
6193 do
6194 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006195 check_renumber(spin);
6196 id = spin->si_newcompID--;
6197 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006198 ci->ci_newID = id;
6199 hash_add(&aff->af_comp, ci->ci_key);
6200 }
6201 *tp++ = id;
6202 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006203 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006204 ++p;
6205 }
6206 }
6207
6208 *tp = NUL;
6209}
6210
6211/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006212 * Check that the new IDs for postponed affixes and compounding don't overrun
6213 * each other. We have almost 255 available, but start at 0-127 to avoid
6214 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6215 * When that is used up an error message is given.
6216 */
6217 static void
6218check_renumber(spin)
6219 spellinfo_T *spin;
6220{
6221 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6222 {
6223 spin->si_newprefID = 127;
6224 spin->si_newcompID = 255;
6225 }
6226}
6227
6228/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006229 * Return TRUE if flag "flag" appears in affix list "afflist".
6230 */
6231 static int
6232flag_in_afflist(flagtype, afflist, flag)
6233 int flagtype;
6234 char_u *afflist;
6235 unsigned flag;
6236{
6237 char_u *p;
6238 unsigned n;
6239
6240 switch (flagtype)
6241 {
6242 case AFT_CHAR:
6243 return vim_strchr(afflist, flag) != NULL;
6244
Bram Moolenaar95529562005-08-25 21:21:38 +00006245 case AFT_CAPLONG:
6246 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006247 for (p = afflist; *p != NUL; )
6248 {
6249#ifdef FEAT_MBYTE
6250 n = mb_ptr2char_adv(&p);
6251#else
6252 n = *p++;
6253#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006254 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006255 && *p != NUL)
6256#ifdef FEAT_MBYTE
6257 n = mb_ptr2char_adv(&p) + (n << 16);
6258#else
6259 n = *p++ + (n << 16);
6260#endif
6261 if (n == flag)
6262 return TRUE;
6263 }
6264 break;
6265
Bram Moolenaar95529562005-08-25 21:21:38 +00006266 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006267 for (p = afflist; *p != NUL; )
6268 {
6269 n = getdigits(&p);
6270 if (n == flag)
6271 return TRUE;
6272 if (*p != NUL) /* skip over comma */
6273 ++p;
6274 }
6275 break;
6276 }
6277 return FALSE;
6278}
6279
6280/*
6281 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6282 */
6283 static void
6284aff_check_number(spinval, affval, name)
6285 int spinval;
6286 int affval;
6287 char *name;
6288{
6289 if (spinval != 0 && spinval != affval)
6290 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6291}
6292
6293/*
6294 * Give a warning when "spinval" and "affval" strings are set and not the same.
6295 */
6296 static void
6297aff_check_string(spinval, affval, name)
6298 char_u *spinval;
6299 char_u *affval;
6300 char *name;
6301{
6302 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6303 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6304}
6305
6306/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006307 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6308 * NULL as equal.
6309 */
6310 static int
6311str_equal(s1, s2)
6312 char_u *s1;
6313 char_u *s2;
6314{
6315 if (s1 == NULL || s2 == NULL)
6316 return s1 == s2;
6317 return STRCMP(s1, s2) == 0;
6318}
6319
6320/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006321 * Add a from-to item to "gap". Used for REP and SAL items.
6322 * They are stored case-folded.
6323 */
6324 static void
6325add_fromto(spin, gap, from, to)
6326 spellinfo_T *spin;
6327 garray_T *gap;
6328 char_u *from;
6329 char_u *to;
6330{
6331 fromto_T *ftp;
6332 char_u word[MAXWLEN];
6333
6334 if (ga_grow(gap, 1) == OK)
6335 {
6336 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006337 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006338 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006339 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006340 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006341 ++gap->ga_len;
6342 }
6343}
6344
6345/*
6346 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6347 */
6348 static int
6349sal_to_bool(s)
6350 char_u *s;
6351{
6352 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6353}
6354
6355/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006356 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6357 * When "s" is NULL FALSE is returned.
6358 */
6359 static int
6360has_non_ascii(s)
6361 char_u *s;
6362{
6363 char_u *p;
6364
6365 if (s != NULL)
6366 for (p = s; *p != NUL; ++p)
6367 if (*p >= 128)
6368 return TRUE;
6369 return FALSE;
6370}
6371
6372/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006373 * Free the structure filled by spell_read_aff().
6374 */
6375 static void
6376spell_free_aff(aff)
6377 afffile_T *aff;
6378{
6379 hashtab_T *ht;
6380 hashitem_T *hi;
6381 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006382 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006383 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006384
6385 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006386
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006387 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006388 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6389 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006390 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006391 for (hi = ht->ht_array; todo > 0; ++hi)
6392 {
6393 if (!HASHITEM_EMPTY(hi))
6394 {
6395 --todo;
6396 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006397 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6398 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006399 }
6400 }
6401 if (ht == &aff->af_suff)
6402 break;
6403 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006404
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006405 hash_clear(&aff->af_pref);
6406 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006407 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006408}
6409
6410/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006411 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006412 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006413 */
6414 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006415spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006416 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006417 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006418 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006419{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006420 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006421 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006422 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006423 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006424 char_u store_afflist[MAXWLEN];
6425 int pfxlen;
6426 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006427 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006428 char_u *pc;
6429 char_u *w;
6430 int l;
6431 hash_T hash;
6432 hashitem_T *hi;
6433 FILE *fd;
6434 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006435 int non_ascii = 0;
6436 int retval = OK;
6437 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006438 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006439 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006440
Bram Moolenaar51485f02005-06-04 21:55:20 +00006441 /*
6442 * Open the file.
6443 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006444 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006445 if (fd == NULL)
6446 {
6447 EMSG2(_(e_notopen), fname);
6448 return FAIL;
6449 }
6450
Bram Moolenaar51485f02005-06-04 21:55:20 +00006451 /* The hashtable is only used to detect duplicated words. */
6452 hash_init(&ht);
6453
Bram Moolenaar4770d092006-01-12 23:22:24 +00006454 vim_snprintf((char *)IObuff, IOSIZE,
6455 _("Reading dictionary file %s ..."), fname);
6456 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006457
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006458 /* start with a message for the first line */
6459 spin->si_msg_count = 999999;
6460
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006461 /* Read and ignore the first line: word count. */
6462 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006463 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006464 EMSG2(_("E760: No word count in %s"), fname);
6465
6466 /*
6467 * Read all the lines in the file one by one.
6468 * The words are converted to 'encoding' here, before being added to
6469 * the hashtable.
6470 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006471 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006472 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006473 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006474 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006475 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006476 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006477
Bram Moolenaar51485f02005-06-04 21:55:20 +00006478 /* Remove CR, LF and white space from the end. White space halfway
6479 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006480 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006481 while (l > 0 && line[l - 1] <= ' ')
6482 --l;
6483 if (l == 0)
6484 continue; /* empty line */
6485 line[l] = NUL;
6486
Bram Moolenaarb765d632005-06-07 21:00:02 +00006487#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006488 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006489 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006490 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006491 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006492 if (pc == NULL)
6493 {
6494 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6495 fname, lnum, line);
6496 continue;
6497 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006498 w = pc;
6499 }
6500 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006501#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006502 {
6503 pc = NULL;
6504 w = line;
6505 }
6506
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006507 /* Truncate the word at the "/", set "afflist" to what follows.
6508 * Replace "\/" by "/" and "\\" by "\". */
6509 afflist = NULL;
6510 for (p = w; *p != NUL; mb_ptr_adv(p))
6511 {
6512 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6513 mch_memmove(p, p + 1, STRLEN(p));
6514 else if (*p == '/')
6515 {
6516 *p = NUL;
6517 afflist = p + 1;
6518 break;
6519 }
6520 }
6521
6522 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6523 if (spin->si_ascii && has_non_ascii(w))
6524 {
6525 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006526 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006527 continue;
6528 }
6529
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006530 /* This takes time, print a message every 10000 words. */
6531 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006532 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006533 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006534 vim_snprintf((char *)message, sizeof(message),
6535 _("line %6d, word %6d - %s"),
6536 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6537 msg_start();
6538 msg_puts_long_attr(message, 0);
6539 msg_clr_eos();
6540 msg_didout = FALSE;
6541 msg_col = 0;
6542 out_flush();
6543 }
6544
Bram Moolenaar51485f02005-06-04 21:55:20 +00006545 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006546 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006547 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006548 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006549 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006550 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006551 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006552 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006553
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 hash = hash_hash(dw);
6555 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006556 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006557 {
6558 if (p_verbose > 0)
6559 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006560 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006561 else if (duplicate == 0)
6562 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6563 fname, lnum, dw);
6564 ++duplicate;
6565 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006566 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006567 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006568
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006569 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006570 store_afflist[0] = NUL;
6571 pfxlen = 0;
6572 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006573 if (afflist != NULL)
6574 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006575 /* Extract flags from the affix list. */
6576 flags |= get_affix_flags(affile, afflist);
6577
Bram Moolenaar6de68532005-08-24 22:08:48 +00006578 if (affile->af_needaffix != 0 && flag_in_afflist(
6579 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006580 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006581
6582 if (affile->af_pfxpostpone)
6583 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006584 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006585
Bram Moolenaar5195e452005-08-19 20:32:47 +00006586 if (spin->si_compflags != NULL)
6587 /* Need to store the list of compound flags with the word.
6588 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006589 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006590 }
6591
Bram Moolenaar51485f02005-06-04 21:55:20 +00006592 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006593 if (store_word(spin, dw, flags, spin->si_region,
6594 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006595 retval = FAIL;
6596
6597 if (afflist != NULL)
6598 {
6599 /* Find all matching suffixes and add the resulting words.
6600 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006601 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006602 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006603 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006604 retval = FAIL;
6605
6606 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006607 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006608 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006609 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006610 retval = FAIL;
6611 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006612
6613 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006614 }
6615
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006616 if (duplicate > 0)
6617 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006618 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006619 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6620 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006621 hash_clear(&ht);
6622
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006623 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006624 return retval;
6625}
6626
6627/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006628 * Check for affix flags in "afflist" that are turned into word flags.
6629 * Return WF_ flags.
6630 */
6631 static int
6632get_affix_flags(affile, afflist)
6633 afffile_T *affile;
6634 char_u *afflist;
6635{
6636 int flags = 0;
6637
6638 if (affile->af_keepcase != 0 && flag_in_afflist(
6639 affile->af_flagtype, afflist, affile->af_keepcase))
6640 flags |= WF_KEEPCAP | WF_FIXCAP;
6641 if (affile->af_rare != 0 && flag_in_afflist(
6642 affile->af_flagtype, afflist, affile->af_rare))
6643 flags |= WF_RARE;
6644 if (affile->af_bad != 0 && flag_in_afflist(
6645 affile->af_flagtype, afflist, affile->af_bad))
6646 flags |= WF_BANNED;
6647 if (affile->af_needcomp != 0 && flag_in_afflist(
6648 affile->af_flagtype, afflist, affile->af_needcomp))
6649 flags |= WF_NEEDCOMP;
6650 if (affile->af_comproot != 0 && flag_in_afflist(
6651 affile->af_flagtype, afflist, affile->af_comproot))
6652 flags |= WF_COMPROOT;
6653 if (affile->af_nosuggest != 0 && flag_in_afflist(
6654 affile->af_flagtype, afflist, affile->af_nosuggest))
6655 flags |= WF_NOSUGGEST;
6656 return flags;
6657}
6658
6659/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006660 * Get the list of prefix IDs from the affix list "afflist".
6661 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006662 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6663 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006664 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006665 static int
6666get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006667 afffile_T *affile;
6668 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006669 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006670{
6671 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006672 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006673 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006674 int id;
6675 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006676 hashitem_T *hi;
6677
Bram Moolenaar6de68532005-08-24 22:08:48 +00006678 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006679 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006680 prevp = p;
6681 if (get_affitem(affile->af_flagtype, &p) != 0)
6682 {
6683 /* A flag is a postponed prefix flag if it appears in "af_pref"
6684 * and it's ID is not zero. */
6685 vim_strncpy(key, prevp, p - prevp);
6686 hi = hash_find(&affile->af_pref, key);
6687 if (!HASHITEM_EMPTY(hi))
6688 {
6689 id = HI2AH(hi)->ah_newID;
6690 if (id != 0)
6691 store_afflist[cnt++] = id;
6692 }
6693 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006694 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006695 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006696 }
6697
Bram Moolenaar5195e452005-08-19 20:32:47 +00006698 store_afflist[cnt] = NUL;
6699 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006700}
6701
6702/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006703 * Get the list of compound IDs from the affix list "afflist" that are used
6704 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006705 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006706 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006707 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006708get_compflags(affile, afflist, store_afflist)
6709 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006710 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006711 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006712{
6713 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006714 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006715 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006716 char_u key[AH_KEY_LEN];
6717 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006718
Bram Moolenaar6de68532005-08-24 22:08:48 +00006719 for (p = afflist; *p != NUL; )
6720 {
6721 prevp = p;
6722 if (get_affitem(affile->af_flagtype, &p) != 0)
6723 {
6724 /* A flag is a compound flag if it appears in "af_comp". */
6725 vim_strncpy(key, prevp, p - prevp);
6726 hi = hash_find(&affile->af_comp, key);
6727 if (!HASHITEM_EMPTY(hi))
6728 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6729 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006730 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006731 ++p;
6732 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006733
Bram Moolenaar5195e452005-08-19 20:32:47 +00006734 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006735}
6736
6737/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006738 * Apply affixes to a word and store the resulting words.
6739 * "ht" is the hashtable with affentry_T that need to be applied, either
6740 * prefixes or suffixes.
6741 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6742 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006743 *
6744 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006745 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006746 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006747store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006748 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006749 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006750 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006751 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006752 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006753 hashtab_T *ht;
6754 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006755 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006756 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006757 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006758 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6759 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006760{
6761 int todo;
6762 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006763 affheader_T *ah;
6764 affentry_T *ae;
6765 regmatch_T regmatch;
6766 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006767 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006768 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006769 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006770 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006771 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006772 int use_pfxlen;
6773 int need_affix;
6774 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006775 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006776 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006777 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006778
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006779 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006780 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006781 {
6782 if (!HASHITEM_EMPTY(hi))
6783 {
6784 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006785 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006786
Bram Moolenaar51485f02005-06-04 21:55:20 +00006787 /* Check that the affix combines, if required, and that the word
6788 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006789 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6790 && flag_in_afflist(affile->af_flagtype, afflist,
6791 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006792 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006793 /* Loop over all affix entries with this name. */
6794 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006795 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006796 /* Check the condition. It's not logical to match case
6797 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006798 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006799 * Another requirement from Myspell is that the chop
6800 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006801 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006802 * prefixes with a chop string and/or flags.
6803 * When a previously added affix had CIRCUMFIX this one
6804 * must have it too, if it had not then this one must not
6805 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006806 regmatch.regprog = ae->ae_prog;
6807 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006808 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006809 || ae->ae_chop != NULL
6810 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006811 && (ae->ae_chop == NULL
6812 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006813 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006814 || vim_regexec(&regmatch, word, (colnr_T)0))
6815 && (((condit & CONDIT_CFIX) == 0)
6816 == ((condit & CONDIT_AFF) == 0
6817 || ae->ae_flags == NULL
6818 || !flag_in_afflist(affile->af_flagtype,
6819 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006820 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006821 /* Match. Remove the chop and add the affix. */
6822 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006823 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006824 /* prefix: chop/add at the start of the word */
6825 if (ae->ae_add == NULL)
6826 *newword = NUL;
6827 else
6828 STRCPY(newword, ae->ae_add);
6829 p = word;
6830 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006831 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006832 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006833#ifdef FEAT_MBYTE
6834 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006835 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006836 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006837 for ( ; i > 0; --i)
6838 mb_ptr_adv(p);
6839 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006840 else
6841#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006842 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006843 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006844 STRCAT(newword, p);
6845 }
6846 else
6847 {
6848 /* suffix: chop/add at the end of the word */
6849 STRCPY(newword, word);
6850 if (ae->ae_chop != NULL)
6851 {
6852 /* Remove chop string. */
6853 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006854 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006855 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006856 mb_ptr_back(newword, p);
6857 *p = NUL;
6858 }
6859 if (ae->ae_add != NULL)
6860 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006861 }
6862
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006863 use_flags = flags;
6864 use_pfxlist = pfxlist;
6865 use_pfxlen = pfxlen;
6866 need_affix = FALSE;
6867 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6868 if (ae->ae_flags != NULL)
6869 {
6870 /* Extract flags from the affix list. */
6871 use_flags |= get_affix_flags(affile, ae->ae_flags);
6872
6873 if (affile->af_needaffix != 0 && flag_in_afflist(
6874 affile->af_flagtype, ae->ae_flags,
6875 affile->af_needaffix))
6876 need_affix = TRUE;
6877
6878 /* When there is a CIRCUMFIX flag the other affix
6879 * must also have it and we don't add the word
6880 * with one affix. */
6881 if (affile->af_circumfix != 0 && flag_in_afflist(
6882 affile->af_flagtype, ae->ae_flags,
6883 affile->af_circumfix))
6884 {
6885 use_condit |= CONDIT_CFIX;
6886 if ((condit & CONDIT_CFIX) == 0)
6887 need_affix = TRUE;
6888 }
6889
6890 if (affile->af_pfxpostpone
6891 || spin->si_compflags != NULL)
6892 {
6893 if (affile->af_pfxpostpone)
6894 /* Get prefix IDS from the affix list. */
6895 use_pfxlen = get_pfxlist(affile,
6896 ae->ae_flags, store_afflist);
6897 else
6898 use_pfxlen = 0;
6899 use_pfxlist = store_afflist;
6900
6901 /* Combine the prefix IDs. Avoid adding the
6902 * same ID twice. */
6903 for (i = 0; i < pfxlen; ++i)
6904 {
6905 for (j = 0; j < use_pfxlen; ++j)
6906 if (pfxlist[i] == use_pfxlist[j])
6907 break;
6908 if (j == use_pfxlen)
6909 use_pfxlist[use_pfxlen++] = pfxlist[i];
6910 }
6911
6912 if (spin->si_compflags != NULL)
6913 /* Get compound IDS from the affix list. */
6914 get_compflags(affile, ae->ae_flags,
6915 use_pfxlist + use_pfxlen);
6916
6917 /* Combine the list of compound flags.
6918 * Concatenate them to the prefix IDs list.
6919 * Avoid adding the same ID twice. */
6920 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6921 {
6922 for (j = use_pfxlen;
6923 use_pfxlist[j] != NUL; ++j)
6924 if (pfxlist[i] == use_pfxlist[j])
6925 break;
6926 if (use_pfxlist[j] == NUL)
6927 {
6928 use_pfxlist[j++] = pfxlist[i];
6929 use_pfxlist[j] = NUL;
6930 }
6931 }
6932 }
6933 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006934
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006935 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006936 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006937 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006938 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006939 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006940 use_pfxlist = pfx_pfxlist;
6941 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006942
6943 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006944 if (spin->si_prefroot != NULL
6945 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006946 {
6947 /* ... add a flag to indicate an affix was used. */
6948 use_flags |= WF_HAS_AFF;
6949
6950 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006951 * affixes is not allowed. But do use the
6952 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006953 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006954 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006955 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006956
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006957 /* When compounding is supported and there is no
6958 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6959 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006960 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006961 {
6962 if (xht != NULL)
6963 use_flags |= WF_NOCOMPAFT;
6964 else
6965 use_flags |= WF_NOCOMPBEF;
6966 }
6967
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006969 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006970 spin->si_region, use_pfxlist,
6971 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006972 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006973
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006974 /* When added a prefix or a first suffix and the affix
6975 * has flags may add a(nother) suffix. RECURSIVE! */
6976 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6977 if (store_aff_word(spin, newword, ae->ae_flags,
6978 affile, &affile->af_suff, xht,
6979 use_condit & (xht == NULL
6980 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006981 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006982 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006983
6984 /* When added a suffix and combining is allowed also
6985 * try adding a prefix additionally. Both for the
6986 * word flags and for the affix flags. RECURSIVE! */
6987 if (xht != NULL && ah->ah_combine)
6988 {
6989 if (store_aff_word(spin, newword,
6990 afflist, affile,
6991 xht, NULL, use_condit,
6992 use_flags, use_pfxlist,
6993 pfxlen) == FAIL
6994 || (ae->ae_flags != NULL
6995 && store_aff_word(spin, newword,
6996 ae->ae_flags, affile,
6997 xht, NULL, use_condit,
6998 use_flags, use_pfxlist,
6999 pfxlen) == FAIL))
7000 retval = FAIL;
7001 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007002 }
7003 }
7004 }
7005 }
7006 }
7007
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007008 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007009}
7010
7011/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007012 * Read a file with a list of words.
7013 */
7014 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007015spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007016 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007017 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007018{
7019 FILE *fd;
7020 long lnum = 0;
7021 char_u rline[MAXLINELEN];
7022 char_u *line;
7023 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007024 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007025 int l;
7026 int retval = OK;
7027 int did_word = FALSE;
7028 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007029 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007030 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007031
7032 /*
7033 * Open the file.
7034 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007035 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007036 if (fd == NULL)
7037 {
7038 EMSG2(_(e_notopen), fname);
7039 return FAIL;
7040 }
7041
Bram Moolenaar4770d092006-01-12 23:22:24 +00007042 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7043 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007044
7045 /*
7046 * Read all the lines in the file one by one.
7047 */
7048 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7049 {
7050 line_breakcheck();
7051 ++lnum;
7052
7053 /* Skip comment lines. */
7054 if (*rline == '#')
7055 continue;
7056
7057 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007059 while (l > 0 && rline[l - 1] <= ' ')
7060 --l;
7061 if (l == 0)
7062 continue; /* empty or blank line */
7063 rline[l] = NUL;
7064
Bram Moolenaar9c102382006-05-03 21:26:49 +00007065 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007066 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007067#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007068 if (spin->si_conv.vc_type != CONV_NONE)
7069 {
7070 pc = string_convert(&spin->si_conv, rline, NULL);
7071 if (pc == NULL)
7072 {
7073 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7074 fname, lnum, rline);
7075 continue;
7076 }
7077 line = pc;
7078 }
7079 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007080#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007081 {
7082 pc = NULL;
7083 line = rline;
7084 }
7085
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007086 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007088 ++line;
7089 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007090 {
7091 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007092 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7093 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007094 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007095 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7096 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007097 else
7098 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007099#ifdef FEAT_MBYTE
7100 char_u *enc;
7101
Bram Moolenaar51485f02005-06-04 21:55:20 +00007102 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007103 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007104 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007105 if (enc != NULL && !spin->si_ascii
7106 && convert_setup(&spin->si_conv, enc,
7107 p_enc) == FAIL)
7108 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007109 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007110 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007111 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007112#else
7113 smsg((char_u *)_("Conversion in %s not supported"), fname);
7114#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007115 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007116 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007117 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007118
Bram Moolenaar3982c542005-06-08 21:56:31 +00007119 if (STRNCMP(line, "regions=", 8) == 0)
7120 {
7121 if (spin->si_region_count > 1)
7122 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7123 fname, lnum, line);
7124 else
7125 {
7126 line += 8;
7127 if (STRLEN(line) > 16)
7128 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7129 fname, lnum, line);
7130 else
7131 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007132 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007133 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007134
7135 /* Adjust the mask for a word valid in all regions. */
7136 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007137 }
7138 }
7139 continue;
7140 }
7141
Bram Moolenaar7887d882005-07-01 22:33:52 +00007142 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7143 fname, lnum, line - 1);
7144 continue;
7145 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007146
Bram Moolenaar7887d882005-07-01 22:33:52 +00007147 flags = 0;
7148 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007149
Bram Moolenaar7887d882005-07-01 22:33:52 +00007150 /* Check for flags and region after a slash. */
7151 p = vim_strchr(line, '/');
7152 if (p != NULL)
7153 {
7154 *p++ = NUL;
7155 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007156 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007157 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007158 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007159 else if (*p == '!') /* Bad, bad, wicked word. */
7160 flags |= WF_BANNED;
7161 else if (*p == '?') /* Rare word. */
7162 flags |= WF_RARE;
7163 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007164 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007165 if ((flags & WF_REGION) == 0) /* first one */
7166 regionmask = 0;
7167 flags |= WF_REGION;
7168
7169 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007170 if (l > spin->si_region_count)
7171 {
7172 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007173 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007174 break;
7175 }
7176 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007177 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007178 else
7179 {
7180 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7181 fname, lnum, p);
7182 break;
7183 }
7184 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007185 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007186 }
7187
7188 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7189 if (spin->si_ascii && has_non_ascii(line))
7190 {
7191 ++non_ascii;
7192 continue;
7193 }
7194
7195 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007196 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007197 {
7198 retval = FAIL;
7199 break;
7200 }
7201 did_word = TRUE;
7202 }
7203
7204 vim_free(pc);
7205 fclose(fd);
7206
Bram Moolenaar4770d092006-01-12 23:22:24 +00007207 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007208 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007209 vim_snprintf((char *)IObuff, IOSIZE,
7210 _("Ignored %d words with non-ASCII characters"), non_ascii);
7211 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007212 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007213
Bram Moolenaar51485f02005-06-04 21:55:20 +00007214 return retval;
7215}
7216
7217/*
7218 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007219 * This avoids calling free() for every little struct we use (and keeping
7220 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007221 * The memory is cleared to all zeros.
7222 * Returns NULL when out of memory.
7223 */
7224 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007225getroom(spin, len, align)
7226 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007227 size_t len; /* length needed */
7228 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007229{
7230 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007231 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007232
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007233 if (align && bl != NULL)
7234 /* Round size up for alignment. On some systems structures need to be
7235 * aligned to the size of a pointer (e.g., SPARC). */
7236 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7237 & ~(sizeof(char *) - 1);
7238
Bram Moolenaar51485f02005-06-04 21:55:20 +00007239 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7240 {
7241 /* Allocate a block of memory. This is not freed until much later. */
7242 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7243 if (bl == NULL)
7244 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007245 bl->sb_next = spin->si_blocks;
7246 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007247 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007248 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007249 }
7250
7251 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007252 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007253
7254 return p;
7255}
7256
7257/*
7258 * Make a copy of a string into memory allocated with getroom().
7259 */
7260 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007261getroom_save(spin, s)
7262 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007263 char_u *s;
7264{
7265 char_u *sc;
7266
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007267 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007268 if (sc != NULL)
7269 STRCPY(sc, s);
7270 return sc;
7271}
7272
7273
7274/*
7275 * Free the list of allocated sblock_T.
7276 */
7277 static void
7278free_blocks(bl)
7279 sblock_T *bl;
7280{
7281 sblock_T *next;
7282
7283 while (bl != NULL)
7284 {
7285 next = bl->sb_next;
7286 vim_free(bl);
7287 bl = next;
7288 }
7289}
7290
7291/*
7292 * Allocate the root of a word tree.
7293 */
7294 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007295wordtree_alloc(spin)
7296 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007297{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007298 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007299}
7300
7301/*
7302 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007303 * Always store it in the case-folded tree. For a keep-case word this is
7304 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7305 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007306 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007307 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7308 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007309 */
7310 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007311store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007312 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007313 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007314 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007315 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007316 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007317 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007318{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007319 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007320 int ct = captype(word, word + len);
7321 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007322 int res = OK;
7323 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007325 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007326 for (p = pfxlist; res == OK; ++p)
7327 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007328 if (!need_affix || (p != NULL && *p != NUL))
7329 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007330 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007331 if (p == NULL || *p == NUL)
7332 break;
7333 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007334 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007335
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007336 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007337 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007338 for (p = pfxlist; res == OK; ++p)
7339 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007340 if (!need_affix || (p != NULL && *p != NUL))
7341 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007342 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007343 if (p == NULL || *p == NUL)
7344 break;
7345 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007346 ++spin->si_keepwcount;
7347 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007348 return res;
7349}
7350
7351/*
7352 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007353 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007354 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007355 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007356 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007357 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007358tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007359 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007360 char_u *word;
7361 wordnode_T *root;
7362 int flags;
7363 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007364 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007365{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007366 wordnode_T *node = root;
7367 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007368 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007369 wordnode_T **prev = NULL;
7370 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007371
Bram Moolenaar51485f02005-06-04 21:55:20 +00007372 /* Add each byte of the word to the tree, including the NUL at the end. */
7373 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007374 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007375 /* When there is more than one reference to this node we need to make
7376 * a copy, so that we can modify it. Copy the whole list of siblings
7377 * (we don't optimize for a partly shared list of siblings). */
7378 if (node != NULL && node->wn_refs > 1)
7379 {
7380 --node->wn_refs;
7381 copyprev = prev;
7382 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7383 {
7384 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007385 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007386 if (np == NULL)
7387 return FAIL;
7388 np->wn_child = copyp->wn_child;
7389 if (np->wn_child != NULL)
7390 ++np->wn_child->wn_refs; /* child gets extra ref */
7391 np->wn_byte = copyp->wn_byte;
7392 if (np->wn_byte == NUL)
7393 {
7394 np->wn_flags = copyp->wn_flags;
7395 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007396 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007397 }
7398
7399 /* Link the new node in the list, there will be one ref. */
7400 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007401 if (copyprev != NULL)
7402 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007403 copyprev = &np->wn_sibling;
7404
7405 /* Let "node" point to the head of the copied list. */
7406 if (copyp == node)
7407 node = np;
7408 }
7409 }
7410
Bram Moolenaar51485f02005-06-04 21:55:20 +00007411 /* Look for the sibling that has the same character. They are sorted
7412 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007413 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007414 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007415 while (node != NULL
7416 && (node->wn_byte < word[i]
7417 || (node->wn_byte == NUL
7418 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007419 ? node->wn_affixID < (unsigned)affixID
7420 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007421 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007422 && (spin->si_sugtree
7423 ? (node->wn_region & 0xffff) < region
7424 : node->wn_affixID
7425 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007426 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007427 prev = &node->wn_sibling;
7428 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007430 if (node == NULL
7431 || node->wn_byte != word[i]
7432 || (word[i] == NUL
7433 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007434 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007435 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007436 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007437 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007438 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007439 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007440 if (np == NULL)
7441 return FAIL;
7442 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007443
7444 /* If "node" is NULL this is a new child or the end of the sibling
7445 * list: ref count is one. Otherwise use ref count of sibling and
7446 * make ref count of sibling one (matters when inserting in front
7447 * of the list of siblings). */
7448 if (node == NULL)
7449 np->wn_refs = 1;
7450 else
7451 {
7452 np->wn_refs = node->wn_refs;
7453 node->wn_refs = 1;
7454 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007455 *prev = np;
7456 np->wn_sibling = node;
7457 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007458 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007459
Bram Moolenaar51485f02005-06-04 21:55:20 +00007460 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007461 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007462 node->wn_flags = flags;
7463 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007464 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007465 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007466 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007467 prev = &node->wn_child;
7468 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007469 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007470#ifdef SPELL_PRINTTREE
7471 smsg("Added \"%s\"", word);
7472 spell_print_tree(root->wn_sibling);
7473#endif
7474
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007475 /* count nr of words added since last message */
7476 ++spin->si_msg_count;
7477
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007478 if (spin->si_compress_cnt > 1)
7479 {
7480 if (--spin->si_compress_cnt == 1)
7481 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007482 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007483 }
7484
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007485 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007486 * When we have allocated lots of memory we need to compress the word tree
7487 * to free up some room. But compression is slow, and we might actually
7488 * need that room, thus only compress in the following situations:
7489 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007490 * "compress_start" blocks.
7491 * 2. When compressed before and used "compress_inc" blocks before
7492 * adding "compress_added" words (si_compress_cnt > 1).
7493 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007494 * (si_compress_cnt == 1) and the number of free nodes drops below the
7495 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007496 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007497#ifndef SPELL_PRINTTREE
7498 if (spin->si_compress_cnt == 1
7499 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007500 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007501#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007502 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007503 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007504 * when the freed up room has been used and another "compress_inc"
7505 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007506 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007507 spin->si_blocks_cnt -= compress_inc;
7508 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007509
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007510 if (spin->si_verbose)
7511 {
7512 msg_start();
7513 msg_puts((char_u *)_(msg_compressing));
7514 msg_clr_eos();
7515 msg_didout = FALSE;
7516 msg_col = 0;
7517 out_flush();
7518 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007519
7520 /* Compress both trees. Either they both have many nodes, which makes
7521 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007522 * compression goes fast. But when filling the souldfold word tree
7523 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007524 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007525 if (affixID >= 0)
7526 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007527 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007528
7529 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007530}
7531
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007532/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007533 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7534 * Sets "sps_flags".
7535 */
7536 int
7537spell_check_msm()
7538{
7539 char_u *p = p_msm;
7540 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007541 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007542 long added = 0;
7543
7544 if (!VIM_ISDIGIT(*p))
7545 return FAIL;
7546 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7547 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7548 if (*p != ',')
7549 return FAIL;
7550 ++p;
7551 if (!VIM_ISDIGIT(*p))
7552 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007553 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007554 if (*p != ',')
7555 return FAIL;
7556 ++p;
7557 if (!VIM_ISDIGIT(*p))
7558 return FAIL;
7559 added = getdigits(&p) * 1024;
7560 if (*p != NUL)
7561 return FAIL;
7562
Bram Moolenaar89d40322006-08-29 15:30:07 +00007563 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007564 return FAIL;
7565
7566 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007567 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007568 compress_added = added;
7569 return OK;
7570}
7571
7572
7573/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007574 * Get a wordnode_T, either from the list of previously freed nodes or
7575 * allocate a new one.
7576 */
7577 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007578get_wordnode(spin)
7579 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007580{
7581 wordnode_T *n;
7582
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007583 if (spin->si_first_free == NULL)
7584 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007585 else
7586 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007587 n = spin->si_first_free;
7588 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007589 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007590 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007591 }
7592#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007593 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007594#endif
7595 return n;
7596}
7597
7598/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007599 * Decrement the reference count on a node (which is the head of a list of
7600 * siblings). If the reference count becomes zero free the node and its
7601 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007602 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007603 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007604 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007605deref_wordnode(spin, node)
7606 spellinfo_T *spin;
7607 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007608{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007609 wordnode_T *np;
7610 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007611
7612 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007613 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007614 for (np = node; np != NULL; np = np->wn_sibling)
7615 {
7616 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007617 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007618 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007619 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007620 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007621 ++cnt; /* length field */
7622 }
7623 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007624}
7625
7626/*
7627 * Free a wordnode_T for re-use later.
7628 * Only the "wn_child" field becomes invalid.
7629 */
7630 static void
7631free_wordnode(spin, n)
7632 spellinfo_T *spin;
7633 wordnode_T *n;
7634{
7635 n->wn_child = spin->si_first_free;
7636 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007637 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007638}
7639
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007640/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007641 * Compress a tree: find tails that are identical and can be shared.
7642 */
7643 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007644wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007645 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007646 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007647{
7648 hashtab_T ht;
7649 int n;
7650 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007651 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007652
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007653 /* Skip the root itself, it's not actually used. The first sibling is the
7654 * start of the tree. */
7655 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007656 {
7657 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007658 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007659
7660#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007661 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007662#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007663 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007664 if (tot > 1000000)
7665 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007666 else if (tot == 0)
7667 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007668 else
7669 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007670 vim_snprintf((char *)IObuff, IOSIZE,
7671 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7672 n, tot, tot - n, perc);
7673 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007674 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007675#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007676 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007677#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007678 hash_clear(&ht);
7679 }
7680}
7681
7682/*
7683 * Compress a node, its siblings and its children, depth first.
7684 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007685 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007686 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007687node_compress(spin, node, ht, tot)
7688 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007689 wordnode_T *node;
7690 hashtab_T *ht;
7691 int *tot; /* total count of nodes before compressing,
7692 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007693{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007694 wordnode_T *np;
7695 wordnode_T *tp;
7696 wordnode_T *child;
7697 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007698 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007699 int len = 0;
7700 unsigned nr, n;
7701 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007702
Bram Moolenaar51485f02005-06-04 21:55:20 +00007703 /*
7704 * Go through the list of siblings. Compress each child and then try
7705 * finding an identical child to replace it.
7706 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007707 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007708 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007709 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007710 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007711 ++len;
7712 if ((child = np->wn_child) != NULL)
7713 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007714 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007715 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007716
7717 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007718 hash = hash_hash(child->wn_u1.hashkey);
7719 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007720 if (!HASHITEM_EMPTY(hi))
7721 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007722 /* There are children we encountered before with a hash value
7723 * identical to the current child. Now check if there is one
7724 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007725 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007726 if (node_equal(child, tp))
7727 {
7728 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007729 * current one. This means the current child and all
7730 * its siblings is unlinked from the tree. */
7731 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007732 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007733 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007734 break;
7735 }
7736 if (tp == NULL)
7737 {
7738 /* No other child with this hash value equals the child of
7739 * the node, add it to the linked list after the first
7740 * item. */
7741 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007742 child->wn_u2.next = tp->wn_u2.next;
7743 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007744 }
7745 }
7746 else
7747 /* No other child has this hash value, add it to the
7748 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007749 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007750 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007751 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007752 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007753
7754 /*
7755 * Make a hash key for the node and its siblings, so that we can quickly
7756 * find a lookalike node. This must be done after compressing the sibling
7757 * list, otherwise the hash key would become invalid by the compression.
7758 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007759 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007760 nr = 0;
7761 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007762 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007763 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007764 /* end node: use wn_flags, wn_region and wn_affixID */
7765 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007766 else
7767 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007768 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007769 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007770 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007771
7772 /* Avoid NUL bytes, it terminates the hash key. */
7773 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007774 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007775 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007776 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007777 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007778 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007779 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007780 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7781 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007782
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007783 /* Check for CTRL-C pressed now and then. */
7784 fast_breakcheck();
7785
Bram Moolenaar51485f02005-06-04 21:55:20 +00007786 return compressed;
7787}
7788
7789/*
7790 * Return TRUE when two nodes have identical siblings and children.
7791 */
7792 static int
7793node_equal(n1, n2)
7794 wordnode_T *n1;
7795 wordnode_T *n2;
7796{
7797 wordnode_T *p1;
7798 wordnode_T *p2;
7799
7800 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7801 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7802 if (p1->wn_byte != p2->wn_byte
7803 || (p1->wn_byte == NUL
7804 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007805 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007806 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007807 : (p1->wn_child != p2->wn_child)))
7808 break;
7809
7810 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007811}
7812
7813/*
7814 * Write a number to file "fd", MSB first, in "len" bytes.
7815 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007816 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007817put_bytes(fd, nr, len)
7818 FILE *fd;
7819 long_u nr;
7820 int len;
7821{
7822 int i;
7823
7824 for (i = len - 1; i >= 0; --i)
7825 putc((int)(nr >> (i * 8)), fd);
7826}
7827
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007828#ifdef _MSC_VER
7829# if (_MSC_VER <= 1200)
7830/* This line is required for VC6 without the service pack. Also see the
7831 * matching #pragma below. */
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007832 # pragma optimize("", off)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007833# endif
7834#endif
7835
Bram Moolenaar4770d092006-01-12 23:22:24 +00007836/*
7837 * Write spin->si_sugtime to file "fd".
7838 */
7839 static void
7840put_sugtime(spin, fd)
7841 spellinfo_T *spin;
7842 FILE *fd;
7843{
7844 int c;
7845 int i;
7846
7847 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7848 * can't use put_bytes() here. */
7849 for (i = 7; i >= 0; --i)
7850 if (i + 1 > sizeof(time_t))
7851 /* ">>" doesn't work well when shifting more bits than avail */
7852 putc(0, fd);
7853 else
7854 {
7855 c = (unsigned)spin->si_sugtime >> (i * 8);
7856 putc(c, fd);
7857 }
7858}
7859
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007860#ifdef _MSC_VER
7861# if (_MSC_VER <= 1200)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007862 # pragma optimize("", on)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007863# endif
7864#endif
7865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866static int
7867#ifdef __BORLANDC__
7868_RTLENTRYF
7869#endif
7870rep_compare __ARGS((const void *s1, const void *s2));
7871
7872/*
7873 * Function given to qsort() to sort the REP items on "from" string.
7874 */
7875 static int
7876#ifdef __BORLANDC__
7877_RTLENTRYF
7878#endif
7879rep_compare(s1, s2)
7880 const void *s1;
7881 const void *s2;
7882{
7883 fromto_T *p1 = (fromto_T *)s1;
7884 fromto_T *p2 = (fromto_T *)s2;
7885
7886 return STRCMP(p1->ft_from, p2->ft_from);
7887}
7888
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007889/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007890 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007891 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007892 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007893 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007894write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007895 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007896 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007897{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007898 FILE *fd;
7899 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007900 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007901 wordnode_T *tree;
7902 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007903 int i;
7904 int l;
7905 garray_T *gap;
7906 fromto_T *ftp;
7907 char_u *p;
7908 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007909 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007910
Bram Moolenaarb765d632005-06-07 21:00:02 +00007911 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007912 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007913 {
7914 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007915 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007916 }
7917
Bram Moolenaar5195e452005-08-19 20:32:47 +00007918 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007919 /* <fileID> */
7920 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007921 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007922 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007923 retval = FAIL;
7924 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007925 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007926
Bram Moolenaar5195e452005-08-19 20:32:47 +00007927 /*
7928 * <SECTIONS>: <section> ... <sectionend>
7929 */
7930
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007931 /* SN_INFO: <infotext> */
7932 if (spin->si_info != NULL)
7933 {
7934 putc(SN_INFO, fd); /* <sectionID> */
7935 putc(0, fd); /* <sectionflags> */
7936
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007937 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007938 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7939 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7940 }
7941
Bram Moolenaar5195e452005-08-19 20:32:47 +00007942 /* SN_REGION: <regionname> ...
7943 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007944 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007945 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007946 putc(SN_REGION, fd); /* <sectionID> */
7947 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7948 l = spin->si_region_count * 2;
7949 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7950 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7951 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007952 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007953 }
7954 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007955 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007956
Bram Moolenaar5195e452005-08-19 20:32:47 +00007957 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7958 *
7959 * The table with character flags and the table for case folding.
7960 * This makes sure the same characters are recognized as word characters
7961 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007962 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963 * 'encoding'.
7964 * Also skip this for an .add.spl file, the main spell file must contain
7965 * the table (avoids that it conflicts). File is shorter too.
7966 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007967 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007968 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007969 char_u folchars[128 * 8];
7970 int flags;
7971
Bram Moolenaard12a1322005-08-21 22:08:24 +00007972 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7974
7975 /* Form the <folchars> string first, we need to know its length. */
7976 l = 0;
7977 for (i = 128; i < 256; ++i)
7978 {
7979#ifdef FEAT_MBYTE
7980 if (has_mbyte)
7981 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7982 else
7983#endif
7984 folchars[l++] = spelltab.st_fold[i];
7985 }
7986 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7987
7988 fputc(128, fd); /* <charflagslen> */
7989 for (i = 128; i < 256; ++i)
7990 {
7991 flags = 0;
7992 if (spelltab.st_isw[i])
7993 flags |= CF_WORD;
7994 if (spelltab.st_isu[i])
7995 flags |= CF_UPPER;
7996 fputc(flags, fd); /* <charflags> */
7997 }
7998
7999 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
8000 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008001 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008002
Bram Moolenaar5195e452005-08-19 20:32:47 +00008003 /* SN_MIDWORD: <midword> */
8004 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008005 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008006 putc(SN_MIDWORD, fd); /* <sectionID> */
8007 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008009 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008010 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008011 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
8012 }
8013
Bram Moolenaar5195e452005-08-19 20:32:47 +00008014 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8015 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008016 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008017 putc(SN_PREFCOND, fd); /* <sectionID> */
8018 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8019
8020 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8021 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8022
8023 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008024 }
8025
Bram Moolenaar5195e452005-08-19 20:32:47 +00008026 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008027 * SN_SAL: <salflags> <salcount> <sal> ...
8028 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008029
Bram Moolenaar5195e452005-08-19 20:32:47 +00008030 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008031 * round 2: SN_SAL section (unless SN_SOFO is used)
8032 * round 3: SN_REPSAL section */
8033 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008034 {
8035 if (round == 1)
8036 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008037 else if (round == 2)
8038 {
8039 /* Don't write SN_SAL when using a SN_SOFO section */
8040 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8041 continue;
8042 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008043 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008044 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008045 gap = &spin->si_repsal;
8046
8047 /* Don't write the section if there are no items. */
8048 if (gap->ga_len == 0)
8049 continue;
8050
8051 /* Sort the REP/REPSAL items. */
8052 if (round != 2)
8053 qsort(gap->ga_data, (size_t)gap->ga_len,
8054 sizeof(fromto_T), rep_compare);
8055
8056 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8057 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008058
Bram Moolenaar5195e452005-08-19 20:32:47 +00008059 /* This is for making suggestions, section is not required. */
8060 putc(0, fd); /* <sectionflags> */
8061
8062 /* Compute the length of what follows. */
8063 l = 2; /* count <repcount> or <salcount> */
8064 for (i = 0; i < gap->ga_len; ++i)
8065 {
8066 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008067 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8068 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008069 }
8070 if (round == 2)
8071 ++l; /* count <salflags> */
8072 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8073
8074 if (round == 2)
8075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 i = 0;
8077 if (spin->si_followup)
8078 i |= SAL_F0LLOWUP;
8079 if (spin->si_collapse)
8080 i |= SAL_COLLAPSE;
8081 if (spin->si_rem_accents)
8082 i |= SAL_REM_ACCENTS;
8083 putc(i, fd); /* <salflags> */
8084 }
8085
8086 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8087 for (i = 0; i < gap->ga_len; ++i)
8088 {
8089 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8090 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8091 ftp = &((fromto_T *)gap->ga_data)[i];
8092 for (rr = 1; rr <= 2; ++rr)
8093 {
8094 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008095 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008096 putc(l, fd);
8097 fwrite(p, l, (size_t)1, fd);
8098 }
8099 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008101 }
8102
Bram Moolenaar5195e452005-08-19 20:32:47 +00008103 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8104 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008105 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8106 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008107 putc(SN_SOFO, fd); /* <sectionID> */
8108 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008109
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008110 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008111 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8112 /* <sectionlen> */
8113
8114 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8115 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008117 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008118 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8119 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008120 }
8121
Bram Moolenaar4770d092006-01-12 23:22:24 +00008122 /* SN_WORDS: <word> ...
8123 * This is for making suggestions, section is not required. */
8124 if (spin->si_commonwords.ht_used > 0)
8125 {
8126 putc(SN_WORDS, fd); /* <sectionID> */
8127 putc(0, fd); /* <sectionflags> */
8128
8129 /* round 1: count the bytes
8130 * round 2: write the bytes */
8131 for (round = 1; round <= 2; ++round)
8132 {
8133 int todo;
8134 int len = 0;
8135 hashitem_T *hi;
8136
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008137 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008138 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8139 if (!HASHITEM_EMPTY(hi))
8140 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008141 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008142 len += l;
8143 if (round == 2) /* <word> */
8144 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8145 --todo;
8146 }
8147 if (round == 1)
8148 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8149 }
8150 }
8151
Bram Moolenaar5195e452005-08-19 20:32:47 +00008152 /* SN_MAP: <mapstr>
8153 * This is for making suggestions, section is not required. */
8154 if (spin->si_map.ga_len > 0)
8155 {
8156 putc(SN_MAP, fd); /* <sectionID> */
8157 putc(0, fd); /* <sectionflags> */
8158 l = spin->si_map.ga_len;
8159 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8160 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8161 /* <mapstr> */
8162 }
8163
Bram Moolenaar4770d092006-01-12 23:22:24 +00008164 /* SN_SUGFILE: <timestamp>
8165 * This is used to notify that a .sug file may be available and at the
8166 * same time allows for checking that a .sug file that is found matches
8167 * with this .spl file. That's because the word numbers must be exactly
8168 * right. */
8169 if (!spin->si_nosugfile
8170 && (spin->si_sal.ga_len > 0
8171 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8172 {
8173 putc(SN_SUGFILE, fd); /* <sectionID> */
8174 putc(0, fd); /* <sectionflags> */
8175 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8176
8177 /* Set si_sugtime and write it to the file. */
8178 spin->si_sugtime = time(NULL);
8179 put_sugtime(spin, fd); /* <timestamp> */
8180 }
8181
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008182 /* SN_NOSPLITSUGS: nothing
8183 * This is used to notify that no suggestions with word splits are to be
8184 * made. */
8185 if (spin->si_nosplitsugs)
8186 {
8187 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8188 putc(0, fd); /* <sectionflags> */
8189 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8190 }
8191
Bram Moolenaar5195e452005-08-19 20:32:47 +00008192 /* SN_COMPOUND: compound info.
8193 * We don't mark it required, when not supported all compound words will
8194 * be bad words. */
8195 if (spin->si_compflags != NULL)
8196 {
8197 putc(SN_COMPOUND, fd); /* <sectionID> */
8198 putc(0, fd); /* <sectionflags> */
8199
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008200 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008201 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008202 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008203 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8204
Bram Moolenaar5195e452005-08-19 20:32:47 +00008205 putc(spin->si_compmax, fd); /* <compmax> */
8206 putc(spin->si_compminlen, fd); /* <compminlen> */
8207 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008208 putc(0, fd); /* for Vim 7.0b compatibility */
8209 putc(spin->si_compoptions, fd); /* <compoptions> */
8210 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8211 /* <comppatcount> */
8212 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8213 {
8214 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008215 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008216 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8217 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008218 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008219 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8220 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008221 }
8222
Bram Moolenaar78622822005-08-23 21:00:13 +00008223 /* SN_NOBREAK: NOBREAK flag */
8224 if (spin->si_nobreak)
8225 {
8226 putc(SN_NOBREAK, fd); /* <sectionID> */
8227 putc(0, fd); /* <sectionflags> */
8228
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008229 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008230 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8231 }
8232
Bram Moolenaar5195e452005-08-19 20:32:47 +00008233 /* SN_SYLLABLE: syllable info.
8234 * We don't mark it required, when not supported syllables will not be
8235 * counted. */
8236 if (spin->si_syllable != NULL)
8237 {
8238 putc(SN_SYLLABLE, fd); /* <sectionID> */
8239 putc(0, fd); /* <sectionflags> */
8240
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008241 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008242 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8243 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8244 }
8245
8246 /* end of <SECTIONS> */
8247 putc(SN_END, fd); /* <sectionend> */
8248
Bram Moolenaar50cde822005-06-05 21:54:54 +00008249
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008250 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008251 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008252 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008253 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008254 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008255 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008256 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008257 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008258 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008259 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008260 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008261 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008262
Bram Moolenaar0c405862005-06-22 22:26:26 +00008263 /* Clear the index and wnode fields in the tree. */
8264 clear_node(tree);
8265
Bram Moolenaar51485f02005-06-04 21:55:20 +00008266 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008267 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008268 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008269 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008270
Bram Moolenaar51485f02005-06-04 21:55:20 +00008271 /* number of nodes in 4 bytes */
8272 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008273 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008274
Bram Moolenaar51485f02005-06-04 21:55:20 +00008275 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008276 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008277 }
8278
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008279 /* Write another byte to check for errors. */
8280 if (putc(0, fd) == EOF)
8281 retval = FAIL;
8282
8283 if (fclose(fd) == EOF)
8284 retval = FAIL;
8285
8286 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008287}
8288
8289/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008290 * Clear the index and wnode fields of "node", it siblings and its
8291 * children. This is needed because they are a union with other items to save
8292 * space.
8293 */
8294 static void
8295clear_node(node)
8296 wordnode_T *node;
8297{
8298 wordnode_T *np;
8299
8300 if (node != NULL)
8301 for (np = node; np != NULL; np = np->wn_sibling)
8302 {
8303 np->wn_u1.index = 0;
8304 np->wn_u2.wnode = NULL;
8305
8306 if (np->wn_byte != NUL)
8307 clear_node(np->wn_child);
8308 }
8309}
8310
8311
8312/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008313 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008314 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008315 * This first writes the list of possible bytes (siblings). Then for each
8316 * byte recursively write the children.
8317 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008318 * NOTE: The code here must match the code in read_tree_node(), since
8319 * assumptions are made about the indexes (so that we don't have to write them
8320 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008321 *
8322 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008323 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008324 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008325put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008326 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008327 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008328 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008329 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008330 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008331{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008332 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008333 int siblingcount = 0;
8334 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008335 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008336
Bram Moolenaar51485f02005-06-04 21:55:20 +00008337 /* If "node" is zero the tree is empty. */
8338 if (node == NULL)
8339 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008340
Bram Moolenaar51485f02005-06-04 21:55:20 +00008341 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008342 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008343
8344 /* Count the number of siblings. */
8345 for (np = node; np != NULL; np = np->wn_sibling)
8346 ++siblingcount;
8347
8348 /* Write the sibling count. */
8349 if (fd != NULL)
8350 putc(siblingcount, fd); /* <siblingcount> */
8351
8352 /* Write each sibling byte and optionally extra info. */
8353 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008354 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008355 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008356 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008357 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008359 /* For a NUL byte (end of word) write the flags etc. */
8360 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008361 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008362 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008363 * associated condition nr (stored in wn_region). The
8364 * byte value is misused to store the "rare" and "not
8365 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008366 if (np->wn_flags == (short_u)PFX_FLAGS)
8367 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008368 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008369 {
8370 putc(BY_FLAGS, fd); /* <byte> */
8371 putc(np->wn_flags, fd); /* <pflags> */
8372 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008373 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008374 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008375 }
8376 else
8377 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008378 /* For word trees we write the flag/region items. */
8379 flags = np->wn_flags;
8380 if (regionmask != 0 && np->wn_region != regionmask)
8381 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008382 if (np->wn_affixID != 0)
8383 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008384 if (flags == 0)
8385 {
8386 /* word without flags or region */
8387 putc(BY_NOFLAGS, fd); /* <byte> */
8388 }
8389 else
8390 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008391 if (np->wn_flags >= 0x100)
8392 {
8393 putc(BY_FLAGS2, fd); /* <byte> */
8394 putc(flags, fd); /* <flags> */
8395 putc((unsigned)flags >> 8, fd); /* <flags2> */
8396 }
8397 else
8398 {
8399 putc(BY_FLAGS, fd); /* <byte> */
8400 putc(flags, fd); /* <flags> */
8401 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008402 if (flags & WF_REGION)
8403 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008404 if (flags & WF_AFX)
8405 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008406 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008407 }
8408 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008409 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008410 else
8411 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008412 if (np->wn_child->wn_u1.index != 0
8413 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008414 {
8415 /* The child is written elsewhere, write the reference. */
8416 if (fd != NULL)
8417 {
8418 putc(BY_INDEX, fd); /* <byte> */
8419 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008420 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008421 }
8422 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008423 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008424 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008425 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008426
Bram Moolenaar51485f02005-06-04 21:55:20 +00008427 if (fd != NULL)
8428 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8429 {
8430 EMSG(_(e_write));
8431 return 0;
8432 }
8433 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008434 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008435
8436 /* Space used in the array when reading: one for each sibling and one for
8437 * the count. */
8438 newindex += siblingcount + 1;
8439
8440 /* Recursively dump the children of each sibling. */
8441 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008442 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8443 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008444 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008445
8446 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008447}
8448
8449
8450/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008451 * ":mkspell [-ascii] outfile infile ..."
8452 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008453 */
8454 void
8455ex_mkspell(eap)
8456 exarg_T *eap;
8457{
8458 int fcount;
8459 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008460 char_u *arg = eap->arg;
8461 int ascii = FALSE;
8462
8463 if (STRNCMP(arg, "-ascii", 6) == 0)
8464 {
8465 ascii = TRUE;
8466 arg = skipwhite(arg + 6);
8467 }
8468
8469 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8470 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008473 FreeWild(fcount, fnames);
8474 }
8475}
8476
8477/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008478 * Create the .sug file.
8479 * Uses the soundfold info in "spin".
8480 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8481 */
8482 static void
8483spell_make_sugfile(spin, wfname)
8484 spellinfo_T *spin;
8485 char_u *wfname;
8486{
8487 char_u fname[MAXPATHL];
8488 int len;
8489 slang_T *slang;
8490 int free_slang = FALSE;
8491
8492 /*
8493 * Read back the .spl file that was written. This fills the required
8494 * info for soundfolding. This also uses less memory than the
8495 * pointer-linked version of the trie. And it avoids having two versions
8496 * of the code for the soundfolding stuff.
8497 * It might have been done already by spell_reload_one().
8498 */
8499 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8500 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8501 break;
8502 if (slang == NULL)
8503 {
8504 spell_message(spin, (char_u *)_("Reading back spell file..."));
8505 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8506 if (slang == NULL)
8507 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008508 free_slang = TRUE;
8509 }
8510
8511 /*
8512 * Clear the info in "spin" that is used.
8513 */
8514 spin->si_blocks = NULL;
8515 spin->si_blocks_cnt = 0;
8516 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8517 spin->si_free_count = 0;
8518 spin->si_first_free = NULL;
8519 spin->si_foldwcount = 0;
8520
8521 /*
8522 * Go through the trie of good words, soundfold each word and add it to
8523 * the soundfold trie.
8524 */
8525 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8526 if (sug_filltree(spin, slang) == FAIL)
8527 goto theend;
8528
8529 /*
8530 * Create the table which links each soundfold word with a list of the
8531 * good words it may come from. Creates buffer "spin->si_spellbuf".
8532 * This also removes the wordnr from the NUL byte entries to make
8533 * compression possible.
8534 */
8535 if (sug_maketable(spin) == FAIL)
8536 goto theend;
8537
8538 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8539 (long)spin->si_spellbuf->b_ml.ml_line_count);
8540
8541 /*
8542 * Compress the soundfold trie.
8543 */
8544 spell_message(spin, (char_u *)_(msg_compressing));
8545 wordtree_compress(spin, spin->si_foldroot);
8546
8547 /*
8548 * Write the .sug file.
8549 * Make the file name by changing ".spl" to ".sug".
8550 */
8551 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008553 fname[len - 2] = 'u';
8554 fname[len - 1] = 'g';
8555 sug_write(spin, fname);
8556
8557theend:
8558 if (free_slang)
8559 slang_free(slang);
8560 free_blocks(spin->si_blocks);
8561 close_spellbuf(spin->si_spellbuf);
8562}
8563
8564/*
8565 * Build the soundfold trie for language "slang".
8566 */
8567 static int
8568sug_filltree(spin, slang)
8569 spellinfo_T *spin;
8570 slang_T *slang;
8571{
8572 char_u *byts;
8573 idx_T *idxs;
8574 int depth;
8575 idx_T arridx[MAXWLEN];
8576 int curi[MAXWLEN];
8577 char_u tword[MAXWLEN];
8578 char_u tsalword[MAXWLEN];
8579 int c;
8580 idx_T n;
8581 unsigned words_done = 0;
8582 int wordcount[MAXWLEN];
8583
8584 /* We use si_foldroot for the souldfolded trie. */
8585 spin->si_foldroot = wordtree_alloc(spin);
8586 if (spin->si_foldroot == NULL)
8587 return FAIL;
8588
8589 /* let tree_add_word() know we're adding to the soundfolded tree */
8590 spin->si_sugtree = TRUE;
8591
8592 /*
8593 * Go through the whole case-folded tree, soundfold each word and put it
8594 * in the trie.
8595 */
8596 byts = slang->sl_fbyts;
8597 idxs = slang->sl_fidxs;
8598
8599 arridx[0] = 0;
8600 curi[0] = 1;
8601 wordcount[0] = 0;
8602
8603 depth = 0;
8604 while (depth >= 0 && !got_int)
8605 {
8606 if (curi[depth] > byts[arridx[depth]])
8607 {
8608 /* Done all bytes at this node, go up one level. */
8609 idxs[arridx[depth]] = wordcount[depth];
8610 if (depth > 0)
8611 wordcount[depth - 1] += wordcount[depth];
8612
8613 --depth;
8614 line_breakcheck();
8615 }
8616 else
8617 {
8618
8619 /* Do one more byte at this node. */
8620 n = arridx[depth] + curi[depth];
8621 ++curi[depth];
8622
8623 c = byts[n];
8624 if (c == 0)
8625 {
8626 /* Sound-fold the word. */
8627 tword[depth] = NUL;
8628 spell_soundfold(slang, tword, TRUE, tsalword);
8629
8630 /* We use the "flags" field for the MSB of the wordnr,
8631 * "region" for the LSB of the wordnr. */
8632 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8633 words_done >> 16, words_done & 0xffff,
8634 0) == FAIL)
8635 return FAIL;
8636
8637 ++words_done;
8638 ++wordcount[depth];
8639
8640 /* Reset the block count each time to avoid compression
8641 * kicking in. */
8642 spin->si_blocks_cnt = 0;
8643
8644 /* Skip over any other NUL bytes (same word with different
8645 * flags). */
8646 while (byts[n + 1] == 0)
8647 {
8648 ++n;
8649 ++curi[depth];
8650 }
8651 }
8652 else
8653 {
8654 /* Normal char, go one level deeper. */
8655 tword[depth++] = c;
8656 arridx[depth] = idxs[n];
8657 curi[depth] = 1;
8658 wordcount[depth] = 0;
8659 }
8660 }
8661 }
8662
8663 smsg((char_u *)_("Total number of words: %d"), words_done);
8664
8665 return OK;
8666}
8667
8668/*
8669 * Make the table that links each word in the soundfold trie to the words it
8670 * can be produced from.
8671 * This is not unlike lines in a file, thus use a memfile to be able to access
8672 * the table efficiently.
8673 * Returns FAIL when out of memory.
8674 */
8675 static int
8676sug_maketable(spin)
8677 spellinfo_T *spin;
8678{
8679 garray_T ga;
8680 int res = OK;
8681
8682 /* Allocate a buffer, open a memline for it and create the swap file
8683 * (uses a temp file, not a .swp file). */
8684 spin->si_spellbuf = open_spellbuf();
8685 if (spin->si_spellbuf == NULL)
8686 return FAIL;
8687
8688 /* Use a buffer to store the line info, avoids allocating many small
8689 * pieces of memory. */
8690 ga_init2(&ga, 1, 100);
8691
8692 /* recursively go through the tree */
8693 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8694 res = FAIL;
8695
8696 ga_clear(&ga);
8697 return res;
8698}
8699
8700/*
8701 * Fill the table for one node and its children.
8702 * Returns the wordnr at the start of the node.
8703 * Returns -1 when out of memory.
8704 */
8705 static int
8706sug_filltable(spin, node, startwordnr, gap)
8707 spellinfo_T *spin;
8708 wordnode_T *node;
8709 int startwordnr;
8710 garray_T *gap; /* place to store line of numbers */
8711{
8712 wordnode_T *p, *np;
8713 int wordnr = startwordnr;
8714 int nr;
8715 int prev_nr;
8716
8717 for (p = node; p != NULL; p = p->wn_sibling)
8718 {
8719 if (p->wn_byte == NUL)
8720 {
8721 gap->ga_len = 0;
8722 prev_nr = 0;
8723 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8724 {
8725 if (ga_grow(gap, 10) == FAIL)
8726 return -1;
8727
8728 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8729 /* Compute the offset from the previous nr and store the
8730 * offset in a way that it takes a minimum number of bytes.
8731 * It's a bit like utf-8, but without the need to mark
8732 * following bytes. */
8733 nr -= prev_nr;
8734 prev_nr += nr;
8735 gap->ga_len += offset2bytes(nr,
8736 (char_u *)gap->ga_data + gap->ga_len);
8737 }
8738
8739 /* add the NUL byte */
8740 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8741
8742 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8743 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8744 return -1;
8745 ++wordnr;
8746
8747 /* Remove extra NUL entries, we no longer need them. We don't
8748 * bother freeing the nodes, the won't be reused anyway. */
8749 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8750 p->wn_sibling = p->wn_sibling->wn_sibling;
8751
8752 /* Clear the flags on the remaining NUL node, so that compression
8753 * works a lot better. */
8754 p->wn_flags = 0;
8755 p->wn_region = 0;
8756 }
8757 else
8758 {
8759 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8760 if (wordnr == -1)
8761 return -1;
8762 }
8763 }
8764 return wordnr;
8765}
8766
8767/*
8768 * Convert an offset into a minimal number of bytes.
8769 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8770 * bytes.
8771 */
8772 static int
8773offset2bytes(nr, buf)
8774 int nr;
8775 char_u *buf;
8776{
8777 int rem;
8778 int b1, b2, b3, b4;
8779
8780 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8781 b1 = nr % 255 + 1;
8782 rem = nr / 255;
8783 b2 = rem % 255 + 1;
8784 rem = rem / 255;
8785 b3 = rem % 255 + 1;
8786 b4 = rem / 255 + 1;
8787
8788 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8789 {
8790 buf[0] = 0xe0 + b4;
8791 buf[1] = b3;
8792 buf[2] = b2;
8793 buf[3] = b1;
8794 return 4;
8795 }
8796 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8797 {
8798 buf[0] = 0xc0 + b3;
8799 buf[1] = b2;
8800 buf[2] = b1;
8801 return 3;
8802 }
8803 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8804 {
8805 buf[0] = 0x80 + b2;
8806 buf[1] = b1;
8807 return 2;
8808 }
8809 /* 1 byte */
8810 buf[0] = b1;
8811 return 1;
8812}
8813
8814/*
8815 * Opposite of offset2bytes().
8816 * "pp" points to the bytes and is advanced over it.
8817 * Returns the offset.
8818 */
8819 static int
8820bytes2offset(pp)
8821 char_u **pp;
8822{
8823 char_u *p = *pp;
8824 int nr;
8825 int c;
8826
8827 c = *p++;
8828 if ((c & 0x80) == 0x00) /* 1 byte */
8829 {
8830 nr = c - 1;
8831 }
8832 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8833 {
8834 nr = (c & 0x3f) - 1;
8835 nr = nr * 255 + (*p++ - 1);
8836 }
8837 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8838 {
8839 nr = (c & 0x1f) - 1;
8840 nr = nr * 255 + (*p++ - 1);
8841 nr = nr * 255 + (*p++ - 1);
8842 }
8843 else /* 4 bytes */
8844 {
8845 nr = (c & 0x0f) - 1;
8846 nr = nr * 255 + (*p++ - 1);
8847 nr = nr * 255 + (*p++ - 1);
8848 nr = nr * 255 + (*p++ - 1);
8849 }
8850
8851 *pp = p;
8852 return nr;
8853}
8854
8855/*
8856 * Write the .sug file in "fname".
8857 */
8858 static void
8859sug_write(spin, fname)
8860 spellinfo_T *spin;
8861 char_u *fname;
8862{
8863 FILE *fd;
8864 wordnode_T *tree;
8865 int nodecount;
8866 int wcount;
8867 char_u *line;
8868 linenr_T lnum;
8869 int len;
8870
8871 /* Create the file. Note that an existing file is silently overwritten! */
8872 fd = mch_fopen((char *)fname, "w");
8873 if (fd == NULL)
8874 {
8875 EMSG2(_(e_notopen), fname);
8876 return;
8877 }
8878
8879 vim_snprintf((char *)IObuff, IOSIZE,
8880 _("Writing suggestion file %s ..."), fname);
8881 spell_message(spin, IObuff);
8882
8883 /*
8884 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8885 */
8886 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8887 {
8888 EMSG(_(e_write));
8889 goto theend;
8890 }
8891 putc(VIMSUGVERSION, fd); /* <versionnr> */
8892
8893 /* Write si_sugtime to the file. */
8894 put_sugtime(spin, fd); /* <timestamp> */
8895
8896 /*
8897 * <SUGWORDTREE>
8898 */
8899 spin->si_memtot = 0;
8900 tree = spin->si_foldroot->wn_sibling;
8901
8902 /* Clear the index and wnode fields in the tree. */
8903 clear_node(tree);
8904
8905 /* Count the number of nodes. Needed to be able to allocate the
8906 * memory when reading the nodes. Also fills in index for shared
8907 * nodes. */
8908 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8909
8910 /* number of nodes in 4 bytes */
8911 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8912 spin->si_memtot += nodecount + nodecount * sizeof(int);
8913
8914 /* Write the nodes. */
8915 (void)put_node(fd, tree, 0, 0, FALSE);
8916
8917 /*
8918 * <SUGTABLE>: <sugwcount> <sugline> ...
8919 */
8920 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8921 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8922
8923 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8924 {
8925 /* <sugline>: <sugnr> ... NUL */
8926 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008927 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008928 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8929 {
8930 EMSG(_(e_write));
8931 goto theend;
8932 }
8933 spin->si_memtot += len;
8934 }
8935
8936 /* Write another byte to check for errors. */
8937 if (putc(0, fd) == EOF)
8938 EMSG(_(e_write));
8939
8940 vim_snprintf((char *)IObuff, IOSIZE,
8941 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8942 spell_message(spin, IObuff);
8943
8944theend:
8945 /* close the file */
8946 fclose(fd);
8947}
8948
8949/*
8950 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8951 * list and only contains text lines. Can use a swapfile to reduce memory
8952 * use.
8953 * Most other fields are invalid! Esp. watch out for string options being
8954 * NULL and there is no undo info.
8955 * Returns NULL when out of memory.
8956 */
8957 static buf_T *
8958open_spellbuf()
8959{
8960 buf_T *buf;
8961
8962 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8963 if (buf != NULL)
8964 {
8965 buf->b_spell = TRUE;
8966 buf->b_p_swf = TRUE; /* may create a swap file */
8967 ml_open(buf);
8968 ml_open_file(buf); /* create swap file now */
8969 }
8970 return buf;
8971}
8972
8973/*
8974 * Close the buffer used for spell info.
8975 */
8976 static void
8977close_spellbuf(buf)
8978 buf_T *buf;
8979{
8980 if (buf != NULL)
8981 {
8982 ml_close(buf, TRUE);
8983 vim_free(buf);
8984 }
8985}
8986
8987
8988/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008989 * Create a Vim spell file from one or more word lists.
8990 * "fnames[0]" is the output file name.
8991 * "fnames[fcount - 1]" is the last input file name.
8992 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8993 * and ".spl" is appended to make the output file name.
8994 */
8995 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008997 int fcount;
8998 char_u **fnames;
8999 int ascii; /* -ascii argument given */
9000 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009002{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009003 char_u fname[MAXPATHL];
9004 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009005 char_u **innames;
9006 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009007 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009008 int i;
9009 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009010 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009011 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009012 spellinfo_T spin;
9013
9014 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009016 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009017 spin.si_followup = TRUE;
9018 spin.si_rem_accents = TRUE;
9019 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009020 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9022 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009023 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009024 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009025 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009026 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009027
Bram Moolenaarb765d632005-06-07 21:00:02 +00009028 /* default: fnames[0] is output file, following are input files */
9029 innames = &fnames[1];
9030 incount = fcount - 1;
9031
9032 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009033 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009034 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009035 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9036 {
9037 /* For ":mkspell path/en.latin1.add" output file is
9038 * "path/en.latin1.add.spl". */
9039 innames = &fnames[0];
9040 incount = 1;
9041 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9042 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009043 else if (fcount == 1)
9044 {
9045 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9046 innames = &fnames[0];
9047 incount = 1;
9048 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9049 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9050 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009051 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9052 {
9053 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009054 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009055 }
9056 else
9057 /* Name should be language, make the file name from it. */
9058 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9059 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9060
9061 /* Check for .ascii.spl. */
9062 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9063 spin.si_ascii = TRUE;
9064
9065 /* Check for .add.spl. */
9066 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9067 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009068 }
9069
Bram Moolenaarb765d632005-06-07 21:00:02 +00009070 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009071 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009072 else if (vim_strchr(gettail(wfname), '_') != NULL)
9073 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009074 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009075 EMSG(_("E754: Only up to 8 regions supported"));
9076 else
9077 {
9078 /* Check for overwriting before doing things that may take a lot of
9079 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009080 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009081 {
9082 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009083 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009084 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009085 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009086 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009087 EMSG2(_(e_isadir2), wfname);
9088 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009089 }
9090
9091 /*
9092 * Init the aff and dic pointers.
9093 * Get the region names if there are more than 2 arguments.
9094 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009095 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009096 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009097 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009098
Bram Moolenaar3982c542005-06-08 21:56:31 +00009099 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009100 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009101 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009102 if (STRLEN(gettail(innames[i])) < 5
9103 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009104 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009105 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9106 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009107 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009108 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9109 spin.si_region_name[i * 2 + 1] =
9110 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009111 }
9112 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009113 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009114
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009115 spin.si_foldroot = wordtree_alloc(&spin);
9116 spin.si_keeproot = wordtree_alloc(&spin);
9117 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009118 if (spin.si_foldroot == NULL
9119 || spin.si_keeproot == NULL
9120 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009121 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009122 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009123 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009124 }
9125
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009126 /* When not producing a .add.spl file clear the character table when
9127 * we encounter one in the .aff file. This means we dump the current
9128 * one in the .spl file if the .aff file doesn't define one. That's
9129 * better than guessing the contents, the table will match a
9130 * previously loaded spell file. */
9131 if (!spin.si_add)
9132 spin.si_clear_chartab = TRUE;
9133
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134 /*
9135 * Read all the .aff and .dic files.
9136 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009137 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009139 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009140 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009141 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009142 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009143
Bram Moolenaarb765d632005-06-07 21:00:02 +00009144 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009145 if (mch_stat((char *)fname, &st) >= 0)
9146 {
9147 /* Read the .aff file. Will init "spin->si_conv" based on the
9148 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009149 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009150 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009151 error = TRUE;
9152 else
9153 {
9154 /* Read the .dic file and store the words in the trees. */
9155 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009156 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009157 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009158 error = TRUE;
9159 }
9160 }
9161 else
9162 {
9163 /* No .aff file, try reading the file as a word list. Store
9164 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009165 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009166 error = TRUE;
9167 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009168
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009170 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009171 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009172#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009173 }
9174
Bram Moolenaar78622822005-08-23 21:00:13 +00009175 if (spin.si_compflags != NULL && spin.si_nobreak)
9176 MSG(_("Warning: both compounding and NOBREAK specified"));
9177
Bram Moolenaar4770d092006-01-12 23:22:24 +00009178 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009179 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009180 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009181 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009182 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009183 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009184 wordtree_compress(&spin, spin.si_foldroot);
9185 wordtree_compress(&spin, spin.si_keeproot);
9186 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009187 }
9188
Bram Moolenaar4770d092006-01-12 23:22:24 +00009189 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009190 {
9191 /*
9192 * Write the info in the spell file.
9193 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009194 vim_snprintf((char *)IObuff, IOSIZE,
9195 _("Writing spell file %s ..."), wfname);
9196 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009197
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009198 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009199
Bram Moolenaar4770d092006-01-12 23:22:24 +00009200 spell_message(&spin, (char_u *)_("Done!"));
9201 vim_snprintf((char *)IObuff, IOSIZE,
9202 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9203 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009204
Bram Moolenaar4770d092006-01-12 23:22:24 +00009205 /*
9206 * If the file is loaded need to reload it.
9207 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009208 if (!error)
9209 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009210 }
9211
9212 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009214 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 ga_clear(&spin.si_sal);
9216 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009217 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009218 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009219 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009220
9221 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009222 for (i = 0; i < incount; ++i)
9223 if (afile[i] != NULL)
9224 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009225
9226 /* Free all the bits and pieces at once. */
9227 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009228
9229 /*
9230 * If there is soundfolding info and no NOSUGFILE item create the
9231 * .sug file with the soundfolded word trie.
9232 */
9233 if (spin.si_sugtime != 0 && !error && !got_int)
9234 spell_make_sugfile(&spin, wfname);
9235
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009236 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009237}
9238
Bram Moolenaar4770d092006-01-12 23:22:24 +00009239/*
9240 * Display a message for spell file processing when 'verbose' is set or using
9241 * ":mkspell". "str" can be IObuff.
9242 */
9243 static void
9244spell_message(spin, str)
9245 spellinfo_T *spin;
9246 char_u *str;
9247{
9248 if (spin->si_verbose || p_verbose > 2)
9249 {
9250 if (!spin->si_verbose)
9251 verbose_enter();
9252 MSG(str);
9253 out_flush();
9254 if (!spin->si_verbose)
9255 verbose_leave();
9256 }
9257}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009258
9259/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009260 * ":[count]spellgood {word}"
9261 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009262 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009263 */
9264 void
9265ex_spell(eap)
9266 exarg_T *eap;
9267{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009268 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009269 eap->forceit ? 0 : (int)eap->line2,
9270 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009271}
9272
9273/*
9274 * Add "word[len]" to 'spellfile' as a good or bad word.
9275 */
9276 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009277spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009278 char_u *word;
9279 int len;
9280 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009281 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009282 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009283 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009284{
Bram Moolenaara3917072006-09-14 08:48:14 +00009285 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009286 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009287 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009288 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009289 char_u fnamebuf[MAXPATHL];
9290 char_u line[MAXWLEN * 2];
9291 long fpos, fpos_next = 0;
9292 int i;
9293 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009294
Bram Moolenaar89d40322006-08-29 15:30:07 +00009295 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009296 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009297 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009298 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009299 int_wordlist = vim_tempname('s');
9300 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009301 return;
9302 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009303 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009304 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009305 else
9306 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009307 /* If 'spellfile' isn't set figure out a good default value. */
9308 if (*curbuf->b_p_spf == NUL)
9309 {
9310 init_spellfile();
9311 new_spf = TRUE;
9312 }
9313
9314 if (*curbuf->b_p_spf == NUL)
9315 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009316 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009317 return;
9318 }
9319
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009320 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9321 {
9322 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009323 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009324 break;
9325 if (*spf == NUL)
9326 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009327 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009328 return;
9329 }
9330 }
9331
Bram Moolenaarb765d632005-06-07 21:00:02 +00009332 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009333 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009334 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9335 buf = NULL;
9336 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009337 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009338 EMSG(_(e_bufloaded));
9339 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009340 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009341
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009342 fname = fnamebuf;
9343 }
9344
Bram Moolenaard0131a82006-03-04 21:46:13 +00009345 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009346 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009347 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009348 * since its flags sort before the one with WF_BANNED. */
9349 fd = mch_fopen((char *)fname, "r");
9350 if (fd != NULL)
9351 {
9352 while (!vim_fgets(line, MAXWLEN * 2, fd))
9353 {
9354 fpos = fpos_next;
9355 fpos_next = ftell(fd);
9356 if (STRNCMP(word, line, len) == 0
9357 && (line[len] == '/' || line[len] < ' '))
9358 {
9359 /* Found duplicate word. Remove it by writing a '#' at
9360 * the start of the line. Mixing reading and writing
9361 * doesn't work for all systems, close the file first. */
9362 fclose(fd);
9363 fd = mch_fopen((char *)fname, "r+");
9364 if (fd == NULL)
9365 break;
9366 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009367 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009368 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009369 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009370 {
9371 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009372 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009373 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009374 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009375 fseek(fd, fpos_next, SEEK_SET);
9376 }
9377 }
9378 fclose(fd);
9379 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009380 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009381
9382 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009383 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009384 fd = mch_fopen((char *)fname, "a");
9385 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009386 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009387 char_u *p;
9388
Bram Moolenaard0131a82006-03-04 21:46:13 +00009389 /* We just initialized the 'spellfile' option and can't open the
9390 * file. We may need to create the "spell" directory first. We
9391 * already checked the runtime directory is writable in
9392 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009393 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009394 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009395 int c = *p;
9396
Bram Moolenaard0131a82006-03-04 21:46:13 +00009397 /* The directory doesn't exist. Try creating it and opening
9398 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009399 *p = NUL;
9400 vim_mkdir(fname, 0755);
9401 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009402 fd = mch_fopen((char *)fname, "a");
9403 }
9404 }
9405
9406 if (fd == NULL)
9407 EMSG2(_(e_notopen), fname);
9408 else
9409 {
9410 if (bad)
9411 fprintf(fd, "%.*s/!\n", len, word);
9412 else
9413 fprintf(fd, "%.*s\n", len, word);
9414 fclose(fd);
9415
9416 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9417 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009418 }
9419 }
9420
Bram Moolenaard0131a82006-03-04 21:46:13 +00009421 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009422 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009423 /* Update the .add.spl file. */
9424 mkspell(1, &fname, FALSE, TRUE, TRUE);
9425
9426 /* If the .add file is edited somewhere, reload it. */
9427 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009428 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009429
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009430 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009431 }
9432}
9433
9434/*
9435 * Initialize 'spellfile' for the current buffer.
9436 */
9437 static void
9438init_spellfile()
9439{
9440 char_u buf[MAXPATHL];
9441 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009442 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009443 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009444 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009445 int aspath = FALSE;
9446 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009447
9448 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9449 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009450 /* Find the end of the language name. Exclude the region. If there
9451 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009452 for (lend = curbuf->b_p_spl; *lend != NUL
9453 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009454 if (vim_ispathsep(*lend))
9455 {
9456 aspath = TRUE;
9457 lstart = lend + 1;
9458 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009459
9460 /* Loop over all entries in 'runtimepath'. Use the first one where we
9461 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009462 rtp = p_rtp;
9463 while (*rtp != NUL)
9464 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009465 if (aspath)
9466 /* Use directory of an entry with path, e.g., for
9467 * "/dir/lg.utf-8.spl" use "/dir". */
9468 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9469 else
9470 /* Copy the path from 'runtimepath' to buf[]. */
9471 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009472 if (filewritable(buf) == 2)
9473 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009474 /* Use the first language name from 'spelllang' and the
9475 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009476 if (aspath)
9477 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9478 else
9479 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009480 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009481 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009482 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9483 if (!filewritable(buf) != 2)
9484 vim_mkdir(buf, 0755);
9485
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009486 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009487 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009488 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009489 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009490 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009491 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9492 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9493 fname != NULL
9494 && strstr((char *)gettail(fname), ".ascii.") != NULL
9495 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009496 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9497 break;
9498 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009499 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009500 }
9501 }
9502}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009503
Bram Moolenaar51485f02005-06-04 21:55:20 +00009504
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009505/*
9506 * Init the chartab used for spelling for ASCII.
9507 * EBCDIC is not supported!
9508 */
9509 static void
9510clear_spell_chartab(sp)
9511 spelltab_T *sp;
9512{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009513 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009514
9515 /* Init everything to FALSE. */
9516 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9517 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9518 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009519 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009520 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009521 sp->st_upper[i] = i;
9522 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009523
9524 /* We include digits. A word shouldn't start with a digit, but handling
9525 * that is done separately. */
9526 for (i = '0'; i <= '9'; ++i)
9527 sp->st_isw[i] = TRUE;
9528 for (i = 'A'; i <= 'Z'; ++i)
9529 {
9530 sp->st_isw[i] = TRUE;
9531 sp->st_isu[i] = TRUE;
9532 sp->st_fold[i] = i + 0x20;
9533 }
9534 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009535 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009536 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009537 sp->st_upper[i] = i - 0x20;
9538 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009539}
9540
9541/*
9542 * Init the chartab used for spelling. Only depends on 'encoding'.
9543 * Called once while starting up and when 'encoding' changes.
9544 * The default is to use isalpha(), but the spell file should define the word
9545 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009546 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009547 */
9548 void
9549init_spell_chartab()
9550{
9551 int i;
9552
9553 did_set_spelltab = FALSE;
9554 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009555#ifdef FEAT_MBYTE
9556 if (enc_dbcs)
9557 {
9558 /* DBCS: assume double-wide characters are word characters. */
9559 for (i = 128; i <= 255; ++i)
9560 if (MB_BYTE2LEN(i) == 2)
9561 spelltab.st_isw[i] = TRUE;
9562 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009563 else if (enc_utf8)
9564 {
9565 for (i = 128; i < 256; ++i)
9566 {
9567 spelltab.st_isu[i] = utf_isupper(i);
9568 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9569 spelltab.st_fold[i] = utf_fold(i);
9570 spelltab.st_upper[i] = utf_toupper(i);
9571 }
9572 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009573 else
9574#endif
9575 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009576 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009577 for (i = 128; i < 256; ++i)
9578 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009579 if (MB_ISUPPER(i))
9580 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009581 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009582 spelltab.st_isu[i] = TRUE;
9583 spelltab.st_fold[i] = MB_TOLOWER(i);
9584 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009585 else if (MB_ISLOWER(i))
9586 {
9587 spelltab.st_isw[i] = TRUE;
9588 spelltab.st_upper[i] = MB_TOUPPER(i);
9589 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009590 }
9591 }
9592}
9593
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009594/*
9595 * Set the spell character tables from strings in the affix file.
9596 */
9597 static int
9598set_spell_chartab(fol, low, upp)
9599 char_u *fol;
9600 char_u *low;
9601 char_u *upp;
9602{
9603 /* We build the new tables here first, so that we can compare with the
9604 * previous one. */
9605 spelltab_T new_st;
9606 char_u *pf = fol, *pl = low, *pu = upp;
9607 int f, l, u;
9608
9609 clear_spell_chartab(&new_st);
9610
9611 while (*pf != NUL)
9612 {
9613 if (*pl == NUL || *pu == NUL)
9614 {
9615 EMSG(_(e_affform));
9616 return FAIL;
9617 }
9618#ifdef FEAT_MBYTE
9619 f = mb_ptr2char_adv(&pf);
9620 l = mb_ptr2char_adv(&pl);
9621 u = mb_ptr2char_adv(&pu);
9622#else
9623 f = *pf++;
9624 l = *pl++;
9625 u = *pu++;
9626#endif
9627 /* Every character that appears is a word character. */
9628 if (f < 256)
9629 new_st.st_isw[f] = TRUE;
9630 if (l < 256)
9631 new_st.st_isw[l] = TRUE;
9632 if (u < 256)
9633 new_st.st_isw[u] = TRUE;
9634
9635 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9636 * case-folding */
9637 if (l < 256 && l != f)
9638 {
9639 if (f >= 256)
9640 {
9641 EMSG(_(e_affrange));
9642 return FAIL;
9643 }
9644 new_st.st_fold[l] = f;
9645 }
9646
9647 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009648 * case-folding, it's upper case and the "UPP" is the upper case of
9649 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009650 if (u < 256 && u != f)
9651 {
9652 if (f >= 256)
9653 {
9654 EMSG(_(e_affrange));
9655 return FAIL;
9656 }
9657 new_st.st_fold[u] = f;
9658 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009659 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660 }
9661 }
9662
9663 if (*pl != NUL || *pu != NUL)
9664 {
9665 EMSG(_(e_affform));
9666 return FAIL;
9667 }
9668
9669 return set_spell_finish(&new_st);
9670}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009671
9672/*
9673 * Set the spell character tables from strings in the .spl file.
9674 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009675 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009676set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009677 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009678 int cnt; /* length of "flags" */
9679 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009680{
9681 /* We build the new tables here first, so that we can compare with the
9682 * previous one. */
9683 spelltab_T new_st;
9684 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009685 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009686 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009687
9688 clear_spell_chartab(&new_st);
9689
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009690 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009691 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009692 if (i < cnt)
9693 {
9694 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9695 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9696 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009697
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009698 if (*p != NUL)
9699 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009700#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009701 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009702#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009703 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009704#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009705 new_st.st_fold[i + 128] = c;
9706 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9707 new_st.st_upper[c] = i + 128;
9708 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009709 }
9710
Bram Moolenaar5195e452005-08-19 20:32:47 +00009711 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009712}
9713
9714 static int
9715set_spell_finish(new_st)
9716 spelltab_T *new_st;
9717{
9718 int i;
9719
9720 if (did_set_spelltab)
9721 {
9722 /* check that it's the same table */
9723 for (i = 0; i < 256; ++i)
9724 {
9725 if (spelltab.st_isw[i] != new_st->st_isw[i]
9726 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009727 || spelltab.st_fold[i] != new_st->st_fold[i]
9728 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009729 {
9730 EMSG(_("E763: Word characters differ between spell files"));
9731 return FAIL;
9732 }
9733 }
9734 }
9735 else
9736 {
9737 /* copy the new spelltab into the one being used */
9738 spelltab = *new_st;
9739 did_set_spelltab = TRUE;
9740 }
9741
9742 return OK;
9743}
9744
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009745/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009746 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009747 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009748 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009749 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009750 */
9751 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009752spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009753 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009754 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009755{
Bram Moolenaarea408852005-06-25 22:49:46 +00009756#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009757 char_u *s;
9758 int l;
9759 int c;
9760
9761 if (has_mbyte)
9762 {
9763 l = MB_BYTE2LEN(*p);
9764 s = p;
9765 if (l == 1)
9766 {
9767 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009768 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009769 {
9770 s = p + 1; /* skip a mid-word character */
9771 l = MB_BYTE2LEN(*s);
9772 }
9773 }
9774 else
9775 {
9776 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009777 if (c < 256 ? buf->b_spell_ismw[c]
9778 : (buf->b_spell_ismw_mb != NULL
9779 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009780 {
9781 s = p + l;
9782 l = MB_BYTE2LEN(*s);
9783 }
9784 }
9785
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009786 c = mb_ptr2char(s);
9787 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009788 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009789 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009790 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009791#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009792
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009793 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9794}
9795
9796/*
9797 * Return TRUE if "p" points to a word character.
9798 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9799 */
9800 static int
9801spell_iswordp_nmw(p)
9802 char_u *p;
9803{
9804#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009805 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009806
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009807 if (has_mbyte)
9808 {
9809 c = mb_ptr2char(p);
9810 if (c > 255)
9811 return mb_get_class(p) >= 2;
9812 return spelltab.st_isw[c];
9813 }
9814#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009815 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009816}
9817
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009818#ifdef FEAT_MBYTE
9819/*
9820 * Return TRUE if "p" points to a word character.
9821 * Wide version of spell_iswordp().
9822 */
9823 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009824spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009825 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009826 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009827{
9828 int *s;
9829
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009830 if (*p < 256 ? buf->b_spell_ismw[*p]
9831 : (buf->b_spell_ismw_mb != NULL
9832 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009833 s = p + 1;
9834 else
9835 s = p;
9836
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009837 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009838 {
9839 if (enc_utf8)
9840 return utf_class(*s) >= 2;
9841 if (enc_dbcs)
9842 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9843 return 0;
9844 }
9845 return spelltab.st_isw[*s];
9846}
9847#endif
9848
Bram Moolenaarea408852005-06-25 22:49:46 +00009849/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009850 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009851 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009852 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009853 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009854write_spell_prefcond(fd, gap)
9855 FILE *fd;
9856 garray_T *gap;
9857{
9858 int i;
9859 char_u *p;
9860 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009861 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009862
Bram Moolenaar5195e452005-08-19 20:32:47 +00009863 if (fd != NULL)
9864 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9865
9866 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009867
9868 for (i = 0; i < gap->ga_len; ++i)
9869 {
9870 /* <prefcond> : <condlen> <condstr> */
9871 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009872 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009873 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009874 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009875 if (fd != NULL)
9876 {
9877 fputc(len, fd);
9878 fwrite(p, (size_t)len, (size_t)1, fd);
9879 }
9880 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009881 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009882 else if (fd != NULL)
9883 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009884 }
9885
Bram Moolenaar5195e452005-08-19 20:32:47 +00009886 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009887}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009888
9889/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009890 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9891 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009892 * When using a multi-byte 'encoding' the length may change!
9893 * Returns FAIL when something wrong.
9894 */
9895 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009896spell_casefold(str, len, buf, buflen)
9897 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009898 int len;
9899 char_u *buf;
9900 int buflen;
9901{
9902 int i;
9903
9904 if (len >= buflen)
9905 {
9906 buf[0] = NUL;
9907 return FAIL; /* result will not fit */
9908 }
9909
9910#ifdef FEAT_MBYTE
9911 if (has_mbyte)
9912 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009913 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009914 char_u *p;
9915 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009916
9917 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009918 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009919 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009920 if (outi + MB_MAXBYTES > buflen)
9921 {
9922 buf[outi] = NUL;
9923 return FAIL;
9924 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009925 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009926 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009927 }
9928 buf[outi] = NUL;
9929 }
9930 else
9931#endif
9932 {
9933 /* Be quick for non-multibyte encodings. */
9934 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009935 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009936 buf[i] = NUL;
9937 }
9938
9939 return OK;
9940}
9941
Bram Moolenaar4770d092006-01-12 23:22:24 +00009942/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009943#define SPS_BEST 1
9944#define SPS_FAST 2
9945#define SPS_DOUBLE 4
9946
Bram Moolenaar4770d092006-01-12 23:22:24 +00009947static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9948static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009949
9950/*
9951 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009952 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009953 */
9954 int
9955spell_check_sps()
9956{
9957 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009958 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009959 char_u buf[MAXPATHL];
9960 int f;
9961
9962 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009963 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009964
9965 for (p = p_sps; *p != NUL; )
9966 {
9967 copy_option_part(&p, buf, MAXPATHL, ",");
9968
9969 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009970 if (VIM_ISDIGIT(*buf))
9971 {
9972 s = buf;
9973 sps_limit = getdigits(&s);
9974 if (*s != NUL && !VIM_ISDIGIT(*s))
9975 f = -1;
9976 }
9977 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009978 f = SPS_BEST;
9979 else if (STRCMP(buf, "fast") == 0)
9980 f = SPS_FAST;
9981 else if (STRCMP(buf, "double") == 0)
9982 f = SPS_DOUBLE;
9983 else if (STRNCMP(buf, "expr:", 5) != 0
9984 && STRNCMP(buf, "file:", 5) != 0)
9985 f = -1;
9986
9987 if (f == -1 || (sps_flags != 0 && f != 0))
9988 {
9989 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009990 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009991 return FAIL;
9992 }
9993 if (f != 0)
9994 sps_flags = f;
9995 }
9996
9997 if (sps_flags == 0)
9998 sps_flags = SPS_BEST;
9999
10000 return OK;
10001}
10002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010003/*
10004 * "z?": Find badly spelled word under or after the cursor.
10005 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010006 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010007 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 */
10009 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010010spell_suggest(count)
10011 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012{
10013 char_u *line;
10014 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 char_u wcopy[MAXWLEN + 2];
10016 char_u *p;
10017 int i;
10018 int c;
10019 suginfo_T sug;
10020 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010021 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010022 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010023 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010024 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010025 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010026
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010027 if (no_spell_checking(curwin))
10028 return;
10029
10030#ifdef FEAT_VISUAL
10031 if (VIsual_active)
10032 {
10033 /* Use the Visually selected text as the bad word. But reject
10034 * a multi-line selection. */
10035 if (curwin->w_cursor.lnum != VIsual.lnum)
10036 {
10037 vim_beep();
10038 return;
10039 }
10040 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10041 if (badlen < 0)
10042 badlen = -badlen;
10043 else
10044 curwin->w_cursor.col = VIsual.col;
10045 ++badlen;
10046 end_visual_mode();
10047 }
10048 else
10049#endif
10050 /* Find the start of the badly spelled word. */
10051 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010052 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010054 /* No bad word or it starts after the cursor: use the word under the
10055 * cursor. */
10056 curwin->w_cursor = prev_cursor;
10057 line = ml_get_curline();
10058 p = line + curwin->w_cursor.col;
10059 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010060 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010061 mb_ptr_back(line, p);
10062 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010063 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010064 mb_ptr_adv(p);
10065
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010066 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010067 {
10068 beep_flush();
10069 return;
10070 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010071 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 }
10073
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010074 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010076 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010077 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010078
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010079 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010080
Bram Moolenaar5195e452005-08-19 20:32:47 +000010081 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10082 * 'spellsuggest', whatever is smaller. */
10083 if (sps_limit > (int)Rows - 2)
10084 limit = (int)Rows - 2;
10085 else
10086 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010087 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010088 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089
10090 if (sug.su_ga.ga_len == 0)
10091 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010092 else if (count > 0)
10093 {
10094 if (count > sug.su_ga.ga_len)
10095 smsg((char_u *)_("Sorry, only %ld suggestions"),
10096 (long)sug.su_ga.ga_len);
10097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 else
10099 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010100 vim_free(repl_from);
10101 repl_from = NULL;
10102 vim_free(repl_to);
10103 repl_to = NULL;
10104
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010105#ifdef FEAT_RIGHTLEFT
10106 /* When 'rightleft' is set the list is drawn right-left. */
10107 cmdmsg_rl = curwin->w_p_rl;
10108 if (cmdmsg_rl)
10109 msg_col = Columns - 1;
10110#endif
10111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010112 /* List the suggestions. */
10113 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010114 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010115 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10117 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010118#ifdef FEAT_RIGHTLEFT
10119 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10120 {
10121 /* And now the rabbit from the high hat: Avoid showing the
10122 * untranslated message rightleft. */
10123 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10124 sug.su_badlen, sug.su_badptr);
10125 }
10126#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 msg_puts(IObuff);
10128 msg_clr_eos();
10129 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 msg_scroll = TRUE;
10132 for (i = 0; i < sug.su_ga.ga_len; ++i)
10133 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010134 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135
10136 /* The suggested word may replace only part of the bad word, add
10137 * the not replaced part. */
10138 STRCPY(wcopy, stp->st_word);
10139 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010140 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 sug.su_badptr + stp->st_orglen,
10142 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010143 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10144#ifdef FEAT_RIGHTLEFT
10145 if (cmdmsg_rl)
10146 rl_mirror(IObuff);
10147#endif
10148 msg_puts(IObuff);
10149
10150 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010151 msg_puts(IObuff);
10152
10153 /* The word may replace more than "su_badlen". */
10154 if (sug.su_badlen < stp->st_orglen)
10155 {
10156 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10157 stp->st_orglen, sug.su_badptr);
10158 msg_puts(IObuff);
10159 }
10160
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010161 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010162 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010163 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010164 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010165 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010166 stp->st_salscore ? "s " : "",
10167 stp->st_score, stp->st_altscore);
10168 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010169 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010170 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010171#ifdef FEAT_RIGHTLEFT
10172 if (cmdmsg_rl)
10173 /* Mirror the numbers, but keep the leading space. */
10174 rl_mirror(IObuff + 1);
10175#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010176 msg_advance(30);
10177 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 msg_putchar('\n');
10180 }
10181
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010182#ifdef FEAT_RIGHTLEFT
10183 cmdmsg_rl = FALSE;
10184 msg_col = 0;
10185#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010187 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010188 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010189 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010190 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 }
10192
Bram Moolenaard12a1322005-08-21 22:08:24 +000010193 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10194 {
10195 /* Save the from and to text for :spellrepall. */
10196 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010197 if (sug.su_badlen > stp->st_orglen)
10198 {
10199 /* Replacing less than "su_badlen", append the remainder to
10200 * repl_to. */
10201 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10202 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10203 sug.su_badlen - stp->st_orglen,
10204 sug.su_badptr + stp->st_orglen);
10205 repl_to = vim_strsave(IObuff);
10206 }
10207 else
10208 {
10209 /* Replacing su_badlen or more, use the whole word. */
10210 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10211 repl_to = vim_strsave(stp->st_word);
10212 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010213
10214 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010215 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010216 if (p != NULL)
10217 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010218 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010219 mch_memmove(p, line, c);
10220 STRCPY(p + c, stp->st_word);
10221 STRCAT(p, sug.su_badptr + stp->st_orglen);
10222 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10223 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010224
10225 /* For redo we use a change-word command. */
10226 ResetRedobuff();
10227 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010228 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010229 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010230 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010231
10232 /* After this "p" may be invalid. */
10233 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010234 }
10235 }
10236 else
10237 curwin->w_cursor = prev_cursor;
10238
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010239 spell_find_cleanup(&sug);
10240}
10241
10242/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010243 * Check if the word at line "lnum" column "col" is required to start with a
10244 * capital. This uses 'spellcapcheck' of the current buffer.
10245 */
10246 static int
10247check_need_cap(lnum, col)
10248 linenr_T lnum;
10249 colnr_T col;
10250{
10251 int need_cap = FALSE;
10252 char_u *line;
10253 char_u *line_copy = NULL;
10254 char_u *p;
10255 colnr_T endcol;
10256 regmatch_T regmatch;
10257
10258 if (curbuf->b_cap_prog == NULL)
10259 return FALSE;
10260
10261 line = ml_get_curline();
10262 endcol = 0;
10263 if ((int)(skipwhite(line) - line) >= (int)col)
10264 {
10265 /* At start of line, check if previous line is empty or sentence
10266 * ends there. */
10267 if (lnum == 1)
10268 need_cap = TRUE;
10269 else
10270 {
10271 line = ml_get(lnum - 1);
10272 if (*skipwhite(line) == NUL)
10273 need_cap = TRUE;
10274 else
10275 {
10276 /* Append a space in place of the line break. */
10277 line_copy = concat_str(line, (char_u *)" ");
10278 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010279 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010280 }
10281 }
10282 }
10283 else
10284 endcol = col;
10285
10286 if (endcol > 0)
10287 {
10288 /* Check if sentence ends before the bad word. */
10289 regmatch.regprog = curbuf->b_cap_prog;
10290 regmatch.rm_ic = FALSE;
10291 p = line + endcol;
10292 for (;;)
10293 {
10294 mb_ptr_back(line, p);
10295 if (p == line || spell_iswordp_nmw(p))
10296 break;
10297 if (vim_regexec(&regmatch, p, 0)
10298 && regmatch.endp[0] == line + endcol)
10299 {
10300 need_cap = TRUE;
10301 break;
10302 }
10303 }
10304 }
10305
10306 vim_free(line_copy);
10307
10308 return need_cap;
10309}
10310
10311
10312/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010313 * ":spellrepall"
10314 */
10315/*ARGSUSED*/
10316 void
10317ex_spellrepall(eap)
10318 exarg_T *eap;
10319{
10320 pos_T pos = curwin->w_cursor;
10321 char_u *frompat;
10322 int addlen;
10323 char_u *line;
10324 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010325 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010326 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010327
10328 if (repl_from == NULL || repl_to == NULL)
10329 {
10330 EMSG(_("E752: No previous spell replacement"));
10331 return;
10332 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010333 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010334
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010335 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010336 if (frompat == NULL)
10337 return;
10338 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10339 p_ws = FALSE;
10340
Bram Moolenaar5195e452005-08-19 20:32:47 +000010341 sub_nsubs = 0;
10342 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010343 curwin->w_cursor.lnum = 0;
10344 while (!got_int)
10345 {
10346 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10347 || u_save_cursor() == FAIL)
10348 break;
10349
10350 /* Only replace when the right word isn't there yet. This happens
10351 * when changing "etc" to "etc.". */
10352 line = ml_get_curline();
10353 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10354 repl_to, STRLEN(repl_to)) != 0)
10355 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010356 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010357 if (p == NULL)
10358 break;
10359 mch_memmove(p, line, curwin->w_cursor.col);
10360 STRCPY(p + curwin->w_cursor.col, repl_to);
10361 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10362 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10363 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010364
10365 if (curwin->w_cursor.lnum != prev_lnum)
10366 {
10367 ++sub_nlines;
10368 prev_lnum = curwin->w_cursor.lnum;
10369 }
10370 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010371 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010372 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010373 }
10374
10375 p_ws = save_ws;
10376 curwin->w_cursor = pos;
10377 vim_free(frompat);
10378
Bram Moolenaar5195e452005-08-19 20:32:47 +000010379 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010380 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010381 else
10382 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010383}
10384
10385/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010386 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10387 * a list of allocated strings.
10388 */
10389 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010390spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010391 garray_T *gap;
10392 char_u *word;
10393 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010394 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010395 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010396{
10397 suginfo_T sug;
10398 int i;
10399 suggest_T *stp;
10400 char_u *wcopy;
10401
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010402 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010403
10404 /* Make room in "gap". */
10405 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010406 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010407 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010408 for (i = 0; i < sug.su_ga.ga_len; ++i)
10409 {
10410 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010411
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010412 /* The suggested word may replace only part of "word", add the not
10413 * replaced part. */
10414 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010415 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010416 if (wcopy == NULL)
10417 break;
10418 STRCPY(wcopy, stp->st_word);
10419 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10420 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10421 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010422 }
10423
10424 spell_find_cleanup(&sug);
10425}
10426
10427/*
10428 * Find spell suggestions for the word at the start of "badptr".
10429 * Return the suggestions in "su->su_ga".
10430 * The maximum number of suggestions is "maxcount".
10431 * Note: does use info for the current window.
10432 * This is based on the mechanisms of Aspell, but completely reimplemented.
10433 */
10434 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010435spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010436 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010437 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010438 suginfo_T *su;
10439 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010440 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010441 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010442 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010443{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010444 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010445 char_u buf[MAXPATHL];
10446 char_u *p;
10447 int do_combine = FALSE;
10448 char_u *sps_copy;
10449#ifdef FEAT_EVAL
10450 static int expr_busy = FALSE;
10451#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010452 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010453 int i;
10454 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010455
10456 /*
10457 * Set the info in "*su".
10458 */
10459 vim_memset(su, 0, sizeof(suginfo_T));
10460 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10461 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010462 if (*badptr == NUL)
10463 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010464 hash_init(&su->su_banned);
10465
10466 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010467 if (badlen != 0)
10468 su->su_badlen = badlen;
10469 else
10470 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010471 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010472 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010473
10474 if (su->su_badlen >= MAXWLEN)
10475 su->su_badlen = MAXWLEN - 1; /* just in case */
10476 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10477 (void)spell_casefold(su->su_badptr, su->su_badlen,
10478 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010479 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010480 su->su_badflags = badword_captype(su->su_badptr,
10481 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010482 if (need_cap)
10483 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010484
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010485 /* Find the default language for sound folding. We simply use the first
10486 * one in 'spelllang' that supports sound folding. That's good for when
10487 * using multiple files for one language, it's not that bad when mixing
10488 * languages (e.g., "pl,en"). */
10489 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10490 {
10491 lp = LANGP_ENTRY(curbuf->b_langp, i);
10492 if (lp->lp_sallang != NULL)
10493 {
10494 su->su_sallang = lp->lp_sallang;
10495 break;
10496 }
10497 }
10498
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010499 /* Soundfold the bad word with the default sound folding, so that we don't
10500 * have to do this many times. */
10501 if (su->su_sallang != NULL)
10502 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10503 su->su_sal_badword);
10504
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010505 /* If the word is not capitalised and spell_check() doesn't consider the
10506 * word to be bad then it might need to be capitalised. Add a suggestion
10507 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010508 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010509 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010510 {
10511 make_case_word(su->su_badword, buf, WF_ONECAP);
10512 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010513 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010514 }
10515
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010516 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010517 if (banbadword)
10518 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010519
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010520 /* Make a copy of 'spellsuggest', because the expression may change it. */
10521 sps_copy = vim_strsave(p_sps);
10522 if (sps_copy == NULL)
10523 return;
10524
10525 /* Loop over the items in 'spellsuggest'. */
10526 for (p = sps_copy; *p != NUL; )
10527 {
10528 copy_option_part(&p, buf, MAXPATHL, ",");
10529
10530 if (STRNCMP(buf, "expr:", 5) == 0)
10531 {
10532#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010533 /* Evaluate an expression. Skip this when called recursively,
10534 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010535 if (!expr_busy)
10536 {
10537 expr_busy = TRUE;
10538 spell_suggest_expr(su, buf + 5);
10539 expr_busy = FALSE;
10540 }
10541#endif
10542 }
10543 else if (STRNCMP(buf, "file:", 5) == 0)
10544 /* Use list of suggestions in a file. */
10545 spell_suggest_file(su, buf + 5);
10546 else
10547 {
10548 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010549 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010550 if (sps_flags & SPS_DOUBLE)
10551 do_combine = TRUE;
10552 }
10553 }
10554
10555 vim_free(sps_copy);
10556
10557 if (do_combine)
10558 /* Combine the two list of suggestions. This must be done last,
10559 * because sorting changes the order again. */
10560 score_combine(su);
10561}
10562
10563#ifdef FEAT_EVAL
10564/*
10565 * Find suggestions by evaluating expression "expr".
10566 */
10567 static void
10568spell_suggest_expr(su, expr)
10569 suginfo_T *su;
10570 char_u *expr;
10571{
10572 list_T *list;
10573 listitem_T *li;
10574 int score;
10575 char_u *p;
10576
10577 /* The work is split up in a few parts to avoid having to export
10578 * suginfo_T.
10579 * First evaluate the expression and get the resulting list. */
10580 list = eval_spell_expr(su->su_badword, expr);
10581 if (list != NULL)
10582 {
10583 /* Loop over the items in the list. */
10584 for (li = list->lv_first; li != NULL; li = li->li_next)
10585 if (li->li_tv.v_type == VAR_LIST)
10586 {
10587 /* Get the word and the score from the items. */
10588 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010589 if (score >= 0 && score <= su->su_maxscore)
10590 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10591 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010592 }
10593 list_unref(list);
10594 }
10595
Bram Moolenaar4770d092006-01-12 23:22:24 +000010596 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10597 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010598 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10599}
10600#endif
10601
10602/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010603 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010604 */
10605 static void
10606spell_suggest_file(su, fname)
10607 suginfo_T *su;
10608 char_u *fname;
10609{
10610 FILE *fd;
10611 char_u line[MAXWLEN * 2];
10612 char_u *p;
10613 int len;
10614 char_u cword[MAXWLEN];
10615
10616 /* Open the file. */
10617 fd = mch_fopen((char *)fname, "r");
10618 if (fd == NULL)
10619 {
10620 EMSG2(_(e_notopen), fname);
10621 return;
10622 }
10623
10624 /* Read it line by line. */
10625 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10626 {
10627 line_breakcheck();
10628
10629 p = vim_strchr(line, '/');
10630 if (p == NULL)
10631 continue; /* No Tab found, just skip the line. */
10632 *p++ = NUL;
10633 if (STRICMP(su->su_badword, line) == 0)
10634 {
10635 /* Match! Isolate the good word, until CR or NL. */
10636 for (len = 0; p[len] >= ' '; ++len)
10637 ;
10638 p[len] = NUL;
10639
10640 /* If the suggestion doesn't have specific case duplicate the case
10641 * of the bad word. */
10642 if (captype(p, NULL) == 0)
10643 {
10644 make_case_word(p, cword, su->su_badflags);
10645 p = cword;
10646 }
10647
10648 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010649 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010650 }
10651 }
10652
10653 fclose(fd);
10654
Bram Moolenaar4770d092006-01-12 23:22:24 +000010655 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10656 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010657 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10658}
10659
10660/*
10661 * Find suggestions for the internal method indicated by "sps_flags".
10662 */
10663 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010664spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010665 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010666 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010667{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010668 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010669 * Load the .sug file(s) that are available and not done yet.
10670 */
10671 suggest_load_files();
10672
10673 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010674 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010675 *
10676 * Set a maximum score to limit the combination of operations that is
10677 * tried.
10678 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010679 suggest_try_special(su);
10680
10681 /*
10682 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10683 * from the .aff file and inserting a space (split the word).
10684 */
10685 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010686
10687 /* For the resulting top-scorers compute the sound-a-like score. */
10688 if (sps_flags & SPS_DOUBLE)
10689 score_comp_sal(su);
10690
10691 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010692 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010693 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010694 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010695 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010696 if (sps_flags & SPS_BEST)
10697 /* Adjust the word score for the suggestions found so far for how
10698 * they sounds like. */
10699 rescore_suggestions(su);
10700
10701 /*
10702 * While going throught the soundfold tree "su_maxscore" is the score
10703 * for the soundfold word, limits the changes that are being tried,
10704 * and "su_sfmaxscore" the rescored score, which is set by
10705 * cleanup_suggestions().
10706 * First find words with a small edit distance, because this is much
10707 * faster and often already finds the top-N suggestions. If we didn't
10708 * find many suggestions try again with a higher edit distance.
10709 * "sl_sounddone" is used to avoid doing the same word twice.
10710 */
10711 suggest_try_soundalike_prep();
10712 su->su_maxscore = SCORE_SFMAX1;
10713 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010714 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010715 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10716 {
10717 /* We didn't find enough matches, try again, allowing more
10718 * changes to the soundfold word. */
10719 su->su_maxscore = SCORE_SFMAX2;
10720 suggest_try_soundalike(su);
10721 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10722 {
10723 /* Still didn't find enough matches, try again, allowing even
10724 * more changes to the soundfold word. */
10725 su->su_maxscore = SCORE_SFMAX3;
10726 suggest_try_soundalike(su);
10727 }
10728 }
10729 su->su_maxscore = su->su_sfmaxscore;
10730 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010731 }
10732
Bram Moolenaar4770d092006-01-12 23:22:24 +000010733 /* When CTRL-C was hit while searching do show the results. Only clear
10734 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010735 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010736 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010737 {
10738 (void)vgetc();
10739 got_int = FALSE;
10740 }
10741
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010742 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010743 {
10744 if (sps_flags & SPS_BEST)
10745 /* Adjust the word score for how it sounds like. */
10746 rescore_suggestions(su);
10747
Bram Moolenaar4770d092006-01-12 23:22:24 +000010748 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10749 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010750 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010751 }
10752}
10753
10754/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010755 * Load the .sug files for languages that have one and weren't loaded yet.
10756 */
10757 static void
10758suggest_load_files()
10759{
10760 langp_T *lp;
10761 int lpi;
10762 slang_T *slang;
10763 char_u *dotp;
10764 FILE *fd;
10765 char_u buf[MAXWLEN];
10766 int i;
10767 time_t timestamp;
10768 int wcount;
10769 int wordnr;
10770 garray_T ga;
10771 int c;
10772
10773 /* Do this for all languages that support sound folding. */
10774 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10775 {
10776 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10777 slang = lp->lp_slang;
10778 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10779 {
10780 /* Change ".spl" to ".sug" and open the file. When the file isn't
10781 * found silently skip it. Do set "sl_sugloaded" so that we
10782 * don't try again and again. */
10783 slang->sl_sugloaded = TRUE;
10784
10785 dotp = vim_strrchr(slang->sl_fname, '.');
10786 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10787 continue;
10788 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010789 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010790 if (fd == NULL)
10791 goto nextone;
10792
10793 /*
10794 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10795 */
10796 for (i = 0; i < VIMSUGMAGICL; ++i)
10797 buf[i] = getc(fd); /* <fileID> */
10798 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10799 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010800 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010801 slang->sl_fname);
10802 goto nextone;
10803 }
10804 c = getc(fd); /* <versionnr> */
10805 if (c < VIMSUGVERSION)
10806 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010807 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010808 slang->sl_fname);
10809 goto nextone;
10810 }
10811 else if (c > VIMSUGVERSION)
10812 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010813 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010814 slang->sl_fname);
10815 goto nextone;
10816 }
10817
10818 /* Check the timestamp, it must be exactly the same as the one in
10819 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010820 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010821 if (timestamp != slang->sl_sugtime)
10822 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010823 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010824 slang->sl_fname);
10825 goto nextone;
10826 }
10827
10828 /*
10829 * <SUGWORDTREE>: <wordtree>
10830 * Read the trie with the soundfolded words.
10831 */
10832 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10833 FALSE, 0) != 0)
10834 {
10835someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010836 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010837 slang->sl_fname);
10838 slang_clear_sug(slang);
10839 goto nextone;
10840 }
10841
10842 /*
10843 * <SUGTABLE>: <sugwcount> <sugline> ...
10844 *
10845 * Read the table with word numbers. We use a file buffer for
10846 * this, because it's so much like a file with lines. Makes it
10847 * possible to swap the info and save on memory use.
10848 */
10849 slang->sl_sugbuf = open_spellbuf();
10850 if (slang->sl_sugbuf == NULL)
10851 goto someerror;
10852 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010853 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010854 if (wcount < 0)
10855 goto someerror;
10856
10857 /* Read all the wordnr lists into the buffer, one NUL terminated
10858 * list per line. */
10859 ga_init2(&ga, 1, 100);
10860 for (wordnr = 0; wordnr < wcount; ++wordnr)
10861 {
10862 ga.ga_len = 0;
10863 for (;;)
10864 {
10865 c = getc(fd); /* <sugline> */
10866 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10867 goto someerror;
10868 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10869 if (c == NUL)
10870 break;
10871 }
10872 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10873 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10874 goto someerror;
10875 }
10876 ga_clear(&ga);
10877
10878 /*
10879 * Need to put word counts in the word tries, so that we can find
10880 * a word by its number.
10881 */
10882 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10883 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10884
10885nextone:
10886 if (fd != NULL)
10887 fclose(fd);
10888 STRCPY(dotp, ".spl");
10889 }
10890 }
10891}
10892
10893
10894/*
10895 * Fill in the wordcount fields for a trie.
10896 * Returns the total number of words.
10897 */
10898 static void
10899tree_count_words(byts, idxs)
10900 char_u *byts;
10901 idx_T *idxs;
10902{
10903 int depth;
10904 idx_T arridx[MAXWLEN];
10905 int curi[MAXWLEN];
10906 int c;
10907 idx_T n;
10908 int wordcount[MAXWLEN];
10909
10910 arridx[0] = 0;
10911 curi[0] = 1;
10912 wordcount[0] = 0;
10913 depth = 0;
10914 while (depth >= 0 && !got_int)
10915 {
10916 if (curi[depth] > byts[arridx[depth]])
10917 {
10918 /* Done all bytes at this node, go up one level. */
10919 idxs[arridx[depth]] = wordcount[depth];
10920 if (depth > 0)
10921 wordcount[depth - 1] += wordcount[depth];
10922
10923 --depth;
10924 fast_breakcheck();
10925 }
10926 else
10927 {
10928 /* Do one more byte at this node. */
10929 n = arridx[depth] + curi[depth];
10930 ++curi[depth];
10931
10932 c = byts[n];
10933 if (c == 0)
10934 {
10935 /* End of word, count it. */
10936 ++wordcount[depth];
10937
10938 /* Skip over any other NUL bytes (same word with different
10939 * flags). */
10940 while (byts[n + 1] == 0)
10941 {
10942 ++n;
10943 ++curi[depth];
10944 }
10945 }
10946 else
10947 {
10948 /* Normal char, go one level deeper to count the words. */
10949 ++depth;
10950 arridx[depth] = idxs[n];
10951 curi[depth] = 1;
10952 wordcount[depth] = 0;
10953 }
10954 }
10955 }
10956}
10957
10958/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010959 * Free the info put in "*su" by spell_find_suggest().
10960 */
10961 static void
10962spell_find_cleanup(su)
10963 suginfo_T *su;
10964{
10965 int i;
10966
10967 /* Free the suggestions. */
10968 for (i = 0; i < su->su_ga.ga_len; ++i)
10969 vim_free(SUG(su->su_ga, i).st_word);
10970 ga_clear(&su->su_ga);
10971 for (i = 0; i < su->su_sga.ga_len; ++i)
10972 vim_free(SUG(su->su_sga, i).st_word);
10973 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974
10975 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010976 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977}
10978
10979/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010980 * Make a copy of "word", with the first letter upper or lower cased, to
10981 * "wcopy[MAXWLEN]". "word" must not be empty.
10982 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 */
10984 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010985onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010986 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 char_u *wcopy;
10988 int upper; /* TRUE: first letter made upper case */
10989{
10990 char_u *p;
10991 int c;
10992 int l;
10993
10994 p = word;
10995#ifdef FEAT_MBYTE
10996 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010997 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 else
10999#endif
11000 c = *p++;
11001 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011002 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011004 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005#ifdef FEAT_MBYTE
11006 if (has_mbyte)
11007 l = mb_char2bytes(c, wcopy);
11008 else
11009#endif
11010 {
11011 l = 1;
11012 wcopy[0] = c;
11013 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011014 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015}
11016
11017/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011018 * Make a copy of "word" with all the letters upper cased into
11019 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 */
11021 static void
11022allcap_copy(word, wcopy)
11023 char_u *word;
11024 char_u *wcopy;
11025{
11026 char_u *s;
11027 char_u *d;
11028 int c;
11029
11030 d = wcopy;
11031 for (s = word; *s != NUL; )
11032 {
11033#ifdef FEAT_MBYTE
11034 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011035 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 else
11037#endif
11038 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011039
11040#ifdef FEAT_MBYTE
11041 /* We only change ß to SS when we are certain latin1 is used. It
11042 * would cause weird errors in other 8-bit encodings. */
11043 if (enc_latin1like && c == 0xdf)
11044 {
11045 c = 'S';
11046 if (d - wcopy >= MAXWLEN - 1)
11047 break;
11048 *d++ = c;
11049 }
11050 else
11051#endif
11052 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053
11054#ifdef FEAT_MBYTE
11055 if (has_mbyte)
11056 {
11057 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11058 break;
11059 d += mb_char2bytes(c, d);
11060 }
11061 else
11062#endif
11063 {
11064 if (d - wcopy >= MAXWLEN - 1)
11065 break;
11066 *d++ = c;
11067 }
11068 }
11069 *d = NUL;
11070}
11071
11072/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011073 * Try finding suggestions by recognizing specific situations.
11074 */
11075 static void
11076suggest_try_special(su)
11077 suginfo_T *su;
11078{
11079 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011080 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011081 int c;
11082 char_u word[MAXWLEN];
11083
11084 /*
11085 * Recognize a word that is repeated: "the the".
11086 */
11087 p = skiptowhite(su->su_fbadword);
11088 len = p - su->su_fbadword;
11089 p = skipwhite(p);
11090 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11091 {
11092 /* Include badflags: if the badword is onecap or allcap
11093 * use that for the goodword too: "The the" -> "The". */
11094 c = su->su_fbadword[len];
11095 su->su_fbadword[len] = NUL;
11096 make_case_word(su->su_fbadword, word, su->su_badflags);
11097 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011098
11099 /* Give a soundalike score of 0, compute the score as if deleting one
11100 * character. */
11101 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011102 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011103 }
11104}
11105
11106/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 * Try finding suggestions by adding/removing/swapping letters.
11108 */
11109 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011110suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 suginfo_T *su;
11112{
11113 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011114 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011116 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011117 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118
11119 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011120 * to find matches (esp. REP items). Append some more text, changing
11121 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011123 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011124 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011125 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011127 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011129 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011130
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011131 /* If reloading a spell file fails it's still in the list but
11132 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011133 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011134 continue;
11135
Bram Moolenaar4770d092006-01-12 23:22:24 +000011136 /* Try it for this language. Will add possible suggestions. */
11137 suggest_trie_walk(su, lp, fword, FALSE);
11138 }
11139}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140
Bram Moolenaar4770d092006-01-12 23:22:24 +000011141/* Check the maximum score, if we go over it we won't try this change. */
11142#define TRY_DEEPER(su, stack, depth, add) \
11143 (stack[depth].ts_score + (add) < su->su_maxscore)
11144
11145/*
11146 * Try finding suggestions by adding/removing/swapping letters.
11147 *
11148 * This uses a state machine. At each node in the tree we try various
11149 * operations. When trying if an operation works "depth" is increased and the
11150 * stack[] is used to store info. This allows combinations, thus insert one
11151 * character, replace one and delete another. The number of changes is
11152 * limited by su->su_maxscore.
11153 *
11154 * After implementing this I noticed an article by Kemal Oflazer that
11155 * describes something similar: "Error-tolerant Finite State Recognition with
11156 * Applications to Morphological Analysis and Spelling Correction" (1996).
11157 * The implementation in the article is simplified and requires a stack of
11158 * unknown depth. The implementation here only needs a stack depth equal to
11159 * the length of the word.
11160 *
11161 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11162 * The mechanism is the same, but we find a match with a sound-folded word
11163 * that comes from one or more original words. Each of these words may be
11164 * added, this is done by add_sound_suggest().
11165 * Don't use:
11166 * the prefix tree or the keep-case tree
11167 * "su->su_badlen"
11168 * anything to do with upper and lower case
11169 * anything to do with word or non-word characters ("spell_iswordp()")
11170 * banned words
11171 * word flags (rare, region, compounding)
11172 * word splitting for now
11173 * "similar_chars()"
11174 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11175 */
11176 static void
11177suggest_trie_walk(su, lp, fword, soundfold)
11178 suginfo_T *su;
11179 langp_T *lp;
11180 char_u *fword;
11181 int soundfold;
11182{
11183 char_u tword[MAXWLEN]; /* good word collected so far */
11184 trystate_T stack[MAXWLEN];
11185 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11186 * concatanation of prefix compound
11187 * words and split word. NUL terminated
11188 * when going deeper but not when coming
11189 * back. */
11190 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11191 trystate_T *sp;
11192 int newscore;
11193 int score;
11194 char_u *byts, *fbyts, *pbyts;
11195 idx_T *idxs, *fidxs, *pidxs;
11196 int depth;
11197 int c, c2, c3;
11198 int n = 0;
11199 int flags;
11200 garray_T *gap;
11201 idx_T arridx;
11202 int len;
11203 char_u *p;
11204 fromto_T *ftp;
11205 int fl = 0, tl;
11206 int repextra = 0; /* extra bytes in fword[] from REP item */
11207 slang_T *slang = lp->lp_slang;
11208 int fword_ends;
11209 int goodword_ends;
11210#ifdef DEBUG_TRIEWALK
11211 /* Stores the name of the change made at each level. */
11212 char_u changename[MAXWLEN][80];
11213#endif
11214 int breakcheckcount = 1000;
11215 int compound_ok;
11216
11217 /*
11218 * Go through the whole case-fold tree, try changes at each node.
11219 * "tword[]" contains the word collected from nodes in the tree.
11220 * "fword[]" the word we are trying to match with (initially the bad
11221 * word).
11222 */
11223 depth = 0;
11224 sp = &stack[0];
11225 vim_memset(sp, 0, sizeof(trystate_T));
11226 sp->ts_curi = 1;
11227
11228 if (soundfold)
11229 {
11230 /* Going through the soundfold tree. */
11231 byts = fbyts = slang->sl_sbyts;
11232 idxs = fidxs = slang->sl_sidxs;
11233 pbyts = NULL;
11234 pidxs = NULL;
11235 sp->ts_prefixdepth = PFD_NOPREFIX;
11236 sp->ts_state = STATE_START;
11237 }
11238 else
11239 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011240 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011241 * When there are postponed prefixes we need to use these first. At
11242 * the end of the prefix we continue in the case-fold tree.
11243 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011244 fbyts = slang->sl_fbyts;
11245 fidxs = slang->sl_fidxs;
11246 pbyts = slang->sl_pbyts;
11247 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011248 if (pbyts != NULL)
11249 {
11250 byts = pbyts;
11251 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011252 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011253 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11254 }
11255 else
11256 {
11257 byts = fbyts;
11258 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011259 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011260 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011261 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011262 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011263
Bram Moolenaar4770d092006-01-12 23:22:24 +000011264 /*
11265 * Loop to find all suggestions. At each round we either:
11266 * - For the current state try one operation, advance "ts_curi",
11267 * increase "depth".
11268 * - When a state is done go to the next, set "ts_state".
11269 * - When all states are tried decrease "depth".
11270 */
11271 while (depth >= 0 && !got_int)
11272 {
11273 sp = &stack[depth];
11274 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011275 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011276 case STATE_START:
11277 case STATE_NOPREFIX:
11278 /*
11279 * Start of node: Deal with NUL bytes, which means
11280 * tword[] may end here.
11281 */
11282 arridx = sp->ts_arridx; /* current node in the tree */
11283 len = byts[arridx]; /* bytes in this node */
11284 arridx += sp->ts_curi; /* index of current byte */
11285
11286 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011288 /* Skip over the NUL bytes, we use them later. */
11289 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11290 ;
11291 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292
Bram Moolenaar4770d092006-01-12 23:22:24 +000011293 /* Always past NUL bytes now. */
11294 n = (int)sp->ts_state;
11295 sp->ts_state = STATE_ENDNUL;
11296 sp->ts_save_badflags = su->su_badflags;
11297
11298 /* At end of a prefix or at start of prefixtree: check for
11299 * following word. */
11300 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011301 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011302 /* Set su->su_badflags to the caps type at this position.
11303 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011304#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011305 if (has_mbyte)
11306 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11307 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011308#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011309 n = sp->ts_fidx;
11310 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11311 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011312 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313#ifdef DEBUG_TRIEWALK
11314 sprintf(changename[depth], "prefix");
11315#endif
11316 go_deeper(stack, depth, 0);
11317 ++depth;
11318 sp = &stack[depth];
11319 sp->ts_prefixdepth = depth - 1;
11320 byts = fbyts;
11321 idxs = fidxs;
11322 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011323
Bram Moolenaar4770d092006-01-12 23:22:24 +000011324 /* Move the prefix to preword[] with the right case
11325 * and make find_keepcap_word() works. */
11326 tword[sp->ts_twordlen] = NUL;
11327 make_case_word(tword + sp->ts_splitoff,
11328 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011329 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011330 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011331 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011332 break;
11333 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011334
Bram Moolenaar4770d092006-01-12 23:22:24 +000011335 if (sp->ts_curi > len || byts[arridx] != 0)
11336 {
11337 /* Past bytes in node and/or past NUL bytes. */
11338 sp->ts_state = STATE_ENDNUL;
11339 sp->ts_save_badflags = su->su_badflags;
11340 break;
11341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342
Bram Moolenaar4770d092006-01-12 23:22:24 +000011343 /*
11344 * End of word in tree.
11345 */
11346 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347
Bram Moolenaar4770d092006-01-12 23:22:24 +000011348 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011349
11350 /* Skip words with the NOSUGGEST flag. */
11351 if (flags & WF_NOSUGGEST)
11352 break;
11353
Bram Moolenaar4770d092006-01-12 23:22:24 +000011354 fword_ends = (fword[sp->ts_fidx] == NUL
11355 || (soundfold
11356 ? vim_iswhite(fword[sp->ts_fidx])
11357 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11358 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359
Bram Moolenaar4770d092006-01-12 23:22:24 +000011360 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011361 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011362 {
11363 /* There was a prefix before the word. Check that the prefix
11364 * can be used with this word. */
11365 /* Count the length of the NULs in the prefix. If there are
11366 * none this must be the first try without a prefix. */
11367 n = stack[sp->ts_prefixdepth].ts_arridx;
11368 len = pbyts[n++];
11369 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11370 ;
11371 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011372 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011373 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011374 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011375 if (c == 0)
11376 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011377
Bram Moolenaar4770d092006-01-12 23:22:24 +000011378 /* Use the WF_RARE flag for a rare prefix. */
11379 if (c & WF_RAREPFX)
11380 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011381
Bram Moolenaar4770d092006-01-12 23:22:24 +000011382 /* Tricky: when checking for both prefix and compounding
11383 * we run into the prefix flag first.
11384 * Remember that it's OK, so that we accept the prefix
11385 * when arriving at a compound flag. */
11386 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011387 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011388 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011389
Bram Moolenaar4770d092006-01-12 23:22:24 +000011390 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11391 * appending another compound word below. */
11392 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011393 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011394 goodword_ends = FALSE;
11395 else
11396 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011397
Bram Moolenaar4770d092006-01-12 23:22:24 +000011398 p = NULL;
11399 compound_ok = TRUE;
11400 if (sp->ts_complen > sp->ts_compsplit)
11401 {
11402 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011403 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011404 /* There was a word before this word. When there was no
11405 * change in this word (it was correct) add the first word
11406 * as a suggestion. If this word was corrected too, we
11407 * need to check if a correct word follows. */
11408 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011409 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011410 && STRNCMP(fword + sp->ts_splitfidx,
11411 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011412 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011413 {
11414 preword[sp->ts_prewordlen] = NUL;
11415 newscore = score_wordcount_adj(slang, sp->ts_score,
11416 preword + sp->ts_prewordlen,
11417 sp->ts_prewordlen > 0);
11418 /* Add the suggestion if the score isn't too bad. */
11419 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011420 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011421 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011422 newscore, 0, FALSE,
11423 lp->lp_sallang, FALSE);
11424 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011425 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011426 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011427 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011428 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011429 /* There was a compound word before this word. If this
11430 * word does not support compounding then give up
11431 * (splitting is tried for the word without compound
11432 * flag). */
11433 if (((unsigned)flags >> 24) == 0
11434 || sp->ts_twordlen - sp->ts_splitoff
11435 < slang->sl_compminlen)
11436 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011437#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011438 /* For multi-byte chars check character length against
11439 * COMPOUNDMIN. */
11440 if (has_mbyte
11441 && slang->sl_compminlen > 0
11442 && mb_charlen(tword + sp->ts_splitoff)
11443 < slang->sl_compminlen)
11444 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011445#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011446
Bram Moolenaar4770d092006-01-12 23:22:24 +000011447 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11448 compflags[sp->ts_complen + 1] = NUL;
11449 vim_strncpy(preword + sp->ts_prewordlen,
11450 tword + sp->ts_splitoff,
11451 sp->ts_twordlen - sp->ts_splitoff);
11452 p = preword;
11453 while (*skiptowhite(p) != NUL)
11454 p = skipwhite(skiptowhite(p));
11455 if (fword_ends && !can_compound(slang, p,
11456 compflags + sp->ts_compsplit))
11457 /* Compound is not allowed. But it may still be
11458 * possible if we add another (short) word. */
11459 compound_ok = FALSE;
11460
11461 /* Get pointer to last char of previous word. */
11462 p = preword + sp->ts_prewordlen;
11463 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011464 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011466
Bram Moolenaar4770d092006-01-12 23:22:24 +000011467 /*
11468 * Form the word with proper case in preword.
11469 * If there is a word from a previous split, append.
11470 * For the soundfold tree don't change the case, simply append.
11471 */
11472 if (soundfold)
11473 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11474 else if (flags & WF_KEEPCAP)
11475 /* Must find the word in the keep-case tree. */
11476 find_keepcap_word(slang, tword + sp->ts_splitoff,
11477 preword + sp->ts_prewordlen);
11478 else
11479 {
11480 /* Include badflags: If the badword is onecap or allcap
11481 * use that for the goodword too. But if the badword is
11482 * allcap and it's only one char long use onecap. */
11483 c = su->su_badflags;
11484 if ((c & WF_ALLCAP)
11485#ifdef FEAT_MBYTE
11486 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11487#else
11488 && su->su_badlen == 1
11489#endif
11490 )
11491 c = WF_ONECAP;
11492 c |= flags;
11493
11494 /* When appending a compound word after a word character don't
11495 * use Onecap. */
11496 if (p != NULL && spell_iswordp_nmw(p))
11497 c &= ~WF_ONECAP;
11498 make_case_word(tword + sp->ts_splitoff,
11499 preword + sp->ts_prewordlen, c);
11500 }
11501
11502 if (!soundfold)
11503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 /* Don't use a banned word. It may appear again as a good
11505 * word, thus remember it. */
11506 if (flags & WF_BANNED)
11507 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011508 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011509 break;
11510 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011511 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011512 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11513 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011514 {
11515 if (slang->sl_compprog == NULL)
11516 break;
11517 /* the word so far was banned but we may try compounding */
11518 goodword_ends = FALSE;
11519 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521
Bram Moolenaar4770d092006-01-12 23:22:24 +000011522 newscore = 0;
11523 if (!soundfold) /* soundfold words don't have flags */
11524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011526 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 newscore += SCORE_REGION;
11528 if (flags & WF_RARE)
11529 newscore += SCORE_RARE;
11530
Bram Moolenaar0c405862005-06-22 22:26:26 +000011531 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011532 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535
Bram Moolenaar4770d092006-01-12 23:22:24 +000011536 /* TODO: how about splitting in the soundfold tree? */
11537 if (fword_ends
11538 && goodword_ends
11539 && sp->ts_fidx >= sp->ts_fidxtry
11540 && compound_ok)
11541 {
11542 /* The badword also ends: add suggestions. */
11543#ifdef DEBUG_TRIEWALK
11544 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011546 int j;
11547
11548 /* print the stack of changes that brought us here */
11549 smsg("------ %s -------", fword);
11550 for (j = 0; j < depth; ++j)
11551 smsg("%s", changename[j]);
11552 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011553#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011554 if (soundfold)
11555 {
11556 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011557 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011558 add_sound_suggest(su, preword, sp->ts_score, lp);
11559 }
11560 else
11561 {
11562 /* Give a penalty when changing non-word char to word
11563 * char, e.g., "thes," -> "these". */
11564 p = fword + sp->ts_fidx;
11565 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011566 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011567 {
11568 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011569 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011570 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011571 newscore += SCORE_NONWORD;
11572 }
11573
Bram Moolenaar4770d092006-01-12 23:22:24 +000011574 /* Give a bonus to words seen before. */
11575 score = score_wordcount_adj(slang,
11576 sp->ts_score + newscore,
11577 preword + sp->ts_prewordlen,
11578 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011579
Bram Moolenaar4770d092006-01-12 23:22:24 +000011580 /* Add the suggestion if the score isn't too bad. */
11581 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011582 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011583 add_suggestion(su, &su->su_ga, preword,
11584 sp->ts_fidx - repextra,
11585 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011586
11587 if (su->su_badflags & WF_MIXCAP)
11588 {
11589 /* We really don't know if the word should be
11590 * upper or lower case, add both. */
11591 c = captype(preword, NULL);
11592 if (c == 0 || c == WF_ALLCAP)
11593 {
11594 make_case_word(tword + sp->ts_splitoff,
11595 preword + sp->ts_prewordlen,
11596 c == 0 ? WF_ALLCAP : 0);
11597
11598 add_suggestion(su, &su->su_ga, preword,
11599 sp->ts_fidx - repextra,
11600 score + SCORE_ICASE, 0, FALSE,
11601 lp->lp_sallang, FALSE);
11602 }
11603 }
11604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011606 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011607
Bram Moolenaar4770d092006-01-12 23:22:24 +000011608 /*
11609 * Try word split and/or compounding.
11610 */
11611 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011612#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011613 /* Don't split halfway a character. */
11614 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011615#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011616 )
11617 {
11618 int try_compound;
11619 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011620
Bram Moolenaar4770d092006-01-12 23:22:24 +000011621 /* If past the end of the bad word don't try a split.
11622 * Otherwise try changing the next word. E.g., find
11623 * suggestions for "the the" where the second "the" is
11624 * different. It's done like a split.
11625 * TODO: word split for soundfold words */
11626 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11627 && !soundfold;
11628
11629 /* Get here in several situations:
11630 * 1. The word in the tree ends:
11631 * If the word allows compounding try that. Otherwise try
11632 * a split by inserting a space. For both check that a
11633 * valid words starts at fword[sp->ts_fidx].
11634 * For NOBREAK do like compounding to be able to check if
11635 * the next word is valid.
11636 * 2. The badword does end, but it was due to a change (e.g.,
11637 * a swap). No need to split, but do check that the
11638 * following word is valid.
11639 * 3. The badword and the word in the tree end. It may still
11640 * be possible to compound another (short) word.
11641 */
11642 try_compound = FALSE;
11643 if (!soundfold
11644 && slang->sl_compprog != NULL
11645 && ((unsigned)flags >> 24) != 0
11646 && sp->ts_twordlen - sp->ts_splitoff
11647 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011648#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011649 && (!has_mbyte
11650 || slang->sl_compminlen == 0
11651 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011652 >= slang->sl_compminlen)
11653#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011654 && (slang->sl_compsylmax < MAXWLEN
11655 || sp->ts_complen + 1 - sp->ts_compsplit
11656 < slang->sl_compmax)
11657 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11658 ? slang->sl_compstartflags
11659 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011660 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011661 {
11662 try_compound = TRUE;
11663 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11664 compflags[sp->ts_complen + 1] = NUL;
11665 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011666
Bram Moolenaar4770d092006-01-12 23:22:24 +000011667 /* For NOBREAK we never try splitting, it won't make any word
11668 * valid. */
11669 if (slang->sl_nobreak)
11670 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011671
Bram Moolenaar4770d092006-01-12 23:22:24 +000011672 /* If we could add a compound word, and it's also possible to
11673 * split at this point, do the split first and set
11674 * TSF_DIDSPLIT to avoid doing it again. */
11675 else if (!fword_ends
11676 && try_compound
11677 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11678 {
11679 try_compound = FALSE;
11680 sp->ts_flags |= TSF_DIDSPLIT;
11681 --sp->ts_curi; /* do the same NUL again */
11682 compflags[sp->ts_complen] = NUL;
11683 }
11684 else
11685 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011686
Bram Moolenaar4770d092006-01-12 23:22:24 +000011687 if (try_split || try_compound)
11688 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011689 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011690 {
11691 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011692 * words so far are valid for compounding. If there
11693 * is only one word it must not have the NEEDCOMPOUND
11694 * flag. */
11695 if (sp->ts_complen == sp->ts_compsplit
11696 && (flags & WF_NEEDCOMP))
11697 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011698 p = preword;
11699 while (*skiptowhite(p) != NUL)
11700 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011701 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011702 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011703 compflags + sp->ts_compsplit))
11704 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011705
11706 if (slang->sl_nosplitsugs)
11707 newscore += SCORE_SPLIT_NO;
11708 else
11709 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011710
11711 /* Give a bonus to words seen before. */
11712 newscore = score_wordcount_adj(slang, newscore,
11713 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011714 }
11715
Bram Moolenaar4770d092006-01-12 23:22:24 +000011716 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011718 go_deeper(stack, depth, newscore);
11719#ifdef DEBUG_TRIEWALK
11720 if (!try_compound && !fword_ends)
11721 sprintf(changename[depth], "%.*s-%s: split",
11722 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11723 else
11724 sprintf(changename[depth], "%.*s-%s: compound",
11725 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11726#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011728 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011729 sp->ts_state = STATE_SPLITUNDO;
11730
11731 ++depth;
11732 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011734 /* Append a space to preword when splitting. */
11735 if (!try_compound && !fword_ends)
11736 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011737 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011738 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011739 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011740
11741 /* If the badword has a non-word character at this
11742 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011743 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011744 * character when the word ends. But only when the
11745 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011746 if (((!try_compound && !spell_iswordp_nmw(fword
11747 + sp->ts_fidx))
11748 || fword_ends)
11749 && fword[sp->ts_fidx] != NUL
11750 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011751 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011752 int l;
11753
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011754#ifdef FEAT_MBYTE
11755 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011756 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011757 else
11758#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011759 l = 1;
11760 if (fword_ends)
11761 {
11762 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011763 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011764 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011765 sp->ts_prewordlen += l;
11766 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011767 }
11768 else
11769 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11770 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011771 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011772
Bram Moolenaard12a1322005-08-21 22:08:24 +000011773 /* When compounding include compound flag in
11774 * compflags[] (already set above). When splitting we
11775 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011776 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011777 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011778 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011779 sp->ts_compsplit = sp->ts_complen;
11780 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011781
Bram Moolenaar53805d12005-08-01 07:08:33 +000011782 /* set su->su_badflags to the caps type at this
11783 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011784#ifdef FEAT_MBYTE
11785 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011786 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011787 else
11788#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011789 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011790 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011791 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011794 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011795
11796 /* If there are postponed prefixes, try these too. */
11797 if (pbyts != NULL)
11798 {
11799 byts = pbyts;
11800 idxs = pidxs;
11801 sp->ts_prefixdepth = PFD_PREFIXTREE;
11802 sp->ts_state = STATE_NOPREFIX;
11803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 }
11805 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011806 }
11807 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808
Bram Moolenaar4770d092006-01-12 23:22:24 +000011809 case STATE_SPLITUNDO:
11810 /* Undo the changes done for word split or compound word. */
11811 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812
Bram Moolenaar4770d092006-01-12 23:22:24 +000011813 /* Continue looking for NUL bytes. */
11814 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011815
Bram Moolenaar4770d092006-01-12 23:22:24 +000011816 /* In case we went into the prefix tree. */
11817 byts = fbyts;
11818 idxs = fidxs;
11819 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820
Bram Moolenaar4770d092006-01-12 23:22:24 +000011821 case STATE_ENDNUL:
11822 /* Past the NUL bytes in the node. */
11823 su->su_badflags = sp->ts_save_badflags;
11824 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011825#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011826 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011827#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011828 )
11829 {
11830 /* The badword ends, can't use STATE_PLAIN. */
11831 sp->ts_state = STATE_DEL;
11832 break;
11833 }
11834 sp->ts_state = STATE_PLAIN;
11835 /*FALLTHROUGH*/
11836
11837 case STATE_PLAIN:
11838 /*
11839 * Go over all possible bytes at this node, add each to tword[]
11840 * and use child node. "ts_curi" is the index.
11841 */
11842 arridx = sp->ts_arridx;
11843 if (sp->ts_curi > byts[arridx])
11844 {
11845 /* Done all bytes at this node, do next state. When still at
11846 * already changed bytes skip the other tricks. */
11847 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011850 sp->ts_state = STATE_FINAL;
11851 }
11852 else
11853 {
11854 arridx += sp->ts_curi++;
11855 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856
Bram Moolenaar4770d092006-01-12 23:22:24 +000011857 /* Normal byte, go one level deeper. If it's not equal to the
11858 * byte in the bad word adjust the score. But don't even try
11859 * when the byte was already changed. And don't try when we
11860 * just deleted this byte, accepting it is always cheaper then
11861 * delete + substitute. */
11862 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011863#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011864 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011865#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011866 )
11867 newscore = 0;
11868 else
11869 newscore = SCORE_SUBST;
11870 if ((newscore == 0
11871 || (sp->ts_fidx >= sp->ts_fidxtry
11872 && ((sp->ts_flags & TSF_DIDDEL) == 0
11873 || c != fword[sp->ts_delidx])))
11874 && TRY_DEEPER(su, stack, depth, newscore))
11875 {
11876 go_deeper(stack, depth, newscore);
11877#ifdef DEBUG_TRIEWALK
11878 if (newscore > 0)
11879 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11880 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11881 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011882 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011883 sprintf(changename[depth], "%.*s-%s: accept %c",
11884 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11885 fword[sp->ts_fidx]);
11886#endif
11887 ++depth;
11888 sp = &stack[depth];
11889 ++sp->ts_fidx;
11890 tword[sp->ts_twordlen++] = c;
11891 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011892#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011893 if (newscore == SCORE_SUBST)
11894 sp->ts_isdiff = DIFF_YES;
11895 if (has_mbyte)
11896 {
11897 /* Multi-byte characters are a bit complicated to
11898 * handle: They differ when any of the bytes differ
11899 * and then their length may also differ. */
11900 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011901 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011902 /* First byte. */
11903 sp->ts_tcharidx = 0;
11904 sp->ts_tcharlen = MB_BYTE2LEN(c);
11905 sp->ts_fcharstart = sp->ts_fidx - 1;
11906 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011907 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011908 }
11909 else if (sp->ts_isdiff == DIFF_INSERT)
11910 /* When inserting trail bytes don't advance in the
11911 * bad word. */
11912 --sp->ts_fidx;
11913 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11914 {
11915 /* Last byte of character. */
11916 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011917 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011918 /* Correct ts_fidx for the byte length of the
11919 * character (we didn't check that before). */
11920 sp->ts_fidx = sp->ts_fcharstart
11921 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011922 fword[sp->ts_fcharstart]);
11923
Bram Moolenaar4770d092006-01-12 23:22:24 +000011924 /* For changing a composing character adjust
11925 * the score from SCORE_SUBST to
11926 * SCORE_SUBCOMP. */
11927 if (enc_utf8
11928 && utf_iscomposing(
11929 mb_ptr2char(tword
11930 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011931 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011932 && utf_iscomposing(
11933 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011934 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011935 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011936 SCORE_SUBST - SCORE_SUBCOMP;
11937
Bram Moolenaar4770d092006-01-12 23:22:24 +000011938 /* For a similar character adjust score from
11939 * SCORE_SUBST to SCORE_SIMILAR. */
11940 else if (!soundfold
11941 && slang->sl_has_map
11942 && similar_chars(slang,
11943 mb_ptr2char(tword
11944 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011945 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011946 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011947 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011948 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011949 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011950 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011951 else if (sp->ts_isdiff == DIFF_INSERT
11952 && sp->ts_twordlen > sp->ts_tcharlen)
11953 {
11954 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11955 c = mb_ptr2char(p);
11956 if (enc_utf8 && utf_iscomposing(c))
11957 {
11958 /* Inserting a composing char doesn't
11959 * count that much. */
11960 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11961 }
11962 else
11963 {
11964 /* If the previous character was the same,
11965 * thus doubling a character, give a bonus
11966 * to the score. Also for the soundfold
11967 * tree (might seem illogical but does
11968 * give better scores). */
11969 mb_ptr_back(tword, p);
11970 if (c == mb_ptr2char(p))
11971 sp->ts_score -= SCORE_INS
11972 - SCORE_INSDUP;
11973 }
11974 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975
Bram Moolenaar4770d092006-01-12 23:22:24 +000011976 /* Starting a new char, reset the length. */
11977 sp->ts_tcharlen = 0;
11978 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011979 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011980 else
11981#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011982 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011983 /* If we found a similar char adjust the score.
11984 * We do this after calling go_deeper() because
11985 * it's slow. */
11986 if (newscore != 0
11987 && !soundfold
11988 && slang->sl_has_map
11989 && similar_chars(slang,
11990 c, fword[sp->ts_fidx - 1]))
11991 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011992 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011994 }
11995 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011996
Bram Moolenaar4770d092006-01-12 23:22:24 +000011997 case STATE_DEL:
11998#ifdef FEAT_MBYTE
11999 /* When past the first byte of a multi-byte char don't try
12000 * delete/insert/swap a character. */
12001 if (has_mbyte && sp->ts_tcharlen > 0)
12002 {
12003 sp->ts_state = STATE_FINAL;
12004 break;
12005 }
12006#endif
12007 /*
12008 * Try skipping one character in the bad word (delete it).
12009 */
12010 sp->ts_state = STATE_INS_PREP;
12011 sp->ts_curi = 1;
12012 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12013 /* Deleting a vowel at the start of a word counts less, see
12014 * soundalike_score(). */
12015 newscore = 2 * SCORE_DEL / 3;
12016 else
12017 newscore = SCORE_DEL;
12018 if (fword[sp->ts_fidx] != NUL
12019 && TRY_DEEPER(su, stack, depth, newscore))
12020 {
12021 go_deeper(stack, depth, newscore);
12022#ifdef DEBUG_TRIEWALK
12023 sprintf(changename[depth], "%.*s-%s: delete %c",
12024 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12025 fword[sp->ts_fidx]);
12026#endif
12027 ++depth;
12028
12029 /* Remember what character we deleted, so that we can avoid
12030 * inserting it again. */
12031 stack[depth].ts_flags |= TSF_DIDDEL;
12032 stack[depth].ts_delidx = sp->ts_fidx;
12033
12034 /* Advance over the character in fword[]. Give a bonus to the
12035 * score if the same character is following "nn" -> "n". It's
12036 * a bit illogical for soundfold tree but it does give better
12037 * results. */
12038#ifdef FEAT_MBYTE
12039 if (has_mbyte)
12040 {
12041 c = mb_ptr2char(fword + sp->ts_fidx);
12042 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12043 if (enc_utf8 && utf_iscomposing(c))
12044 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12045 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12046 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12047 }
12048 else
12049#endif
12050 {
12051 ++stack[depth].ts_fidx;
12052 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12053 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12054 }
12055 break;
12056 }
12057 /*FALLTHROUGH*/
12058
12059 case STATE_INS_PREP:
12060 if (sp->ts_flags & TSF_DIDDEL)
12061 {
12062 /* If we just deleted a byte then inserting won't make sense,
12063 * a substitute is always cheaper. */
12064 sp->ts_state = STATE_SWAP;
12065 break;
12066 }
12067
12068 /* skip over NUL bytes */
12069 n = sp->ts_arridx;
12070 for (;;)
12071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 if (sp->ts_curi > byts[n])
12073 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012074 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012075 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012076 break;
12077 }
12078 if (byts[n + sp->ts_curi] != NUL)
12079 {
12080 /* Found a byte to insert. */
12081 sp->ts_state = STATE_INS;
12082 break;
12083 }
12084 ++sp->ts_curi;
12085 }
12086 break;
12087
12088 /*FALLTHROUGH*/
12089
12090 case STATE_INS:
12091 /* Insert one byte. Repeat this for each possible byte at this
12092 * node. */
12093 n = sp->ts_arridx;
12094 if (sp->ts_curi > byts[n])
12095 {
12096 /* Done all bytes at this node, go to next state. */
12097 sp->ts_state = STATE_SWAP;
12098 break;
12099 }
12100
12101 /* Do one more byte at this node, but:
12102 * - Skip NUL bytes.
12103 * - Skip the byte if it's equal to the byte in the word,
12104 * accepting that byte is always better.
12105 */
12106 n += sp->ts_curi++;
12107 c = byts[n];
12108 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12109 /* Inserting a vowel at the start of a word counts less,
12110 * see soundalike_score(). */
12111 newscore = 2 * SCORE_INS / 3;
12112 else
12113 newscore = SCORE_INS;
12114 if (c != fword[sp->ts_fidx]
12115 && TRY_DEEPER(su, stack, depth, newscore))
12116 {
12117 go_deeper(stack, depth, newscore);
12118#ifdef DEBUG_TRIEWALK
12119 sprintf(changename[depth], "%.*s-%s: insert %c",
12120 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12121 c);
12122#endif
12123 ++depth;
12124 sp = &stack[depth];
12125 tword[sp->ts_twordlen++] = c;
12126 sp->ts_arridx = idxs[n];
12127#ifdef FEAT_MBYTE
12128 if (has_mbyte)
12129 {
12130 fl = MB_BYTE2LEN(c);
12131 if (fl > 1)
12132 {
12133 /* There are following bytes for the same character.
12134 * We must find all bytes before trying
12135 * delete/insert/swap/etc. */
12136 sp->ts_tcharlen = fl;
12137 sp->ts_tcharidx = 1;
12138 sp->ts_isdiff = DIFF_INSERT;
12139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012140 }
12141 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012142 fl = 1;
12143 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012144#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012145 {
12146 /* If the previous character was the same, thus doubling a
12147 * character, give a bonus to the score. Also for
12148 * soundfold words (illogical but does give a better
12149 * score). */
12150 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012151 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012152 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012154 }
12155 break;
12156
12157 case STATE_SWAP:
12158 /*
12159 * Swap two bytes in the bad word: "12" -> "21".
12160 * We change "fword" here, it's changed back afterwards at
12161 * STATE_UNSWAP.
12162 */
12163 p = fword + sp->ts_fidx;
12164 c = *p;
12165 if (c == NUL)
12166 {
12167 /* End of word, can't swap or replace. */
12168 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012170 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012171
Bram Moolenaar4770d092006-01-12 23:22:24 +000012172 /* Don't swap if the first character is not a word character.
12173 * SWAP3 etc. also don't make sense then. */
12174 if (!soundfold && !spell_iswordp(p, curbuf))
12175 {
12176 sp->ts_state = STATE_REP_INI;
12177 break;
12178 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012179
Bram Moolenaar4770d092006-01-12 23:22:24 +000012180#ifdef FEAT_MBYTE
12181 if (has_mbyte)
12182 {
12183 n = mb_cptr2len(p);
12184 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012185 if (p[n] == NUL)
12186 c2 = NUL;
12187 else if (!soundfold && !spell_iswordp(p + n, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012188 c2 = c; /* don't swap non-word char */
12189 else
12190 c2 = mb_ptr2char(p + n);
12191 }
12192 else
12193#endif
12194 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012195 if (p[1] == NUL)
12196 c2 = NUL;
12197 else if (!soundfold && !spell_iswordp(p + 1, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012198 c2 = c; /* don't swap non-word char */
12199 else
12200 c2 = p[1];
12201 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012202
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012203 /* When the second character is NUL we can't swap. */
12204 if (c2 == NUL)
12205 {
12206 sp->ts_state = STATE_REP_INI;
12207 break;
12208 }
12209
Bram Moolenaar4770d092006-01-12 23:22:24 +000012210 /* When characters are identical, swap won't do anything.
12211 * Also get here if the second char is not a word character. */
12212 if (c == c2)
12213 {
12214 sp->ts_state = STATE_SWAP3;
12215 break;
12216 }
12217 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12218 {
12219 go_deeper(stack, depth, SCORE_SWAP);
12220#ifdef DEBUG_TRIEWALK
12221 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12222 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12223 c, c2);
12224#endif
12225 sp->ts_state = STATE_UNSWAP;
12226 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012227#ifdef FEAT_MBYTE
12228 if (has_mbyte)
12229 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012230 fl = mb_char2len(c2);
12231 mch_memmove(p, p + n, fl);
12232 mb_char2bytes(c, p + fl);
12233 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012234 }
12235 else
12236#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012237 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012238 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012239 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012240 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012241 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012242 }
12243 else
12244 /* If this swap doesn't work then SWAP3 won't either. */
12245 sp->ts_state = STATE_REP_INI;
12246 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012247
Bram Moolenaar4770d092006-01-12 23:22:24 +000012248 case STATE_UNSWAP:
12249 /* Undo the STATE_SWAP swap: "21" -> "12". */
12250 p = fword + sp->ts_fidx;
12251#ifdef FEAT_MBYTE
12252 if (has_mbyte)
12253 {
12254 n = MB_BYTE2LEN(*p);
12255 c = mb_ptr2char(p + n);
12256 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12257 mb_char2bytes(c, p);
12258 }
12259 else
12260#endif
12261 {
12262 c = *p;
12263 *p = p[1];
12264 p[1] = c;
12265 }
12266 /*FALLTHROUGH*/
12267
12268 case STATE_SWAP3:
12269 /* Swap two bytes, skipping one: "123" -> "321". We change
12270 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12271 p = fword + sp->ts_fidx;
12272#ifdef FEAT_MBYTE
12273 if (has_mbyte)
12274 {
12275 n = mb_cptr2len(p);
12276 c = mb_ptr2char(p);
12277 fl = mb_cptr2len(p + n);
12278 c2 = mb_ptr2char(p + n);
12279 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12280 c3 = c; /* don't swap non-word char */
12281 else
12282 c3 = mb_ptr2char(p + n + fl);
12283 }
12284 else
12285#endif
12286 {
12287 c = *p;
12288 c2 = p[1];
12289 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12290 c3 = c; /* don't swap non-word char */
12291 else
12292 c3 = p[2];
12293 }
12294
12295 /* When characters are identical: "121" then SWAP3 result is
12296 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12297 * same as SWAP on next char: "112". Thus skip all swapping.
12298 * Also skip when c3 is NUL.
12299 * Also get here when the third character is not a word character.
12300 * Second character may any char: "a.b" -> "b.a" */
12301 if (c == c3 || c3 == NUL)
12302 {
12303 sp->ts_state = STATE_REP_INI;
12304 break;
12305 }
12306 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12307 {
12308 go_deeper(stack, depth, SCORE_SWAP3);
12309#ifdef DEBUG_TRIEWALK
12310 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12311 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12312 c, c3);
12313#endif
12314 sp->ts_state = STATE_UNSWAP3;
12315 ++depth;
12316#ifdef FEAT_MBYTE
12317 if (has_mbyte)
12318 {
12319 tl = mb_char2len(c3);
12320 mch_memmove(p, p + n + fl, tl);
12321 mb_char2bytes(c2, p + tl);
12322 mb_char2bytes(c, p + fl + tl);
12323 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12324 }
12325 else
12326#endif
12327 {
12328 p[0] = p[2];
12329 p[2] = c;
12330 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12331 }
12332 }
12333 else
12334 sp->ts_state = STATE_REP_INI;
12335 break;
12336
12337 case STATE_UNSWAP3:
12338 /* Undo STATE_SWAP3: "321" -> "123" */
12339 p = fword + sp->ts_fidx;
12340#ifdef FEAT_MBYTE
12341 if (has_mbyte)
12342 {
12343 n = MB_BYTE2LEN(*p);
12344 c2 = mb_ptr2char(p + n);
12345 fl = MB_BYTE2LEN(p[n]);
12346 c = mb_ptr2char(p + n + fl);
12347 tl = MB_BYTE2LEN(p[n + fl]);
12348 mch_memmove(p + fl + tl, p, n);
12349 mb_char2bytes(c, p);
12350 mb_char2bytes(c2, p + tl);
12351 p = p + tl;
12352 }
12353 else
12354#endif
12355 {
12356 c = *p;
12357 *p = p[2];
12358 p[2] = c;
12359 ++p;
12360 }
12361
12362 if (!soundfold && !spell_iswordp(p, curbuf))
12363 {
12364 /* Middle char is not a word char, skip the rotate. First and
12365 * third char were already checked at swap and swap3. */
12366 sp->ts_state = STATE_REP_INI;
12367 break;
12368 }
12369
12370 /* Rotate three characters left: "123" -> "231". We change
12371 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12372 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12373 {
12374 go_deeper(stack, depth, SCORE_SWAP3);
12375#ifdef DEBUG_TRIEWALK
12376 p = fword + sp->ts_fidx;
12377 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12378 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12379 p[0], p[1], p[2]);
12380#endif
12381 sp->ts_state = STATE_UNROT3L;
12382 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012383 p = fword + sp->ts_fidx;
12384#ifdef FEAT_MBYTE
12385 if (has_mbyte)
12386 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012387 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012388 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012389 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012390 fl += mb_cptr2len(p + n + fl);
12391 mch_memmove(p, p + n, fl);
12392 mb_char2bytes(c, p + fl);
12393 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012394 }
12395 else
12396#endif
12397 {
12398 c = *p;
12399 *p = p[1];
12400 p[1] = p[2];
12401 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012402 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012403 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012404 }
12405 else
12406 sp->ts_state = STATE_REP_INI;
12407 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012408
Bram Moolenaar4770d092006-01-12 23:22:24 +000012409 case STATE_UNROT3L:
12410 /* Undo ROT3L: "231" -> "123" */
12411 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012412#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012413 if (has_mbyte)
12414 {
12415 n = MB_BYTE2LEN(*p);
12416 n += MB_BYTE2LEN(p[n]);
12417 c = mb_ptr2char(p + n);
12418 tl = MB_BYTE2LEN(p[n]);
12419 mch_memmove(p + tl, p, n);
12420 mb_char2bytes(c, p);
12421 }
12422 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012423#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012424 {
12425 c = p[2];
12426 p[2] = p[1];
12427 p[1] = *p;
12428 *p = c;
12429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012430
Bram Moolenaar4770d092006-01-12 23:22:24 +000012431 /* Rotate three bytes right: "123" -> "312". We change "fword"
12432 * here, it's changed back afterwards at STATE_UNROT3R. */
12433 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12434 {
12435 go_deeper(stack, depth, SCORE_SWAP3);
12436#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012437 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012438 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12439 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12440 p[0], p[1], p[2]);
12441#endif
12442 sp->ts_state = STATE_UNROT3R;
12443 ++depth;
12444 p = fword + sp->ts_fidx;
12445#ifdef FEAT_MBYTE
12446 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012447 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012448 n = mb_cptr2len(p);
12449 n += mb_cptr2len(p + n);
12450 c = mb_ptr2char(p + n);
12451 tl = mb_cptr2len(p + n);
12452 mch_memmove(p + tl, p, n);
12453 mb_char2bytes(c, p);
12454 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012455 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012456 else
12457#endif
12458 {
12459 c = p[2];
12460 p[2] = p[1];
12461 p[1] = *p;
12462 *p = c;
12463 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12464 }
12465 }
12466 else
12467 sp->ts_state = STATE_REP_INI;
12468 break;
12469
12470 case STATE_UNROT3R:
12471 /* Undo ROT3R: "312" -> "123" */
12472 p = fword + sp->ts_fidx;
12473#ifdef FEAT_MBYTE
12474 if (has_mbyte)
12475 {
12476 c = mb_ptr2char(p);
12477 tl = MB_BYTE2LEN(*p);
12478 n = MB_BYTE2LEN(p[tl]);
12479 n += MB_BYTE2LEN(p[tl + n]);
12480 mch_memmove(p, p + tl, n);
12481 mb_char2bytes(c, p + n);
12482 }
12483 else
12484#endif
12485 {
12486 c = *p;
12487 *p = p[1];
12488 p[1] = p[2];
12489 p[2] = c;
12490 }
12491 /*FALLTHROUGH*/
12492
12493 case STATE_REP_INI:
12494 /* Check if matching with REP items from the .aff file would work.
12495 * Quickly skip if:
12496 * - there are no REP items and we are not in the soundfold trie
12497 * - the score is going to be too high anyway
12498 * - already applied a REP item or swapped here */
12499 if ((lp->lp_replang == NULL && !soundfold)
12500 || sp->ts_score + SCORE_REP >= su->su_maxscore
12501 || sp->ts_fidx < sp->ts_fidxtry)
12502 {
12503 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012506
Bram Moolenaar4770d092006-01-12 23:22:24 +000012507 /* Use the first byte to quickly find the first entry that may
12508 * match. If the index is -1 there is none. */
12509 if (soundfold)
12510 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12511 else
12512 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513
Bram Moolenaar4770d092006-01-12 23:22:24 +000012514 if (sp->ts_curi < 0)
12515 {
12516 sp->ts_state = STATE_FINAL;
12517 break;
12518 }
12519
12520 sp->ts_state = STATE_REP;
12521 /*FALLTHROUGH*/
12522
12523 case STATE_REP:
12524 /* Try matching with REP items from the .aff file. For each match
12525 * replace the characters and check if the resulting word is
12526 * valid. */
12527 p = fword + sp->ts_fidx;
12528
12529 if (soundfold)
12530 gap = &slang->sl_repsal;
12531 else
12532 gap = &lp->lp_replang->sl_rep;
12533 while (sp->ts_curi < gap->ga_len)
12534 {
12535 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12536 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012537 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012538 /* past possible matching entries */
12539 sp->ts_curi = gap->ga_len;
12540 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012541 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012542 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12543 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12544 {
12545 go_deeper(stack, depth, SCORE_REP);
12546#ifdef DEBUG_TRIEWALK
12547 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12548 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12549 ftp->ft_from, ftp->ft_to);
12550#endif
12551 /* Need to undo this afterwards. */
12552 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012553
Bram Moolenaar4770d092006-01-12 23:22:24 +000012554 /* Change the "from" to the "to" string. */
12555 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012556 fl = (int)STRLEN(ftp->ft_from);
12557 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012558 if (fl != tl)
12559 {
12560 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12561 repextra += tl - fl;
12562 }
12563 mch_memmove(p, ftp->ft_to, tl);
12564 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12565#ifdef FEAT_MBYTE
12566 stack[depth].ts_tcharlen = 0;
12567#endif
12568 break;
12569 }
12570 }
12571
12572 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12573 /* No (more) matches. */
12574 sp->ts_state = STATE_FINAL;
12575
12576 break;
12577
12578 case STATE_REP_UNDO:
12579 /* Undo a REP replacement and continue with the next one. */
12580 if (soundfold)
12581 gap = &slang->sl_repsal;
12582 else
12583 gap = &lp->lp_replang->sl_rep;
12584 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012585 fl = (int)STRLEN(ftp->ft_from);
12586 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012587 p = fword + sp->ts_fidx;
12588 if (fl != tl)
12589 {
12590 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12591 repextra -= tl - fl;
12592 }
12593 mch_memmove(p, ftp->ft_from, fl);
12594 sp->ts_state = STATE_REP;
12595 break;
12596
12597 default:
12598 /* Did all possible states at this level, go up one level. */
12599 --depth;
12600
12601 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12602 {
12603 /* Continue in or go back to the prefix tree. */
12604 byts = pbyts;
12605 idxs = pidxs;
12606 }
12607
12608 /* Don't check for CTRL-C too often, it takes time. */
12609 if (--breakcheckcount == 0)
12610 {
12611 ui_breakcheck();
12612 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012613 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012614 }
12615 }
12616}
12617
Bram Moolenaar4770d092006-01-12 23:22:24 +000012618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012620 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012621 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012622 static void
12623go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012624 trystate_T *stack;
12625 int depth;
12626 int score_add;
12627{
Bram Moolenaarea424162005-06-16 21:51:00 +000012628 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012630 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012632 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633}
12634
Bram Moolenaar53805d12005-08-01 07:08:33 +000012635#ifdef FEAT_MBYTE
12636/*
12637 * Case-folding may change the number of bytes: Count nr of chars in
12638 * fword[flen] and return the byte length of that many chars in "word".
12639 */
12640 static int
12641nofold_len(fword, flen, word)
12642 char_u *fword;
12643 int flen;
12644 char_u *word;
12645{
12646 char_u *p;
12647 int i = 0;
12648
12649 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12650 ++i;
12651 for (p = word; i > 0; mb_ptr_adv(p))
12652 --i;
12653 return (int)(p - word);
12654}
12655#endif
12656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012657/*
12658 * "fword" is a good word with case folded. Find the matching keep-case
12659 * words and put it in "kword".
12660 * Theoretically there could be several keep-case words that result in the
12661 * same case-folded word, but we only find one...
12662 */
12663 static void
12664find_keepcap_word(slang, fword, kword)
12665 slang_T *slang;
12666 char_u *fword;
12667 char_u *kword;
12668{
12669 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12670 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012671 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672
12673 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012674 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012675 int round[MAXWLEN];
12676 int fwordidx[MAXWLEN];
12677 int uwordidx[MAXWLEN];
12678 int kwordlen[MAXWLEN];
12679
12680 int flen, ulen;
12681 int l;
12682 int len;
12683 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012684 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 char_u *p;
12686 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012687 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688
12689 if (byts == NULL)
12690 {
12691 /* array is empty: "cannot happen" */
12692 *kword = NUL;
12693 return;
12694 }
12695
12696 /* Make an all-cap version of "fword". */
12697 allcap_copy(fword, uword);
12698
12699 /*
12700 * Each character needs to be tried both case-folded and upper-case.
12701 * All this gets very complicated if we keep in mind that changing case
12702 * may change the byte length of a multi-byte character...
12703 */
12704 depth = 0;
12705 arridx[0] = 0;
12706 round[0] = 0;
12707 fwordidx[0] = 0;
12708 uwordidx[0] = 0;
12709 kwordlen[0] = 0;
12710 while (depth >= 0)
12711 {
12712 if (fword[fwordidx[depth]] == NUL)
12713 {
12714 /* We are at the end of "fword". If the tree allows a word to end
12715 * here we have found a match. */
12716 if (byts[arridx[depth] + 1] == 0)
12717 {
12718 kword[kwordlen[depth]] = NUL;
12719 return;
12720 }
12721
12722 /* kword is getting too long, continue one level up */
12723 --depth;
12724 }
12725 else if (++round[depth] > 2)
12726 {
12727 /* tried both fold-case and upper-case character, continue one
12728 * level up */
12729 --depth;
12730 }
12731 else
12732 {
12733 /*
12734 * round[depth] == 1: Try using the folded-case character.
12735 * round[depth] == 2: Try using the upper-case character.
12736 */
12737#ifdef FEAT_MBYTE
12738 if (has_mbyte)
12739 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012740 flen = mb_cptr2len(fword + fwordidx[depth]);
12741 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 }
12743 else
12744#endif
12745 ulen = flen = 1;
12746 if (round[depth] == 1)
12747 {
12748 p = fword + fwordidx[depth];
12749 l = flen;
12750 }
12751 else
12752 {
12753 p = uword + uwordidx[depth];
12754 l = ulen;
12755 }
12756
12757 for (tryidx = arridx[depth]; l > 0; --l)
12758 {
12759 /* Perform a binary search in the list of accepted bytes. */
12760 len = byts[tryidx++];
12761 c = *p++;
12762 lo = tryidx;
12763 hi = tryidx + len - 1;
12764 while (lo < hi)
12765 {
12766 m = (lo + hi) / 2;
12767 if (byts[m] > c)
12768 hi = m - 1;
12769 else if (byts[m] < c)
12770 lo = m + 1;
12771 else
12772 {
12773 lo = hi = m;
12774 break;
12775 }
12776 }
12777
12778 /* Stop if there is no matching byte. */
12779 if (hi < lo || byts[lo] != c)
12780 break;
12781
12782 /* Continue at the child (if there is one). */
12783 tryidx = idxs[lo];
12784 }
12785
12786 if (l == 0)
12787 {
12788 /*
12789 * Found the matching char. Copy it to "kword" and go a
12790 * level deeper.
12791 */
12792 if (round[depth] == 1)
12793 {
12794 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12795 flen);
12796 kwordlen[depth + 1] = kwordlen[depth] + flen;
12797 }
12798 else
12799 {
12800 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12801 ulen);
12802 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12803 }
12804 fwordidx[depth + 1] = fwordidx[depth] + flen;
12805 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12806
12807 ++depth;
12808 arridx[depth] = tryidx;
12809 round[depth] = 0;
12810 }
12811 }
12812 }
12813
12814 /* Didn't find it: "cannot happen". */
12815 *kword = NUL;
12816}
12817
12818/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012819 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12820 * su->su_sga.
12821 */
12822 static void
12823score_comp_sal(su)
12824 suginfo_T *su;
12825{
12826 langp_T *lp;
12827 char_u badsound[MAXWLEN];
12828 int i;
12829 suggest_T *stp;
12830 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012831 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012832 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012833
12834 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12835 return;
12836
12837 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012838 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012839 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012840 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012841 if (lp->lp_slang->sl_sal.ga_len > 0)
12842 {
12843 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012844 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012845
12846 for (i = 0; i < su->su_ga.ga_len; ++i)
12847 {
12848 stp = &SUG(su->su_ga, i);
12849
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012850 /* Case-fold the suggested word, sound-fold it and compute the
12851 * sound-a-like score. */
12852 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012853 if (score < SCORE_MAXMAX)
12854 {
12855 /* Add the suggestion. */
12856 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12857 sstp->st_word = vim_strsave(stp->st_word);
12858 if (sstp->st_word != NULL)
12859 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012860 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012861 sstp->st_score = score;
12862 sstp->st_altscore = 0;
12863 sstp->st_orglen = stp->st_orglen;
12864 ++su->su_sga.ga_len;
12865 }
12866 }
12867 }
12868 break;
12869 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012870 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012871}
12872
12873/*
12874 * Combine the list of suggestions in su->su_ga and su->su_sga.
12875 * They are intwined.
12876 */
12877 static void
12878score_combine(su)
12879 suginfo_T *su;
12880{
12881 int i;
12882 int j;
12883 garray_T ga;
12884 garray_T *gap;
12885 langp_T *lp;
12886 suggest_T *stp;
12887 char_u *p;
12888 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012889 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012890 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012891 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012892
12893 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012894 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012895 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012896 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012897 if (lp->lp_slang->sl_sal.ga_len > 0)
12898 {
12899 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012900 slang = lp->lp_slang;
12901 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012902
12903 for (i = 0; i < su->su_ga.ga_len; ++i)
12904 {
12905 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012906 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012907 if (stp->st_altscore == SCORE_MAXMAX)
12908 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12909 else
12910 stp->st_score = (stp->st_score * 3
12911 + stp->st_altscore) / 4;
12912 stp->st_salscore = FALSE;
12913 }
12914 break;
12915 }
12916 }
12917
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012918 if (slang == NULL) /* Using "double" without sound folding. */
12919 {
12920 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12921 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012922 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012923 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012924
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012925 /* Add the alternate score to su_sga. */
12926 for (i = 0; i < su->su_sga.ga_len; ++i)
12927 {
12928 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012929 stp->st_altscore = spell_edit_score(slang,
12930 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012931 if (stp->st_score == SCORE_MAXMAX)
12932 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12933 else
12934 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12935 stp->st_salscore = TRUE;
12936 }
12937
Bram Moolenaar4770d092006-01-12 23:22:24 +000012938 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12939 * for both lists. */
12940 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012941 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012942 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012943 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12944
12945 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12946 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12947 return;
12948
12949 stp = &SUG(ga, 0);
12950 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12951 {
12952 /* round 1: get a suggestion from su_ga
12953 * round 2: get a suggestion from su_sga */
12954 for (round = 1; round <= 2; ++round)
12955 {
12956 gap = round == 1 ? &su->su_ga : &su->su_sga;
12957 if (i < gap->ga_len)
12958 {
12959 /* Don't add a word if it's already there. */
12960 p = SUG(*gap, i).st_word;
12961 for (j = 0; j < ga.ga_len; ++j)
12962 if (STRCMP(stp[j].st_word, p) == 0)
12963 break;
12964 if (j == ga.ga_len)
12965 stp[ga.ga_len++] = SUG(*gap, i);
12966 else
12967 vim_free(p);
12968 }
12969 }
12970 }
12971
12972 ga_clear(&su->su_ga);
12973 ga_clear(&su->su_sga);
12974
12975 /* Truncate the list to the number of suggestions that will be displayed. */
12976 if (ga.ga_len > su->su_maxcount)
12977 {
12978 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12979 vim_free(stp[i].st_word);
12980 ga.ga_len = su->su_maxcount;
12981 }
12982
12983 su->su_ga = ga;
12984}
12985
12986/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012987 * For the goodword in "stp" compute the soundalike score compared to the
12988 * badword.
12989 */
12990 static int
12991stp_sal_score(stp, su, slang, badsound)
12992 suggest_T *stp;
12993 suginfo_T *su;
12994 slang_T *slang;
12995 char_u *badsound; /* sound-folded badword */
12996{
12997 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012998 char_u *pbad;
12999 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013000 char_u badsound2[MAXWLEN];
13001 char_u fword[MAXWLEN];
13002 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013003 char_u goodword[MAXWLEN];
13004 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013005
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013006 lendiff = (int)(su->su_badlen - stp->st_orglen);
13007 if (lendiff >= 0)
13008 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013009 else
13010 {
13011 /* soundfold the bad word with more characters following */
13012 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13013
13014 /* When joining two words the sound often changes a lot. E.g., "t he"
13015 * sounds like "t h" while "the" sounds like "@". Avoid that by
13016 * removing the space. Don't do it when the good word also contains a
13017 * space. */
13018 if (vim_iswhite(su->su_badptr[su->su_badlen])
13019 && *skiptowhite(stp->st_word) == NUL)
13020 for (p = fword; *(p = skiptowhite(p)) != NUL; )
13021 mch_memmove(p, p + 1, STRLEN(p));
13022
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013023 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013024 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013025 }
13026
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013027 if (lendiff > 0)
13028 {
13029 /* Add part of the bad word to the good word, so that we soundfold
13030 * what replaces the bad word. */
13031 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013032 vim_strncpy(goodword + stp->st_wordlen,
13033 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013034 pgood = goodword;
13035 }
13036 else
13037 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013038
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013039 /* Sound-fold the word and compute the score for the difference. */
13040 spell_soundfold(slang, pgood, FALSE, goodsound);
13041
13042 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013043}
13044
Bram Moolenaar4770d092006-01-12 23:22:24 +000013045/* structure used to store soundfolded words that add_sound_suggest() has
13046 * handled already. */
13047typedef struct
13048{
13049 short sft_score; /* lowest score used */
13050 char_u sft_word[1]; /* soundfolded word, actually longer */
13051} sftword_T;
13052
13053static sftword_T dumsft;
13054#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13055#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13056
13057/*
13058 * Prepare for calling suggest_try_soundalike().
13059 */
13060 static void
13061suggest_try_soundalike_prep()
13062{
13063 langp_T *lp;
13064 int lpi;
13065 slang_T *slang;
13066
13067 /* Do this for all languages that support sound folding and for which a
13068 * .sug file has been loaded. */
13069 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13070 {
13071 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13072 slang = lp->lp_slang;
13073 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13074 /* prepare the hashtable used by add_sound_suggest() */
13075 hash_init(&slang->sl_sounddone);
13076 }
13077}
13078
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013079/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013080 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013081 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013082 */
13083 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013084suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 suginfo_T *su;
13086{
13087 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013089 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013090 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091
Bram Moolenaar4770d092006-01-12 23:22:24 +000013092 /* Do this for all languages that support sound folding and for which a
13093 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013094 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013095 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013096 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13097 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013098 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 {
13100 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013101 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102
Bram Moolenaar4770d092006-01-12 23:22:24 +000013103 /* try all kinds of inserts/deletes/swaps/etc. */
13104 /* TODO: also soundfold the next words, so that we can try joining
13105 * and splitting */
13106 suggest_trie_walk(su, lp, salword, TRUE);
13107 }
13108 }
13109}
13110
13111/*
13112 * Finish up after calling suggest_try_soundalike().
13113 */
13114 static void
13115suggest_try_soundalike_finish()
13116{
13117 langp_T *lp;
13118 int lpi;
13119 slang_T *slang;
13120 int todo;
13121 hashitem_T *hi;
13122
13123 /* Do this for all languages that support sound folding and for which a
13124 * .sug file has been loaded. */
13125 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13126 {
13127 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13128 slang = lp->lp_slang;
13129 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13130 {
13131 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013132 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013133 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13134 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013135 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013136 vim_free(HI2SFT(hi));
13137 --todo;
13138 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013139
13140 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013141 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013142 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013143 }
13144 }
13145}
13146
13147/*
13148 * A match with a soundfolded word is found. Add the good word(s) that
13149 * produce this soundfolded word.
13150 */
13151 static void
13152add_sound_suggest(su, goodword, score, lp)
13153 suginfo_T *su;
13154 char_u *goodword;
13155 int score; /* soundfold score */
13156 langp_T *lp;
13157{
13158 slang_T *slang = lp->lp_slang; /* language for sound folding */
13159 int sfwordnr;
13160 char_u *nrline;
13161 int orgnr;
13162 char_u theword[MAXWLEN];
13163 int i;
13164 int wlen;
13165 char_u *byts;
13166 idx_T *idxs;
13167 int n;
13168 int wordcount;
13169 int wc;
13170 int goodscore;
13171 hash_T hash;
13172 hashitem_T *hi;
13173 sftword_T *sft;
13174 int bc, gc;
13175 int limit;
13176
13177 /*
13178 * It's very well possible that the same soundfold word is found several
13179 * times with different scores. Since the following is quite slow only do
13180 * the words that have a better score than before. Use a hashtable to
13181 * remember the words that have been done.
13182 */
13183 hash = hash_hash(goodword);
13184 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13185 if (HASHITEM_EMPTY(hi))
13186 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013187 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13188 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013189 if (sft != NULL)
13190 {
13191 sft->sft_score = score;
13192 STRCPY(sft->sft_word, goodword);
13193 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13194 }
13195 }
13196 else
13197 {
13198 sft = HI2SFT(hi);
13199 if (score >= sft->sft_score)
13200 return;
13201 sft->sft_score = score;
13202 }
13203
13204 /*
13205 * Find the word nr in the soundfold tree.
13206 */
13207 sfwordnr = soundfold_find(slang, goodword);
13208 if (sfwordnr < 0)
13209 {
13210 EMSG2(_(e_intern2), "add_sound_suggest()");
13211 return;
13212 }
13213
13214 /*
13215 * go over the list of good words that produce this soundfold word
13216 */
13217 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13218 orgnr = 0;
13219 while (*nrline != NUL)
13220 {
13221 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13222 * previous wordnr. */
13223 orgnr += bytes2offset(&nrline);
13224
13225 byts = slang->sl_fbyts;
13226 idxs = slang->sl_fidxs;
13227
13228 /* Lookup the word "orgnr" one of the two tries. */
13229 n = 0;
13230 wlen = 0;
13231 wordcount = 0;
13232 for (;;)
13233 {
13234 i = 1;
13235 if (wordcount == orgnr && byts[n + 1] == NUL)
13236 break; /* found end of word */
13237
13238 if (byts[n + 1] == NUL)
13239 ++wordcount;
13240
13241 /* skip over the NUL bytes */
13242 for ( ; byts[n + i] == NUL; ++i)
13243 if (i > byts[n]) /* safety check */
13244 {
13245 STRCPY(theword + wlen, "BAD");
13246 goto badword;
13247 }
13248
13249 /* One of the siblings must have the word. */
13250 for ( ; i < byts[n]; ++i)
13251 {
13252 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13253 if (wordcount + wc > orgnr)
13254 break;
13255 wordcount += wc;
13256 }
13257
13258 theword[wlen++] = byts[n + i];
13259 n = idxs[n + i];
13260 }
13261badword:
13262 theword[wlen] = NUL;
13263
13264 /* Go over the possible flags and regions. */
13265 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13266 {
13267 char_u cword[MAXWLEN];
13268 char_u *p;
13269 int flags = (int)idxs[n + i];
13270
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013271 /* Skip words with the NOSUGGEST flag */
13272 if (flags & WF_NOSUGGEST)
13273 continue;
13274
Bram Moolenaar4770d092006-01-12 23:22:24 +000013275 if (flags & WF_KEEPCAP)
13276 {
13277 /* Must find the word in the keep-case tree. */
13278 find_keepcap_word(slang, theword, cword);
13279 p = cword;
13280 }
13281 else
13282 {
13283 flags |= su->su_badflags;
13284 if ((flags & WF_CAPMASK) != 0)
13285 {
13286 /* Need to fix case according to "flags". */
13287 make_case_word(theword, cword, flags);
13288 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013289 }
13290 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013291 p = theword;
13292 }
13293
13294 /* Add the suggestion. */
13295 if (sps_flags & SPS_DOUBLE)
13296 {
13297 /* Add the suggestion if the score isn't too bad. */
13298 if (score <= su->su_maxscore)
13299 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13300 score, 0, FALSE, slang, FALSE);
13301 }
13302 else
13303 {
13304 /* Add a penalty for words in another region. */
13305 if ((flags & WF_REGION)
13306 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13307 goodscore = SCORE_REGION;
13308 else
13309 goodscore = 0;
13310
13311 /* Add a small penalty for changing the first letter from
13312 * lower to upper case. Helps for "tath" -> "Kath", which is
13313 * less common thatn "tath" -> "path". Don't do it when the
13314 * letter is the same, that has already been counted. */
13315 gc = PTR2CHAR(p);
13316 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013318 bc = PTR2CHAR(su->su_badword);
13319 if (!SPELL_ISUPPER(bc)
13320 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13321 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 }
13323
Bram Moolenaar4770d092006-01-12 23:22:24 +000013324 /* Compute the score for the good word. This only does letter
13325 * insert/delete/swap/replace. REP items are not considered,
13326 * which may make the score a bit higher.
13327 * Use a limit for the score to make it work faster. Use
13328 * MAXSCORE(), because RESCORE() will change the score.
13329 * If the limit is very high then the iterative method is
13330 * inefficient, using an array is quicker. */
13331 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13332 if (limit > SCORE_LIMITMAX)
13333 goodscore += spell_edit_score(slang, su->su_badword, p);
13334 else
13335 goodscore += spell_edit_score_limit(slang, su->su_badword,
13336 p, limit);
13337
13338 /* When going over the limit don't bother to do the rest. */
13339 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013341 /* Give a bonus to words seen before. */
13342 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013343
Bram Moolenaar4770d092006-01-12 23:22:24 +000013344 /* Add the suggestion if the score isn't too bad. */
13345 goodscore = RESCORE(goodscore, score);
13346 if (goodscore <= su->su_sfmaxscore)
13347 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13348 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013350 }
13351 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013352 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013353 }
13354}
13355
13356/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013357 * Find word "word" in fold-case tree for "slang" and return the word number.
13358 */
13359 static int
13360soundfold_find(slang, word)
13361 slang_T *slang;
13362 char_u *word;
13363{
13364 idx_T arridx = 0;
13365 int len;
13366 int wlen = 0;
13367 int c;
13368 char_u *ptr = word;
13369 char_u *byts;
13370 idx_T *idxs;
13371 int wordnr = 0;
13372
13373 byts = slang->sl_sbyts;
13374 idxs = slang->sl_sidxs;
13375
13376 for (;;)
13377 {
13378 /* First byte is the number of possible bytes. */
13379 len = byts[arridx++];
13380
13381 /* If the first possible byte is a zero the word could end here.
13382 * If the word ends we found the word. If not skip the NUL bytes. */
13383 c = ptr[wlen];
13384 if (byts[arridx] == NUL)
13385 {
13386 if (c == NUL)
13387 break;
13388
13389 /* Skip over the zeros, there can be several. */
13390 while (len > 0 && byts[arridx] == NUL)
13391 {
13392 ++arridx;
13393 --len;
13394 }
13395 if (len == 0)
13396 return -1; /* no children, word should have ended here */
13397 ++wordnr;
13398 }
13399
13400 /* If the word ends we didn't find it. */
13401 if (c == NUL)
13402 return -1;
13403
13404 /* Perform a binary search in the list of accepted bytes. */
13405 if (c == TAB) /* <Tab> is handled like <Space> */
13406 c = ' ';
13407 while (byts[arridx] < c)
13408 {
13409 /* The word count is in the first idxs[] entry of the child. */
13410 wordnr += idxs[idxs[arridx]];
13411 ++arridx;
13412 if (--len == 0) /* end of the bytes, didn't find it */
13413 return -1;
13414 }
13415 if (byts[arridx] != c) /* didn't find the byte */
13416 return -1;
13417
13418 /* Continue at the child (if there is one). */
13419 arridx = idxs[arridx];
13420 ++wlen;
13421
13422 /* One space in the good word may stand for several spaces in the
13423 * checked word. */
13424 if (c == ' ')
13425 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13426 ++wlen;
13427 }
13428
13429 return wordnr;
13430}
13431
13432/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013433 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 */
13435 static void
13436make_case_word(fword, cword, flags)
13437 char_u *fword;
13438 char_u *cword;
13439 int flags;
13440{
13441 if (flags & WF_ALLCAP)
13442 /* Make it all upper-case */
13443 allcap_copy(fword, cword);
13444 else if (flags & WF_ONECAP)
13445 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013446 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 else
13448 /* Use goodword as-is. */
13449 STRCPY(cword, fword);
13450}
13451
Bram Moolenaarea424162005-06-16 21:51:00 +000013452/*
13453 * Use map string "map" for languages "lp".
13454 */
13455 static void
13456set_map_str(lp, map)
13457 slang_T *lp;
13458 char_u *map;
13459{
13460 char_u *p;
13461 int headc = 0;
13462 int c;
13463 int i;
13464
13465 if (*map == NUL)
13466 {
13467 lp->sl_has_map = FALSE;
13468 return;
13469 }
13470 lp->sl_has_map = TRUE;
13471
Bram Moolenaar4770d092006-01-12 23:22:24 +000013472 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013473 for (i = 0; i < 256; ++i)
13474 lp->sl_map_array[i] = 0;
13475#ifdef FEAT_MBYTE
13476 hash_init(&lp->sl_map_hash);
13477#endif
13478
13479 /*
13480 * The similar characters are stored separated with slashes:
13481 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13482 * before the same slash. For characters above 255 sl_map_hash is used.
13483 */
13484 for (p = map; *p != NUL; )
13485 {
13486#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013487 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013488#else
13489 c = *p++;
13490#endif
13491 if (c == '/')
13492 headc = 0;
13493 else
13494 {
13495 if (headc == 0)
13496 headc = c;
13497
13498#ifdef FEAT_MBYTE
13499 /* Characters above 255 don't fit in sl_map_array[], put them in
13500 * the hash table. Each entry is the char, a NUL the headchar and
13501 * a NUL. */
13502 if (c >= 256)
13503 {
13504 int cl = mb_char2len(c);
13505 int headcl = mb_char2len(headc);
13506 char_u *b;
13507 hash_T hash;
13508 hashitem_T *hi;
13509
13510 b = alloc((unsigned)(cl + headcl + 2));
13511 if (b == NULL)
13512 return;
13513 mb_char2bytes(c, b);
13514 b[cl] = NUL;
13515 mb_char2bytes(headc, b + cl + 1);
13516 b[cl + 1 + headcl] = NUL;
13517 hash = hash_hash(b);
13518 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13519 if (HASHITEM_EMPTY(hi))
13520 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13521 else
13522 {
13523 /* This should have been checked when generating the .spl
13524 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013525 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013526 vim_free(b);
13527 }
13528 }
13529 else
13530#endif
13531 lp->sl_map_array[c] = headc;
13532 }
13533 }
13534}
13535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536/*
13537 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13538 * lines in the .aff file.
13539 */
13540 static int
13541similar_chars(slang, c1, c2)
13542 slang_T *slang;
13543 int c1;
13544 int c2;
13545{
Bram Moolenaarea424162005-06-16 21:51:00 +000013546 int m1, m2;
13547#ifdef FEAT_MBYTE
13548 char_u buf[MB_MAXBYTES];
13549 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550
Bram Moolenaarea424162005-06-16 21:51:00 +000013551 if (c1 >= 256)
13552 {
13553 buf[mb_char2bytes(c1, buf)] = 0;
13554 hi = hash_find(&slang->sl_map_hash, buf);
13555 if (HASHITEM_EMPTY(hi))
13556 m1 = 0;
13557 else
13558 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13559 }
13560 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013561#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013562 m1 = slang->sl_map_array[c1];
13563 if (m1 == 0)
13564 return FALSE;
13565
13566
13567#ifdef FEAT_MBYTE
13568 if (c2 >= 256)
13569 {
13570 buf[mb_char2bytes(c2, buf)] = 0;
13571 hi = hash_find(&slang->sl_map_hash, buf);
13572 if (HASHITEM_EMPTY(hi))
13573 m2 = 0;
13574 else
13575 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13576 }
13577 else
13578#endif
13579 m2 = slang->sl_map_array[c2];
13580
13581 return m1 == m2;
13582}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583
13584/*
13585 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013586 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013587 */
13588 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013589add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13590 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013592 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013594 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013595 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013596 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013597 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013598 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013599 int maxsf; /* su_maxscore applies to soundfold score,
13600 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013602 int goodlen; /* len of goodword changed */
13603 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013605 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013607 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013609 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13610 * "thee the" is added next to changing the first "the" the "thee". */
13611 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013612 pbad = su->su_badptr + badlenarg;
13613 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013614 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013615 goodlen = (int)(pgood - goodword);
13616 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013617 if (goodlen <= 0 || badlen <= 0)
13618 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013619 mb_ptr_back(goodword, pgood);
13620 mb_ptr_back(su->su_badptr, pbad);
13621#ifdef FEAT_MBYTE
13622 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013623 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013624 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13625 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013626 }
13627 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013628#endif
13629 if (*pgood != *pbad)
13630 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013631 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013632
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013633 if (badlen == 0 && goodlen == 0)
13634 /* goodword doesn't change anything; may happen for "the the" changing
13635 * the first "the" to itself. */
13636 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013637
Bram Moolenaar89d40322006-08-29 15:30:07 +000013638 if (gap->ga_len == 0)
13639 i = -1;
13640 else
13641 {
13642 /* Check if the word is already there. Also check the length that is
13643 * being replaced "thes," -> "these" is a different suggestion from
13644 * "thes" -> "these". */
13645 stp = &SUG(*gap, 0);
13646 for (i = gap->ga_len; --i >= 0; ++stp)
13647 if (stp->st_wordlen == goodlen
13648 && stp->st_orglen == badlen
13649 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013651 /*
13652 * Found it. Remember the word with the lowest score.
13653 */
13654 if (stp->st_slang == NULL)
13655 stp->st_slang = slang;
13656
13657 new_sug.st_score = score;
13658 new_sug.st_altscore = altscore;
13659 new_sug.st_had_bonus = had_bonus;
13660
13661 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013662 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013663 /* Only one of the two had the soundalike score computed.
13664 * Need to do that for the other one now, otherwise the
13665 * scores can't be compared. This happens because
13666 * suggest_try_change() doesn't compute the soundalike
13667 * word to keep it fast, while some special methods set
13668 * the soundalike score to zero. */
13669 if (had_bonus)
13670 rescore_one(su, stp);
13671 else
13672 {
13673 new_sug.st_word = stp->st_word;
13674 new_sug.st_wordlen = stp->st_wordlen;
13675 new_sug.st_slang = stp->st_slang;
13676 new_sug.st_orglen = badlen;
13677 rescore_one(su, &new_sug);
13678 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680
Bram Moolenaar89d40322006-08-29 15:30:07 +000013681 if (stp->st_score > new_sug.st_score)
13682 {
13683 stp->st_score = new_sug.st_score;
13684 stp->st_altscore = new_sug.st_altscore;
13685 stp->st_had_bonus = new_sug.st_had_bonus;
13686 }
13687 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013688 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690
Bram Moolenaar4770d092006-01-12 23:22:24 +000013691 if (i < 0 && ga_grow(gap, 1) == OK)
13692 {
13693 /* Add a suggestion. */
13694 stp = &SUG(*gap, gap->ga_len);
13695 stp->st_word = vim_strnsave(goodword, goodlen);
13696 if (stp->st_word != NULL)
13697 {
13698 stp->st_wordlen = goodlen;
13699 stp->st_score = score;
13700 stp->st_altscore = altscore;
13701 stp->st_had_bonus = had_bonus;
13702 stp->st_orglen = badlen;
13703 stp->st_slang = slang;
13704 ++gap->ga_len;
13705
13706 /* If we have too many suggestions now, sort the list and keep
13707 * the best suggestions. */
13708 if (gap->ga_len > SUG_MAX_COUNT(su))
13709 {
13710 if (maxsf)
13711 su->su_sfmaxscore = cleanup_suggestions(gap,
13712 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13713 else
13714 {
13715 i = su->su_maxscore;
13716 su->su_maxscore = cleanup_suggestions(gap,
13717 su->su_maxscore, SUG_CLEAN_COUNT(su));
13718 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 }
13720 }
13721 }
13722}
13723
13724/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013725 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13726 * for split words, such as "the the". Remove these from the list here.
13727 */
13728 static void
13729check_suggestions(su, gap)
13730 suginfo_T *su;
13731 garray_T *gap; /* either su_ga or su_sga */
13732{
13733 suggest_T *stp;
13734 int i;
13735 char_u longword[MAXWLEN + 1];
13736 int len;
13737 hlf_T attr;
13738
13739 stp = &SUG(*gap, 0);
13740 for (i = gap->ga_len - 1; i >= 0; --i)
13741 {
13742 /* Need to append what follows to check for "the the". */
13743 STRCPY(longword, stp[i].st_word);
13744 len = stp[i].st_wordlen;
13745 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13746 MAXWLEN - len);
13747 attr = HLF_COUNT;
13748 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13749 if (attr != HLF_COUNT)
13750 {
13751 /* Remove this entry. */
13752 vim_free(stp[i].st_word);
13753 --gap->ga_len;
13754 if (i < gap->ga_len)
13755 mch_memmove(stp + i, stp + i + 1,
13756 sizeof(suggest_T) * (gap->ga_len - i));
13757 }
13758 }
13759}
13760
13761
13762/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 * Add a word to be banned.
13764 */
13765 static void
13766add_banned(su, word)
13767 suginfo_T *su;
13768 char_u *word;
13769{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013770 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 hash_T hash;
13772 hashitem_T *hi;
13773
Bram Moolenaar4770d092006-01-12 23:22:24 +000013774 hash = hash_hash(word);
13775 hi = hash_lookup(&su->su_banned, word, hash);
13776 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013778 s = vim_strsave(word);
13779 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 hash_add_item(&su->su_banned, hi, s, hash);
13781 }
13782}
13783
13784/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013785 * Recompute the score for all suggestions if sound-folding is possible. This
13786 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013787 */
13788 static void
13789rescore_suggestions(su)
13790 suginfo_T *su;
13791{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013792 int i;
13793
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013794 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013795 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013796 rescore_one(su, &SUG(su->su_ga, i));
13797}
13798
13799/*
13800 * Recompute the score for one suggestion if sound-folding is possible.
13801 */
13802 static void
13803rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013804 suginfo_T *su;
13805 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013806{
13807 slang_T *slang = stp->st_slang;
13808 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013809 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013810
13811 /* Only rescore suggestions that have no sal score yet and do have a
13812 * language. */
13813 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13814 {
13815 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013816 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013817 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013818 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013819 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013820 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013821 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013822
13823 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013824 if (stp->st_altscore == SCORE_MAXMAX)
13825 stp->st_altscore = SCORE_BIG;
13826 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13827 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013828 }
13829}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831static int
13832#ifdef __BORLANDC__
13833_RTLENTRYF
13834#endif
13835sug_compare __ARGS((const void *s1, const void *s2));
13836
13837/*
13838 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013839 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 */
13841 static int
13842#ifdef __BORLANDC__
13843_RTLENTRYF
13844#endif
13845sug_compare(s1, s2)
13846 const void *s1;
13847 const void *s2;
13848{
13849 suggest_T *p1 = (suggest_T *)s1;
13850 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013851 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013852
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013853 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013854 {
13855 n = p1->st_altscore - p2->st_altscore;
13856 if (n == 0)
13857 n = STRICMP(p1->st_word, p2->st_word);
13858 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013859 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013860}
13861
13862/*
13863 * Cleanup the suggestions:
13864 * - Sort on score.
13865 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013866 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013868 static int
13869cleanup_suggestions(gap, maxscore, keep)
13870 garray_T *gap;
13871 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013872 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013874 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013875 int i;
13876
13877 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013878 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013879
13880 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013881 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013883 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013885 gap->ga_len = keep;
13886 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013888 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889}
13890
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013891#if defined(FEAT_EVAL) || defined(PROTO)
13892/*
13893 * Soundfold a string, for soundfold().
13894 * Result is in allocated memory, NULL for an error.
13895 */
13896 char_u *
13897eval_soundfold(word)
13898 char_u *word;
13899{
13900 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013901 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013902 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013903
13904 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13905 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013906 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013907 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013908 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013909 if (lp->lp_slang->sl_sal.ga_len > 0)
13910 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013911 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013912 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013913 return vim_strsave(sound);
13914 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013915 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013916
13917 /* No language with sound folding, return word as-is. */
13918 return vim_strsave(word);
13919}
13920#endif
13921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922/*
13923 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013924 *
13925 * There are many ways to turn a word into a sound-a-like representation. The
13926 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13927 * swedish name matching - survey and test of different algorithms" by Klas
13928 * Erikson.
13929 *
13930 * We support two methods:
13931 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13932 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 */
13934 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013935spell_soundfold(slang, inword, folded, res)
13936 slang_T *slang;
13937 char_u *inword;
13938 int folded; /* "inword" is already case-folded */
13939 char_u *res;
13940{
13941 char_u fword[MAXWLEN];
13942 char_u *word;
13943
13944 if (slang->sl_sofo)
13945 /* SOFOFROM and SOFOTO used */
13946 spell_soundfold_sofo(slang, inword, res);
13947 else
13948 {
13949 /* SAL items used. Requires the word to be case-folded. */
13950 if (folded)
13951 word = inword;
13952 else
13953 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013954 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013955 word = fword;
13956 }
13957
13958#ifdef FEAT_MBYTE
13959 if (has_mbyte)
13960 spell_soundfold_wsal(slang, word, res);
13961 else
13962#endif
13963 spell_soundfold_sal(slang, word, res);
13964 }
13965}
13966
13967/*
13968 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13969 * SOFOTO lines.
13970 */
13971 static void
13972spell_soundfold_sofo(slang, inword, res)
13973 slang_T *slang;
13974 char_u *inword;
13975 char_u *res;
13976{
13977 char_u *s;
13978 int ri = 0;
13979 int c;
13980
13981#ifdef FEAT_MBYTE
13982 if (has_mbyte)
13983 {
13984 int prevc = 0;
13985 int *ip;
13986
13987 /* The sl_sal_first[] table contains the translation for chars up to
13988 * 255, sl_sal the rest. */
13989 for (s = inword; *s != NUL; )
13990 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013991 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013992 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13993 c = ' ';
13994 else if (c < 256)
13995 c = slang->sl_sal_first[c];
13996 else
13997 {
13998 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13999 if (ip == NULL) /* empty list, can't match */
14000 c = NUL;
14001 else
14002 for (;;) /* find "c" in the list */
14003 {
14004 if (*ip == 0) /* not found */
14005 {
14006 c = NUL;
14007 break;
14008 }
14009 if (*ip == c) /* match! */
14010 {
14011 c = ip[1];
14012 break;
14013 }
14014 ip += 2;
14015 }
14016 }
14017
14018 if (c != NUL && c != prevc)
14019 {
14020 ri += mb_char2bytes(c, res + ri);
14021 if (ri + MB_MAXBYTES > MAXWLEN)
14022 break;
14023 prevc = c;
14024 }
14025 }
14026 }
14027 else
14028#endif
14029 {
14030 /* The sl_sal_first[] table contains the translation. */
14031 for (s = inword; (c = *s) != NUL; ++s)
14032 {
14033 if (vim_iswhite(c))
14034 c = ' ';
14035 else
14036 c = slang->sl_sal_first[c];
14037 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14038 res[ri++] = c;
14039 }
14040 }
14041
14042 res[ri] = NUL;
14043}
14044
14045 static void
14046spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 slang_T *slang;
14048 char_u *inword;
14049 char_u *res;
14050{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014051 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014053 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014055 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014057 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058 int n, k = 0;
14059 int z0;
14060 int k0;
14061 int n0;
14062 int c;
14063 int pri;
14064 int p0 = -333;
14065 int c0;
14066
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014067 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014068 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 if (slang->sl_rem_accents)
14070 {
14071 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014072 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014074 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014075 {
14076 *t++ = ' ';
14077 s = skipwhite(s);
14078 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014079 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014081 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 *t++ = *s;
14083 ++s;
14084 }
14085 }
14086 *t = NUL;
14087 }
14088 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014089 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014091 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092
14093 /*
14094 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014095 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014097 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 while ((c = word[i]) != NUL)
14099 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014100 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 n = slang->sl_sal_first[c];
14102 z0 = 0;
14103
14104 if (n >= 0)
14105 {
14106 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014107 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014109 /* Quickly skip entries that don't match the word. Most
14110 * entries are less then three chars, optimize for that. */
14111 k = smp[n].sm_leadlen;
14112 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014114 if (word[i + 1] != s[1])
14115 continue;
14116 if (k > 2)
14117 {
14118 for (j = 2; j < k; ++j)
14119 if (word[i + j] != s[j])
14120 break;
14121 if (j < k)
14122 continue;
14123 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 }
14125
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014126 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014127 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014128 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014129 while (*pf != NUL && *pf != word[i + k])
14130 ++pf;
14131 if (*pf == NUL)
14132 continue;
14133 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014134 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014135 s = smp[n].sm_rules;
14136 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014137
14138 p0 = *s;
14139 k0 = k;
14140 while (*s == '-' && k > 1)
14141 {
14142 k--;
14143 s++;
14144 }
14145 if (*s == '<')
14146 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014147 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 {
14149 /* determine priority */
14150 pri = *s - '0';
14151 s++;
14152 }
14153 if (*s == '^' && *(s + 1) == '^')
14154 s++;
14155
14156 if (*s == NUL
14157 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014158 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014159 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014161 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014163 && spell_iswordp(word + i - 1, curbuf)
14164 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 {
14166 /* search for followup rules, if: */
14167 /* followup and k > 1 and NO '-' in searchstring */
14168 c0 = word[i + k - 1];
14169 n0 = slang->sl_sal_first[c0];
14170
14171 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014172 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 {
14174 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014175 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014177 /* Quickly skip entries that don't match the word.
14178 * */
14179 k0 = smp[n0].sm_leadlen;
14180 if (k0 > 1)
14181 {
14182 if (word[i + k] != s[1])
14183 continue;
14184 if (k0 > 2)
14185 {
14186 pf = word + i + k + 1;
14187 for (j = 2; j < k0; ++j)
14188 if (*pf++ != s[j])
14189 break;
14190 if (j < k0)
14191 continue;
14192 }
14193 }
14194 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014196 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014197 {
14198 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014199 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014200 while (*pf != NUL && *pf != word[i + k0])
14201 ++pf;
14202 if (*pf == NUL)
14203 continue;
14204 ++k0;
14205 }
14206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014208 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 while (*s == '-')
14210 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014211 /* "k0" gets NOT reduced because
14212 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 s++;
14214 }
14215 if (*s == '<')
14216 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014217 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 {
14219 p0 = *s - '0';
14220 s++;
14221 }
14222
14223 if (*s == NUL
14224 /* *s == '^' cuts */
14225 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014226 && !spell_iswordp(word + i + k0,
14227 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 {
14229 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232
14233 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 /* rule fits; stop search */
14237 break;
14238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014239 }
14240
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014241 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 }
14244
14245 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014246 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014247 if (s == NULL)
14248 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014249 pf = smp[n].sm_rules;
14250 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 if (p0 == 1 && z == 0)
14252 {
14253 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014254 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14255 || res[reslen - 1] == *s))
14256 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 z0 = 1;
14258 z = 1;
14259 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014260 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 {
14262 word[i + k0] = *s;
14263 k0++;
14264 s++;
14265 }
14266 if (k > k0)
14267 mch_memmove(word + i + k0, word + i + k,
14268 STRLEN(word + i + k) + 1);
14269
14270 /* new "actual letter" */
14271 c = word[i];
14272 }
14273 else
14274 {
14275 /* no '<' rule used */
14276 i += k - 1;
14277 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014278 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014280 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014281 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 s++;
14283 }
14284 /* new "actual letter" */
14285 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014286 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 {
14288 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014289 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 mch_memmove(word, word + i + 1,
14291 STRLEN(word + i + 1) + 1);
14292 i = 0;
14293 z0 = 1;
14294 }
14295 }
14296 break;
14297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 }
14299 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014300 else if (vim_iswhite(c))
14301 {
14302 c = ' ';
14303 k = 1;
14304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305
14306 if (z0 == 0)
14307 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014308 if (k && !p0 && reslen < MAXWLEN && c != NUL
14309 && (!slang->sl_collapse || reslen == 0
14310 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014312 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313
14314 i++;
14315 z = 0;
14316 k = 0;
14317 }
14318 }
14319
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014320 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321}
14322
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014323#ifdef FEAT_MBYTE
14324/*
14325 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14326 * Multi-byte version of spell_soundfold().
14327 */
14328 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014329spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014330 slang_T *slang;
14331 char_u *inword;
14332 char_u *res;
14333{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014334 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014335 int word[MAXWLEN];
14336 int wres[MAXWLEN];
14337 int l;
14338 char_u *s;
14339 int *ws;
14340 char_u *t;
14341 int *pf;
14342 int i, j, z;
14343 int reslen;
14344 int n, k = 0;
14345 int z0;
14346 int k0;
14347 int n0;
14348 int c;
14349 int pri;
14350 int p0 = -333;
14351 int c0;
14352 int did_white = FALSE;
14353
14354 /*
14355 * Convert the multi-byte string to a wide-character string.
14356 * Remove accents, if wanted. We actually remove all non-word characters.
14357 * But keep white space.
14358 */
14359 n = 0;
14360 for (s = inword; *s != NUL; )
14361 {
14362 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014363 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014364 if (slang->sl_rem_accents)
14365 {
14366 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14367 {
14368 if (did_white)
14369 continue;
14370 c = ' ';
14371 did_white = TRUE;
14372 }
14373 else
14374 {
14375 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014376 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014377 continue;
14378 }
14379 }
14380 word[n++] = c;
14381 }
14382 word[n] = NUL;
14383
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014384 /*
14385 * This comes from Aspell phonet.cpp.
14386 * Converted from C++ to C. Added support for multi-byte chars.
14387 * Changed to keep spaces.
14388 */
14389 i = reslen = z = 0;
14390 while ((c = word[i]) != NUL)
14391 {
14392 /* Start with the first rule that has the character in the word. */
14393 n = slang->sl_sal_first[c & 0xff];
14394 z0 = 0;
14395
14396 if (n >= 0)
14397 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014398 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014399 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14400 {
14401 /* Quickly skip entries that don't match the word. Most
14402 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014403 if (c != ws[0])
14404 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014405 k = smp[n].sm_leadlen;
14406 if (k > 1)
14407 {
14408 if (word[i + 1] != ws[1])
14409 continue;
14410 if (k > 2)
14411 {
14412 for (j = 2; j < k; ++j)
14413 if (word[i + j] != ws[j])
14414 break;
14415 if (j < k)
14416 continue;
14417 }
14418 }
14419
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014420 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014421 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014422 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014423 while (*pf != NUL && *pf != word[i + k])
14424 ++pf;
14425 if (*pf == NUL)
14426 continue;
14427 ++k;
14428 }
14429 s = smp[n].sm_rules;
14430 pri = 5; /* default priority */
14431
14432 p0 = *s;
14433 k0 = k;
14434 while (*s == '-' && k > 1)
14435 {
14436 k--;
14437 s++;
14438 }
14439 if (*s == '<')
14440 s++;
14441 if (VIM_ISDIGIT(*s))
14442 {
14443 /* determine priority */
14444 pri = *s - '0';
14445 s++;
14446 }
14447 if (*s == '^' && *(s + 1) == '^')
14448 s++;
14449
14450 if (*s == NUL
14451 || (*s == '^'
14452 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014453 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014454 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014455 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014456 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014457 && spell_iswordp_w(word + i - 1, curbuf)
14458 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014459 {
14460 /* search for followup rules, if: */
14461 /* followup and k > 1 and NO '-' in searchstring */
14462 c0 = word[i + k - 1];
14463 n0 = slang->sl_sal_first[c0 & 0xff];
14464
14465 if (slang->sl_followup && k > 1 && n0 >= 0
14466 && p0 != '-' && word[i + k] != NUL)
14467 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014468 /* Test follow-up rule for "word[i + k]"; loop over
14469 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014470 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14471 == (c0 & 0xff); ++n0)
14472 {
14473 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014474 */
14475 if (c0 != ws[0])
14476 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014477 k0 = smp[n0].sm_leadlen;
14478 if (k0 > 1)
14479 {
14480 if (word[i + k] != ws[1])
14481 continue;
14482 if (k0 > 2)
14483 {
14484 pf = word + i + k + 1;
14485 for (j = 2; j < k0; ++j)
14486 if (*pf++ != ws[j])
14487 break;
14488 if (j < k0)
14489 continue;
14490 }
14491 }
14492 k0 += k - 1;
14493
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014494 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014495 {
14496 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014497 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014498 while (*pf != NUL && *pf != word[i + k0])
14499 ++pf;
14500 if (*pf == NUL)
14501 continue;
14502 ++k0;
14503 }
14504
14505 p0 = 5;
14506 s = smp[n0].sm_rules;
14507 while (*s == '-')
14508 {
14509 /* "k0" gets NOT reduced because
14510 * "if (k0 == k)" */
14511 s++;
14512 }
14513 if (*s == '<')
14514 s++;
14515 if (VIM_ISDIGIT(*s))
14516 {
14517 p0 = *s - '0';
14518 s++;
14519 }
14520
14521 if (*s == NUL
14522 /* *s == '^' cuts */
14523 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014524 && !spell_iswordp_w(word + i + k0,
14525 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014526 {
14527 if (k0 == k)
14528 /* this is just a piece of the string */
14529 continue;
14530
14531 if (p0 < pri)
14532 /* priority too low */
14533 continue;
14534 /* rule fits; stop search */
14535 break;
14536 }
14537 }
14538
14539 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14540 == (c0 & 0xff))
14541 continue;
14542 }
14543
14544 /* replace string */
14545 ws = smp[n].sm_to_w;
14546 s = smp[n].sm_rules;
14547 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14548 if (p0 == 1 && z == 0)
14549 {
14550 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014551 if (reslen > 0 && ws != NULL && *ws != NUL
14552 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014553 || wres[reslen - 1] == *ws))
14554 reslen--;
14555 z0 = 1;
14556 z = 1;
14557 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014558 if (ws != NULL)
14559 while (*ws != NUL && word[i + k0] != NUL)
14560 {
14561 word[i + k0] = *ws;
14562 k0++;
14563 ws++;
14564 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014565 if (k > k0)
14566 mch_memmove(word + i + k0, word + i + k,
14567 sizeof(int) * (STRLEN(word + i + k) + 1));
14568
14569 /* new "actual letter" */
14570 c = word[i];
14571 }
14572 else
14573 {
14574 /* no '<' rule used */
14575 i += k - 1;
14576 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014577 if (ws != NULL)
14578 while (*ws != NUL && ws[1] != NUL
14579 && reslen < MAXWLEN)
14580 {
14581 if (reslen == 0 || wres[reslen - 1] != *ws)
14582 wres[reslen++] = *ws;
14583 ws++;
14584 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014585 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014586 if (ws == NULL)
14587 c = NUL;
14588 else
14589 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014590 if (strstr((char *)s, "^^") != NULL)
14591 {
14592 if (c != NUL)
14593 wres[reslen++] = c;
14594 mch_memmove(word, word + i + 1,
14595 sizeof(int) * (STRLEN(word + i + 1) + 1));
14596 i = 0;
14597 z0 = 1;
14598 }
14599 }
14600 break;
14601 }
14602 }
14603 }
14604 else if (vim_iswhite(c))
14605 {
14606 c = ' ';
14607 k = 1;
14608 }
14609
14610 if (z0 == 0)
14611 {
14612 if (k && !p0 && reslen < MAXWLEN && c != NUL
14613 && (!slang->sl_collapse || reslen == 0
14614 || wres[reslen - 1] != c))
14615 /* condense only double letters */
14616 wres[reslen++] = c;
14617
14618 i++;
14619 z = 0;
14620 k = 0;
14621 }
14622 }
14623
14624 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14625 l = 0;
14626 for (n = 0; n < reslen; ++n)
14627 {
14628 l += mb_char2bytes(wres[n], res + l);
14629 if (l + MB_MAXBYTES > MAXWLEN)
14630 break;
14631 }
14632 res[l] = NUL;
14633}
14634#endif
14635
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014636/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014637 * Compute a score for two sound-a-like words.
14638 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14639 * Instead of a generic loop we write out the code. That keeps it fast by
14640 * avoiding checks that will not be possible.
14641 */
14642 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014643soundalike_score(goodstart, badstart)
14644 char_u *goodstart; /* sound-folded good word */
14645 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014646{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014647 char_u *goodsound = goodstart;
14648 char_u *badsound = badstart;
14649 int goodlen;
14650 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014651 int n;
14652 char_u *pl, *ps;
14653 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014654 int score = 0;
14655
14656 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14657 * counted so much, vowels halfway the word aren't counted at all. */
14658 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14659 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014660 if (badsound[1] == goodsound[1]
14661 || (badsound[1] != NUL
14662 && goodsound[1] != NUL
14663 && badsound[2] == goodsound[2]))
14664 {
14665 /* handle like a substitute */
14666 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014667 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014668 {
14669 score = 2 * SCORE_DEL / 3;
14670 if (*badsound == '*')
14671 ++badsound;
14672 else
14673 ++goodsound;
14674 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014675 }
14676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014677 goodlen = (int)STRLEN(goodsound);
14678 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014679
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014680 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014681 * changes. */
14682 n = goodlen - badlen;
14683 if (n < -2 || n > 2)
14684 return SCORE_MAXMAX;
14685
14686 if (n > 0)
14687 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014688 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014689 ps = badsound;
14690 }
14691 else
14692 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014693 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014694 ps = goodsound;
14695 }
14696
14697 /* Skip over the identical part. */
14698 while (*pl == *ps && *pl != NUL)
14699 {
14700 ++pl;
14701 ++ps;
14702 }
14703
14704 switch (n)
14705 {
14706 case -2:
14707 case 2:
14708 /*
14709 * Must delete two characters from "pl".
14710 */
14711 ++pl; /* first delete */
14712 while (*pl == *ps)
14713 {
14714 ++pl;
14715 ++ps;
14716 }
14717 /* strings must be equal after second delete */
14718 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014719 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014720
14721 /* Failed to compare. */
14722 break;
14723
14724 case -1:
14725 case 1:
14726 /*
14727 * Minimal one delete from "pl" required.
14728 */
14729
14730 /* 1: delete */
14731 pl2 = pl + 1;
14732 ps2 = ps;
14733 while (*pl2 == *ps2)
14734 {
14735 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014736 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014737 ++pl2;
14738 ++ps2;
14739 }
14740
14741 /* 2: delete then swap, then rest must be equal */
14742 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14743 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014744 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014745
14746 /* 3: delete then substitute, then the rest must be equal */
14747 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014748 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014749
14750 /* 4: first swap then delete */
14751 if (pl[0] == ps[1] && pl[1] == ps[0])
14752 {
14753 pl2 = pl + 2; /* swap, skip two chars */
14754 ps2 = ps + 2;
14755 while (*pl2 == *ps2)
14756 {
14757 ++pl2;
14758 ++ps2;
14759 }
14760 /* delete a char and then strings must be equal */
14761 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014762 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014763 }
14764
14765 /* 5: first substitute then delete */
14766 pl2 = pl + 1; /* substitute, skip one char */
14767 ps2 = ps + 1;
14768 while (*pl2 == *ps2)
14769 {
14770 ++pl2;
14771 ++ps2;
14772 }
14773 /* delete a char and then strings must be equal */
14774 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014775 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014776
14777 /* Failed to compare. */
14778 break;
14779
14780 case 0:
14781 /*
14782 * Lenghts are equal, thus changes must result in same length: An
14783 * insert is only possible in combination with a delete.
14784 * 1: check if for identical strings
14785 */
14786 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014787 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014788
14789 /* 2: swap */
14790 if (pl[0] == ps[1] && pl[1] == ps[0])
14791 {
14792 pl2 = pl + 2; /* swap, skip two chars */
14793 ps2 = ps + 2;
14794 while (*pl2 == *ps2)
14795 {
14796 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014797 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014798 ++pl2;
14799 ++ps2;
14800 }
14801 /* 3: swap and swap again */
14802 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14803 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014804 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014805
14806 /* 4: swap and substitute */
14807 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014808 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014809 }
14810
14811 /* 5: substitute */
14812 pl2 = pl + 1;
14813 ps2 = ps + 1;
14814 while (*pl2 == *ps2)
14815 {
14816 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014817 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014818 ++pl2;
14819 ++ps2;
14820 }
14821
14822 /* 6: substitute and swap */
14823 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14824 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014825 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014826
14827 /* 7: substitute and substitute */
14828 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014829 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014830
14831 /* 8: insert then delete */
14832 pl2 = pl;
14833 ps2 = ps + 1;
14834 while (*pl2 == *ps2)
14835 {
14836 ++pl2;
14837 ++ps2;
14838 }
14839 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014840 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014841
14842 /* 9: delete then insert */
14843 pl2 = pl + 1;
14844 ps2 = ps;
14845 while (*pl2 == *ps2)
14846 {
14847 ++pl2;
14848 ++ps2;
14849 }
14850 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014851 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014852
14853 /* Failed to compare. */
14854 break;
14855 }
14856
14857 return SCORE_MAXMAX;
14858}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014860/*
14861 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014862 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014863 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014864 * The algorithm is described by Du and Chang, 1992.
14865 * The implementation of the algorithm comes from Aspell editdist.cpp,
14866 * edit_distance(). It has been converted from C++ to C and modified to
14867 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 */
14869 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014870spell_edit_score(slang, badword, goodword)
14871 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014872 char_u *badword;
14873 char_u *goodword;
14874{
14875 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014876 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877 int j, i;
14878 int t;
14879 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014880 int pbc, pgc;
14881#ifdef FEAT_MBYTE
14882 char_u *p;
14883 int wbadword[MAXWLEN];
14884 int wgoodword[MAXWLEN];
14885
14886 if (has_mbyte)
14887 {
14888 /* Get the characters from the multi-byte strings and put them in an
14889 * int array for easy access. */
14890 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014891 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014892 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014893 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014894 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014895 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014896 }
14897 else
14898#endif
14899 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014900 badlen = (int)STRLEN(badword) + 1;
14901 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014903
14904 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14905#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14907 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014908 if (cnt == NULL)
14909 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910
14911 CNT(0, 0) = 0;
14912 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014913 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914
14915 for (i = 1; i <= badlen; ++i)
14916 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014917 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 for (j = 1; j <= goodlen; ++j)
14919 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014920#ifdef FEAT_MBYTE
14921 if (has_mbyte)
14922 {
14923 bc = wbadword[i - 1];
14924 gc = wgoodword[j - 1];
14925 }
14926 else
14927#endif
14928 {
14929 bc = badword[i - 1];
14930 gc = goodword[j - 1];
14931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014932 if (bc == gc)
14933 CNT(i, j) = CNT(i - 1, j - 1);
14934 else
14935 {
14936 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014937 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14939 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014940 {
14941 /* For a similar character use SCORE_SIMILAR. */
14942 if (slang != NULL
14943 && slang->sl_has_map
14944 && similar_chars(slang, gc, bc))
14945 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14946 else
14947 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14948 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014950 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014951 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014952#ifdef FEAT_MBYTE
14953 if (has_mbyte)
14954 {
14955 pbc = wbadword[i - 2];
14956 pgc = wgoodword[j - 2];
14957 }
14958 else
14959#endif
14960 {
14961 pbc = badword[i - 2];
14962 pgc = goodword[j - 2];
14963 }
14964 if (bc == pgc && pbc == gc)
14965 {
14966 t = SCORE_SWAP + CNT(i - 2, j - 2);
14967 if (t < CNT(i, j))
14968 CNT(i, j) = t;
14969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 }
14971 t = SCORE_DEL + CNT(i - 1, j);
14972 if (t < CNT(i, j))
14973 CNT(i, j) = t;
14974 t = SCORE_INS + CNT(i, j - 1);
14975 if (t < CNT(i, j))
14976 CNT(i, j) = t;
14977 }
14978 }
14979 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014980
14981 i = CNT(badlen - 1, goodlen - 1);
14982 vim_free(cnt);
14983 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014984}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014985
Bram Moolenaar4770d092006-01-12 23:22:24 +000014986typedef struct
14987{
14988 int badi;
14989 int goodi;
14990 int score;
14991} limitscore_T;
14992
14993/*
14994 * Like spell_edit_score(), but with a limit on the score to make it faster.
14995 * May return SCORE_MAXMAX when the score is higher than "limit".
14996 *
14997 * This uses a stack for the edits still to be tried.
14998 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14999 * for multi-byte characters.
15000 */
15001 static int
15002spell_edit_score_limit(slang, badword, goodword, limit)
15003 slang_T *slang;
15004 char_u *badword;
15005 char_u *goodword;
15006 int limit;
15007{
15008 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15009 int stackidx;
15010 int bi, gi;
15011 int bi2, gi2;
15012 int bc, gc;
15013 int score;
15014 int score_off;
15015 int minscore;
15016 int round;
15017
15018#ifdef FEAT_MBYTE
15019 /* Multi-byte characters require a bit more work, use a different function
15020 * to avoid testing "has_mbyte" quite often. */
15021 if (has_mbyte)
15022 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15023#endif
15024
15025 /*
15026 * The idea is to go from start to end over the words. So long as
15027 * characters are equal just continue, this always gives the lowest score.
15028 * When there is a difference try several alternatives. Each alternative
15029 * increases "score" for the edit distance. Some of the alternatives are
15030 * pushed unto a stack and tried later, some are tried right away. At the
15031 * end of the word the score for one alternative is known. The lowest
15032 * possible score is stored in "minscore".
15033 */
15034 stackidx = 0;
15035 bi = 0;
15036 gi = 0;
15037 score = 0;
15038 minscore = limit + 1;
15039
15040 for (;;)
15041 {
15042 /* Skip over an equal part, score remains the same. */
15043 for (;;)
15044 {
15045 bc = badword[bi];
15046 gc = goodword[gi];
15047 if (bc != gc) /* stop at a char that's different */
15048 break;
15049 if (bc == NUL) /* both words end */
15050 {
15051 if (score < minscore)
15052 minscore = score;
15053 goto pop; /* do next alternative */
15054 }
15055 ++bi;
15056 ++gi;
15057 }
15058
15059 if (gc == NUL) /* goodword ends, delete badword chars */
15060 {
15061 do
15062 {
15063 if ((score += SCORE_DEL) >= minscore)
15064 goto pop; /* do next alternative */
15065 } while (badword[++bi] != NUL);
15066 minscore = score;
15067 }
15068 else if (bc == NUL) /* badword ends, insert badword chars */
15069 {
15070 do
15071 {
15072 if ((score += SCORE_INS) >= minscore)
15073 goto pop; /* do next alternative */
15074 } while (goodword[++gi] != NUL);
15075 minscore = score;
15076 }
15077 else /* both words continue */
15078 {
15079 /* If not close to the limit, perform a change. Only try changes
15080 * that may lead to a lower score than "minscore".
15081 * round 0: try deleting a char from badword
15082 * round 1: try inserting a char in badword */
15083 for (round = 0; round <= 1; ++round)
15084 {
15085 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15086 if (score_off < minscore)
15087 {
15088 if (score_off + SCORE_EDIT_MIN >= minscore)
15089 {
15090 /* Near the limit, rest of the words must match. We
15091 * can check that right now, no need to push an item
15092 * onto the stack. */
15093 bi2 = bi + 1 - round;
15094 gi2 = gi + round;
15095 while (goodword[gi2] == badword[bi2])
15096 {
15097 if (goodword[gi2] == NUL)
15098 {
15099 minscore = score_off;
15100 break;
15101 }
15102 ++bi2;
15103 ++gi2;
15104 }
15105 }
15106 else
15107 {
15108 /* try deleting/inserting a character later */
15109 stack[stackidx].badi = bi + 1 - round;
15110 stack[stackidx].goodi = gi + round;
15111 stack[stackidx].score = score_off;
15112 ++stackidx;
15113 }
15114 }
15115 }
15116
15117 if (score + SCORE_SWAP < minscore)
15118 {
15119 /* If swapping two characters makes a match then the
15120 * substitution is more expensive, thus there is no need to
15121 * try both. */
15122 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15123 {
15124 /* Swap two characters, that is: skip them. */
15125 gi += 2;
15126 bi += 2;
15127 score += SCORE_SWAP;
15128 continue;
15129 }
15130 }
15131
15132 /* Substitute one character for another which is the same
15133 * thing as deleting a character from both goodword and badword.
15134 * Use a better score when there is only a case difference. */
15135 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15136 score += SCORE_ICASE;
15137 else
15138 {
15139 /* For a similar character use SCORE_SIMILAR. */
15140 if (slang != NULL
15141 && slang->sl_has_map
15142 && similar_chars(slang, gc, bc))
15143 score += SCORE_SIMILAR;
15144 else
15145 score += SCORE_SUBST;
15146 }
15147
15148 if (score < minscore)
15149 {
15150 /* Do the substitution. */
15151 ++gi;
15152 ++bi;
15153 continue;
15154 }
15155 }
15156pop:
15157 /*
15158 * Get here to try the next alternative, pop it from the stack.
15159 */
15160 if (stackidx == 0) /* stack is empty, finished */
15161 break;
15162
15163 /* pop an item from the stack */
15164 --stackidx;
15165 gi = stack[stackidx].goodi;
15166 bi = stack[stackidx].badi;
15167 score = stack[stackidx].score;
15168 }
15169
15170 /* When the score goes over "limit" it may actually be much higher.
15171 * Return a very large number to avoid going below the limit when giving a
15172 * bonus. */
15173 if (minscore > limit)
15174 return SCORE_MAXMAX;
15175 return minscore;
15176}
15177
15178#ifdef FEAT_MBYTE
15179/*
15180 * Multi-byte version of spell_edit_score_limit().
15181 * Keep it in sync with the above!
15182 */
15183 static int
15184spell_edit_score_limit_w(slang, badword, goodword, limit)
15185 slang_T *slang;
15186 char_u *badword;
15187 char_u *goodword;
15188 int limit;
15189{
15190 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15191 int stackidx;
15192 int bi, gi;
15193 int bi2, gi2;
15194 int bc, gc;
15195 int score;
15196 int score_off;
15197 int minscore;
15198 int round;
15199 char_u *p;
15200 int wbadword[MAXWLEN];
15201 int wgoodword[MAXWLEN];
15202
15203 /* Get the characters from the multi-byte strings and put them in an
15204 * int array for easy access. */
15205 bi = 0;
15206 for (p = badword; *p != NUL; )
15207 wbadword[bi++] = mb_cptr2char_adv(&p);
15208 wbadword[bi++] = 0;
15209 gi = 0;
15210 for (p = goodword; *p != NUL; )
15211 wgoodword[gi++] = mb_cptr2char_adv(&p);
15212 wgoodword[gi++] = 0;
15213
15214 /*
15215 * The idea is to go from start to end over the words. So long as
15216 * characters are equal just continue, this always gives the lowest score.
15217 * When there is a difference try several alternatives. Each alternative
15218 * increases "score" for the edit distance. Some of the alternatives are
15219 * pushed unto a stack and tried later, some are tried right away. At the
15220 * end of the word the score for one alternative is known. The lowest
15221 * possible score is stored in "minscore".
15222 */
15223 stackidx = 0;
15224 bi = 0;
15225 gi = 0;
15226 score = 0;
15227 minscore = limit + 1;
15228
15229 for (;;)
15230 {
15231 /* Skip over an equal part, score remains the same. */
15232 for (;;)
15233 {
15234 bc = wbadword[bi];
15235 gc = wgoodword[gi];
15236
15237 if (bc != gc) /* stop at a char that's different */
15238 break;
15239 if (bc == NUL) /* both words end */
15240 {
15241 if (score < minscore)
15242 minscore = score;
15243 goto pop; /* do next alternative */
15244 }
15245 ++bi;
15246 ++gi;
15247 }
15248
15249 if (gc == NUL) /* goodword ends, delete badword chars */
15250 {
15251 do
15252 {
15253 if ((score += SCORE_DEL) >= minscore)
15254 goto pop; /* do next alternative */
15255 } while (wbadword[++bi] != NUL);
15256 minscore = score;
15257 }
15258 else if (bc == NUL) /* badword ends, insert badword chars */
15259 {
15260 do
15261 {
15262 if ((score += SCORE_INS) >= minscore)
15263 goto pop; /* do next alternative */
15264 } while (wgoodword[++gi] != NUL);
15265 minscore = score;
15266 }
15267 else /* both words continue */
15268 {
15269 /* If not close to the limit, perform a change. Only try changes
15270 * that may lead to a lower score than "minscore".
15271 * round 0: try deleting a char from badword
15272 * round 1: try inserting a char in badword */
15273 for (round = 0; round <= 1; ++round)
15274 {
15275 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15276 if (score_off < minscore)
15277 {
15278 if (score_off + SCORE_EDIT_MIN >= minscore)
15279 {
15280 /* Near the limit, rest of the words must match. We
15281 * can check that right now, no need to push an item
15282 * onto the stack. */
15283 bi2 = bi + 1 - round;
15284 gi2 = gi + round;
15285 while (wgoodword[gi2] == wbadword[bi2])
15286 {
15287 if (wgoodword[gi2] == NUL)
15288 {
15289 minscore = score_off;
15290 break;
15291 }
15292 ++bi2;
15293 ++gi2;
15294 }
15295 }
15296 else
15297 {
15298 /* try deleting a character from badword later */
15299 stack[stackidx].badi = bi + 1 - round;
15300 stack[stackidx].goodi = gi + round;
15301 stack[stackidx].score = score_off;
15302 ++stackidx;
15303 }
15304 }
15305 }
15306
15307 if (score + SCORE_SWAP < minscore)
15308 {
15309 /* If swapping two characters makes a match then the
15310 * substitution is more expensive, thus there is no need to
15311 * try both. */
15312 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15313 {
15314 /* Swap two characters, that is: skip them. */
15315 gi += 2;
15316 bi += 2;
15317 score += SCORE_SWAP;
15318 continue;
15319 }
15320 }
15321
15322 /* Substitute one character for another which is the same
15323 * thing as deleting a character from both goodword and badword.
15324 * Use a better score when there is only a case difference. */
15325 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15326 score += SCORE_ICASE;
15327 else
15328 {
15329 /* For a similar character use SCORE_SIMILAR. */
15330 if (slang != NULL
15331 && slang->sl_has_map
15332 && similar_chars(slang, gc, bc))
15333 score += SCORE_SIMILAR;
15334 else
15335 score += SCORE_SUBST;
15336 }
15337
15338 if (score < minscore)
15339 {
15340 /* Do the substitution. */
15341 ++gi;
15342 ++bi;
15343 continue;
15344 }
15345 }
15346pop:
15347 /*
15348 * Get here to try the next alternative, pop it from the stack.
15349 */
15350 if (stackidx == 0) /* stack is empty, finished */
15351 break;
15352
15353 /* pop an item from the stack */
15354 --stackidx;
15355 gi = stack[stackidx].goodi;
15356 bi = stack[stackidx].badi;
15357 score = stack[stackidx].score;
15358 }
15359
15360 /* When the score goes over "limit" it may actually be much higher.
15361 * Return a very large number to avoid going below the limit when giving a
15362 * bonus. */
15363 if (minscore > limit)
15364 return SCORE_MAXMAX;
15365 return minscore;
15366}
15367#endif
15368
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015369/*
15370 * ":spellinfo"
15371 */
15372/*ARGSUSED*/
15373 void
15374ex_spellinfo(eap)
15375 exarg_T *eap;
15376{
15377 int lpi;
15378 langp_T *lp;
15379 char_u *p;
15380
15381 if (no_spell_checking(curwin))
15382 return;
15383
15384 msg_start();
15385 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15386 {
15387 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15388 msg_puts((char_u *)"file: ");
15389 msg_puts(lp->lp_slang->sl_fname);
15390 msg_putchar('\n');
15391 p = lp->lp_slang->sl_info;
15392 if (p != NULL)
15393 {
15394 msg_puts(p);
15395 msg_putchar('\n');
15396 }
15397 }
15398 msg_end();
15399}
15400
Bram Moolenaar4770d092006-01-12 23:22:24 +000015401#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15402#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015403#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015404#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15405#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015406
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015407/*
15408 * ":spelldump"
15409 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015410 void
15411ex_spelldump(eap)
15412 exarg_T *eap;
15413{
15414 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015415
15416 if (no_spell_checking(curwin))
15417 return;
15418
15419 /* Create a new empty buffer by splitting the window. */
15420 do_cmdline_cmd((char_u *)"new");
15421 if (!bufempty() || !buf_valid(buf))
15422 return;
15423
15424 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15425
15426 /* Delete the empty line that we started with. */
15427 if (curbuf->b_ml.ml_line_count > 1)
15428 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15429
15430 redraw_later(NOT_VALID);
15431}
15432
15433/*
15434 * Go through all possible words and:
15435 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15436 * "ic" and "dir" are not used.
15437 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15438 */
15439 void
15440spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15441 buf_T *buf; /* buffer with spell checking */
15442 char_u *pat; /* leading part of the word */
15443 int ic; /* ignore case */
15444 int *dir; /* direction for adding matches */
15445 int dumpflags_arg; /* DUMPFLAG_* */
15446{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015447 langp_T *lp;
15448 slang_T *slang;
15449 idx_T arridx[MAXWLEN];
15450 int curi[MAXWLEN];
15451 char_u word[MAXWLEN];
15452 int c;
15453 char_u *byts;
15454 idx_T *idxs;
15455 linenr_T lnum = 0;
15456 int round;
15457 int depth;
15458 int n;
15459 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015460 char_u *region_names = NULL; /* region names being used */
15461 int do_region = TRUE; /* dump region names and numbers */
15462 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015463 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015464 int dumpflags = dumpflags_arg;
15465 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015466
Bram Moolenaard0131a82006-03-04 21:46:13 +000015467 /* When ignoring case or when the pattern starts with capital pass this on
15468 * to dump_word(). */
15469 if (pat != NULL)
15470 {
15471 if (ic)
15472 dumpflags |= DUMPFLAG_ICASE;
15473 else
15474 {
15475 n = captype(pat, NULL);
15476 if (n == WF_ONECAP)
15477 dumpflags |= DUMPFLAG_ONECAP;
15478 else if (n == WF_ALLCAP
15479#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015480 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015481#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015482 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015483#endif
15484 )
15485 dumpflags |= DUMPFLAG_ALLCAP;
15486 }
15487 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015488
Bram Moolenaar7887d882005-07-01 22:33:52 +000015489 /* Find out if we can support regions: All languages must support the same
15490 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015491 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015492 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015493 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015494 p = lp->lp_slang->sl_regions;
15495 if (p[0] != 0)
15496 {
15497 if (region_names == NULL) /* first language with regions */
15498 region_names = p;
15499 else if (STRCMP(region_names, p) != 0)
15500 {
15501 do_region = FALSE; /* region names are different */
15502 break;
15503 }
15504 }
15505 }
15506
15507 if (do_region && region_names != NULL)
15508 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015509 if (pat == NULL)
15510 {
15511 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15512 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15513 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015514 }
15515 else
15516 do_region = FALSE;
15517
15518 /*
15519 * Loop over all files loaded for the entries in 'spelllang'.
15520 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015521 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015522 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015523 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015524 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015525 if (slang->sl_fbyts == NULL) /* reloading failed */
15526 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015527
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015528 if (pat == NULL)
15529 {
15530 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15531 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15532 }
15533
15534 /* When matching with a pattern and there are no prefixes only use
15535 * parts of the tree that match "pat". */
15536 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015537 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015538 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015539 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015540
15541 /* round 1: case-folded tree
15542 * round 2: keep-case tree */
15543 for (round = 1; round <= 2; ++round)
15544 {
15545 if (round == 1)
15546 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015547 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548 byts = slang->sl_fbyts;
15549 idxs = slang->sl_fidxs;
15550 }
15551 else
15552 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015553 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015554 byts = slang->sl_kbyts;
15555 idxs = slang->sl_kidxs;
15556 }
15557 if (byts == NULL)
15558 continue; /* array is empty */
15559
15560 depth = 0;
15561 arridx[0] = 0;
15562 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015563 while (depth >= 0 && !got_int
15564 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015565 {
15566 if (curi[depth] > byts[arridx[depth]])
15567 {
15568 /* Done all bytes at this node, go up one level. */
15569 --depth;
15570 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015571 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015572 }
15573 else
15574 {
15575 /* Do one more byte at this node. */
15576 n = arridx[depth] + curi[depth];
15577 ++curi[depth];
15578 c = byts[n];
15579 if (c == 0)
15580 {
15581 /* End of word, deal with the word.
15582 * Don't use keep-case words in the fold-case tree,
15583 * they will appear in the keep-case tree.
15584 * Only use the word when the region matches. */
15585 flags = (int)idxs[n];
15586 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015587 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015588 && (do_region
15589 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015590 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015591 & lp->lp_region) != 0))
15592 {
15593 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015594 if (!do_region)
15595 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015596
15597 /* Dump the basic word if there is no prefix or
15598 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015599 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015600 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015601 {
15602 dump_word(slang, word, pat, dir,
15603 dumpflags, flags, lnum);
15604 if (pat == NULL)
15605 ++lnum;
15606 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015607
15608 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015609 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015610 lnum = dump_prefixes(slang, word, pat, dir,
15611 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015612 }
15613 }
15614 else
15615 {
15616 /* Normal char, go one level deeper. */
15617 word[depth++] = c;
15618 arridx[depth] = idxs[n];
15619 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015620
15621 /* Check if this characters matches with the pattern.
15622 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015623 * Always ignore case here, dump_word() will check
15624 * proper case later. This isn't exactly right when
15625 * length changes for multi-byte characters with
15626 * ignore case... */
15627 if (depth <= patlen
15628 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015629 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015630 }
15631 }
15632 }
15633 }
15634 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015635}
15636
15637/*
15638 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015639 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015640 */
15641 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015642dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015643 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015644 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015645 char_u *pat;
15646 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015647 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015648 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015649 linenr_T lnum;
15650{
15651 int keepcap = FALSE;
15652 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015653 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015654 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015655 char_u badword[MAXWLEN + 10];
15656 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015657 int flags = wordflags;
15658
15659 if (dumpflags & DUMPFLAG_ONECAP)
15660 flags |= WF_ONECAP;
15661 if (dumpflags & DUMPFLAG_ALLCAP)
15662 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015663
Bram Moolenaar4770d092006-01-12 23:22:24 +000015664 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015665 {
15666 /* Need to fix case according to "flags". */
15667 make_case_word(word, cword, flags);
15668 p = cword;
15669 }
15670 else
15671 {
15672 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015673 if ((dumpflags & DUMPFLAG_KEEPCASE)
15674 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015675 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015676 keepcap = TRUE;
15677 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015678 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015679
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015680 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015681 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015682 /* Add flags and regions after a slash. */
15683 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015684 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015685 STRCPY(badword, p);
15686 STRCAT(badword, "/");
15687 if (keepcap)
15688 STRCAT(badword, "=");
15689 if (flags & WF_BANNED)
15690 STRCAT(badword, "!");
15691 else if (flags & WF_RARE)
15692 STRCAT(badword, "?");
15693 if (flags & WF_REGION)
15694 for (i = 0; i < 7; ++i)
15695 if (flags & (0x10000 << i))
15696 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15697 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015698 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015699
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015700 if (dumpflags & DUMPFLAG_COUNT)
15701 {
15702 hashitem_T *hi;
15703
15704 /* Include the word count for ":spelldump!". */
15705 hi = hash_find(&slang->sl_wordcount, tw);
15706 if (!HASHITEM_EMPTY(hi))
15707 {
15708 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15709 tw, HI2WC(hi)->wc_count);
15710 p = IObuff;
15711 }
15712 }
15713
15714 ml_append(lnum, p, (colnr_T)0, FALSE);
15715 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015716 else if (((dumpflags & DUMPFLAG_ICASE)
15717 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15718 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015719 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015720 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015721 /* if dir was BACKWARD then honor it just once */
15722 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015723}
15724
15725/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015726 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15727 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015728 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015729 * Return the updated line number.
15730 */
15731 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015732dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015733 slang_T *slang;
15734 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015735 char_u *pat;
15736 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015737 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015738 int flags; /* flags with prefix ID */
15739 linenr_T startlnum;
15740{
15741 idx_T arridx[MAXWLEN];
15742 int curi[MAXWLEN];
15743 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015744 char_u word_up[MAXWLEN];
15745 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015746 int c;
15747 char_u *byts;
15748 idx_T *idxs;
15749 linenr_T lnum = startlnum;
15750 int depth;
15751 int n;
15752 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015753 int i;
15754
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015755 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015756 * upper-case letter in word_up[]. */
15757 c = PTR2CHAR(word);
15758 if (SPELL_TOUPPER(c) != c)
15759 {
15760 onecap_copy(word, word_up, TRUE);
15761 has_word_up = TRUE;
15762 }
15763
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015764 byts = slang->sl_pbyts;
15765 idxs = slang->sl_pidxs;
15766 if (byts != NULL) /* array not is empty */
15767 {
15768 /*
15769 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015770 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015771 */
15772 depth = 0;
15773 arridx[0] = 0;
15774 curi[0] = 1;
15775 while (depth >= 0 && !got_int)
15776 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015777 n = arridx[depth];
15778 len = byts[n];
15779 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015780 {
15781 /* Done all bytes at this node, go up one level. */
15782 --depth;
15783 line_breakcheck();
15784 }
15785 else
15786 {
15787 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015788 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015789 ++curi[depth];
15790 c = byts[n];
15791 if (c == 0)
15792 {
15793 /* End of prefix, find out how many IDs there are. */
15794 for (i = 1; i < len; ++i)
15795 if (byts[n + i] != 0)
15796 break;
15797 curi[depth] += i - 1;
15798
Bram Moolenaar53805d12005-08-01 07:08:33 +000015799 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15800 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015801 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015802 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015803 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015804 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015805 : flags, lnum);
15806 if (lnum != 0)
15807 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015808 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015809
15810 /* Check for prefix that matches the word when the
15811 * first letter is upper-case, but only if the prefix has
15812 * a condition. */
15813 if (has_word_up)
15814 {
15815 c = valid_word_prefix(i, n, flags, word_up, slang,
15816 TRUE);
15817 if (c != 0)
15818 {
15819 vim_strncpy(prefix + depth, word_up,
15820 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015821 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015822 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015823 : flags, lnum);
15824 if (lnum != 0)
15825 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015826 }
15827 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015828 }
15829 else
15830 {
15831 /* Normal char, go one level deeper. */
15832 prefix[depth++] = c;
15833 arridx[depth] = idxs[n];
15834 curi[depth] = 1;
15835 }
15836 }
15837 }
15838 }
15839
15840 return lnum;
15841}
15842
Bram Moolenaar95529562005-08-25 21:21:38 +000015843/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015844 * Move "p" to the end of word "start".
15845 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015846 */
15847 char_u *
15848spell_to_word_end(start, buf)
15849 char_u *start;
15850 buf_T *buf;
15851{
15852 char_u *p = start;
15853
15854 while (*p != NUL && spell_iswordp(p, buf))
15855 mb_ptr_adv(p);
15856 return p;
15857}
15858
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015859#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015860/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015861 * For Insert mode completion CTRL-X s:
15862 * Find start of the word in front of column "startcol".
15863 * We don't check if it is badly spelled, with completion we can only change
15864 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015865 * Returns the column number of the word.
15866 */
15867 int
15868spell_word_start(startcol)
15869 int startcol;
15870{
15871 char_u *line;
15872 char_u *p;
15873 int col = 0;
15874
Bram Moolenaar95529562005-08-25 21:21:38 +000015875 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015876 return startcol;
15877
15878 /* Find a word character before "startcol". */
15879 line = ml_get_curline();
15880 for (p = line + startcol; p > line; )
15881 {
15882 mb_ptr_back(line, p);
15883 if (spell_iswordp_nmw(p))
15884 break;
15885 }
15886
15887 /* Go back to start of the word. */
15888 while (p > line)
15889 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015890 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015891 mb_ptr_back(line, p);
15892 if (!spell_iswordp(p, curbuf))
15893 break;
15894 col = 0;
15895 }
15896
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015897 return col;
15898}
15899
15900/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015901 * Need to check for 'spellcapcheck' now, the word is removed before
15902 * expand_spelling() is called. Therefore the ugly global variable.
15903 */
15904static int spell_expand_need_cap;
15905
15906 void
15907spell_expand_check_cap(col)
15908 colnr_T col;
15909{
15910 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15911}
15912
15913/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015914 * Get list of spelling suggestions.
15915 * Used for Insert mode completion CTRL-X ?.
15916 * Returns the number of matches. The matches are in "matchp[]", array of
15917 * allocated strings.
15918 */
15919/*ARGSUSED*/
15920 int
15921expand_spelling(lnum, col, pat, matchp)
15922 linenr_T lnum;
15923 int col;
15924 char_u *pat;
15925 char_u ***matchp;
15926{
15927 garray_T ga;
15928
Bram Moolenaar4770d092006-01-12 23:22:24 +000015929 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015930 *matchp = ga.ga_data;
15931 return ga.ga_len;
15932}
15933#endif
15934
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015935#endif /* FEAT_SPELL */