blob: 8c9a89184a4709ba1d0157ab7c416a32944c2354 [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,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000080 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 */
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
Bram Moolenaar4770d092006-01-12 23:22:24 +0000314#ifndef UNIX /* it's in os_unix.h for Unix */
315# include <time.h> /* for time_t */
316#endif
317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000318#define MAXWLEN 250 /* Assume max. word len is this many bytes.
319 Some places assume a word length fits in a
320 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000321
Bram Moolenaare52325c2005-08-22 22:54:29 +0000322/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000323 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000324#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000325typedef int idx_T;
326#else
327typedef long idx_T;
328#endif
329
330/* Flags used for a word. Only the lowest byte can be used, the region byte
331 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000332#define WF_REGION 0x01 /* region byte follows */
333#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
334#define WF_ALLCAP 0x04 /* word must be all capitals */
335#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000336#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000337#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000338#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000339#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000340
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000341/* for <flags2>, shifted up one byte to be used in wn_flags */
342#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000343#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000344#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000345#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000346#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
347#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000348
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000349/* only used for su_badflags */
350#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
351
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000352#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000353
Bram Moolenaar53805d12005-08-01 07:08:33 +0000354/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000355#define WFP_RARE 0x01 /* rare prefix */
356#define WFP_NC 0x02 /* prefix is not combining */
357#define WFP_UP 0x04 /* to-upper prefix */
358#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
359#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000360
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000361/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
362 * byte) and prefcondnr (two bytes). */
363#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
364#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
365#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
366#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
367 * COMPOUNDPERMITFLAG */
368#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
369 * COMPOUNDFORBIDFLAG */
370
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000371
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000372/* flags for <compoptions> */
373#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
374#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
375#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
376#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
377
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000378/* Special byte values for <byte>. Some are only used in the tree for
379 * postponed prefixes, some only in the other trees. This is a bit messy... */
380#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000381 * postponed prefix: no <pflags> */
382#define BY_INDEX 1 /* child is shared, index follows */
383#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
384 * postponed prefix: <pflags> follows */
385#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
386 * follow; never used in prefix tree */
387#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000388
Bram Moolenaar4770d092006-01-12 23:22:24 +0000389/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
390 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 * One replacement: from "ft_from" to "ft_to". */
392typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000394 char_u *ft_from;
395 char_u *ft_to;
396} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000398/* Info from "SAL" entries in ".aff" file used in sl_sal.
399 * The info is split for quick processing by spell_soundfold().
400 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
401typedef struct salitem_S
402{
403 char_u *sm_lead; /* leading letters */
404 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000405 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000406 char_u *sm_rules; /* rules like ^, $, priority */
407 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000408#ifdef FEAT_MBYTE
409 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000410 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000411 int *sm_to_w; /* wide character copy of "sm_to" */
412#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000413} salitem_T;
414
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000415#ifdef FEAT_MBYTE
416typedef int salfirst_T;
417#else
418typedef short salfirst_T;
419#endif
420
Bram Moolenaar5195e452005-08-19 20:32:47 +0000421/* Values for SP_*ERROR are negative, positive values are used by
422 * read_cnt_string(). */
423#define SP_TRUNCERROR -1 /* spell file truncated error */
424#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000425#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000428 * Structure used to store words and other info for one language, loaded from
429 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000430 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
431 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
432 *
433 * The "byts" array stores the possible bytes in each tree node, preceded by
434 * the number of possible bytes, sorted on byte value:
435 * <len> <byte1> <byte2> ...
436 * The "idxs" array stores the index of the child node corresponding to the
437 * byte in "byts".
438 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000439 * the flags, region mask and affixID for the word. There may be several
440 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000441 */
442typedef struct slang_S slang_T;
443struct slang_S
444{
445 slang_T *sl_next; /* next language */
446 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000447 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000448 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000449
Bram Moolenaar51485f02005-06-04 21:55:20 +0000450 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000451 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000452 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000453 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000454 char_u *sl_pbyts; /* prefix tree word bytes */
455 idx_T *sl_pidxs; /* prefix tree word indexes */
456
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000457 char_u *sl_info; /* infotext string or NULL */
458
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000461 char_u *sl_midword; /* MIDWORD string or NULL */
462
Bram Moolenaar4770d092006-01-12 23:22:24 +0000463 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
464
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000465 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000466 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000467 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000468 int sl_compoptions; /* COMP_* flags */
469 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000470 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 * (NULL when no compounding) */
472 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000473 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000474 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000475 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
476 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000477
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000478 int sl_prefixcnt; /* number of items in "sl_prefprog" */
479 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000481 garray_T sl_rep; /* list of fromto_T entries from REP lines */
482 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
483 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000484 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000485 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000486 there is none */
487 int sl_followup; /* SAL followup */
488 int sl_collapse; /* SAL collapse_result */
489 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000490 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
491 * "sl_sal_first" maps chars, when has_mbyte
492 * "sl_sal" is a list of wide char lists. */
493 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
494 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000495 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000496
497 /* Info from the .sug file. Loaded on demand. */
498 time_t sl_sugtime; /* timestamp for .sug file */
499 char_u *sl_sbyts; /* soundfolded word bytes */
500 idx_T *sl_sidxs; /* soundfolded word indexes */
501 buf_T *sl_sugbuf; /* buffer with word number table */
502 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
503 load */
504
Bram Moolenaarea424162005-06-16 21:51:00 +0000505 int sl_has_map; /* TRUE if there is a MAP line */
506#ifdef FEAT_MBYTE
507 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
508 int sl_map_array[256]; /* MAP for first 256 chars */
509#else
510 char_u sl_map_array[256]; /* MAP for first 256 chars */
511#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000512 hashtab_T sl_sounddone; /* table with soundfolded words that have
513 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000514};
515
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000516/* First language that is loaded, start of the linked list of loaded
517 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000518static slang_T *first_lang = NULL;
519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000520/* Flags used in .spl file for soundsalike flags. */
521#define SAL_F0LLOWUP 1
522#define SAL_COLLAPSE 2
523#define SAL_REM_ACCENTS 4
524
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525/*
526 * Structure used in "b_langp", filled from 'spelllang'.
527 */
528typedef struct langp_S
529{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000530 slang_T *lp_slang; /* info for this language */
531 slang_T *lp_sallang; /* language used for sound folding or NULL */
532 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000533 int lp_region; /* bitmask for region or REGION_ALL */
534} langp_T;
535
536#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
537
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000538#define REGION_ALL 0xff /* word valid in all regions */
539
Bram Moolenaar5195e452005-08-19 20:32:47 +0000540#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
541#define VIMSPELLMAGICL 8
542#define VIMSPELLVERSION 50
543
Bram Moolenaar4770d092006-01-12 23:22:24 +0000544#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
545#define VIMSUGMAGICL 6
546#define VIMSUGVERSION 1
547
Bram Moolenaar5195e452005-08-19 20:32:47 +0000548/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
549#define SN_REGION 0 /* <regionname> section */
550#define SN_CHARFLAGS 1 /* charflags section */
551#define SN_MIDWORD 2 /* <midword> section */
552#define SN_PREFCOND 3 /* <prefcond> section */
553#define SN_REP 4 /* REP items section */
554#define SN_SAL 5 /* SAL items section */
555#define SN_SOFO 6 /* soundfolding section */
556#define SN_MAP 7 /* MAP items section */
557#define SN_COMPOUND 8 /* compound words section */
558#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000559#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000560#define SN_SUGFILE 11 /* timestamp for .sug file */
561#define SN_REPSAL 12 /* REPSAL items section */
562#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000563#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000564#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000565#define SN_END 255 /* end of sections */
566
567#define SNF_REQUIRED 1 /* <sectionflags>: required section */
568
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000569/* Result values. Lower number is accepted over higher one. */
570#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000572#define SP_RARE 1
573#define SP_LOCAL 2
574#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000575
Bram Moolenaar7887d882005-07-01 22:33:52 +0000576/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000577static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000578
Bram Moolenaar4770d092006-01-12 23:22:24 +0000579typedef struct wordcount_S
580{
581 short_u wc_count; /* nr of times word was seen */
582 char_u wc_word[1]; /* word, actually longer */
583} wordcount_T;
584
585static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000586#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000587#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
588#define MAXWORDCOUNT 0xffff
589
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000590/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000591 * Information used when looking for suggestions.
592 */
593typedef struct suginfo_S
594{
595 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000596 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000597 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000598 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000599 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000600 char_u *su_badptr; /* start of bad word in line */
601 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000602 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
604 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000605 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000606 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000607 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000608} suginfo_T;
609
610/* One word suggestion. Used in "si_ga". */
611typedef struct suggest_S
612{
613 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615 int st_orglen; /* length of replaced text */
616 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000617 int st_altscore; /* used when st_score compares equal */
618 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000619 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000620 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621} suggest_T;
622
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000624
Bram Moolenaar4770d092006-01-12 23:22:24 +0000625/* TRUE if a word appears in the list of banned words. */
626#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
627
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000628/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000629 * what is displayed, because when rescore_suggestions() is called the score
630 * may change and wrong suggestions may be removed later. */
631#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000632
633/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
634 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000635#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000636
637/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000638#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000639#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000641#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000643#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000645#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646#define SCORE_SUBST 93 /* substitute a character */
647#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000648#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000649#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000650#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000651#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000652#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000653#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000654#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000655#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000656
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000657#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
659 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000660
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661#define SCORE_COMMON1 30 /* subtracted for words seen before */
662#define SCORE_COMMON2 40 /* subtracted for words often seen */
663#define SCORE_COMMON3 50 /* subtracted for words very often seen */
664#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
665#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
666
667/* When trying changed soundfold words it becomes slow when trying more than
668 * two changes. With less then two changes it's slightly faster but we miss a
669 * few good suggestions. In rare cases we need to try three of four changes.
670 */
671#define SCORE_SFMAX1 200 /* maximum score for first try */
672#define SCORE_SFMAX2 300 /* maximum score for second try */
673#define SCORE_SFMAX3 400 /* maximum score for third try */
674
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000675#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000676#define SCORE_MAXMAX 999999 /* accept any score */
677#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
678
679/* for spell_edit_score_limit() we need to know the minimum value of
680 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
681#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000682
683/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000684 * Structure to store info for word matching.
685 */
686typedef struct matchinf_S
687{
688 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000689
690 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000692 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000693 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000694 char_u *mi_cend; /* char after what was used for
695 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000696
697 /* case-folded text */
698 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000699 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000701 /* for when checking word after a prefix */
702 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000703 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000704 int mi_prefcnt; /* number of entries at mi_prefarridx */
705 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000706#ifdef FEAT_MBYTE
707 int mi_cprefixlen; /* byte length of prefix in original
708 case */
709#else
710# define mi_cprefixlen mi_prefixlen /* it's the same value */
711#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000712
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000713 /* for when checking a compound word */
714 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000715 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
716 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000717 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000718
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000719 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000720 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000721 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000722 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000723
724 /* for NOBREAK */
725 int mi_result2; /* "mi_resul" without following word */
726 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000727} matchinf_T;
728
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000729/*
730 * The tables used for recognizing word characters according to spelling.
731 * These are only used for the first 256 characters of 'encoding'.
732 */
733typedef struct spelltab_S
734{
735 char_u st_isw[256]; /* flags: is word char */
736 char_u st_isu[256]; /* flags: is uppercase char */
737 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000738 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000739} spelltab_T;
740
741static spelltab_T spelltab;
742static int did_set_spelltab;
743
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000744#define CF_WORD 0x01
745#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000746
747static void clear_spell_chartab __ARGS((spelltab_T *sp));
748static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000749static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
750static int spell_iswordp_nmw __ARGS((char_u *p));
751#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000752static int spell_mb_isword_class __ARGS((int cl));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000753static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
754#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000755static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000756
757/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000758 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000759 */
760typedef enum
761{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000762 STATE_START = 0, /* At start of node check for NUL bytes (goodword
763 * ends); if badword ends there is a match, otherwise
764 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000765 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000766 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000767 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
768 STATE_PLAIN, /* Use each byte of the node. */
769 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000770 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000771 STATE_INS, /* Insert a byte in the bad word. */
772 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000773 STATE_UNSWAP, /* Undo swap two characters. */
774 STATE_SWAP3, /* Swap two characters over three. */
775 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000778 STATE_REP_INI, /* Prepare for using REP items. */
779 STATE_REP, /* Use matching REP items from the .aff file. */
780 STATE_REP_UNDO, /* Undo a REP item replacement. */
781 STATE_FINAL /* End of this node. */
782} state_T;
783
784/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000785 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000786 */
787typedef struct trystate_S
788{
Bram Moolenaarea424162005-06-16 21:51:00 +0000789 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000790 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000792 short ts_curi; /* index in list of child nodes */
793 char_u ts_fidx; /* index in fword[], case-folded bad word */
794 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
795 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000796 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000797 * PFD_PREFIXTREE or PFD_NOPREFIX */
798 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000799#ifdef FEAT_MBYTE
800 char_u ts_tcharlen; /* number of bytes in tword character */
801 char_u ts_tcharidx; /* current byte index in tword character */
802 char_u ts_isdiff; /* DIFF_ values */
803 char_u ts_fcharstart; /* index in fword where badword char started */
804#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000805 char_u ts_prewordlen; /* length of word in "preword[]" */
806 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000807 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000808 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000809 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000810 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000811 char_u ts_delidx; /* index in fword for char that was deleted,
812 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000813} trystate_T;
814
Bram Moolenaarea424162005-06-16 21:51:00 +0000815/* values for ts_isdiff */
816#define DIFF_NONE 0 /* no different byte (yet) */
817#define DIFF_YES 1 /* different byte found */
818#define DIFF_INSERT 2 /* inserting character */
819
Bram Moolenaard12a1322005-08-21 22:08:24 +0000820/* values for ts_flags */
821#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
822#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000823#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000824
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000825/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000826#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000828#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000829
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000830/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000831#define FIND_FOLDWORD 0 /* find word case-folded */
832#define FIND_KEEPWORD 1 /* find keep-case word */
833#define FIND_PREFIX 2 /* find word after prefix */
834#define FIND_COMPOUND 3 /* find case-folded compound word */
835#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000836
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000837static slang_T *slang_alloc __ARGS((char_u *lang));
838static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000839static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000840static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000841static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000842static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000843static 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 +0000844static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000845static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000846static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000847static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000848static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000849static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000850static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000851static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000852static 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 +0000853static int get2c __ARGS((FILE *fd));
854static int get3c __ARGS((FILE *fd));
855static int get4c __ARGS((FILE *fd));
856static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000857static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000858static char_u *read_string __ARGS((FILE *fd, int cnt));
859static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
860static int read_charflags_section __ARGS((FILE *fd));
861static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000862static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000863static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000864static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
865static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
866static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000867static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
868static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000869static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870static int init_syl_tab __ARGS((slang_T *slang));
871static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000872static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
873static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000874#ifdef FEAT_MBYTE
875static int *mb_str2wide __ARGS((char_u *s));
876#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000877static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
878static 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 +0000879static void clear_midword __ARGS((buf_T *buf));
880static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000881static int find_region __ARGS((char_u *rp, char_u *region));
882static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000883static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000884static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000885static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000887static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000888static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000889static 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 +0000890#ifdef FEAT_EVAL
891static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
892#endif
893static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000894static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
895static void suggest_load_files __ARGS((void));
896static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000897static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000898static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000899static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000900static void suggest_try_special __ARGS((suginfo_T *su));
901static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000902static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
903static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000904#ifdef FEAT_MBYTE
905static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
906#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000907static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000908static void score_comp_sal __ARGS((suginfo_T *su));
909static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000910static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000911static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000912static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000913static void suggest_try_soundalike_finish __ARGS((void));
914static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
915static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000916static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000917static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000918static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000919static 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));
920static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000922static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000923static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000924static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000925static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
926static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
927static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000928#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000929static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000930#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000931static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000932static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
933static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
934#ifdef FEAT_MBYTE
935static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
936#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000937static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
938static 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 +0000939static buf_T *open_spellbuf __ARGS((void));
940static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000941
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000942/*
943 * Use our own character-case definitions, because the current locale may
944 * differ from what the .spl file uses.
945 * These must not be called with negative number!
946 */
947#ifndef FEAT_MBYTE
948/* Non-multi-byte implementation. */
949# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
950# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
951# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
952#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000953# if defined(HAVE_WCHAR_H)
954# include <wchar.h> /* for towupper() and towlower() */
955# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000956/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
957 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
958 * the "w" library function for characters above 255 if available. */
959# ifdef HAVE_TOWLOWER
960# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
961 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
962# else
963# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
964 : (c) < 256 ? spelltab.st_fold[c] : (c))
965# endif
966
967# ifdef HAVE_TOWUPPER
968# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
969 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
970# else
971# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
972 : (c) < 256 ? spelltab.st_upper[c] : (c))
973# endif
974
975# ifdef HAVE_ISWUPPER
976# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
977 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
978# else
979# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000980 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000981# endif
982#endif
983
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000984
985static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000986static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000987static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000988static char *e_affname = N_("Affix name too long in %s line %d: %s");
989static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
990static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000991static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000992
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000993/* Remember what "z?" replaced. */
994static char_u *repl_from = NULL;
995static char_u *repl_to = NULL;
996
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997/*
998 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000999 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001000 * "*attrp" is set to the highlight index for a badly spelled word. For a
1001 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001002 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001003 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001004 * "capcol" is used to check for a Capitalised word after the end of a
1005 * sentence. If it's zero then perform the check. Return the column where to
1006 * check next, or -1 when no sentence end was found. If it's NULL then don't
1007 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001009 * Returns the length of the word in bytes, also when it's OK, so that the
1010 * caller can skip over the word.
1011 */
1012 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001013spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001015 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001016 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001017 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001018 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019{
1020 matchinf_T mi; /* Most things are put in "mi" so that it can
1021 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001022 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001023 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001024 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001025 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001026 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001027
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001028 /* A word never starts at a space or a control character. Return quickly
1029 * then, skipping over the character. */
1030 if (*ptr <= ' ')
1031 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001032
1033 /* Return here when loading language files failed. */
1034 if (wp->w_buffer->b_langp.ga_len == 0)
1035 return 1;
1036
Bram Moolenaar5195e452005-08-19 20:32:47 +00001037 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001038
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001039 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001040 * 0X99FF. But always do check spelling to find "3GPP" and "11
1041 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001042 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001043 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001044 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1045 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001046 else
1047 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001048 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001050
Bram Moolenaar0c405862005-06-22 22:26:26 +00001051 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001052 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001053 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001054 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001055 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001056 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001058 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001059 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001060
1061 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1062 {
1063 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001064 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001065 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001066 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001067 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001068 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001069 if (capcol != NULL)
1070 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001071
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001072 /* We always use the characters up to the next non-word character,
1073 * also for bad words. */
1074 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001075
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001076 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001077 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001078
Bram Moolenaar5195e452005-08-19 20:32:47 +00001079 /* case-fold the word with one non-word character, so that we can check
1080 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001081 if (*mi.mi_fend != NUL)
1082 mb_ptr_adv(mi.mi_fend);
1083
1084 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1085 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001086 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001087
1088 /* The word is bad unless we recognize it. */
1089 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001090 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001091
1092 /*
1093 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001094 * We check them all, because a word may be matched longer in another
1095 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001096 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001097 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001098 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001099 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1100
1101 /* If reloading fails the language is still in the list but everything
1102 * has been cleared. */
1103 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1104 continue;
1105
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001106 /* Check for a matching word in case-folded words. */
1107 find_word(&mi, FIND_FOLDWORD);
1108
1109 /* Check for a matching word in keep-case words. */
1110 find_word(&mi, FIND_KEEPWORD);
1111
1112 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001113 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001114
1115 /* For a NOBREAK language, may want to use a word without a following
1116 * word as a backup. */
1117 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1118 && mi.mi_result2 != SP_BAD)
1119 {
1120 mi.mi_result = mi.mi_result2;
1121 mi.mi_end = mi.mi_end2;
1122 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001123
1124 /* Count the word in the first language where it's found to be OK. */
1125 if (count_word && mi.mi_result == SP_OK)
1126 {
1127 count_common_word(mi.mi_lp->lp_slang, ptr,
1128 (int)(mi.mi_end - ptr), 1);
1129 count_word = FALSE;
1130 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001131 }
1132
1133 if (mi.mi_result != SP_OK)
1134 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001135 /* If we found a number skip over it. Allows for "42nd". Do flag
1136 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001137 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001138 {
1139 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1140 return nrlen;
1141 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001142
1143 /* When we are at a non-word character there is no error, just
1144 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001145 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001146 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001147 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1148 {
1149 regmatch_T regmatch;
1150
1151 /* Check for end of sentence. */
1152 regmatch.regprog = wp->w_buffer->b_cap_prog;
1153 regmatch.rm_ic = FALSE;
1154 if (vim_regexec(&regmatch, ptr, 0))
1155 *capcol = (int)(regmatch.endp[0] - ptr);
1156 }
1157
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001158#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001159 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001160 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001161#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001163 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001164 else if (mi.mi_end == ptr)
1165 /* Always include at least one character. Required for when there
1166 * is a mixup in "midword". */
1167 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001168 else if (mi.mi_result == SP_BAD
1169 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1170 {
1171 char_u *p, *fp;
1172 int save_result = mi.mi_result;
1173
1174 /* First language in 'spelllang' is NOBREAK. Find first position
1175 * at which any word would be valid. */
1176 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001177 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001178 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001179 p = mi.mi_word;
1180 fp = mi.mi_fword;
1181 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001182 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001183 mb_ptr_adv(p);
1184 mb_ptr_adv(fp);
1185 if (p >= mi.mi_end)
1186 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001187 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001188 find_word(&mi, FIND_COMPOUND);
1189 if (mi.mi_result != SP_BAD)
1190 {
1191 mi.mi_end = p;
1192 break;
1193 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001194 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001195 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001196 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001198
1199 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001200 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001201 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001202 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001203 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001204 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001205 }
1206
Bram Moolenaar5195e452005-08-19 20:32:47 +00001207 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1208 {
1209 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001210 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001211 return wrongcaplen;
1212 }
1213
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 return (int)(mi.mi_end - ptr);
1215}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001216
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217/*
1218 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001219 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1220 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1221 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1222 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001223 *
1224 * For a match mip->mi_result is updated.
1225 */
1226 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001227find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001228 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001229 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001231 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001232 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001233 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 int endidxcnt = 0;
1235 int len;
1236 int wlen = 0;
1237 int flen;
1238 int c;
1239 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001240 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001241#ifdef FEAT_MBYTE
1242 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001243#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001244 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001245 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001246 slang_T *slang = mip->mi_lp->lp_slang;
1247 unsigned flags;
1248 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001249 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001250 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001251 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001252 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001253
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001254 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001255 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256 /* Check for word with matching case in keep-case tree. */
1257 ptr = mip->mi_word;
1258 flen = 9999; /* no case folding, always enough bytes */
1259 byts = slang->sl_kbyts;
1260 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001261
1262 if (mode == FIND_KEEPCOMPOUND)
1263 /* Skip over the previously found word(s). */
1264 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001265 }
1266 else
1267 {
1268 /* Check for case-folded in case-folded tree. */
1269 ptr = mip->mi_fword;
1270 flen = mip->mi_fwordlen; /* available case-folded bytes */
1271 byts = slang->sl_fbyts;
1272 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001273
1274 if (mode == FIND_PREFIX)
1275 {
1276 /* Skip over the prefix. */
1277 wlen = mip->mi_prefixlen;
1278 flen -= mip->mi_prefixlen;
1279 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001280 else if (mode == FIND_COMPOUND)
1281 {
1282 /* Skip over the previously found word(s). */
1283 wlen = mip->mi_compoff;
1284 flen -= mip->mi_compoff;
1285 }
1286
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001287 }
1288
Bram Moolenaar51485f02005-06-04 21:55:20 +00001289 if (byts == NULL)
1290 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001291
Bram Moolenaar51485f02005-06-04 21:55:20 +00001292 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001293 * Repeat advancing in the tree until:
1294 * - there is a byte that doesn't match,
1295 * - we reach the end of the tree,
1296 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001297 */
1298 for (;;)
1299 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001300 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001301 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001302
1303 len = byts[arridx++];
1304
1305 /* If the first possible byte is a zero the word could end here.
1306 * Remember this index, we first check for the longest word. */
1307 if (byts[arridx] == 0)
1308 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001309 if (endidxcnt == MAXWLEN)
1310 {
1311 /* Must be a corrupted spell file. */
1312 EMSG(_(e_format));
1313 return;
1314 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001315 endlen[endidxcnt] = wlen;
1316 endidx[endidxcnt++] = arridx++;
1317 --len;
1318
1319 /* Skip over the zeros, there can be several flag/region
1320 * combinations. */
1321 while (len > 0 && byts[arridx] == 0)
1322 {
1323 ++arridx;
1324 --len;
1325 }
1326 if (len == 0)
1327 break; /* no children, word must end here */
1328 }
1329
1330 /* Stop looking at end of the line. */
1331 if (ptr[wlen] == NUL)
1332 break;
1333
1334 /* Perform a binary search in the list of accepted bytes. */
1335 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001336 if (c == TAB) /* <Tab> is handled like <Space> */
1337 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001338 lo = arridx;
1339 hi = arridx + len - 1;
1340 while (lo < hi)
1341 {
1342 m = (lo + hi) / 2;
1343 if (byts[m] > c)
1344 hi = m - 1;
1345 else if (byts[m] < c)
1346 lo = m + 1;
1347 else
1348 {
1349 lo = hi = m;
1350 break;
1351 }
1352 }
1353
1354 /* Stop if there is no matching byte. */
1355 if (hi < lo || byts[lo] != c)
1356 break;
1357
1358 /* Continue at the child (if there is one). */
1359 arridx = idxs[lo];
1360 ++wlen;
1361 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001362
1363 /* One space in the good word may stand for several spaces in the
1364 * checked word. */
1365 if (c == ' ')
1366 {
1367 for (;;)
1368 {
1369 if (flen <= 0 && *mip->mi_fend != NUL)
1370 flen = fold_more(mip);
1371 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1372 break;
1373 ++wlen;
1374 --flen;
1375 }
1376 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377 }
1378
1379 /*
1380 * Verify that one of the possible endings is valid. Try the longest
1381 * first.
1382 */
1383 while (endidxcnt > 0)
1384 {
1385 --endidxcnt;
1386 arridx = endidx[endidxcnt];
1387 wlen = endlen[endidxcnt];
1388
1389#ifdef FEAT_MBYTE
1390 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1391 continue; /* not at first byte of character */
1392#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001393 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001394 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001395 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001396 continue; /* next char is a word character */
1397 word_ends = FALSE;
1398 }
1399 else
1400 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001401 /* The prefix flag is before compound flags. Once a valid prefix flag
1402 * has been found we try compound flags. */
1403 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001404
1405#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001406 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407 {
1408 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 * when folding case. This can be slow, take a shortcut when the
1410 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 if (STRNCMP(ptr, p, wlen) != 0)
1413 {
1414 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1415 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001416 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001417 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 }
1419#endif
1420
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001421 /* Check flags and region. For FIND_PREFIX check the condition and
1422 * prefix ID.
1423 * Repeat this if there are more flags/region alternatives until there
1424 * is a match. */
1425 res = SP_BAD;
1426 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1427 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428 {
1429 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001430
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001431 /* For the fold-case tree check that the case of the checked word
1432 * matches with what the word in the tree requires.
1433 * For keep-case tree the case is always right. For prefixes we
1434 * don't bother to check. */
1435 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001436 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001437 if (mip->mi_cend != mip->mi_word + wlen)
1438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001439 /* mi_capflags was set for a different word length, need
1440 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001441 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 }
1444
Bram Moolenaar0c405862005-06-22 22:26:26 +00001445 if (mip->mi_capflags == WF_KEEPCAP
1446 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001447 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 }
1449
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001450 /* When mode is FIND_PREFIX the word must support the prefix:
1451 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001452 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001453 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001455 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001456 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001457 mip->mi_word + mip->mi_cprefixlen, slang,
1458 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001459 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001461
1462 /* Use the WF_RARE flag for a rare prefix. */
1463 if (c & WF_RAREPFX)
1464 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001465 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001466 }
1467
Bram Moolenaar78622822005-08-23 21:00:13 +00001468 if (slang->sl_nobreak)
1469 {
1470 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1471 && (flags & WF_BANNED) == 0)
1472 {
1473 /* NOBREAK: found a valid following word. That's all we
1474 * need to know, so return. */
1475 mip->mi_result = SP_OK;
1476 break;
1477 }
1478 }
1479
1480 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1481 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001482 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001483 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001484 * COMPOUNDMIN reject it quickly.
1485 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001486 * that's too short... Myspell compatibility requires this
1487 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001488 if (((unsigned)flags >> 24) == 0
1489 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001490 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001491#ifdef FEAT_MBYTE
1492 /* For multi-byte chars check character length against
1493 * COMPOUNDMIN. */
1494 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001495 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001496 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1497 wlen - mip->mi_compoff) < slang->sl_compminlen)
1498 continue;
1499#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001500
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001501 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001502 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001503 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1504 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001505 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001506 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001507
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001508 /* Don't allow compounding on a side where an affix was added,
1509 * unless COMPOUNDPERMITFLAG was used. */
1510 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1511 continue;
1512 if (!word_ends && (flags & WF_NOCOMPAFT))
1513 continue;
1514
Bram Moolenaard12a1322005-08-21 22:08:24 +00001515 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001516 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001517 ? slang->sl_compstartflags
1518 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001519 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001520 continue;
1521
Bram Moolenaare52325c2005-08-22 22:54:29 +00001522 if (mode == FIND_COMPOUND)
1523 {
1524 int capflags;
1525
1526 /* Need to check the caps type of the appended compound
1527 * word. */
1528#ifdef FEAT_MBYTE
1529 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1530 mip->mi_compoff) != 0)
1531 {
1532 /* case folding may have changed the length */
1533 p = mip->mi_word;
1534 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1535 mb_ptr_adv(p);
1536 }
1537 else
1538#endif
1539 p = mip->mi_word + mip->mi_compoff;
1540 capflags = captype(p, mip->mi_word + wlen);
1541 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1542 && (flags & WF_FIXCAP) != 0))
1543 continue;
1544
1545 if (capflags != WF_ALLCAP)
1546 {
1547 /* When the character before the word is a word
1548 * character we do not accept a Onecap word. We do
1549 * accept a no-caps word, even when the dictionary
1550 * word specifies ONECAP. */
1551 mb_ptr_back(mip->mi_word, p);
1552 if (spell_iswordp_nmw(p)
1553 ? capflags == WF_ONECAP
1554 : (flags & WF_ONECAP) != 0
1555 && capflags != WF_ONECAP)
1556 continue;
1557 }
1558 }
1559
Bram Moolenaar5195e452005-08-19 20:32:47 +00001560 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001561 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001562 * the number of syllables must not be too large. */
1563 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1564 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1565 if (word_ends)
1566 {
1567 char_u fword[MAXWLEN];
1568
1569 if (slang->sl_compsylmax < MAXWLEN)
1570 {
1571 /* "fword" is only needed for checking syllables. */
1572 if (ptr == mip->mi_word)
1573 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1574 else
1575 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1576 }
1577 if (!can_compound(slang, fword, mip->mi_compflags))
1578 continue;
1579 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001580 }
1581
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001582 /* Check NEEDCOMPOUND: can't use word without compounding. */
1583 else if (flags & WF_NEEDCOMP)
1584 continue;
1585
Bram Moolenaar78622822005-08-23 21:00:13 +00001586 nobreak_result = SP_OK;
1587
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001588 if (!word_ends)
1589 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001590 int save_result = mip->mi_result;
1591 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001592 langp_T *save_lp = mip->mi_lp;
1593 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001594
1595 /* Check that a valid word follows. If there is one and we
1596 * are compounding, it will set "mi_result", thus we are
1597 * always finished here. For NOBREAK we only check that a
1598 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001599 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001600 if (slang->sl_nobreak)
1601 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001602
1603 /* Find following word in case-folded tree. */
1604 mip->mi_compoff = endlen[endidxcnt];
1605#ifdef FEAT_MBYTE
1606 if (has_mbyte && mode == FIND_KEEPWORD)
1607 {
1608 /* Compute byte length in case-folded word from "wlen":
1609 * byte length in keep-case word. Length may change when
1610 * folding case. This can be slow, take a shortcut when
1611 * the case-folded word is equal to the keep-case word. */
1612 p = mip->mi_fword;
1613 if (STRNCMP(ptr, p, wlen) != 0)
1614 {
1615 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1616 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001617 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001618 }
1619 }
1620#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001621 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001622 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001623 if (flags & WF_COMPROOT)
1624 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001625
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001626 /* For NOBREAK we need to try all NOBREAK languages, at least
1627 * to find the ".add" file(s). */
1628 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001629 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001630 if (slang->sl_nobreak)
1631 {
1632 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1633 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1634 || !mip->mi_lp->lp_slang->sl_nobreak)
1635 continue;
1636 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001637
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001638 find_word(mip, FIND_COMPOUND);
1639
1640 /* When NOBREAK any word that matches is OK. Otherwise we
1641 * need to find the longest match, thus try with keep-case
1642 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001643 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1644 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001645 /* Find following word in keep-case tree. */
1646 mip->mi_compoff = wlen;
1647 find_word(mip, FIND_KEEPCOMPOUND);
1648
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001649#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1650 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1651 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001652 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1653 {
1654 /* Check for following word with prefix. */
1655 mip->mi_compoff = c;
1656 find_prefix(mip, FIND_COMPOUND);
1657 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001658#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001659 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001660
1661 if (!slang->sl_nobreak)
1662 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001663 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001664 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001665 if (flags & WF_COMPROOT)
1666 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001667 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001668
Bram Moolenaar78622822005-08-23 21:00:13 +00001669 if (slang->sl_nobreak)
1670 {
1671 nobreak_result = mip->mi_result;
1672 mip->mi_result = save_result;
1673 mip->mi_end = save_end;
1674 }
1675 else
1676 {
1677 if (mip->mi_result == SP_OK)
1678 break;
1679 continue;
1680 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001681 }
1682
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001683 if (flags & WF_BANNED)
1684 res = SP_BANNED;
1685 else if (flags & WF_REGION)
1686 {
1687 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001688 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001689 res = SP_OK;
1690 else
1691 res = SP_LOCAL;
1692 }
1693 else if (flags & WF_RARE)
1694 res = SP_RARE;
1695 else
1696 res = SP_OK;
1697
Bram Moolenaar78622822005-08-23 21:00:13 +00001698 /* Always use the longest match and the best result. For NOBREAK
1699 * we separately keep the longest match without a following good
1700 * word as a fall-back. */
1701 if (nobreak_result == SP_BAD)
1702 {
1703 if (mip->mi_result2 > res)
1704 {
1705 mip->mi_result2 = res;
1706 mip->mi_end2 = mip->mi_word + wlen;
1707 }
1708 else if (mip->mi_result2 == res
1709 && mip->mi_end2 < mip->mi_word + wlen)
1710 mip->mi_end2 = mip->mi_word + wlen;
1711 }
1712 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001713 {
1714 mip->mi_result = res;
1715 mip->mi_end = mip->mi_word + wlen;
1716 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001717 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001718 mip->mi_end = mip->mi_word + wlen;
1719
Bram Moolenaar78622822005-08-23 21:00:13 +00001720 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001721 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001722 }
1723
Bram Moolenaar78622822005-08-23 21:00:13 +00001724 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001726 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001727}
1728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001730 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1731 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001732 */
1733 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001734can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001735 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001736 char_u *word;
1737 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001738{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001739 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001740#ifdef FEAT_MBYTE
1741 char_u uflags[MAXWLEN * 2];
1742 int i;
1743#endif
1744 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001745
1746 if (slang->sl_compprog == NULL)
1747 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001748#ifdef FEAT_MBYTE
1749 if (enc_utf8)
1750 {
1751 /* Need to convert the single byte flags to utf8 characters. */
1752 p = uflags;
1753 for (i = 0; flags[i] != NUL; ++i)
1754 p += mb_char2bytes(flags[i], p);
1755 *p = NUL;
1756 p = uflags;
1757 }
1758 else
1759#endif
1760 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001761 regmatch.regprog = slang->sl_compprog;
1762 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001763 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001764 return FALSE;
1765
Bram Moolenaare52325c2005-08-22 22:54:29 +00001766 /* Count the number of syllables. This may be slow, do it last. If there
1767 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001768 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001769 if (slang->sl_compsylmax < MAXWLEN
1770 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001771 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001772 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001773}
1774
1775/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001776 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1777 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001778 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001779 */
1780 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001781valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001782 int totprefcnt; /* nr of prefix IDs */
1783 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001784 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001785 char_u *word;
1786 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001787 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001788{
1789 int prefcnt;
1790 int pidx;
1791 regprog_T *rp;
1792 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001793 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001794
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001795 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001796 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1797 {
1798 pidx = slang->sl_pidxs[arridx + prefcnt];
1799
1800 /* Check the prefix ID. */
1801 if (prefid != (pidx & 0xff))
1802 continue;
1803
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001804 /* Check if the prefix doesn't combine and the word already has a
1805 * suffix. */
1806 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1807 continue;
1808
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001809 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001810 * stored in the two bytes above the prefix ID byte. */
1811 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001812 if (rp != NULL)
1813 {
1814 regmatch.regprog = rp;
1815 regmatch.rm_ic = FALSE;
1816 if (!vim_regexec(&regmatch, word, 0))
1817 continue;
1818 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001819 else if (cond_req)
1820 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001821
Bram Moolenaar53805d12005-08-01 07:08:33 +00001822 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001823 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001824 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001825 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001826}
1827
1828/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001829 * Check if the word at "mip->mi_word" has a matching prefix.
1830 * If it does, then check the following word.
1831 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001832 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1833 * prefix in a compound word.
1834 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001835 * For a match mip->mi_result is updated.
1836 */
1837 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001838find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001839 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001840 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001841{
1842 idx_T arridx = 0;
1843 int len;
1844 int wlen = 0;
1845 int flen;
1846 int c;
1847 char_u *ptr;
1848 idx_T lo, hi, m;
1849 slang_T *slang = mip->mi_lp->lp_slang;
1850 char_u *byts;
1851 idx_T *idxs;
1852
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001853 byts = slang->sl_pbyts;
1854 if (byts == NULL)
1855 return; /* array is empty */
1856
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001857 /* We use the case-folded word here, since prefixes are always
1858 * case-folded. */
1859 ptr = mip->mi_fword;
1860 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001861 if (mode == FIND_COMPOUND)
1862 {
1863 /* Skip over the previously found word(s). */
1864 ptr += mip->mi_compoff;
1865 flen -= mip->mi_compoff;
1866 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001867 idxs = slang->sl_pidxs;
1868
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001869 /*
1870 * Repeat advancing in the tree until:
1871 * - there is a byte that doesn't match,
1872 * - we reach the end of the tree,
1873 * - or we reach the end of the line.
1874 */
1875 for (;;)
1876 {
1877 if (flen == 0 && *mip->mi_fend != NUL)
1878 flen = fold_more(mip);
1879
1880 len = byts[arridx++];
1881
1882 /* If the first possible byte is a zero the prefix could end here.
1883 * Check if the following word matches and supports the prefix. */
1884 if (byts[arridx] == 0)
1885 {
1886 /* There can be several prefixes with different conditions. We
1887 * try them all, since we don't know which one will give the
1888 * longest match. The word is the same each time, pass the list
1889 * of possible prefixes to find_word(). */
1890 mip->mi_prefarridx = arridx;
1891 mip->mi_prefcnt = len;
1892 while (len > 0 && byts[arridx] == 0)
1893 {
1894 ++arridx;
1895 --len;
1896 }
1897 mip->mi_prefcnt -= len;
1898
1899 /* Find the word that comes after the prefix. */
1900 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001901 if (mode == FIND_COMPOUND)
1902 /* Skip over the previously found word(s). */
1903 mip->mi_prefixlen += mip->mi_compoff;
1904
Bram Moolenaar53805d12005-08-01 07:08:33 +00001905#ifdef FEAT_MBYTE
1906 if (has_mbyte)
1907 {
1908 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001909 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1910 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001911 }
1912 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001913 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001914#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001915 find_word(mip, FIND_PREFIX);
1916
1917
1918 if (len == 0)
1919 break; /* no children, word must end here */
1920 }
1921
1922 /* Stop looking at end of the line. */
1923 if (ptr[wlen] == NUL)
1924 break;
1925
1926 /* Perform a binary search in the list of accepted bytes. */
1927 c = ptr[wlen];
1928 lo = arridx;
1929 hi = arridx + len - 1;
1930 while (lo < hi)
1931 {
1932 m = (lo + hi) / 2;
1933 if (byts[m] > c)
1934 hi = m - 1;
1935 else if (byts[m] < c)
1936 lo = m + 1;
1937 else
1938 {
1939 lo = hi = m;
1940 break;
1941 }
1942 }
1943
1944 /* Stop if there is no matching byte. */
1945 if (hi < lo || byts[lo] != c)
1946 break;
1947
1948 /* Continue at the child (if there is one). */
1949 arridx = idxs[lo];
1950 ++wlen;
1951 --flen;
1952 }
1953}
1954
1955/*
1956 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001957 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001958 * Return the length of the folded chars in bytes.
1959 */
1960 static int
1961fold_more(mip)
1962 matchinf_T *mip;
1963{
1964 int flen;
1965 char_u *p;
1966
1967 p = mip->mi_fend;
1968 do
1969 {
1970 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001971 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001972
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001973 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001974 if (*mip->mi_fend != NUL)
1975 mb_ptr_adv(mip->mi_fend);
1976
1977 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1978 mip->mi_fword + mip->mi_fwordlen,
1979 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001980 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001981 mip->mi_fwordlen += flen;
1982 return flen;
1983}
1984
1985/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 * Check case flags for a word. Return TRUE if the word has the requested
1987 * case.
1988 */
1989 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001990spell_valid_case(wordflags, treeflags)
1991 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 int treeflags; /* flags for the word in the spell tree */
1993{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001994 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001996 && ((treeflags & WF_ONECAP) == 0
1997 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998}
1999
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002000/*
2001 * Return TRUE if spell checking is not enabled.
2002 */
2003 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002004no_spell_checking(wp)
2005 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002006{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002007 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2008 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002009 {
2010 EMSG(_("E756: Spell checking is not enabled"));
2011 return TRUE;
2012 }
2013 return FALSE;
2014}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002015
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002016/*
2017 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002018 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2019 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002020 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2021 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002022 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002023 */
2024 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002025spell_move_to(wp, dir, allwords, curline, attrp)
2026 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002027 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002028 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002029 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002030 hlf_T *attrp; /* return: attributes of bad word or NULL
2031 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002032{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002033 linenr_T lnum;
2034 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002035 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002036 char_u *line;
2037 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002038 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002039 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002040 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002041# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002042 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002043# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002044 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002045 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002046 char_u *buf = NULL;
2047 int buflen = 0;
2048 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002049 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002050 int found_one = FALSE;
2051 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002052
Bram Moolenaar95529562005-08-25 21:21:38 +00002053 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002054 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002056 /*
2057 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002058 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002059 *
2060 * When searching backwards, we continue in the line to find the last
2061 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002062 *
2063 * We concatenate the start of the next line, so that wrapped words work
2064 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2065 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002066 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002067 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002068 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002069
2070 while (!got_int)
2071 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002072 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002074 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002075 if (buflen < len + MAXWLEN + 2)
2076 {
2077 vim_free(buf);
2078 buflen = len + MAXWLEN + 2;
2079 buf = alloc(buflen);
2080 if (buf == NULL)
2081 break;
2082 }
2083
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002084 /* In first line check first word for Capital. */
2085 if (lnum == 1)
2086 capcol = 0;
2087
2088 /* For checking first word with a capital skip white space. */
2089 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002090 capcol = (int)(skipwhite(line) - line);
2091 else if (curline && wp == curwin)
2092 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002094 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 if (check_need_cap(lnum, col))
2096 capcol = col;
2097
2098 /* Need to get the line again, may have looked at the previous
2099 * one. */
2100 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2101 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002102
Bram Moolenaar0c405862005-06-22 22:26:26 +00002103 /* Copy the line into "buf" and append the start of the next line if
2104 * possible. */
2105 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002106 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002107 spell_cat_line(buf + STRLEN(buf),
2108 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002109
2110 p = buf + skip;
2111 endp = buf + len;
2112 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002113 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002114 /* When searching backward don't search after the cursor. Unless
2115 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002116 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002117 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002118 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002119 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 break;
2121
2122 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002123 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002124 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002125
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002126 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002127 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002128 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002129 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 /* When searching forward only accept a bad word after
2132 * the cursor. */
2133 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002134 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002135 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002136 && (wrapped
2137 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002138 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002139 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002140 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002141# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002142 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002144 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002145 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002146 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002147 if (!can_spell)
2148 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002149 }
2150 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002151#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002153
Bram Moolenaar51485f02005-06-04 21:55:20 +00002154 if (can_spell)
2155 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002156 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002158 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002159#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002160 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002161#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002162 if (dir == FORWARD)
2163 {
2164 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002165 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002166 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002167 if (attrp != NULL)
2168 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002169 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002170 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002171 else if (curline)
2172 /* Insert mode completion: put cursor after
2173 * the bad word. */
2174 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002175 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002176 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002177 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002178 else
2179 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002181 }
2182
Bram Moolenaar51485f02005-06-04 21:55:20 +00002183 /* advance to character after the word */
2184 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002185 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002186 }
2187
Bram Moolenaar5195e452005-08-19 20:32:47 +00002188 if (dir == BACKWARD && found_pos.lnum != 0)
2189 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002190 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002191 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002192 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002193 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002194 }
2195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002197 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002199 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002200 if (dir == BACKWARD)
2201 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002202 /* If we are back at the starting line and searched it again there
2203 * is no match, give up. */
2204 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002205 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002206
2207 if (lnum > 1)
2208 --lnum;
2209 else if (!p_ws)
2210 break; /* at first line and 'nowrapscan' */
2211 else
2212 {
2213 /* Wrap around to the end of the buffer. May search the
2214 * starting line again and accept the last match. */
2215 lnum = wp->w_buffer->b_ml.ml_line_count;
2216 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002217 if (!shortmess(SHM_SEARCH))
2218 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002219 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002220 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002221 }
2222 else
2223 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002224 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2225 ++lnum;
2226 else if (!p_ws)
2227 break; /* at first line and 'nowrapscan' */
2228 else
2229 {
2230 /* Wrap around to the start of the buffer. May search the
2231 * starting line again and accept the first match. */
2232 lnum = 1;
2233 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002234 if (!shortmess(SHM_SEARCH))
2235 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002236 }
2237
2238 /* If we are back at the starting line and there is no match then
2239 * give up. */
2240 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002241 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002242
2243 /* Skip the characters at the start of the next line that were
2244 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002245 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002246 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002247 else
2248 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002249
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002250 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002251 --capcol;
2252
2253 /* But after empty line check first word in next line */
2254 if (*skipwhite(line) == NUL)
2255 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002256 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002257
2258 line_breakcheck();
2259 }
2260
Bram Moolenaar0c405862005-06-22 22:26:26 +00002261 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002262 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002263}
2264
2265/*
2266 * For spell checking: concatenate the start of the following line "line" into
2267 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002268 * Keep the blanks at the start of the next line, this is used in win_line()
2269 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002270 */
2271 void
2272spell_cat_line(buf, line, maxlen)
2273 char_u *buf;
2274 char_u *line;
2275 int maxlen;
2276{
2277 char_u *p;
2278 int n;
2279
2280 p = skipwhite(line);
2281 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2282 p = skipwhite(p + 1);
2283
2284 if (*p != NUL)
2285 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002286 /* Only worth concatenating if there is something else than spaces to
2287 * concatenate. */
2288 n = (int)(p - line) + 1;
2289 if (n < maxlen - 1)
2290 {
2291 vim_memset(buf, ' ', n);
2292 vim_strncpy(buf + n, p, maxlen - 1 - n);
2293 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002294 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002295}
2296
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002297/*
2298 * Structure used for the cookie argument of do_in_runtimepath().
2299 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002300typedef struct spelload_S
2301{
2302 char_u sl_lang[MAXWLEN + 1]; /* language name */
2303 slang_T *sl_slang; /* resulting slang_T struct */
2304 int sl_nobreak; /* NOBREAK language found */
2305} spelload_T;
2306
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002307/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002308 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002309 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002310 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002311 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002312spell_load_lang(lang)
2313 char_u *lang;
2314{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002315 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002317 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002318#ifdef FEAT_AUTOCMD
2319 int round;
2320#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002321
Bram Moolenaarb765d632005-06-07 21:00:02 +00002322 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002323 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002324 STRCPY(sl.sl_lang, lang);
2325 sl.sl_slang = NULL;
2326 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002327
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002328#ifdef FEAT_AUTOCMD
2329 /* We may retry when no spell file is found for the language, an
2330 * autocommand may load it then. */
2331 for (round = 1; round <= 2; ++round)
2332#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002333 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002334 /*
2335 * Find the first spell file for "lang" in 'runtimepath' and load it.
2336 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002337 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002338 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002339 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002340
2341 if (r == FAIL && *sl.sl_lang != NUL)
2342 {
2343 /* Try loading the ASCII version. */
2344 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2345 "spell/%s.ascii.spl", lang);
2346 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2347
2348#ifdef FEAT_AUTOCMD
2349 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2350 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2351 curbuf->b_fname, FALSE, curbuf))
2352 continue;
2353 break;
2354#endif
2355 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002356#ifdef FEAT_AUTOCMD
2357 break;
2358#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002359 }
2360
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002361 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002362 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002363 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2364 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002365 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002366 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002367 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002368 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002369 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002370 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002371 }
2372}
2373
2374/*
2375 * Return the encoding used for spell checking: Use 'encoding', except that we
2376 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2377 */
2378 static char_u *
2379spell_enc()
2380{
2381
2382#ifdef FEAT_MBYTE
2383 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2384 return p_enc;
2385#endif
2386 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002387}
2388
2389/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002390 * Get the name of the .spl file for the internal wordlist into
2391 * "fname[MAXPATHL]".
2392 */
2393 static void
2394int_wordlist_spl(fname)
2395 char_u *fname;
2396{
2397 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2398 int_wordlist, spell_enc());
2399}
2400
2401/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002402 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403 * Caller must fill "sl_next".
2404 */
2405 static slang_T *
2406slang_alloc(lang)
2407 char_u *lang;
2408{
2409 slang_T *lp;
2410
Bram Moolenaar51485f02005-06-04 21:55:20 +00002411 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002412 if (lp != NULL)
2413 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002414 if (lang != NULL)
2415 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002417 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002418 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002419 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002420 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002421 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002422
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002423 return lp;
2424}
2425
2426/*
2427 * Free the contents of an slang_T and the structure itself.
2428 */
2429 static void
2430slang_free(lp)
2431 slang_T *lp;
2432{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002433 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002434 vim_free(lp->sl_fname);
2435 slang_clear(lp);
2436 vim_free(lp);
2437}
2438
2439/*
2440 * Clear an slang_T so that the file can be reloaded.
2441 */
2442 static void
2443slang_clear(lp)
2444 slang_T *lp;
2445{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002446 garray_T *gap;
2447 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002448 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002449 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002450 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451
Bram Moolenaar51485f02005-06-04 21:55:20 +00002452 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002453 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002454 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002455 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002456 vim_free(lp->sl_pbyts);
2457 lp->sl_pbyts = NULL;
2458
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002460 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002461 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002462 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002463 vim_free(lp->sl_pidxs);
2464 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465
Bram Moolenaar4770d092006-01-12 23:22:24 +00002466 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002468 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2469 while (gap->ga_len > 0)
2470 {
2471 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2472 vim_free(ftp->ft_from);
2473 vim_free(ftp->ft_to);
2474 }
2475 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002477
2478 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002479 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002480 {
2481 /* "ga_len" is set to 1 without adding an item for latin1 */
2482 if (gap->ga_data != NULL)
2483 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2484 for (i = 0; i < gap->ga_len; ++i)
2485 vim_free(((int **)gap->ga_data)[i]);
2486 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002487 else
2488 /* SAL items: free salitem_T items */
2489 while (gap->ga_len > 0)
2490 {
2491 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2492 vim_free(smp->sm_lead);
2493 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2494 vim_free(smp->sm_to);
2495#ifdef FEAT_MBYTE
2496 vim_free(smp->sm_lead_w);
2497 vim_free(smp->sm_oneof_w);
2498 vim_free(smp->sm_to_w);
2499#endif
2500 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002501 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002503 for (i = 0; i < lp->sl_prefixcnt; ++i)
2504 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002505 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002506 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002507 lp->sl_prefprog = NULL;
2508
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002509 vim_free(lp->sl_info);
2510 lp->sl_info = NULL;
2511
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002512 vim_free(lp->sl_midword);
2513 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002514
Bram Moolenaar5195e452005-08-19 20:32:47 +00002515 vim_free(lp->sl_compprog);
2516 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002517 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518 lp->sl_compprog = NULL;
2519 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002520 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521
2522 vim_free(lp->sl_syllable);
2523 lp->sl_syllable = NULL;
2524 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002525
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002526 ga_clear_strings(&lp->sl_comppat);
2527
Bram Moolenaar4770d092006-01-12 23:22:24 +00002528 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2529 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002530
Bram Moolenaar4770d092006-01-12 23:22:24 +00002531#ifdef FEAT_MBYTE
2532 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002533#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002534
Bram Moolenaar4770d092006-01-12 23:22:24 +00002535 /* Clear info from .sug file. */
2536 slang_clear_sug(lp);
2537
Bram Moolenaar5195e452005-08-19 20:32:47 +00002538 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002539 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002540 lp->sl_compsylmax = MAXWLEN;
2541 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002542}
2543
2544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002545 * Clear the info from the .sug file in "lp".
2546 */
2547 static void
2548slang_clear_sug(lp)
2549 slang_T *lp;
2550{
2551 vim_free(lp->sl_sbyts);
2552 lp->sl_sbyts = NULL;
2553 vim_free(lp->sl_sidxs);
2554 lp->sl_sidxs = NULL;
2555 close_spellbuf(lp->sl_sugbuf);
2556 lp->sl_sugbuf = NULL;
2557 lp->sl_sugloaded = FALSE;
2558 lp->sl_sugtime = 0;
2559}
2560
2561/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002562 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563 * Invoked through do_in_runtimepath().
2564 */
2565 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002566spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002568 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002570 spelload_T *slp = (spelload_T *)cookie;
2571 slang_T *slang;
2572
2573 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2574 if (slang != NULL)
2575 {
2576 /* When a previously loaded file has NOBREAK also use it for the
2577 * ".add" files. */
2578 if (slp->sl_nobreak && slang->sl_add)
2579 slang->sl_nobreak = TRUE;
2580 else if (slang->sl_nobreak)
2581 slp->sl_nobreak = TRUE;
2582
2583 slp->sl_slang = slang;
2584 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002585}
2586
2587/*
2588 * Load one spell file and store the info into a slang_T.
2589 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002590 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2592 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2593 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2594 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002595 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002596 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2597 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002598 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002599 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002600 static slang_T *
2601spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002602 char_u *fname;
2603 char_u *lang;
2604 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002606{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002607 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002608 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002611 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002612 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002613 char_u *save_sourcing_name = sourcing_name;
2614 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002615 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002616 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002617 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002618
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002620 if (fd == NULL)
2621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622 if (!silent)
2623 EMSG2(_(e_notopen), fname);
2624 else if (p_verbose > 2)
2625 {
2626 verbose_enter();
2627 smsg((char_u *)e_notopen, fname);
2628 verbose_leave();
2629 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002630 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002631 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002632 if (p_verbose > 2)
2633 {
2634 verbose_enter();
2635 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2636 verbose_leave();
2637 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638
Bram Moolenaarb765d632005-06-07 21:00:02 +00002639 if (old_lp == NULL)
2640 {
2641 lp = slang_alloc(lang);
2642 if (lp == NULL)
2643 goto endFAIL;
2644
2645 /* Remember the file name, used to reload the file when it's updated. */
2646 lp->sl_fname = vim_strsave(fname);
2647 if (lp->sl_fname == NULL)
2648 goto endFAIL;
2649
2650 /* Check for .add.spl. */
2651 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2652 }
2653 else
2654 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002655
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002656 /* Set sourcing_name, so that error messages mention the file name. */
2657 sourcing_name = fname;
2658 sourcing_lnum = 0;
2659
Bram Moolenaar4770d092006-01-12 23:22:24 +00002660 /*
2661 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002662 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002663 for (i = 0; i < VIMSPELLMAGICL; ++i)
2664 buf[i] = getc(fd); /* <fileID> */
2665 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2666 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002667 EMSG(_("E757: This does not look like a spell file"));
2668 goto endFAIL;
2669 }
2670 c = getc(fd); /* <versionnr> */
2671 if (c < VIMSPELLVERSION)
2672 {
2673 EMSG(_("E771: Old spell file, needs to be updated"));
2674 goto endFAIL;
2675 }
2676 else if (c > VIMSPELLVERSION)
2677 {
2678 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002679 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680 }
2681
Bram Moolenaar5195e452005-08-19 20:32:47 +00002682
2683 /*
2684 * <SECTIONS>: <section> ... <sectionend>
2685 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2686 */
2687 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002688 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002689 n = getc(fd); /* <sectionID> or <sectionend> */
2690 if (n == SN_END)
2691 break;
2692 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002693 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 if (len < 0)
2695 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002696
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697 res = 0;
2698 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002699 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 case SN_INFO:
2701 lp->sl_info = read_string(fd, len); /* <infotext> */
2702 if (lp->sl_info == NULL)
2703 goto endFAIL;
2704 break;
2705
Bram Moolenaar5195e452005-08-19 20:32:47 +00002706 case SN_REGION:
2707 res = read_region_section(fd, lp, len);
2708 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002709
Bram Moolenaar5195e452005-08-19 20:32:47 +00002710 case SN_CHARFLAGS:
2711 res = read_charflags_section(fd);
2712 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002713
Bram Moolenaar5195e452005-08-19 20:32:47 +00002714 case SN_MIDWORD:
2715 lp->sl_midword = read_string(fd, len); /* <midword> */
2716 if (lp->sl_midword == NULL)
2717 goto endFAIL;
2718 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002719
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 case SN_PREFCOND:
2721 res = read_prefcond_section(fd, lp);
2722 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002723
Bram Moolenaar5195e452005-08-19 20:32:47 +00002724 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002725 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2726 break;
2727
2728 case SN_REPSAL:
2729 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002730 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002731
Bram Moolenaar5195e452005-08-19 20:32:47 +00002732 case SN_SAL:
2733 res = read_sal_section(fd, lp);
2734 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002735
Bram Moolenaar5195e452005-08-19 20:32:47 +00002736 case SN_SOFO:
2737 res = read_sofo_section(fd, lp);
2738 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002739
Bram Moolenaar5195e452005-08-19 20:32:47 +00002740 case SN_MAP:
2741 p = read_string(fd, len); /* <mapstr> */
2742 if (p == NULL)
2743 goto endFAIL;
2744 set_map_str(lp, p);
2745 vim_free(p);
2746 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002747
Bram Moolenaar4770d092006-01-12 23:22:24 +00002748 case SN_WORDS:
2749 res = read_words_section(fd, lp, len);
2750 break;
2751
2752 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002753 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002754 break;
2755
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002756 case SN_NOSPLITSUGS:
2757 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2758 break;
2759
Bram Moolenaar5195e452005-08-19 20:32:47 +00002760 case SN_COMPOUND:
2761 res = read_compound(fd, lp, len);
2762 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002763
Bram Moolenaar78622822005-08-23 21:00:13 +00002764 case SN_NOBREAK:
2765 lp->sl_nobreak = TRUE;
2766 break;
2767
Bram Moolenaar5195e452005-08-19 20:32:47 +00002768 case SN_SYLLABLE:
2769 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2770 if (lp->sl_syllable == NULL)
2771 goto endFAIL;
2772 if (init_syl_tab(lp) == FAIL)
2773 goto endFAIL;
2774 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002775
Bram Moolenaar5195e452005-08-19 20:32:47 +00002776 default:
2777 /* Unsupported section. When it's required give an error
2778 * message. When it's not required skip the contents. */
2779 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002780 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002781 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002782 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002783 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002784 while (--len >= 0)
2785 if (getc(fd) < 0)
2786 goto truncerr;
2787 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002788 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002789someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002790 if (res == SP_FORMERROR)
2791 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002792 EMSG(_(e_format));
2793 goto endFAIL;
2794 }
2795 if (res == SP_TRUNCERROR)
2796 {
2797truncerr:
2798 EMSG(_(e_spell_trunc));
2799 goto endFAIL;
2800 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002801 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002802 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002803 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002804
Bram Moolenaar4770d092006-01-12 23:22:24 +00002805 /* <LWORDTREE> */
2806 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2807 if (res != 0)
2808 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002809
Bram Moolenaar4770d092006-01-12 23:22:24 +00002810 /* <KWORDTREE> */
2811 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2812 if (res != 0)
2813 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002814
Bram Moolenaar4770d092006-01-12 23:22:24 +00002815 /* <PREFIXTREE> */
2816 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2817 lp->sl_prefixcnt);
2818 if (res != 0)
2819 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002820
Bram Moolenaarb765d632005-06-07 21:00:02 +00002821 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002822 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002823 {
2824 lp->sl_next = first_lang;
2825 first_lang = lp;
2826 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002827
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002828 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002829
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002830endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002831 if (lang != NULL)
2832 /* truncating the name signals the error to spell_load_lang() */
2833 *lang = NUL;
2834 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002835 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002836 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002837
2838endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 if (fd != NULL)
2840 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002841 sourcing_name = save_sourcing_name;
2842 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843
2844 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002845}
2846
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002847/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002848 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2849 */
2850 static int
2851get2c(fd)
2852 FILE *fd;
2853{
2854 long n;
2855
2856 n = getc(fd);
2857 n = (n << 8) + getc(fd);
2858 return n;
2859}
2860
2861/*
2862 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2863 */
2864 static int
2865get3c(fd)
2866 FILE *fd;
2867{
2868 long n;
2869
2870 n = getc(fd);
2871 n = (n << 8) + getc(fd);
2872 n = (n << 8) + getc(fd);
2873 return n;
2874}
2875
2876/*
2877 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2878 */
2879 static int
2880get4c(fd)
2881 FILE *fd;
2882{
2883 long n;
2884
2885 n = getc(fd);
2886 n = (n << 8) + getc(fd);
2887 n = (n << 8) + getc(fd);
2888 n = (n << 8) + getc(fd);
2889 return n;
2890}
2891
2892/*
2893 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2894 */
2895 static time_t
2896get8c(fd)
2897 FILE *fd;
2898{
2899 time_t n = 0;
2900 int i;
2901
2902 for (i = 0; i < 8; ++i)
2903 n = (n << 8) + getc(fd);
2904 return n;
2905}
2906
2907/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002908 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002909 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002910 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002911 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2912 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002913 */
2914 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002915read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002916 FILE *fd;
2917 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002918 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002919{
2920 int cnt = 0;
2921 int i;
2922 char_u *str;
2923
2924 /* read the length bytes, MSB first */
2925 for (i = 0; i < cnt_bytes; ++i)
2926 cnt = (cnt << 8) + getc(fd);
2927 if (cnt < 0)
2928 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002929 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002930 return NULL;
2931 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002932 *cntp = cnt;
2933 if (cnt == 0)
2934 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002935
Bram Moolenaar5195e452005-08-19 20:32:47 +00002936 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002937 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002938 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002939 return str;
2940}
2941
Bram Moolenaar7887d882005-07-01 22:33:52 +00002942/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 * Read a string of length "cnt" from "fd" into allocated memory.
Bram Moolenaara7241f52008-06-24 20:39:31 +00002944 * Returns NULL when out of memory or unable to read that many bytes.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002945 */
2946 static char_u *
2947read_string(fd, cnt)
2948 FILE *fd;
2949 int cnt;
2950{
2951 char_u *str;
2952 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00002953 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002954
2955 /* allocate memory */
2956 str = alloc((unsigned)cnt + 1);
2957 if (str != NULL)
2958 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002959 /* Read the string. Quit when running into the EOF. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002960 for (i = 0; i < cnt; ++i)
Bram Moolenaara7241f52008-06-24 20:39:31 +00002961 {
2962 c = getc(fd);
2963 if (c == EOF)
2964 {
2965 vim_free(str);
2966 return NULL;
2967 }
2968 str[i] = c;
2969 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002970 str[i] = NUL;
2971 }
2972 return str;
2973}
2974
2975/*
2976 * Read SN_REGION: <regionname> ...
2977 * Return SP_*ERROR flags.
2978 */
2979 static int
2980read_region_section(fd, lp, len)
2981 FILE *fd;
2982 slang_T *lp;
2983 int len;
2984{
2985 int i;
2986
2987 if (len > 16)
2988 return SP_FORMERROR;
2989 for (i = 0; i < len; ++i)
2990 lp->sl_regions[i] = getc(fd); /* <regionname> */
2991 lp->sl_regions[len] = NUL;
2992 return 0;
2993}
2994
2995/*
2996 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2997 * <folcharslen> <folchars>
2998 * Return SP_*ERROR flags.
2999 */
3000 static int
3001read_charflags_section(fd)
3002 FILE *fd;
3003{
3004 char_u *flags;
3005 char_u *fol;
3006 int flagslen, follen;
3007
3008 /* <charflagslen> <charflags> */
3009 flags = read_cnt_string(fd, 1, &flagslen);
3010 if (flagslen < 0)
3011 return flagslen;
3012
3013 /* <folcharslen> <folchars> */
3014 fol = read_cnt_string(fd, 2, &follen);
3015 if (follen < 0)
3016 {
3017 vim_free(flags);
3018 return follen;
3019 }
3020
3021 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3022 if (flags != NULL && fol != NULL)
3023 set_spell_charflags(flags, flagslen, fol);
3024
3025 vim_free(flags);
3026 vim_free(fol);
3027
3028 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3029 if ((flags == NULL) != (fol == NULL))
3030 return SP_FORMERROR;
3031 return 0;
3032}
3033
3034/*
3035 * Read SN_PREFCOND section.
3036 * Return SP_*ERROR flags.
3037 */
3038 static int
3039read_prefcond_section(fd, lp)
3040 FILE *fd;
3041 slang_T *lp;
3042{
3043 int cnt;
3044 int i;
3045 int n;
3046 char_u *p;
3047 char_u buf[MAXWLEN + 1];
3048
3049 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003050 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003051 if (cnt <= 0)
3052 return SP_FORMERROR;
3053
3054 lp->sl_prefprog = (regprog_T **)alloc_clear(
3055 (unsigned)sizeof(regprog_T *) * cnt);
3056 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003057 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003058 lp->sl_prefixcnt = cnt;
3059
3060 for (i = 0; i < cnt; ++i)
3061 {
3062 /* <prefcond> : <condlen> <condstr> */
3063 n = getc(fd); /* <condlen> */
3064 if (n < 0 || n >= MAXWLEN)
3065 return SP_FORMERROR;
3066
3067 /* When <condlen> is zero we have an empty condition. Otherwise
3068 * compile the regexp program used to check for the condition. */
3069 if (n > 0)
3070 {
3071 buf[0] = '^'; /* always match at one position only */
3072 p = buf + 1;
3073 while (n-- > 0)
3074 *p++ = getc(fd); /* <condstr> */
3075 *p = NUL;
3076 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3077 }
3078 }
3079 return 0;
3080}
3081
3082/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003083 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003084 * Return SP_*ERROR flags.
3085 */
3086 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003087read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003088 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003089 garray_T *gap;
3090 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003091{
3092 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003093 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003094 int i;
3095
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003096 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003097 if (cnt < 0)
3098 return SP_TRUNCERROR;
3099
Bram Moolenaar5195e452005-08-19 20:32:47 +00003100 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003101 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003102
3103 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3104 for (; gap->ga_len < cnt; ++gap->ga_len)
3105 {
3106 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3107 ftp->ft_from = read_cnt_string(fd, 1, &i);
3108 if (i < 0)
3109 return i;
3110 if (i == 0)
3111 return SP_FORMERROR;
3112 ftp->ft_to = read_cnt_string(fd, 1, &i);
3113 if (i <= 0)
3114 {
3115 vim_free(ftp->ft_from);
3116 if (i < 0)
3117 return i;
3118 return SP_FORMERROR;
3119 }
3120 }
3121
3122 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003123 for (i = 0; i < 256; ++i)
3124 first[i] = -1;
3125 for (i = 0; i < gap->ga_len; ++i)
3126 {
3127 ftp = &((fromto_T *)gap->ga_data)[i];
3128 if (first[*ftp->ft_from] == -1)
3129 first[*ftp->ft_from] = i;
3130 }
3131 return 0;
3132}
3133
3134/*
3135 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3136 * Return SP_*ERROR flags.
3137 */
3138 static int
3139read_sal_section(fd, slang)
3140 FILE *fd;
3141 slang_T *slang;
3142{
3143 int i;
3144 int cnt;
3145 garray_T *gap;
3146 salitem_T *smp;
3147 int ccnt;
3148 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003149 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150
3151 slang->sl_sofo = FALSE;
3152
3153 i = getc(fd); /* <salflags> */
3154 if (i & SAL_F0LLOWUP)
3155 slang->sl_followup = TRUE;
3156 if (i & SAL_COLLAPSE)
3157 slang->sl_collapse = TRUE;
3158 if (i & SAL_REM_ACCENTS)
3159 slang->sl_rem_accents = TRUE;
3160
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003161 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003162 if (cnt < 0)
3163 return SP_TRUNCERROR;
3164
3165 gap = &slang->sl_sal;
3166 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003167 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003168 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003169
3170 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3171 for (; gap->ga_len < cnt; ++gap->ga_len)
3172 {
3173 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3174 ccnt = getc(fd); /* <salfromlen> */
3175 if (ccnt < 0)
3176 return SP_TRUNCERROR;
3177 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003178 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003179 smp->sm_lead = p;
3180
3181 /* Read up to the first special char into sm_lead. */
3182 for (i = 0; i < ccnt; ++i)
3183 {
3184 c = getc(fd); /* <salfrom> */
3185 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3186 break;
3187 *p++ = c;
3188 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003189 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003190 *p++ = NUL;
3191
3192 /* Put (abc) chars in sm_oneof, if any. */
3193 if (c == '(')
3194 {
3195 smp->sm_oneof = p;
3196 for (++i; i < ccnt; ++i)
3197 {
3198 c = getc(fd); /* <salfrom> */
3199 if (c == ')')
3200 break;
3201 *p++ = c;
3202 }
3203 *p++ = NUL;
3204 if (++i < ccnt)
3205 c = getc(fd);
3206 }
3207 else
3208 smp->sm_oneof = NULL;
3209
3210 /* Any following chars go in sm_rules. */
3211 smp->sm_rules = p;
3212 if (i < ccnt)
3213 /* store the char we got while checking for end of sm_lead */
3214 *p++ = c;
3215 for (++i; i < ccnt; ++i)
3216 *p++ = getc(fd); /* <salfrom> */
3217 *p++ = NUL;
3218
3219 /* <saltolen> <salto> */
3220 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3221 if (ccnt < 0)
3222 {
3223 vim_free(smp->sm_lead);
3224 return ccnt;
3225 }
3226
3227#ifdef FEAT_MBYTE
3228 if (has_mbyte)
3229 {
3230 /* convert the multi-byte strings to wide char strings */
3231 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3232 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3233 if (smp->sm_oneof == NULL)
3234 smp->sm_oneof_w = NULL;
3235 else
3236 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3237 if (smp->sm_to == NULL)
3238 smp->sm_to_w = NULL;
3239 else
3240 smp->sm_to_w = mb_str2wide(smp->sm_to);
3241 if (smp->sm_lead_w == NULL
3242 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3243 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3244 {
3245 vim_free(smp->sm_lead);
3246 vim_free(smp->sm_to);
3247 vim_free(smp->sm_lead_w);
3248 vim_free(smp->sm_oneof_w);
3249 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003250 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003251 }
3252 }
3253#endif
3254 }
3255
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003256 if (gap->ga_len > 0)
3257 {
3258 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3259 * that we need to check the index every time. */
3260 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3261 if ((p = alloc(1)) == NULL)
3262 return SP_OTHERERROR;
3263 p[0] = NUL;
3264 smp->sm_lead = p;
3265 smp->sm_leadlen = 0;
3266 smp->sm_oneof = NULL;
3267 smp->sm_rules = p;
3268 smp->sm_to = NULL;
3269#ifdef FEAT_MBYTE
3270 if (has_mbyte)
3271 {
3272 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3273 smp->sm_leadlen = 0;
3274 smp->sm_oneof_w = NULL;
3275 smp->sm_to_w = NULL;
3276 }
3277#endif
3278 ++gap->ga_len;
3279 }
3280
Bram Moolenaar5195e452005-08-19 20:32:47 +00003281 /* Fill the first-index table. */
3282 set_sal_first(slang);
3283
3284 return 0;
3285}
3286
3287/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003288 * Read SN_WORDS: <word> ...
3289 * Return SP_*ERROR flags.
3290 */
3291 static int
3292read_words_section(fd, lp, len)
3293 FILE *fd;
3294 slang_T *lp;
3295 int len;
3296{
3297 int done = 0;
3298 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003299 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003300 char_u word[MAXWLEN];
3301
3302 while (done < len)
3303 {
3304 /* Read one word at a time. */
3305 for (i = 0; ; ++i)
3306 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003307 c = getc(fd);
3308 if (c == EOF)
3309 return SP_TRUNCERROR;
3310 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003311 if (word[i] == NUL)
3312 break;
3313 if (i == MAXWLEN - 1)
3314 return SP_FORMERROR;
3315 }
3316
3317 /* Init the count to 10. */
3318 count_common_word(lp, word, -1, 10);
3319 done += i + 1;
3320 }
3321 return 0;
3322}
3323
3324/*
3325 * Add a word to the hashtable of common words.
3326 * If it's already there then the counter is increased.
3327 */
3328 static void
3329count_common_word(lp, word, len, count)
3330 slang_T *lp;
3331 char_u *word;
3332 int len; /* word length, -1 for upto NUL */
3333 int count; /* 1 to count once, 10 to init */
3334{
3335 hash_T hash;
3336 hashitem_T *hi;
3337 wordcount_T *wc;
3338 char_u buf[MAXWLEN];
3339 char_u *p;
3340
3341 if (len == -1)
3342 p = word;
3343 else
3344 {
3345 vim_strncpy(buf, word, len);
3346 p = buf;
3347 }
3348
3349 hash = hash_hash(p);
3350 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3351 if (HASHITEM_EMPTY(hi))
3352 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003353 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003354 if (wc == NULL)
3355 return;
3356 STRCPY(wc->wc_word, p);
3357 wc->wc_count = count;
3358 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3359 }
3360 else
3361 {
3362 wc = HI2WC(hi);
3363 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3364 wc->wc_count = MAXWORDCOUNT;
3365 }
3366}
3367
3368/*
3369 * Adjust the score of common words.
3370 */
3371 static int
3372score_wordcount_adj(slang, score, word, split)
3373 slang_T *slang;
3374 int score;
3375 char_u *word;
3376 int split; /* word was split, less bonus */
3377{
3378 hashitem_T *hi;
3379 wordcount_T *wc;
3380 int bonus;
3381 int newscore;
3382
3383 hi = hash_find(&slang->sl_wordcount, word);
3384 if (!HASHITEM_EMPTY(hi))
3385 {
3386 wc = HI2WC(hi);
3387 if (wc->wc_count < SCORE_THRES2)
3388 bonus = SCORE_COMMON1;
3389 else if (wc->wc_count < SCORE_THRES3)
3390 bonus = SCORE_COMMON2;
3391 else
3392 bonus = SCORE_COMMON3;
3393 if (split)
3394 newscore = score - bonus / 2;
3395 else
3396 newscore = score - bonus;
3397 if (newscore < 0)
3398 return 0;
3399 return newscore;
3400 }
3401 return score;
3402}
3403
3404/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003405 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3406 * Return SP_*ERROR flags.
3407 */
3408 static int
3409read_sofo_section(fd, slang)
3410 FILE *fd;
3411 slang_T *slang;
3412{
3413 int cnt;
3414 char_u *from, *to;
3415 int res;
3416
3417 slang->sl_sofo = TRUE;
3418
3419 /* <sofofromlen> <sofofrom> */
3420 from = read_cnt_string(fd, 2, &cnt);
3421 if (cnt < 0)
3422 return cnt;
3423
3424 /* <sofotolen> <sofoto> */
3425 to = read_cnt_string(fd, 2, &cnt);
3426 if (cnt < 0)
3427 {
3428 vim_free(from);
3429 return cnt;
3430 }
3431
3432 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3433 if (from != NULL && to != NULL)
3434 res = set_sofo(slang, from, to);
3435 else if (from != NULL || to != NULL)
3436 res = SP_FORMERROR; /* only one of two strings is an error */
3437 else
3438 res = 0;
3439
3440 vim_free(from);
3441 vim_free(to);
3442 return res;
3443}
3444
3445/*
3446 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003447 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003448 * Returns SP_*ERROR flags.
3449 */
3450 static int
3451read_compound(fd, slang, len)
3452 FILE *fd;
3453 slang_T *slang;
3454 int len;
3455{
3456 int todo = len;
3457 int c;
3458 int atstart;
3459 char_u *pat;
3460 char_u *pp;
3461 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003462 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003463 int cnt;
3464 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003465
3466 if (todo < 2)
3467 return SP_FORMERROR; /* need at least two bytes */
3468
3469 --todo;
3470 c = getc(fd); /* <compmax> */
3471 if (c < 2)
3472 c = MAXWLEN;
3473 slang->sl_compmax = c;
3474
3475 --todo;
3476 c = getc(fd); /* <compminlen> */
3477 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003478 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003479 slang->sl_compminlen = c;
3480
3481 --todo;
3482 c = getc(fd); /* <compsylmax> */
3483 if (c < 1)
3484 c = MAXWLEN;
3485 slang->sl_compsylmax = c;
3486
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003487 c = getc(fd); /* <compoptions> */
3488 if (c != 0)
3489 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3490 else
3491 {
3492 --todo;
3493 c = getc(fd); /* only use the lower byte for now */
3494 --todo;
3495 slang->sl_compoptions = c;
3496
3497 gap = &slang->sl_comppat;
3498 c = get2c(fd); /* <comppatcount> */
3499 todo -= 2;
3500 ga_init2(gap, sizeof(char_u *), c);
3501 if (ga_grow(gap, c) == OK)
3502 while (--c >= 0)
3503 {
3504 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3505 read_cnt_string(fd, 1, &cnt);
3506 /* <comppatlen> <comppattext> */
3507 if (cnt < 0)
3508 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003509 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003510 }
3511 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003512 if (todo < 0)
3513 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003514
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003515 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003516 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003517 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3518 * Conversion to utf-8 may double the size. */
3519 c = todo * 2 + 7;
3520#ifdef FEAT_MBYTE
3521 if (enc_utf8)
3522 c += todo * 2;
3523#endif
3524 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003525 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003526 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003527
Bram Moolenaard12a1322005-08-21 22:08:24 +00003528 /* We also need a list of all flags that can appear at the start and one
3529 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003530 cp = alloc(todo + 1);
3531 if (cp == NULL)
3532 {
3533 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003534 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003535 }
3536 slang->sl_compstartflags = cp;
3537 *cp = NUL;
3538
Bram Moolenaard12a1322005-08-21 22:08:24 +00003539 ap = alloc(todo + 1);
3540 if (ap == NULL)
3541 {
3542 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003543 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003544 }
3545 slang->sl_compallflags = ap;
3546 *ap = NUL;
3547
Bram Moolenaar5195e452005-08-19 20:32:47 +00003548 pp = pat;
3549 *pp++ = '^';
3550 *pp++ = '\\';
3551 *pp++ = '(';
3552
3553 atstart = 1;
3554 while (todo-- > 0)
3555 {
3556 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003557 if (c == EOF)
3558 {
3559 vim_free(pat);
3560 return SP_TRUNCERROR;
3561 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003562
3563 /* Add all flags to "sl_compallflags". */
3564 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003565 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003566 {
3567 *ap++ = c;
3568 *ap = NUL;
3569 }
3570
Bram Moolenaar5195e452005-08-19 20:32:47 +00003571 if (atstart != 0)
3572 {
3573 /* At start of item: copy flags to "sl_compstartflags". For a
3574 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3575 if (c == '[')
3576 atstart = 2;
3577 else if (c == ']')
3578 atstart = 0;
3579 else
3580 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003581 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003582 {
3583 *cp++ = c;
3584 *cp = NUL;
3585 }
3586 if (atstart == 1)
3587 atstart = 0;
3588 }
3589 }
3590 if (c == '/') /* slash separates two items */
3591 {
3592 *pp++ = '\\';
3593 *pp++ = '|';
3594 atstart = 1;
3595 }
3596 else /* normal char, "[abc]" and '*' are copied as-is */
3597 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003598 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003599 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003600#ifdef FEAT_MBYTE
3601 if (enc_utf8)
3602 pp += mb_char2bytes(c, pp);
3603 else
3604#endif
3605 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003606 }
3607 }
3608
3609 *pp++ = '\\';
3610 *pp++ = ')';
3611 *pp++ = '$';
3612 *pp = NUL;
3613
3614 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3615 vim_free(pat);
3616 if (slang->sl_compprog == NULL)
3617 return SP_FORMERROR;
3618
3619 return 0;
3620}
3621
Bram Moolenaar6de68532005-08-24 22:08:48 +00003622/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003623 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003624 * Like strchr() but independent of locale.
3625 */
3626 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003627byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003628 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003629 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003630{
3631 char_u *p;
3632
3633 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003634 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003635 return TRUE;
3636 return FALSE;
3637}
3638
Bram Moolenaar5195e452005-08-19 20:32:47 +00003639#define SY_MAXLEN 30
3640typedef struct syl_item_S
3641{
3642 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3643 int sy_len;
3644} syl_item_T;
3645
3646/*
3647 * Truncate "slang->sl_syllable" at the first slash and put the following items
3648 * in "slang->sl_syl_items".
3649 */
3650 static int
3651init_syl_tab(slang)
3652 slang_T *slang;
3653{
3654 char_u *p;
3655 char_u *s;
3656 int l;
3657 syl_item_T *syl;
3658
3659 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3660 p = vim_strchr(slang->sl_syllable, '/');
3661 while (p != NULL)
3662 {
3663 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003664 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003665 break;
3666 s = p;
3667 p = vim_strchr(p, '/');
3668 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003669 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003670 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003671 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003672 if (l >= SY_MAXLEN)
3673 return SP_FORMERROR;
3674 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003675 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003676 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3677 + slang->sl_syl_items.ga_len++;
3678 vim_strncpy(syl->sy_chars, s, l);
3679 syl->sy_len = l;
3680 }
3681 return OK;
3682}
3683
3684/*
3685 * Count the number of syllables in "word".
3686 * When "word" contains spaces the syllables after the last space are counted.
3687 * Returns zero if syllables are not defines.
3688 */
3689 static int
3690count_syllables(slang, word)
3691 slang_T *slang;
3692 char_u *word;
3693{
3694 int cnt = 0;
3695 int skip = FALSE;
3696 char_u *p;
3697 int len;
3698 int i;
3699 syl_item_T *syl;
3700 int c;
3701
3702 if (slang->sl_syllable == NULL)
3703 return 0;
3704
3705 for (p = word; *p != NUL; p += len)
3706 {
3707 /* When running into a space reset counter. */
3708 if (*p == ' ')
3709 {
3710 len = 1;
3711 cnt = 0;
3712 continue;
3713 }
3714
3715 /* Find longest match of syllable items. */
3716 len = 0;
3717 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3718 {
3719 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3720 if (syl->sy_len > len
3721 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3722 len = syl->sy_len;
3723 }
3724 if (len != 0) /* found a match, count syllable */
3725 {
3726 ++cnt;
3727 skip = FALSE;
3728 }
3729 else
3730 {
3731 /* No recognized syllable item, at least a syllable char then? */
3732#ifdef FEAT_MBYTE
3733 c = mb_ptr2char(p);
3734 len = (*mb_ptr2len)(p);
3735#else
3736 c = *p;
3737 len = 1;
3738#endif
3739 if (vim_strchr(slang->sl_syllable, c) == NULL)
3740 skip = FALSE; /* No, search for next syllable */
3741 else if (!skip)
3742 {
3743 ++cnt; /* Yes, count it */
3744 skip = TRUE; /* don't count following syllable chars */
3745 }
3746 }
3747 }
3748 return cnt;
3749}
3750
3751/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003752 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003753 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003754 */
3755 static int
3756set_sofo(lp, from, to)
3757 slang_T *lp;
3758 char_u *from;
3759 char_u *to;
3760{
3761 int i;
3762
3763#ifdef FEAT_MBYTE
3764 garray_T *gap;
3765 char_u *s;
3766 char_u *p;
3767 int c;
3768 int *inp;
3769
3770 if (has_mbyte)
3771 {
3772 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3773 * characters. The index is the low byte of the character.
3774 * The list contains from-to pairs with a terminating NUL.
3775 * sl_sal_first[] is used for latin1 "from" characters. */
3776 gap = &lp->sl_sal;
3777 ga_init2(gap, sizeof(int *), 1);
3778 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003779 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003780 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3781 gap->ga_len = 256;
3782
3783 /* First count the number of items for each list. Temporarily use
3784 * sl_sal_first[] for this. */
3785 for (p = from, s = to; *p != NUL && *s != NUL; )
3786 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003787 c = mb_cptr2char_adv(&p);
3788 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003789 if (c >= 256)
3790 ++lp->sl_sal_first[c & 0xff];
3791 }
3792 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003793 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003794
3795 /* Allocate the lists. */
3796 for (i = 0; i < 256; ++i)
3797 if (lp->sl_sal_first[i] > 0)
3798 {
3799 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3800 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003801 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003802 ((int **)gap->ga_data)[i] = (int *)p;
3803 *(int *)p = 0;
3804 }
3805
3806 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3807 * list. */
3808 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3809 for (p = from, s = to; *p != NUL && *s != NUL; )
3810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003811 c = mb_cptr2char_adv(&p);
3812 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003813 if (c >= 256)
3814 {
3815 /* Append the from-to chars at the end of the list with
3816 * the low byte. */
3817 inp = ((int **)gap->ga_data)[c & 0xff];
3818 while (*inp != 0)
3819 ++inp;
3820 *inp++ = c; /* from char */
3821 *inp++ = i; /* to char */
3822 *inp++ = NUL; /* NUL at the end */
3823 }
3824 else
3825 /* mapping byte to char is done in sl_sal_first[] */
3826 lp->sl_sal_first[c] = i;
3827 }
3828 }
3829 else
3830#endif
3831 {
3832 /* mapping bytes to bytes is done in sl_sal_first[] */
3833 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003834 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003835
3836 for (i = 0; to[i] != NUL; ++i)
3837 lp->sl_sal_first[from[i]] = to[i];
3838 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3839 }
3840
Bram Moolenaar5195e452005-08-19 20:32:47 +00003841 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003842}
3843
3844/*
3845 * Fill the first-index table for "lp".
3846 */
3847 static void
3848set_sal_first(lp)
3849 slang_T *lp;
3850{
3851 salfirst_T *sfirst;
3852 int i;
3853 salitem_T *smp;
3854 int c;
3855 garray_T *gap = &lp->sl_sal;
3856
3857 sfirst = lp->sl_sal_first;
3858 for (i = 0; i < 256; ++i)
3859 sfirst[i] = -1;
3860 smp = (salitem_T *)gap->ga_data;
3861 for (i = 0; i < gap->ga_len; ++i)
3862 {
3863#ifdef FEAT_MBYTE
3864 if (has_mbyte)
3865 /* Use the lowest byte of the first character. For latin1 it's
3866 * the character, for other encodings it should differ for most
3867 * characters. */
3868 c = *smp[i].sm_lead_w & 0xff;
3869 else
3870#endif
3871 c = *smp[i].sm_lead;
3872 if (sfirst[c] == -1)
3873 {
3874 sfirst[c] = i;
3875#ifdef FEAT_MBYTE
3876 if (has_mbyte)
3877 {
3878 int n;
3879
3880 /* Make sure all entries with this byte are following each
3881 * other. Move the ones that are in the wrong position. Do
3882 * keep the same ordering! */
3883 while (i + 1 < gap->ga_len
3884 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3885 /* Skip over entry with same index byte. */
3886 ++i;
3887
3888 for (n = 1; i + n < gap->ga_len; ++n)
3889 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3890 {
3891 salitem_T tsal;
3892
3893 /* Move entry with same index byte after the entries
3894 * we already found. */
3895 ++i;
3896 --n;
3897 tsal = smp[i + n];
3898 mch_memmove(smp + i + 1, smp + i,
3899 sizeof(salitem_T) * n);
3900 smp[i] = tsal;
3901 }
3902 }
3903#endif
3904 }
3905 }
3906}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003907
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003908#ifdef FEAT_MBYTE
3909/*
3910 * Turn a multi-byte string into a wide character string.
3911 * Return it in allocated memory (NULL for out-of-memory)
3912 */
3913 static int *
3914mb_str2wide(s)
3915 char_u *s;
3916{
3917 int *res;
3918 char_u *p;
3919 int i = 0;
3920
3921 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3922 if (res != NULL)
3923 {
3924 for (p = s; *p != NUL; )
3925 res[i++] = mb_ptr2char_adv(&p);
3926 res[i] = NUL;
3927 }
3928 return res;
3929}
3930#endif
3931
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003932/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003933 * Read a tree from the .spl or .sug file.
3934 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3935 * This is skipped when the tree has zero length.
3936 * Returns zero when OK, SP_ value for an error.
3937 */
3938 static int
3939spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3940 FILE *fd;
3941 char_u **bytsp;
3942 idx_T **idxsp;
3943 int prefixtree; /* TRUE for the prefix tree */
3944 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3945{
3946 int len;
3947 int idx;
3948 char_u *bp;
3949 idx_T *ip;
3950
3951 /* The tree size was computed when writing the file, so that we can
3952 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003953 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003954 if (len < 0)
3955 return SP_TRUNCERROR;
3956 if (len > 0)
3957 {
3958 /* Allocate the byte array. */
3959 bp = lalloc((long_u)len, TRUE);
3960 if (bp == NULL)
3961 return SP_OTHERERROR;
3962 *bytsp = bp;
3963
3964 /* Allocate the index array. */
3965 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3966 if (ip == NULL)
3967 return SP_OTHERERROR;
3968 *idxsp = ip;
3969
3970 /* Recursively read the tree and store it in the array. */
3971 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3972 if (idx < 0)
3973 return idx;
3974 }
3975 return 0;
3976}
3977
3978/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003979 * Read one row of siblings from the spell file and store it in the byte array
3980 * "byts" and index array "idxs". Recursively read the children.
3981 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003982 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003984 * Returns the index (>= 0) following the siblings.
3985 * Returns SP_TRUNCERROR if the file is shorter than expected.
3986 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003987 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003988 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003989read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003990 FILE *fd;
3991 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003992 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003993 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003994 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003995 int prefixtree; /* TRUE for reading PREFIXTREE */
3996 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003997{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003998 int len;
3999 int i;
4000 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004001 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004002 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004003 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004004#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004005
Bram Moolenaar51485f02005-06-04 21:55:20 +00004006 len = getc(fd); /* <siblingcount> */
4007 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004008 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004009
4010 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004011 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004012 byts[idx++] = len;
4013
4014 /* Read the byte values, flag/region bytes and shared indexes. */
4015 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004016 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004017 c = getc(fd); /* <byte> */
4018 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004019 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004020 if (c <= BY_SPECIAL)
4021 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004022 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004023 {
4024 /* No flags, all regions. */
4025 idxs[idx] = 0;
4026 c = 0;
4027 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004028 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004029 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004030 if (prefixtree)
4031 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004032 /* Read the optional pflags byte, the prefix ID and the
4033 * condition nr. In idxs[] store the prefix ID in the low
4034 * byte, the condition index shifted up 8 bits, the flags
4035 * shifted up 24 bits. */
4036 if (c == BY_FLAGS)
4037 c = getc(fd) << 24; /* <pflags> */
4038 else
4039 c = 0;
4040
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004041 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004042
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004043 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004044 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004045 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004046 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004047 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004048 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004049 {
4050 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004051 * idxs[] the flags go in the low two bytes, region above
4052 * that and prefix ID above the region. */
4053 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004054 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004055 if (c2 == BY_FLAGS2)
4056 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004057 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004058 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004059 if (c & WF_AFX)
4060 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004061 }
4062
Bram Moolenaar51485f02005-06-04 21:55:20 +00004063 idxs[idx] = c;
4064 c = 0;
4065 }
4066 else /* c == BY_INDEX */
4067 {
4068 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004069 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004070 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004071 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004072 idxs[idx] = n + SHARED_MASK;
4073 c = getc(fd); /* <xbyte> */
4074 }
4075 }
4076 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077 }
4078
Bram Moolenaar51485f02005-06-04 21:55:20 +00004079 /* Recursively read the children for non-shared siblings.
4080 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4081 * remove SHARED_MASK) */
4082 for (i = 1; i <= len; ++i)
4083 if (byts[startidx + i] != 0)
4084 {
4085 if (idxs[startidx + i] & SHARED_MASK)
4086 idxs[startidx + i] &= ~SHARED_MASK;
4087 else
4088 {
4089 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004090 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004091 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 if (idx < 0)
4093 break;
4094 }
4095 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096
Bram Moolenaar51485f02005-06-04 21:55:20 +00004097 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004098}
4099
4100/*
4101 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004102 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004103 */
4104 char_u *
4105did_set_spelllang(buf)
4106 buf_T *buf;
4107{
4108 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004109 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004111 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004112 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004113 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004114 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004115 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004116 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004118 int len;
4119 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004120 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004121 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004122 char_u *use_region = NULL;
4123 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004124 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004125 int i, j;
4126 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004127 static int recursive = FALSE;
4128 char_u *ret_msg = NULL;
4129 char_u *spl_copy;
4130
4131 /* We don't want to do this recursively. May happen when a language is
4132 * not available and the SpellFileMissing autocommand opens a new buffer
4133 * in which 'spell' is set. */
4134 if (recursive)
4135 return NULL;
4136 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004137
4138 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004139 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004140
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004141 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4142 * it under our fingers. */
4143 spl_copy = vim_strsave(buf->b_p_spl);
4144 if (spl_copy == NULL)
4145 goto theend;
4146
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004147 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004148 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004149 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004150 /* Get one language name. */
4151 copy_option_part(&splp, lang, MAXWLEN, ",");
4152
Bram Moolenaar5482f332005-04-17 20:18:43 +00004153 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004154 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004155
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004156 /* If the name ends in ".spl" use it as the name of the spell file.
4157 * If there is a region name let "region" point to it and remove it
4158 * from the name. */
4159 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4160 {
4161 filename = TRUE;
4162
Bram Moolenaarb6356332005-07-18 21:40:44 +00004163 /* Locate a region and remove it from the file name. */
4164 p = vim_strchr(gettail(lang), '_');
4165 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4166 && !ASCII_ISALPHA(p[3]))
4167 {
4168 vim_strncpy(region_cp, p + 1, 2);
4169 mch_memmove(p, p + 3, len - (p - lang) - 2);
4170 len -= 3;
4171 region = region_cp;
4172 }
4173 else
4174 dont_use_region = TRUE;
4175
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004176 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004177 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4178 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004179 break;
4180 }
4181 else
4182 {
4183 filename = FALSE;
4184 if (len > 3 && lang[len - 3] == '_')
4185 {
4186 region = lang + len - 2;
4187 len -= 3;
4188 lang[len] = NUL;
4189 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004190 else
4191 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004192
4193 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004194 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4195 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004196 break;
4197 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004198
Bram Moolenaarb6356332005-07-18 21:40:44 +00004199 if (region != NULL)
4200 {
4201 /* If the region differs from what was used before then don't
4202 * use it for 'spellfile'. */
4203 if (use_region != NULL && STRCMP(region, use_region) != 0)
4204 dont_use_region = TRUE;
4205 use_region = region;
4206 }
4207
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004208 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004209 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004210 {
4211 if (filename)
4212 (void)spell_load_file(lang, lang, NULL, FALSE);
4213 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004214 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004215 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004216#ifdef FEAT_AUTOCMD
4217 /* SpellFileMissing autocommands may do anything, including
4218 * destroying the buffer we are using... */
4219 if (!buf_valid(buf))
4220 {
4221 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4222 goto theend;
4223 }
4224#endif
4225 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004226 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004228 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004229 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004230 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004231 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4232 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4233 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004234 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004235 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004236 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004237 {
4238 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004239 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004240 if (c == REGION_ALL)
4241 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004242 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004243 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004244 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004245 /* This addition file is for other regions. */
4246 region_mask = 0;
4247 }
4248 else
4249 /* This is probably an error. Give a warning and
4250 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004251 smsg((char_u *)
4252 _("Warning: region %s not supported"),
4253 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004254 }
4255 else
4256 region_mask = 1 << c;
4257 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004258
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004259 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004260 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004261 if (ga_grow(&ga, 1) == FAIL)
4262 {
4263 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004264 ret_msg = e_outofmem;
4265 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004266 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004267 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004268 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4269 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004270 use_midword(slang, buf);
4271 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004272 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004273 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004274 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004275 }
4276
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004277 /* round 0: load int_wordlist, if possible.
4278 * round 1: load first name in 'spellfile'.
4279 * round 2: load second name in 'spellfile.
4280 * etc. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004281 spf = buf->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004282 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004284 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004285 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004286 /* Internal wordlist, if there is one. */
4287 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004288 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004289 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004290 }
4291 else
4292 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004293 /* One entry in 'spellfile'. */
4294 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4295 STRCAT(spf_name, ".spl");
4296
4297 /* If it was already found above then skip it. */
4298 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004299 {
4300 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4301 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004302 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004303 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004304 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004305 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004306 }
4307
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004308 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004309 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4310 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004314 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004315 * region name, the region is ignored otherwise. for int_wordlist
4316 * use an arbitrary name. */
4317 if (round == 0)
4318 STRCPY(lang, "internal wordlist");
4319 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004320 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004321 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004322 p = vim_strchr(lang, '.');
4323 if (p != NULL)
4324 *p = NUL; /* truncate at ".encoding.add" */
4325 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004326 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004327
4328 /* If one of the languages has NOBREAK we assume the addition
4329 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004330 if (slang != NULL && nobreak)
4331 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004333 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004335 region_mask = REGION_ALL;
4336 if (use_region != NULL && !dont_use_region)
4337 {
4338 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004339 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004340 if (c != REGION_ALL)
4341 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004343 /* This spell file is for other regions. */
4344 region_mask = 0;
4345 }
4346
4347 if (region_mask != 0)
4348 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004349 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4350 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4351 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004352 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4353 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004354 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 }
4357 }
4358
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004359 /* Everything is fine, store the new b_langp value. */
4360 ga_clear(&buf->b_langp);
4361 buf->b_langp = ga;
4362
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004363 /* For each language figure out what language to use for sound folding and
4364 * REP items. If the language doesn't support it itself use another one
4365 * with the same name. E.g. for "en-math" use "en". */
4366 for (i = 0; i < ga.ga_len; ++i)
4367 {
4368 lp = LANGP_ENTRY(ga, i);
4369
4370 /* sound folding */
4371 if (lp->lp_slang->sl_sal.ga_len > 0)
4372 /* language does sound folding itself */
4373 lp->lp_sallang = lp->lp_slang;
4374 else
4375 /* find first similar language that does sound folding */
4376 for (j = 0; j < ga.ga_len; ++j)
4377 {
4378 lp2 = LANGP_ENTRY(ga, j);
4379 if (lp2->lp_slang->sl_sal.ga_len > 0
4380 && STRNCMP(lp->lp_slang->sl_name,
4381 lp2->lp_slang->sl_name, 2) == 0)
4382 {
4383 lp->lp_sallang = lp2->lp_slang;
4384 break;
4385 }
4386 }
4387
4388 /* REP items */
4389 if (lp->lp_slang->sl_rep.ga_len > 0)
4390 /* language has REP items itself */
4391 lp->lp_replang = lp->lp_slang;
4392 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004393 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004394 for (j = 0; j < ga.ga_len; ++j)
4395 {
4396 lp2 = LANGP_ENTRY(ga, j);
4397 if (lp2->lp_slang->sl_rep.ga_len > 0
4398 && STRNCMP(lp->lp_slang->sl_name,
4399 lp2->lp_slang->sl_name, 2) == 0)
4400 {
4401 lp->lp_replang = lp2->lp_slang;
4402 break;
4403 }
4404 }
4405 }
4406
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004407theend:
4408 vim_free(spl_copy);
4409 recursive = FALSE;
4410 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004411}
4412
4413/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004414 * Clear the midword characters for buffer "buf".
4415 */
4416 static void
4417clear_midword(buf)
4418 buf_T *buf;
4419{
4420 vim_memset(buf->b_spell_ismw, 0, 256);
4421#ifdef FEAT_MBYTE
4422 vim_free(buf->b_spell_ismw_mb);
4423 buf->b_spell_ismw_mb = NULL;
4424#endif
4425}
4426
4427/*
4428 * Use the "sl_midword" field of language "lp" for buffer "buf".
4429 * They add up to any currently used midword characters.
4430 */
4431 static void
4432use_midword(lp, buf)
4433 slang_T *lp;
4434 buf_T *buf;
4435{
4436 char_u *p;
4437
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004438 if (lp->sl_midword == NULL) /* there aren't any */
4439 return;
4440
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004441 for (p = lp->sl_midword; *p != NUL; )
4442#ifdef FEAT_MBYTE
4443 if (has_mbyte)
4444 {
4445 int c, l, n;
4446 char_u *bp;
4447
4448 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004449 l = (*mb_ptr2len)(p);
4450 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004451 buf->b_spell_ismw[c] = TRUE;
4452 else if (buf->b_spell_ismw_mb == NULL)
4453 /* First multi-byte char in "b_spell_ismw_mb". */
4454 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4455 else
4456 {
4457 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004458 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004459 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4460 if (bp != NULL)
4461 {
4462 vim_free(buf->b_spell_ismw_mb);
4463 buf->b_spell_ismw_mb = bp;
4464 vim_strncpy(bp + n, p, l);
4465 }
4466 }
4467 p += l;
4468 }
4469 else
4470#endif
4471 buf->b_spell_ismw[*p++] = TRUE;
4472}
4473
4474/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 * Find the region "region[2]" in "rp" (points to "sl_regions").
4476 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004477 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004478 */
4479 static int
4480find_region(rp, region)
4481 char_u *rp;
4482 char_u *region;
4483{
4484 int i;
4485
4486 for (i = 0; ; i += 2)
4487 {
4488 if (rp[i] == NUL)
4489 return REGION_ALL;
4490 if (rp[i] == region[0] && rp[i + 1] == region[1])
4491 break;
4492 }
4493 return i / 2;
4494}
4495
4496/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004498 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004499 * Word WF_ONECAP
4500 * W WORD WF_ALLCAP
4501 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004502 */
4503 static int
4504captype(word, end)
4505 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004506 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004507{
4508 char_u *p;
4509 int c;
4510 int firstcap;
4511 int allcap;
4512 int past_second = FALSE; /* past second word char */
4513
4514 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004515 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004516 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004517 return 0; /* only non-word characters, illegal word */
4518#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004519 if (has_mbyte)
4520 c = mb_ptr2char_adv(&p);
4521 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004522#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004523 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004524 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525
4526 /*
4527 * Need to check all letters to find a word with mixed upper/lower.
4528 * But a word with an upper char only at start is a ONECAP.
4529 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004530 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004531 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004532 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004533 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004534 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535 {
4536 /* UUl -> KEEPCAP */
4537 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004538 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004539 allcap = FALSE;
4540 }
4541 else if (!allcap)
4542 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004544 past_second = TRUE;
4545 }
4546
4547 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004548 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004549 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004550 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004551 return 0;
4552}
4553
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004554/*
4555 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4556 * capital. So that make_case_word() can turn WOrd into Word.
4557 * Add ALLCAP for "WOrD".
4558 */
4559 static int
4560badword_captype(word, end)
4561 char_u *word;
4562 char_u *end;
4563{
4564 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004565 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004566 int l, u;
4567 int first;
4568 char_u *p;
4569
4570 if (flags & WF_KEEPCAP)
4571 {
4572 /* Count the number of UPPER and lower case letters. */
4573 l = u = 0;
4574 first = FALSE;
4575 for (p = word; p < end; mb_ptr_adv(p))
4576 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004577 c = PTR2CHAR(p);
4578 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004579 {
4580 ++u;
4581 if (p == word)
4582 first = TRUE;
4583 }
4584 else
4585 ++l;
4586 }
4587
4588 /* If there are more UPPER than lower case letters suggest an
4589 * ALLCAP word. Otherwise, if the first letter is UPPER then
4590 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4591 * require three upper case letters. */
4592 if (u > l && u > 2)
4593 flags |= WF_ALLCAP;
4594 else if (first)
4595 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004596
4597 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4598 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004599 }
4600 return flags;
4601}
4602
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004603# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4604/*
4605 * Free all languages.
4606 */
4607 void
4608spell_free_all()
4609{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004610 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004611 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004612 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004613
4614 /* Go through all buffers and handle 'spelllang'. */
4615 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4616 ga_clear(&buf->b_langp);
4617
4618 while (first_lang != NULL)
4619 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004620 slang = first_lang;
4621 first_lang = slang->sl_next;
4622 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004623 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004624
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004625 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004626 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004627 /* Delete the internal wordlist and its .spl file */
4628 mch_remove(int_wordlist);
4629 int_wordlist_spl(fname);
4630 mch_remove(fname);
4631 vim_free(int_wordlist);
4632 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004633 }
4634
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004635 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004636
4637 vim_free(repl_to);
4638 repl_to = NULL;
4639 vim_free(repl_from);
4640 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004641}
4642# endif
4643
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004644# if defined(FEAT_MBYTE) || defined(PROTO)
4645/*
4646 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004647 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004648 */
4649 void
4650spell_reload()
4651{
4652 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004653 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004654
Bram Moolenaarea408852005-06-25 22:49:46 +00004655 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004656 init_spell_chartab();
4657
4658 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004659 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004660
4661 /* Go through all buffers and handle 'spelllang'. */
4662 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4663 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004664 /* Only load the wordlists when 'spelllang' is set and there is a
4665 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004666 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004667 {
4668 FOR_ALL_WINDOWS(wp)
4669 if (wp->w_buffer == buf && wp->w_p_spell)
4670 {
4671 (void)did_set_spelllang(buf);
4672# ifdef FEAT_WINDOWS
4673 break;
4674# endif
4675 }
4676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004677 }
4678}
4679# endif
4680
Bram Moolenaarb765d632005-06-07 21:00:02 +00004681/*
4682 * Reload the spell file "fname" if it's loaded.
4683 */
4684 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004686 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004688{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004689 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004691
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004692 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004693 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004694 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004695 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004696 slang_clear(slang);
4697 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004698 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004699 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004700 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004702 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004703 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704
4705 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004706 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 if (added_word && !didit)
4708 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004709}
4710
4711
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004712/*
4713 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004714 */
4715
Bram Moolenaar51485f02005-06-04 21:55:20 +00004716#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004717 and .dic file. */
4718/*
4719 * Main structure to store the contents of a ".aff" file.
4720 */
4721typedef struct afffile_S
4722{
4723 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004724 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004725 unsigned af_rare; /* RARE ID for rare word */
4726 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004727 unsigned af_bad; /* BAD ID for banned word */
4728 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004729 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004730 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004731 unsigned af_comproot; /* COMPOUNDROOT ID */
4732 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4733 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004734 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004735 int af_pfxpostpone; /* postpone prefixes without chop string and
4736 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004737 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4738 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004739 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004740} afffile_T;
4741
Bram Moolenaar6de68532005-08-24 22:08:48 +00004742#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004743#define AFT_LONG 1 /* flags are two characters */
4744#define AFT_CAPLONG 2 /* flags are one or two characters */
4745#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004746
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004747typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004748/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4749struct affentry_S
4750{
4751 affentry_T *ae_next; /* next affix with same name/number */
4752 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4753 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004754 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004755 char_u *ae_cond; /* condition (NULL for ".") */
4756 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004757 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4758 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004759};
4760
Bram Moolenaar6de68532005-08-24 22:08:48 +00004761#ifdef FEAT_MBYTE
4762# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4763#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004764# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004765#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004766
Bram Moolenaar51485f02005-06-04 21:55:20 +00004767/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4768typedef struct affheader_S
4769{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004770 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4771 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4772 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004773 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004774 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004775 affentry_T *ah_first; /* first affix entry */
4776} affheader_T;
4777
4778#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4779
Bram Moolenaar6de68532005-08-24 22:08:48 +00004780/* Flag used in compound items. */
4781typedef struct compitem_S
4782{
4783 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4784 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4785 int ci_newID; /* affix ID after renumbering. */
4786} compitem_T;
4787
4788#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4789
Bram Moolenaar51485f02005-06-04 21:55:20 +00004790/*
4791 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004792 * the need to keep track of each allocated thing, everything is freed all at
4793 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004794 */
4795#define SBLOCKSIZE 16000 /* size of sb_data */
4796typedef struct sblock_S sblock_T;
4797struct sblock_S
4798{
4799 sblock_T *sb_next; /* next block in list */
4800 int sb_used; /* nr of bytes already in use */
4801 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802};
4803
4804/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004805 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004807typedef struct wordnode_S wordnode_T;
4808struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004809{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004810 union /* shared to save space */
4811 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004812 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004813 int index; /* index in written nodes (valid after first
4814 round) */
4815 } wn_u1;
4816 union /* shared to save space */
4817 {
4818 wordnode_T *next; /* next node with same hash key */
4819 wordnode_T *wnode; /* parent node that will write this node */
4820 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004821 wordnode_T *wn_child; /* child (next byte in word) */
4822 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4823 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004824 int wn_refs; /* Nr. of references to this node. Only
4825 relevant for first node in a list of
4826 siblings, in following siblings it is
4827 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004828 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004829
4830 /* Info for when "wn_byte" is NUL.
4831 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4832 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4833 * "wn_region" the LSW of the wordnr. */
4834 char_u wn_affixID; /* supported/required prefix ID or 0 */
4835 short_u wn_flags; /* WF_ flags */
4836 short wn_region; /* region mask */
4837
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004838#ifdef SPELL_PRINTTREE
4839 int wn_nr; /* sequence nr for printing */
4840#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004841};
4842
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004843#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4844
Bram Moolenaar51485f02005-06-04 21:55:20 +00004845#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004846
Bram Moolenaar51485f02005-06-04 21:55:20 +00004847/*
4848 * Info used while reading the spell files.
4849 */
4850typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004851{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004852 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004853 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004854
Bram Moolenaar51485f02005-06-04 21:55:20 +00004855 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004856 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004857
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004858 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004859
Bram Moolenaar4770d092006-01-12 23:22:24 +00004860 long si_sugtree; /* creating the soundfolding trie */
4861
Bram Moolenaar51485f02005-06-04 21:55:20 +00004862 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004863 long si_blocks_cnt; /* memory blocks allocated */
4864 long si_compress_cnt; /* words to add before lowering
4865 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004866 wordnode_T *si_first_free; /* List of nodes that have been freed during
4867 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004868 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004869#ifdef SPELL_PRINTTREE
4870 int si_wordnode_nr; /* sequence nr for nodes */
4871#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004872 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004873
Bram Moolenaar51485f02005-06-04 21:55:20 +00004874 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004875 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004876 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004877 int si_region; /* region mask */
4878 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004879 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004880 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004881 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004882 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004883 int si_region_count; /* number of regions supported (1 when there
4884 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004885 char_u si_region_name[16]; /* region names; used only if
4886 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004887
4888 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004889 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004890 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004891 char_u *si_sofofr; /* SOFOFROM text */
4892 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004893 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004894 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 int si_followup; /* soundsalike: ? */
4896 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004897 hashtab_T si_commonwords; /* hashtable for common words */
4898 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899 int si_rem_accents; /* soundsalike: remove accents */
4900 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004901 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004902 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004903 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004904 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004905 int si_compoptions; /* COMP_ flags */
4906 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4907 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004908 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004909 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004910 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004911 garray_T si_prefcond; /* table with conditions for postponed
4912 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004913 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004914 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004915} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004916
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004917static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004918static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004919static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004920static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4921static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4922static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004923static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004924static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4925static void aff_check_number __ARGS((int spinval, int affval, char *name));
4926static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004927static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004928static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4929static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004930static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004931static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004932static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004933static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004934static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004935static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004936static 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 +00004937static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4938static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4939static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004940static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004941static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004942static 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 +00004943static 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 +00004944static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004945static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004946static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4947static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4948static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004949static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004950static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004951static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004952static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004953static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004954static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4955static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4956static int sug_maketable __ARGS((spellinfo_T *spin));
4957static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4958static int offset2bytes __ARGS((int nr, char_u *buf));
4959static int bytes2offset __ARGS((char_u **pp));
4960static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004962static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004963static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004964
Bram Moolenaar53805d12005-08-01 07:08:33 +00004965/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4966 * but it must be negative to indicate the prefix tree to tree_add_word().
4967 * Use a negative number with the lower 8 bits zero. */
4968#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004969
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004970/* flags for "condit" argument of store_aff_word() */
4971#define CONDIT_COMB 1 /* affix must combine */
4972#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4973#define CONDIT_SUF 4 /* add a suffix for matching flags */
4974#define CONDIT_AFF 8 /* word already has an affix */
4975
Bram Moolenaar5195e452005-08-19 20:32:47 +00004976/*
4977 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4978 */
4979static long compress_start = 30000; /* memory / SBLOCKSIZE */
4980static long compress_inc = 100; /* memory / SBLOCKSIZE */
4981static long compress_added = 500000; /* word count */
4982
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004983#ifdef SPELL_PRINTTREE
4984/*
4985 * For debugging the tree code: print the current tree in a (more or less)
4986 * readable format, so that we can see what happens when adding a word and/or
4987 * compressing the tree.
4988 * Based on code from Olaf Seibert.
4989 */
4990#define PRINTLINESIZE 1000
4991#define PRINTWIDTH 6
4992
4993#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4994 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4995
4996static char line1[PRINTLINESIZE];
4997static char line2[PRINTLINESIZE];
4998static char line3[PRINTLINESIZE];
4999
5000 static void
5001spell_clear_flags(wordnode_T *node)
5002{
5003 wordnode_T *np;
5004
5005 for (np = node; np != NULL; np = np->wn_sibling)
5006 {
5007 np->wn_u1.index = FALSE;
5008 spell_clear_flags(np->wn_child);
5009 }
5010}
5011
5012 static void
5013spell_print_node(wordnode_T *node, int depth)
5014{
5015 if (node->wn_u1.index)
5016 {
5017 /* Done this node before, print the reference. */
5018 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5019 PRINTSOME(line2, depth, " ", 0, 0);
5020 PRINTSOME(line3, depth, " ", 0, 0);
5021 msg(line1);
5022 msg(line2);
5023 msg(line3);
5024 }
5025 else
5026 {
5027 node->wn_u1.index = TRUE;
5028
5029 if (node->wn_byte != NUL)
5030 {
5031 if (node->wn_child != NULL)
5032 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5033 else
5034 /* Cannot happen? */
5035 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5036 }
5037 else
5038 PRINTSOME(line1, depth, " $ ", 0, 0);
5039
5040 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5041
5042 if (node->wn_sibling != NULL)
5043 PRINTSOME(line3, depth, " | ", 0, 0);
5044 else
5045 PRINTSOME(line3, depth, " ", 0, 0);
5046
5047 if (node->wn_byte == NUL)
5048 {
5049 msg(line1);
5050 msg(line2);
5051 msg(line3);
5052 }
5053
5054 /* do the children */
5055 if (node->wn_byte != NUL && node->wn_child != NULL)
5056 spell_print_node(node->wn_child, depth + 1);
5057
5058 /* do the siblings */
5059 if (node->wn_sibling != NULL)
5060 {
5061 /* get rid of all parent details except | */
5062 STRCPY(line1, line3);
5063 STRCPY(line2, line3);
5064 spell_print_node(node->wn_sibling, depth);
5065 }
5066 }
5067}
5068
5069 static void
5070spell_print_tree(wordnode_T *root)
5071{
5072 if (root != NULL)
5073 {
5074 /* Clear the "wn_u1.index" fields, used to remember what has been
5075 * done. */
5076 spell_clear_flags(root);
5077
5078 /* Recursively print the tree. */
5079 spell_print_node(root, 0);
5080 }
5081}
5082#endif /* SPELL_PRINTTREE */
5083
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005084/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005085 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005086 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005087 */
5088 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005089spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005090 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005091 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005092{
5093 FILE *fd;
5094 afffile_T *aff;
5095 char_u rline[MAXLINELEN];
5096 char_u *line;
5097 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005098#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005099 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005100 int itemcnt;
5101 char_u *p;
5102 int lnum = 0;
5103 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005104 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005105 int aff_todo = 0;
5106 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005107 char_u *low = NULL;
5108 char_u *fol = NULL;
5109 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005110 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005111 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005113 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005114 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005115 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005116 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005117 int compminlen = 0; /* COMPOUNDMIN value */
5118 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005119 int compoptions = 0; /* COMP_ flags */
5120 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005121 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005122 concatenated */
5123 char_u *midword = NULL; /* MIDWORD value */
5124 char_u *syllable = NULL; /* SYLLABLE value */
5125 char_u *sofofrom = NULL; /* SOFOFROM value */
5126 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005127
Bram Moolenaar51485f02005-06-04 21:55:20 +00005128 /*
5129 * Open the file.
5130 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005131 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005132 if (fd == NULL)
5133 {
5134 EMSG2(_(e_notopen), fname);
5135 return NULL;
5136 }
5137
Bram Moolenaar4770d092006-01-12 23:22:24 +00005138 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5139 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141 /* Only do REP lines when not done in another .aff file already. */
5142 do_rep = spin->si_rep.ga_len == 0;
5143
Bram Moolenaar4770d092006-01-12 23:22:24 +00005144 /* Only do REPSAL lines when not done in another .aff file already. */
5145 do_repsal = spin->si_repsal.ga_len == 0;
5146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005147 /* Only do SAL lines when not done in another .aff file already. */
5148 do_sal = spin->si_sal.ga_len == 0;
5149
5150 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005151 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005152
Bram Moolenaar51485f02005-06-04 21:55:20 +00005153 /*
5154 * Allocate and init the afffile_T structure.
5155 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005156 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005157 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005158 {
5159 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005160 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005161 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005162 hash_init(&aff->af_pref);
5163 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005164 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165
5166 /*
5167 * Read all the lines in the file one by one.
5168 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005169 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005170 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005171 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005172 ++lnum;
5173
5174 /* Skip comment lines. */
5175 if (*rline == '#')
5176 continue;
5177
5178 /* Convert from "SET" to 'encoding' when needed. */
5179 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005180#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005181 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005182 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005183 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005184 if (pc == NULL)
5185 {
5186 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5187 fname, lnum, rline);
5188 continue;
5189 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005190 line = pc;
5191 }
5192 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005193#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005194 {
5195 pc = NULL;
5196 line = rline;
5197 }
5198
5199 /* Split the line up in white separated items. Put a NUL after each
5200 * item. */
5201 itemcnt = 0;
5202 for (p = line; ; )
5203 {
5204 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5205 ++p;
5206 if (*p == NUL)
5207 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005208 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005209 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005210 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005211 /* A few items have arbitrary text argument, don't split them. */
5212 if (itemcnt == 2 && spell_info_item(items[0]))
5213 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5214 ++p;
5215 else
5216 while (*p > ' ') /* skip until white space or CR/NL */
5217 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005218 if (*p == NUL)
5219 break;
5220 *p++ = NUL;
5221 }
5222
5223 /* Handle non-empty lines. */
5224 if (itemcnt > 0)
5225 {
5226 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5227 && aff->af_enc == NULL)
5228 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005229#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005230 /* Setup for conversion from "ENC" to 'encoding'. */
5231 aff->af_enc = enc_canonize(items[1]);
5232 if (aff->af_enc != NULL && !spin->si_ascii
5233 && convert_setup(&spin->si_conv, aff->af_enc,
5234 p_enc) == FAIL)
5235 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5236 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005237 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005238#else
5239 smsg((char_u *)_("Conversion in %s not supported"), fname);
5240#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005241 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005242 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5243 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005244 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005245 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005246 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005247 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005248 aff->af_flagtype = AFT_NUM;
5249 else if (STRCMP(items[1], "caplong") == 0)
5250 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005251 else
5252 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5253 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005254 if (aff->af_rare != 0
5255 || aff->af_keepcase != 0
5256 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005257 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005258 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005259 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005260 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005261 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005262 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005263 || aff->af_suff.ht_used > 0
5264 || aff->af_pref.ht_used > 0)
5265 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5266 fname, lnum, items[1]);
5267 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005268 else if (spell_info_item(items[0]))
5269 {
5270 p = (char_u *)getroom(spin,
5271 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5272 + STRLEN(items[0])
5273 + STRLEN(items[1]) + 3, FALSE);
5274 if (p != NULL)
5275 {
5276 if (spin->si_info != NULL)
5277 {
5278 STRCPY(p, spin->si_info);
5279 STRCAT(p, "\n");
5280 }
5281 STRCAT(p, items[0]);
5282 STRCAT(p, " ");
5283 STRCAT(p, items[1]);
5284 spin->si_info = p;
5285 }
5286 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005287 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5288 && midword == NULL)
5289 {
5290 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005295 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005296 /* TODO: remove "RAR" later */
5297 else if ((STRCMP(items[0], "RAR") == 0
5298 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5299 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005300 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005301 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005302 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005303 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005304 /* TODO: remove "KEP" later */
5305 else if ((STRCMP(items[0], "KEP") == 0
5306 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5307 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005308 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005309 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005310 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005311 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005312 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5313 && aff->af_bad == 0)
5314 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005315 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5316 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005317 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005318 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5319 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005320 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005321 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5322 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005323 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005324 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5325 && aff->af_circumfix == 0)
5326 {
5327 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5328 fname, lnum);
5329 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005330 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5331 && aff->af_nosuggest == 0)
5332 {
5333 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5334 fname, lnum);
5335 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005336 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5337 && aff->af_needcomp == 0)
5338 {
5339 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5340 fname, lnum);
5341 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005342 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5343 && aff->af_comproot == 0)
5344 {
5345 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5346 fname, lnum);
5347 }
5348 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5349 && itemcnt == 2 && aff->af_compforbid == 0)
5350 {
5351 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5352 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005353 if (aff->af_pref.ht_used > 0)
5354 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5355 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005356 }
5357 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5358 && itemcnt == 2 && aff->af_comppermit == 0)
5359 {
5360 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5361 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005362 if (aff->af_pref.ht_used > 0)
5363 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5364 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005365 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005366 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005367 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005368 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005369 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005370 * "Na" into "Na+", "1234" into "1234+". */
5371 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005372 if (p != NULL)
5373 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005374 STRCPY(p, items[1]);
5375 STRCAT(p, "+");
5376 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005377 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005378 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005379 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005380 {
5381 /* Concatenate this string to previously defined ones, using a
5382 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005383 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005384 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005385 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005386 p = getroom(spin, l, FALSE);
5387 if (p != NULL)
5388 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005389 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005390 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005391 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005392 STRCAT(p, "/");
5393 }
5394 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005395 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005396 }
5397 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005398 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005399 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005400 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005401 compmax = atoi((char *)items[1]);
5402 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005403 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005404 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005405 }
5406 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005407 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005408 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005409 compminlen = atoi((char *)items[1]);
5410 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005411 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5412 fname, lnum, items[1]);
5413 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005414 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005415 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005416 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005417 compsylmax = atoi((char *)items[1]);
5418 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005419 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5420 fname, lnum, items[1]);
5421 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005422 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5423 {
5424 compoptions |= COMP_CHECKDUP;
5425 }
5426 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5427 {
5428 compoptions |= COMP_CHECKREP;
5429 }
5430 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5431 {
5432 compoptions |= COMP_CHECKCASE;
5433 }
5434 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5435 && itemcnt == 1)
5436 {
5437 compoptions |= COMP_CHECKTRIPLE;
5438 }
5439 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5440 && itemcnt == 2)
5441 {
5442 if (atoi((char *)items[1]) == 0)
5443 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5444 fname, lnum, items[1]);
5445 }
5446 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5447 && itemcnt == 3)
5448 {
5449 garray_T *gap = &spin->si_comppat;
5450 int i;
5451
5452 /* Only add the couple if it isn't already there. */
5453 for (i = 0; i < gap->ga_len - 1; i += 2)
5454 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5455 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5456 items[2]) == 0)
5457 break;
5458 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5459 {
5460 ((char_u **)(gap->ga_data))[gap->ga_len++]
5461 = getroom_save(spin, items[1]);
5462 ((char_u **)(gap->ga_data))[gap->ga_len++]
5463 = getroom_save(spin, items[2]);
5464 }
5465 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005466 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005467 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005468 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005469 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005470 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005471 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5472 {
5473 spin->si_nobreak = TRUE;
5474 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005475 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5476 {
5477 spin->si_nosplitsugs = TRUE;
5478 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005479 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5480 {
5481 spin->si_nosugfile = TRUE;
5482 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005483 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5484 {
5485 aff->af_pfxpostpone = TRUE;
5486 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005487 else if ((STRCMP(items[0], "PFX") == 0
5488 || STRCMP(items[0], "SFX") == 0)
5489 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005490 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005491 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005492 int lasti = 4;
5493 char_u key[AH_KEY_LEN];
5494
5495 if (*items[0] == 'P')
5496 tp = &aff->af_pref;
5497 else
5498 tp = &aff->af_suff;
5499
5500 /* Myspell allows the same affix name to be used multiple
5501 * times. The affix files that do this have an undocumented
5502 * "S" flag on all but the last block, thus we check for that
5503 * and store it in ah_follows. */
5504 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5505 hi = hash_find(tp, key);
5506 if (!HASHITEM_EMPTY(hi))
5507 {
5508 cur_aff = HI2AH(hi);
5509 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5510 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5511 fname, lnum, items[1]);
5512 if (!cur_aff->ah_follows)
5513 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5514 fname, lnum, items[1]);
5515 }
5516 else
5517 {
5518 /* New affix letter. */
5519 cur_aff = (affheader_T *)getroom(spin,
5520 sizeof(affheader_T), TRUE);
5521 if (cur_aff == NULL)
5522 break;
5523 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5524 fname, lnum);
5525 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5526 break;
5527 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005528 || cur_aff->ah_flag == aff->af_rare
5529 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005530 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005531 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005532 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005533 || cur_aff->ah_flag == aff->af_needcomp
5534 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005535 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 +00005536 fname, lnum, items[1]);
5537 STRCPY(cur_aff->ah_key, items[1]);
5538 hash_add(tp, cur_aff->ah_key);
5539
5540 cur_aff->ah_combine = (*items[2] == 'Y');
5541 }
5542
5543 /* Check for the "S" flag, which apparently means that another
5544 * block with the same affix name is following. */
5545 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5546 {
5547 ++lasti;
5548 cur_aff->ah_follows = TRUE;
5549 }
5550 else
5551 cur_aff->ah_follows = FALSE;
5552
Bram Moolenaar8db73182005-06-17 21:51:16 +00005553 /* Myspell allows extra text after the item, but that might
5554 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005555 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005556 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005557
Bram Moolenaar95529562005-08-25 21:21:38 +00005558 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005559 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5560 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005561
Bram Moolenaar95529562005-08-25 21:21:38 +00005562 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005563 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005564 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005565 {
5566 /* Use a new number in the .spl file later, to be able
5567 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005568 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005569 cur_aff->ah_newID = ++spin->si_newprefID;
5570
5571 /* We only really use ah_newID if the prefix is
5572 * postponed. We know that only after handling all
5573 * the items. */
5574 did_postpone_prefix = FALSE;
5575 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005576 else
5577 /* Did use the ID in a previous block. */
5578 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005579 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005580
Bram Moolenaar51485f02005-06-04 21:55:20 +00005581 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005582 }
5583 else if ((STRCMP(items[0], "PFX") == 0
5584 || STRCMP(items[0], "SFX") == 0)
5585 && aff_todo > 0
5586 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005587 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005588 {
5589 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005590 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005591 int lasti = 5;
5592
Bram Moolenaar8db73182005-06-17 21:51:16 +00005593 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005594 * mean mistakes go unnoticed. Require a comment-starter.
5595 * Hunspell uses a "-" item. */
5596 if (itemcnt > lasti && *items[lasti] != '#'
5597 && (STRCMP(items[lasti], "-") != 0
5598 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005599 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005600
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005601 /* New item for an affix letter. */
5602 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005603 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005604 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005605 if (aff_entry == NULL)
5606 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005607
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005608 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005609 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005610 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005611 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005612 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005613
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005614 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005615 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5616 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005617 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005618 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005619 aff_process_flags(aff, aff_entry);
5620 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005621 }
5622
Bram Moolenaar51485f02005-06-04 21:55:20 +00005623 /* Don't use an affix entry with non-ASCII characters when
5624 * "spin->si_ascii" is TRUE. */
5625 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005626 || has_non_ascii(aff_entry->ae_add)))
5627 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005628 aff_entry->ae_next = cur_aff->ah_first;
5629 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005630
5631 if (STRCMP(items[4], ".") != 0)
5632 {
5633 char_u buf[MAXLINELEN];
5634
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005635 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005636 if (*items[0] == 'P')
5637 sprintf((char *)buf, "^%s", items[4]);
5638 else
5639 sprintf((char *)buf, "%s$", items[4]);
5640 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005641 RE_MAGIC + RE_STRING + RE_STRICT);
5642 if (aff_entry->ae_prog == NULL)
5643 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5644 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005645 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005646
5647 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005648 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005649 * Can't be done for an affix with flags, ignoring
5650 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005651 if (*items[0] == 'P' && aff->af_pfxpostpone
5652 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005653 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005654 /* When the chop string is one lower-case letter and
5655 * the add string ends in the upper-case letter we set
5656 * the "upper" flag, clear "ae_chop" and remove the
5657 * letters from "ae_add". The condition must either
5658 * be empty or start with the same letter. */
5659 if (aff_entry->ae_chop != NULL
5660 && aff_entry->ae_add != NULL
5661#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005662 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005663 aff_entry->ae_chop)] == NUL
5664#else
5665 && aff_entry->ae_chop[1] == NUL
5666#endif
5667 )
5668 {
5669 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005670
Bram Moolenaar53805d12005-08-01 07:08:33 +00005671 c = PTR2CHAR(aff_entry->ae_chop);
5672 c_up = SPELL_TOUPPER(c);
5673 if (c_up != c
5674 && (aff_entry->ae_cond == NULL
5675 || PTR2CHAR(aff_entry->ae_cond) == c))
5676 {
5677 p = aff_entry->ae_add
5678 + STRLEN(aff_entry->ae_add);
5679 mb_ptr_back(aff_entry->ae_add, p);
5680 if (PTR2CHAR(p) == c_up)
5681 {
5682 upper = TRUE;
5683 aff_entry->ae_chop = NULL;
5684 *p = NUL;
5685
5686 /* The condition is matched with the
5687 * actual word, thus must check for the
5688 * upper-case letter. */
5689 if (aff_entry->ae_cond != NULL)
5690 {
5691 char_u buf[MAXLINELEN];
5692#ifdef FEAT_MBYTE
5693 if (has_mbyte)
5694 {
5695 onecap_copy(items[4], buf, TRUE);
5696 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005697 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005698 }
5699 else
5700#endif
5701 *aff_entry->ae_cond = c_up;
5702 if (aff_entry->ae_cond != NULL)
5703 {
5704 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005705 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005706 vim_free(aff_entry->ae_prog);
5707 aff_entry->ae_prog = vim_regcomp(
5708 buf, RE_MAGIC + RE_STRING);
5709 }
5710 }
5711 }
5712 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005713 }
5714
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005715 if (aff_entry->ae_chop == NULL
5716 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005717 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005718 int idx;
5719 char_u **pp;
5720 int n;
5721
Bram Moolenaar6de68532005-08-24 22:08:48 +00005722 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005723 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5724 --idx)
5725 {
5726 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5727 if (str_equal(p, aff_entry->ae_cond))
5728 break;
5729 }
5730 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5731 {
5732 /* Not found, add a new condition. */
5733 idx = spin->si_prefcond.ga_len++;
5734 pp = ((char_u **)spin->si_prefcond.ga_data)
5735 + idx;
5736 if (aff_entry->ae_cond == NULL)
5737 *pp = NULL;
5738 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005739 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005740 aff_entry->ae_cond);
5741 }
5742
5743 /* Add the prefix to the prefix tree. */
5744 if (aff_entry->ae_add == NULL)
5745 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005746 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005747 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005748
Bram Moolenaar53805d12005-08-01 07:08:33 +00005749 /* PFX_FLAGS is a negative number, so that
5750 * tree_add_word() knows this is the prefix tree. */
5751 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005752 if (!cur_aff->ah_combine)
5753 n |= WFP_NC;
5754 if (upper)
5755 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005756 if (aff_entry->ae_comppermit)
5757 n |= WFP_COMPPERMIT;
5758 if (aff_entry->ae_compforbid)
5759 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005760 tree_add_word(spin, p, spin->si_prefroot, n,
5761 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005762 did_postpone_prefix = TRUE;
5763 }
5764
5765 /* Didn't actually use ah_newID, backup si_newprefID. */
5766 if (aff_todo == 0 && !did_postpone_prefix)
5767 {
5768 --spin->si_newprefID;
5769 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005770 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005771 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005772 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005773 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005774 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5775 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005776 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005777 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005778 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005779 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5780 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005781 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005782 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005783 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005784 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5785 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005786 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005787 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005788 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005789 else if ((STRCMP(items[0], "REP") == 0
5790 || STRCMP(items[0], "REPSAL") == 0)
5791 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005793 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005794 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005795 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005796 fname, lnum);
5797 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005798 else if ((STRCMP(items[0], "REP") == 0
5799 || STRCMP(items[0], "REPSAL") == 0)
5800 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005801 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005802 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005803 /* Myspell ignores extra arguments, we require it starts with
5804 * # to detect mistakes. */
5805 if (itemcnt > 3 && items[3][0] != '#')
5806 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005807 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005808 {
5809 /* Replace underscore with space (can't include a space
5810 * directly). */
5811 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5812 if (*p == '_')
5813 *p = ' ';
5814 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5815 if (*p == '_')
5816 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005817 add_fromto(spin, items[0][3] == 'S'
5818 ? &spin->si_repsal
5819 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005821 }
5822 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5823 {
5824 /* MAP item or count */
5825 if (!found_map)
5826 {
5827 /* First line contains the count. */
5828 found_map = TRUE;
5829 if (!isdigit(*items[1]))
5830 smsg((char_u *)_("Expected MAP count in %s line %d"),
5831 fname, lnum);
5832 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005833 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005834 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005835 int c;
5836
5837 /* Check that every character appears only once. */
5838 for (p = items[1]; *p != NUL; )
5839 {
5840#ifdef FEAT_MBYTE
5841 c = mb_ptr2char_adv(&p);
5842#else
5843 c = *p++;
5844#endif
5845 if ((spin->si_map.ga_len > 0
5846 && vim_strchr(spin->si_map.ga_data, c)
5847 != NULL)
5848 || vim_strchr(p, c) != NULL)
5849 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5850 fname, lnum);
5851 }
5852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005853 /* We simply concatenate all the MAP strings, separated by
5854 * slashes. */
5855 ga_concat(&spin->si_map, items[1]);
5856 ga_append(&spin->si_map, '/');
5857 }
5858 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005859 /* Accept "SAL from to" and "SAL from to # comment". */
5860 else if (STRCMP(items[0], "SAL") == 0
5861 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005862 {
5863 if (do_sal)
5864 {
5865 /* SAL item (sounds-a-like)
5866 * Either one of the known keys or a from-to pair. */
5867 if (STRCMP(items[1], "followup") == 0)
5868 spin->si_followup = sal_to_bool(items[2]);
5869 else if (STRCMP(items[1], "collapse_result") == 0)
5870 spin->si_collapse = sal_to_bool(items[2]);
5871 else if (STRCMP(items[1], "remove_accents") == 0)
5872 spin->si_rem_accents = sal_to_bool(items[2]);
5873 else
5874 /* when "to" is "_" it means empty */
5875 add_fromto(spin, &spin->si_sal, items[1],
5876 STRCMP(items[2], "_") == 0 ? (char_u *)""
5877 : items[2]);
5878 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005879 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005880 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005881 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005882 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005883 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005884 }
5885 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005886 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005887 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005888 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005889 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005890 else if (STRCMP(items[0], "COMMON") == 0)
5891 {
5892 int i;
5893
5894 for (i = 1; i < itemcnt; ++i)
5895 {
5896 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5897 items[i])))
5898 {
5899 p = vim_strsave(items[i]);
5900 if (p == NULL)
5901 break;
5902 hash_add(&spin->si_commonwords, p);
5903 }
5904 }
5905 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005906 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005907 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005908 fname, lnum, items[0]);
5909 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005910 }
5911
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005912 if (fol != NULL || low != NULL || upp != NULL)
5913 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005914 if (spin->si_clear_chartab)
5915 {
5916 /* Clear the char type tables, don't want to use any of the
5917 * currently used spell properties. */
5918 init_spell_chartab();
5919 spin->si_clear_chartab = FALSE;
5920 }
5921
Bram Moolenaar3982c542005-06-08 21:56:31 +00005922 /*
5923 * Don't write a word table for an ASCII file, so that we don't check
5924 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005925 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005926 * mb_get_class(), the list of chars in the file will be incomplete.
5927 */
5928 if (!spin->si_ascii
5929#ifdef FEAT_MBYTE
5930 && !enc_utf8
5931#endif
5932 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005933 {
5934 if (fol == NULL || low == NULL || upp == NULL)
5935 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5936 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005937 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005938 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005939
5940 vim_free(fol);
5941 vim_free(low);
5942 vim_free(upp);
5943 }
5944
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005945 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005946 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005947 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005948 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005949 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005950 }
5951
Bram Moolenaar6de68532005-08-24 22:08:48 +00005952 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005953 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005954 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5955 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005956 }
5957
Bram Moolenaar6de68532005-08-24 22:08:48 +00005958 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005959 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005960 if (syllable == NULL)
5961 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5962 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5963 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005964 }
5965
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005966 if (compoptions != 0)
5967 {
5968 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5969 spin->si_compoptions |= compoptions;
5970 }
5971
Bram Moolenaar6de68532005-08-24 22:08:48 +00005972 if (compflags != NULL)
5973 process_compflags(spin, aff, compflags);
5974
5975 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005976 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005977 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005978 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005979 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005980 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005981 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005982 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00005983 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005984 }
5985
Bram Moolenaar6de68532005-08-24 22:08:48 +00005986 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005987 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005988 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5989 spin->si_syllable = syllable;
5990 }
5991
5992 if (sofofrom != NULL || sofoto != NULL)
5993 {
5994 if (sofofrom == NULL || sofoto == NULL)
5995 smsg((char_u *)_("Missing SOFO%s line in %s"),
5996 sofofrom == NULL ? "FROM" : "TO", fname);
5997 else if (spin->si_sal.ga_len > 0)
5998 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005999 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006000 {
6001 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6002 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6003 spin->si_sofofr = sofofrom;
6004 spin->si_sofoto = sofoto;
6005 }
6006 }
6007
6008 if (midword != NULL)
6009 {
6010 aff_check_string(spin->si_midword, midword, "MIDWORD");
6011 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006012 }
6013
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006014 vim_free(pc);
6015 fclose(fd);
6016 return aff;
6017}
6018
6019/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006020 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6021 * ae_flags to ae_comppermit and ae_compforbid.
6022 */
6023 static void
6024aff_process_flags(affile, entry)
6025 afffile_T *affile;
6026 affentry_T *entry;
6027{
6028 char_u *p;
6029 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006030 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006031
6032 if (entry->ae_flags != NULL
6033 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6034 {
6035 for (p = entry->ae_flags; *p != NUL; )
6036 {
6037 prevp = p;
6038 flag = get_affitem(affile->af_flagtype, &p);
6039 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6040 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006041 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006042 p = prevp;
6043 if (flag == affile->af_comppermit)
6044 entry->ae_comppermit = TRUE;
6045 else
6046 entry->ae_compforbid = TRUE;
6047 }
6048 if (affile->af_flagtype == AFT_NUM && *p == ',')
6049 ++p;
6050 }
6051 if (*entry->ae_flags == NUL)
6052 entry->ae_flags = NULL; /* nothing left */
6053 }
6054}
6055
6056/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006057 * Return TRUE if "s" is the name of an info item in the affix file.
6058 */
6059 static int
6060spell_info_item(s)
6061 char_u *s;
6062{
6063 return STRCMP(s, "NAME") == 0
6064 || STRCMP(s, "HOME") == 0
6065 || STRCMP(s, "VERSION") == 0
6066 || STRCMP(s, "AUTHOR") == 0
6067 || STRCMP(s, "EMAIL") == 0
6068 || STRCMP(s, "COPYRIGHT") == 0;
6069}
6070
6071/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006072 * Turn an affix flag name into a number, according to the FLAG type.
6073 * returns zero for failure.
6074 */
6075 static unsigned
6076affitem2flag(flagtype, item, fname, lnum)
6077 int flagtype;
6078 char_u *item;
6079 char_u *fname;
6080 int lnum;
6081{
6082 unsigned res;
6083 char_u *p = item;
6084
6085 res = get_affitem(flagtype, &p);
6086 if (res == 0)
6087 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006088 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006089 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6090 fname, lnum, item);
6091 else
6092 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6093 fname, lnum, item);
6094 }
6095 if (*p != NUL)
6096 {
6097 smsg((char_u *)_(e_affname), fname, lnum, item);
6098 return 0;
6099 }
6100
6101 return res;
6102}
6103
6104/*
6105 * Get one affix name from "*pp" and advance the pointer.
6106 * Returns zero for an error, still advances the pointer then.
6107 */
6108 static unsigned
6109get_affitem(flagtype, pp)
6110 int flagtype;
6111 char_u **pp;
6112{
6113 int res;
6114
Bram Moolenaar95529562005-08-25 21:21:38 +00006115 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006116 {
6117 if (!VIM_ISDIGIT(**pp))
6118 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006119 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006120 return 0;
6121 }
6122 res = getdigits(pp);
6123 }
6124 else
6125 {
6126#ifdef FEAT_MBYTE
6127 res = mb_ptr2char_adv(pp);
6128#else
6129 res = *(*pp)++;
6130#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006131 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006132 && res >= 'A' && res <= 'Z'))
6133 {
6134 if (**pp == NUL)
6135 return 0;
6136#ifdef FEAT_MBYTE
6137 res = mb_ptr2char_adv(pp) + (res << 16);
6138#else
6139 res = *(*pp)++ + (res << 16);
6140#endif
6141 }
6142 }
6143 return res;
6144}
6145
6146/*
6147 * Process the "compflags" string used in an affix file and append it to
6148 * spin->si_compflags.
6149 * The processing involves changing the affix names to ID numbers, so that
6150 * they fit in one byte.
6151 */
6152 static void
6153process_compflags(spin, aff, compflags)
6154 spellinfo_T *spin;
6155 afffile_T *aff;
6156 char_u *compflags;
6157{
6158 char_u *p;
6159 char_u *prevp;
6160 unsigned flag;
6161 compitem_T *ci;
6162 int id;
6163 int len;
6164 char_u *tp;
6165 char_u key[AH_KEY_LEN];
6166 hashitem_T *hi;
6167
6168 /* Make room for the old and the new compflags, concatenated with a / in
6169 * between. Processing it makes it shorter, but we don't know by how
6170 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006171 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006172 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006173 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006174 p = getroom(spin, len, FALSE);
6175 if (p == NULL)
6176 return;
6177 if (spin->si_compflags != NULL)
6178 {
6179 STRCPY(p, spin->si_compflags);
6180 STRCAT(p, "/");
6181 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006182 spin->si_compflags = p;
6183 tp = p + STRLEN(p);
6184
6185 for (p = compflags; *p != NUL; )
6186 {
6187 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6188 /* Copy non-flag characters directly. */
6189 *tp++ = *p++;
6190 else
6191 {
6192 /* First get the flag number, also checks validity. */
6193 prevp = p;
6194 flag = get_affitem(aff->af_flagtype, &p);
6195 if (flag != 0)
6196 {
6197 /* Find the flag in the hashtable. If it was used before, use
6198 * the existing ID. Otherwise add a new entry. */
6199 vim_strncpy(key, prevp, p - prevp);
6200 hi = hash_find(&aff->af_comp, key);
6201 if (!HASHITEM_EMPTY(hi))
6202 id = HI2CI(hi)->ci_newID;
6203 else
6204 {
6205 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6206 if (ci == NULL)
6207 break;
6208 STRCPY(ci->ci_key, key);
6209 ci->ci_flag = flag;
6210 /* Avoid using a flag ID that has a special meaning in a
6211 * regexp (also inside []). */
6212 do
6213 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006214 check_renumber(spin);
6215 id = spin->si_newcompID--;
6216 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006217 ci->ci_newID = id;
6218 hash_add(&aff->af_comp, ci->ci_key);
6219 }
6220 *tp++ = id;
6221 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006222 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006223 ++p;
6224 }
6225 }
6226
6227 *tp = NUL;
6228}
6229
6230/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006231 * Check that the new IDs for postponed affixes and compounding don't overrun
6232 * each other. We have almost 255 available, but start at 0-127 to avoid
6233 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6234 * When that is used up an error message is given.
6235 */
6236 static void
6237check_renumber(spin)
6238 spellinfo_T *spin;
6239{
6240 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6241 {
6242 spin->si_newprefID = 127;
6243 spin->si_newcompID = 255;
6244 }
6245}
6246
6247/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006248 * Return TRUE if flag "flag" appears in affix list "afflist".
6249 */
6250 static int
6251flag_in_afflist(flagtype, afflist, flag)
6252 int flagtype;
6253 char_u *afflist;
6254 unsigned flag;
6255{
6256 char_u *p;
6257 unsigned n;
6258
6259 switch (flagtype)
6260 {
6261 case AFT_CHAR:
6262 return vim_strchr(afflist, flag) != NULL;
6263
Bram Moolenaar95529562005-08-25 21:21:38 +00006264 case AFT_CAPLONG:
6265 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006266 for (p = afflist; *p != NUL; )
6267 {
6268#ifdef FEAT_MBYTE
6269 n = mb_ptr2char_adv(&p);
6270#else
6271 n = *p++;
6272#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006273 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006274 && *p != NUL)
6275#ifdef FEAT_MBYTE
6276 n = mb_ptr2char_adv(&p) + (n << 16);
6277#else
6278 n = *p++ + (n << 16);
6279#endif
6280 if (n == flag)
6281 return TRUE;
6282 }
6283 break;
6284
Bram Moolenaar95529562005-08-25 21:21:38 +00006285 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006286 for (p = afflist; *p != NUL; )
6287 {
6288 n = getdigits(&p);
6289 if (n == flag)
6290 return TRUE;
6291 if (*p != NUL) /* skip over comma */
6292 ++p;
6293 }
6294 break;
6295 }
6296 return FALSE;
6297}
6298
6299/*
6300 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6301 */
6302 static void
6303aff_check_number(spinval, affval, name)
6304 int spinval;
6305 int affval;
6306 char *name;
6307{
6308 if (spinval != 0 && spinval != affval)
6309 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6310}
6311
6312/*
6313 * Give a warning when "spinval" and "affval" strings are set and not the same.
6314 */
6315 static void
6316aff_check_string(spinval, affval, name)
6317 char_u *spinval;
6318 char_u *affval;
6319 char *name;
6320{
6321 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6322 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6323}
6324
6325/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006326 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6327 * NULL as equal.
6328 */
6329 static int
6330str_equal(s1, s2)
6331 char_u *s1;
6332 char_u *s2;
6333{
6334 if (s1 == NULL || s2 == NULL)
6335 return s1 == s2;
6336 return STRCMP(s1, s2) == 0;
6337}
6338
6339/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006340 * Add a from-to item to "gap". Used for REP and SAL items.
6341 * They are stored case-folded.
6342 */
6343 static void
6344add_fromto(spin, gap, from, to)
6345 spellinfo_T *spin;
6346 garray_T *gap;
6347 char_u *from;
6348 char_u *to;
6349{
6350 fromto_T *ftp;
6351 char_u word[MAXWLEN];
6352
6353 if (ga_grow(gap, 1) == OK)
6354 {
6355 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006356 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006357 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006358 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006359 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006360 ++gap->ga_len;
6361 }
6362}
6363
6364/*
6365 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6366 */
6367 static int
6368sal_to_bool(s)
6369 char_u *s;
6370{
6371 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6372}
6373
6374/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006375 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6376 * When "s" is NULL FALSE is returned.
6377 */
6378 static int
6379has_non_ascii(s)
6380 char_u *s;
6381{
6382 char_u *p;
6383
6384 if (s != NULL)
6385 for (p = s; *p != NUL; ++p)
6386 if (*p >= 128)
6387 return TRUE;
6388 return FALSE;
6389}
6390
6391/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006392 * Free the structure filled by spell_read_aff().
6393 */
6394 static void
6395spell_free_aff(aff)
6396 afffile_T *aff;
6397{
6398 hashtab_T *ht;
6399 hashitem_T *hi;
6400 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006401 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006402 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006403
6404 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006405
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006406 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006407 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6408 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006409 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006410 for (hi = ht->ht_array; todo > 0; ++hi)
6411 {
6412 if (!HASHITEM_EMPTY(hi))
6413 {
6414 --todo;
6415 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006416 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6417 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006418 }
6419 }
6420 if (ht == &aff->af_suff)
6421 break;
6422 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006423
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006424 hash_clear(&aff->af_pref);
6425 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006426 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006427}
6428
6429/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006430 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006431 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006432 */
6433 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006434spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006435 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006437 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006438{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006439 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006440 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006441 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006442 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006443 char_u store_afflist[MAXWLEN];
6444 int pfxlen;
6445 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006446 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006447 char_u *pc;
6448 char_u *w;
6449 int l;
6450 hash_T hash;
6451 hashitem_T *hi;
6452 FILE *fd;
6453 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006454 int non_ascii = 0;
6455 int retval = OK;
6456 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006457 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006458 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459
Bram Moolenaar51485f02005-06-04 21:55:20 +00006460 /*
6461 * Open the file.
6462 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006463 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006464 if (fd == NULL)
6465 {
6466 EMSG2(_(e_notopen), fname);
6467 return FAIL;
6468 }
6469
Bram Moolenaar51485f02005-06-04 21:55:20 +00006470 /* The hashtable is only used to detect duplicated words. */
6471 hash_init(&ht);
6472
Bram Moolenaar4770d092006-01-12 23:22:24 +00006473 vim_snprintf((char *)IObuff, IOSIZE,
6474 _("Reading dictionary file %s ..."), fname);
6475 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006476
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006477 /* start with a message for the first line */
6478 spin->si_msg_count = 999999;
6479
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006480 /* Read and ignore the first line: word count. */
6481 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006482 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006483 EMSG2(_("E760: No word count in %s"), fname);
6484
6485 /*
6486 * Read all the lines in the file one by one.
6487 * The words are converted to 'encoding' here, before being added to
6488 * the hashtable.
6489 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006490 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006491 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006492 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006493 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006494 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006495 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006496
Bram Moolenaar51485f02005-06-04 21:55:20 +00006497 /* Remove CR, LF and white space from the end. White space halfway
6498 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006499 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006500 while (l > 0 && line[l - 1] <= ' ')
6501 --l;
6502 if (l == 0)
6503 continue; /* empty line */
6504 line[l] = NUL;
6505
Bram Moolenaarb765d632005-06-07 21:00:02 +00006506#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006507 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006508 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006509 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006510 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006511 if (pc == NULL)
6512 {
6513 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6514 fname, lnum, line);
6515 continue;
6516 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 w = pc;
6518 }
6519 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006520#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006521 {
6522 pc = NULL;
6523 w = line;
6524 }
6525
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006526 /* Truncate the word at the "/", set "afflist" to what follows.
6527 * Replace "\/" by "/" and "\\" by "\". */
6528 afflist = NULL;
6529 for (p = w; *p != NUL; mb_ptr_adv(p))
6530 {
6531 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006532 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006533 else if (*p == '/')
6534 {
6535 *p = NUL;
6536 afflist = p + 1;
6537 break;
6538 }
6539 }
6540
6541 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6542 if (spin->si_ascii && has_non_ascii(w))
6543 {
6544 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006545 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006546 continue;
6547 }
6548
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006549 /* This takes time, print a message every 10000 words. */
6550 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006551 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006552 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006553 vim_snprintf((char *)message, sizeof(message),
6554 _("line %6d, word %6d - %s"),
6555 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6556 msg_start();
6557 msg_puts_long_attr(message, 0);
6558 msg_clr_eos();
6559 msg_didout = FALSE;
6560 msg_col = 0;
6561 out_flush();
6562 }
6563
Bram Moolenaar51485f02005-06-04 21:55:20 +00006564 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006565 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006566 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006567 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006568 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006569 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006570 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006571 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006572
Bram Moolenaar51485f02005-06-04 21:55:20 +00006573 hash = hash_hash(dw);
6574 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006576 {
6577 if (p_verbose > 0)
6578 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006579 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006580 else if (duplicate == 0)
6581 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6582 fname, lnum, dw);
6583 ++duplicate;
6584 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006585 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006586 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006587
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006588 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006589 store_afflist[0] = NUL;
6590 pfxlen = 0;
6591 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006592 if (afflist != NULL)
6593 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006594 /* Extract flags from the affix list. */
6595 flags |= get_affix_flags(affile, afflist);
6596
Bram Moolenaar6de68532005-08-24 22:08:48 +00006597 if (affile->af_needaffix != 0 && flag_in_afflist(
6598 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006599 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006600
6601 if (affile->af_pfxpostpone)
6602 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006603 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006604
Bram Moolenaar5195e452005-08-19 20:32:47 +00006605 if (spin->si_compflags != NULL)
6606 /* Need to store the list of compound flags with the word.
6607 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006608 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006609 }
6610
Bram Moolenaar51485f02005-06-04 21:55:20 +00006611 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006612 if (store_word(spin, dw, flags, spin->si_region,
6613 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006614 retval = FAIL;
6615
6616 if (afflist != NULL)
6617 {
6618 /* Find all matching suffixes and add the resulting words.
6619 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006620 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006621 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006622 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 retval = FAIL;
6624
6625 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006626 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006627 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006628 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006629 retval = FAIL;
6630 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006631
6632 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006633 }
6634
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006635 if (duplicate > 0)
6636 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006637 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006638 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6639 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006640 hash_clear(&ht);
6641
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006642 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006643 return retval;
6644}
6645
6646/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006647 * Check for affix flags in "afflist" that are turned into word flags.
6648 * Return WF_ flags.
6649 */
6650 static int
6651get_affix_flags(affile, afflist)
6652 afffile_T *affile;
6653 char_u *afflist;
6654{
6655 int flags = 0;
6656
6657 if (affile->af_keepcase != 0 && flag_in_afflist(
6658 affile->af_flagtype, afflist, affile->af_keepcase))
6659 flags |= WF_KEEPCAP | WF_FIXCAP;
6660 if (affile->af_rare != 0 && flag_in_afflist(
6661 affile->af_flagtype, afflist, affile->af_rare))
6662 flags |= WF_RARE;
6663 if (affile->af_bad != 0 && flag_in_afflist(
6664 affile->af_flagtype, afflist, affile->af_bad))
6665 flags |= WF_BANNED;
6666 if (affile->af_needcomp != 0 && flag_in_afflist(
6667 affile->af_flagtype, afflist, affile->af_needcomp))
6668 flags |= WF_NEEDCOMP;
6669 if (affile->af_comproot != 0 && flag_in_afflist(
6670 affile->af_flagtype, afflist, affile->af_comproot))
6671 flags |= WF_COMPROOT;
6672 if (affile->af_nosuggest != 0 && flag_in_afflist(
6673 affile->af_flagtype, afflist, affile->af_nosuggest))
6674 flags |= WF_NOSUGGEST;
6675 return flags;
6676}
6677
6678/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006679 * Get the list of prefix IDs from the affix list "afflist".
6680 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006681 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6682 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006683 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006684 static int
6685get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006686 afffile_T *affile;
6687 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006688 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006689{
6690 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006691 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006692 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006693 int id;
6694 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006695 hashitem_T *hi;
6696
Bram Moolenaar6de68532005-08-24 22:08:48 +00006697 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006698 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006699 prevp = p;
6700 if (get_affitem(affile->af_flagtype, &p) != 0)
6701 {
6702 /* A flag is a postponed prefix flag if it appears in "af_pref"
6703 * and it's ID is not zero. */
6704 vim_strncpy(key, prevp, p - prevp);
6705 hi = hash_find(&affile->af_pref, key);
6706 if (!HASHITEM_EMPTY(hi))
6707 {
6708 id = HI2AH(hi)->ah_newID;
6709 if (id != 0)
6710 store_afflist[cnt++] = id;
6711 }
6712 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006713 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006714 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006715 }
6716
Bram Moolenaar5195e452005-08-19 20:32:47 +00006717 store_afflist[cnt] = NUL;
6718 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006719}
6720
6721/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006722 * Get the list of compound IDs from the affix list "afflist" that are used
6723 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006724 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006725 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006726 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006727get_compflags(affile, afflist, store_afflist)
6728 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006729 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006730 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006731{
6732 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006733 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006734 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006735 char_u key[AH_KEY_LEN];
6736 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006737
Bram Moolenaar6de68532005-08-24 22:08:48 +00006738 for (p = afflist; *p != NUL; )
6739 {
6740 prevp = p;
6741 if (get_affitem(affile->af_flagtype, &p) != 0)
6742 {
6743 /* A flag is a compound flag if it appears in "af_comp". */
6744 vim_strncpy(key, prevp, p - prevp);
6745 hi = hash_find(&affile->af_comp, key);
6746 if (!HASHITEM_EMPTY(hi))
6747 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6748 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006749 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006750 ++p;
6751 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006752
Bram Moolenaar5195e452005-08-19 20:32:47 +00006753 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006754}
6755
6756/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757 * Apply affixes to a word and store the resulting words.
6758 * "ht" is the hashtable with affentry_T that need to be applied, either
6759 * prefixes or suffixes.
6760 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6761 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006762 *
6763 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006764 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006765 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006766store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006767 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006769 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006770 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006771 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006772 hashtab_T *ht;
6773 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006774 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006775 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006776 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006777 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6778 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006779{
6780 int todo;
6781 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006782 affheader_T *ah;
6783 affentry_T *ae;
6784 regmatch_T regmatch;
6785 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006786 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006787 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006788 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006789 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006790 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006791 int use_pfxlen;
6792 int need_affix;
6793 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006794 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006795 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006796 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006797
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006798 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006799 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006800 {
6801 if (!HASHITEM_EMPTY(hi))
6802 {
6803 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006804 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006805
Bram Moolenaar51485f02005-06-04 21:55:20 +00006806 /* Check that the affix combines, if required, and that the word
6807 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006808 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6809 && flag_in_afflist(affile->af_flagtype, afflist,
6810 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006811 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006812 /* Loop over all affix entries with this name. */
6813 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006814 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006815 /* Check the condition. It's not logical to match case
6816 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006817 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006818 * Another requirement from Myspell is that the chop
6819 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006820 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006821 * prefixes with a chop string and/or flags.
6822 * When a previously added affix had CIRCUMFIX this one
6823 * must have it too, if it had not then this one must not
6824 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006825 regmatch.regprog = ae->ae_prog;
6826 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006827 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006828 || ae->ae_chop != NULL
6829 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006830 && (ae->ae_chop == NULL
6831 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006832 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006833 || vim_regexec(&regmatch, word, (colnr_T)0))
6834 && (((condit & CONDIT_CFIX) == 0)
6835 == ((condit & CONDIT_AFF) == 0
6836 || ae->ae_flags == NULL
6837 || !flag_in_afflist(affile->af_flagtype,
6838 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006839 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006840 /* Match. Remove the chop and add the affix. */
6841 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006842 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006843 /* prefix: chop/add at the start of the word */
6844 if (ae->ae_add == NULL)
6845 *newword = NUL;
6846 else
6847 STRCPY(newword, ae->ae_add);
6848 p = word;
6849 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006850 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006851 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006852#ifdef FEAT_MBYTE
6853 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006854 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006855 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006856 for ( ; i > 0; --i)
6857 mb_ptr_adv(p);
6858 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006859 else
6860#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006861 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006862 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006863 STRCAT(newword, p);
6864 }
6865 else
6866 {
6867 /* suffix: chop/add at the end of the word */
6868 STRCPY(newword, word);
6869 if (ae->ae_chop != NULL)
6870 {
6871 /* Remove chop string. */
6872 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006874 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006875 mb_ptr_back(newword, p);
6876 *p = NUL;
6877 }
6878 if (ae->ae_add != NULL)
6879 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006880 }
6881
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006882 use_flags = flags;
6883 use_pfxlist = pfxlist;
6884 use_pfxlen = pfxlen;
6885 need_affix = FALSE;
6886 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6887 if (ae->ae_flags != NULL)
6888 {
6889 /* Extract flags from the affix list. */
6890 use_flags |= get_affix_flags(affile, ae->ae_flags);
6891
6892 if (affile->af_needaffix != 0 && flag_in_afflist(
6893 affile->af_flagtype, ae->ae_flags,
6894 affile->af_needaffix))
6895 need_affix = TRUE;
6896
6897 /* When there is a CIRCUMFIX flag the other affix
6898 * must also have it and we don't add the word
6899 * with one affix. */
6900 if (affile->af_circumfix != 0 && flag_in_afflist(
6901 affile->af_flagtype, ae->ae_flags,
6902 affile->af_circumfix))
6903 {
6904 use_condit |= CONDIT_CFIX;
6905 if ((condit & CONDIT_CFIX) == 0)
6906 need_affix = TRUE;
6907 }
6908
6909 if (affile->af_pfxpostpone
6910 || spin->si_compflags != NULL)
6911 {
6912 if (affile->af_pfxpostpone)
6913 /* Get prefix IDS from the affix list. */
6914 use_pfxlen = get_pfxlist(affile,
6915 ae->ae_flags, store_afflist);
6916 else
6917 use_pfxlen = 0;
6918 use_pfxlist = store_afflist;
6919
6920 /* Combine the prefix IDs. Avoid adding the
6921 * same ID twice. */
6922 for (i = 0; i < pfxlen; ++i)
6923 {
6924 for (j = 0; j < use_pfxlen; ++j)
6925 if (pfxlist[i] == use_pfxlist[j])
6926 break;
6927 if (j == use_pfxlen)
6928 use_pfxlist[use_pfxlen++] = pfxlist[i];
6929 }
6930
6931 if (spin->si_compflags != NULL)
6932 /* Get compound IDS from the affix list. */
6933 get_compflags(affile, ae->ae_flags,
6934 use_pfxlist + use_pfxlen);
6935
6936 /* Combine the list of compound flags.
6937 * Concatenate them to the prefix IDs list.
6938 * Avoid adding the same ID twice. */
6939 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6940 {
6941 for (j = use_pfxlen;
6942 use_pfxlist[j] != NUL; ++j)
6943 if (pfxlist[i] == use_pfxlist[j])
6944 break;
6945 if (use_pfxlist[j] == NUL)
6946 {
6947 use_pfxlist[j++] = pfxlist[i];
6948 use_pfxlist[j] = NUL;
6949 }
6950 }
6951 }
6952 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006953
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006954 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006955 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006956 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006957 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006958 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006959 use_pfxlist = pfx_pfxlist;
6960 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006961
6962 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006963 if (spin->si_prefroot != NULL
6964 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006965 {
6966 /* ... add a flag to indicate an affix was used. */
6967 use_flags |= WF_HAS_AFF;
6968
6969 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006970 * affixes is not allowed. But do use the
6971 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006972 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006973 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006974 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006975
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006976 /* When compounding is supported and there is no
6977 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6978 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006979 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006980 {
6981 if (xht != NULL)
6982 use_flags |= WF_NOCOMPAFT;
6983 else
6984 use_flags |= WF_NOCOMPBEF;
6985 }
6986
Bram Moolenaar51485f02005-06-04 21:55:20 +00006987 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006988 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006989 spin->si_region, use_pfxlist,
6990 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006991 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006992
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006993 /* When added a prefix or a first suffix and the affix
6994 * has flags may add a(nother) suffix. RECURSIVE! */
6995 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6996 if (store_aff_word(spin, newword, ae->ae_flags,
6997 affile, &affile->af_suff, xht,
6998 use_condit & (xht == NULL
6999 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007000 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007001 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007002
7003 /* When added a suffix and combining is allowed also
7004 * try adding a prefix additionally. Both for the
7005 * word flags and for the affix flags. RECURSIVE! */
7006 if (xht != NULL && ah->ah_combine)
7007 {
7008 if (store_aff_word(spin, newword,
7009 afflist, affile,
7010 xht, NULL, use_condit,
7011 use_flags, use_pfxlist,
7012 pfxlen) == FAIL
7013 || (ae->ae_flags != NULL
7014 && store_aff_word(spin, newword,
7015 ae->ae_flags, affile,
7016 xht, NULL, use_condit,
7017 use_flags, use_pfxlist,
7018 pfxlen) == FAIL))
7019 retval = FAIL;
7020 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007021 }
7022 }
7023 }
7024 }
7025 }
7026
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007027 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007028}
7029
7030/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007031 * Read a file with a list of words.
7032 */
7033 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007034spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007035 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007036 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007037{
7038 FILE *fd;
7039 long lnum = 0;
7040 char_u rline[MAXLINELEN];
7041 char_u *line;
7042 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007043 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007044 int l;
7045 int retval = OK;
7046 int did_word = FALSE;
7047 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007048 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007049 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007050
7051 /*
7052 * Open the file.
7053 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007054 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007055 if (fd == NULL)
7056 {
7057 EMSG2(_(e_notopen), fname);
7058 return FAIL;
7059 }
7060
Bram Moolenaar4770d092006-01-12 23:22:24 +00007061 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7062 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007063
7064 /*
7065 * Read all the lines in the file one by one.
7066 */
7067 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7068 {
7069 line_breakcheck();
7070 ++lnum;
7071
7072 /* Skip comment lines. */
7073 if (*rline == '#')
7074 continue;
7075
7076 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007077 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007078 while (l > 0 && rline[l - 1] <= ' ')
7079 --l;
7080 if (l == 0)
7081 continue; /* empty or blank line */
7082 rline[l] = NUL;
7083
Bram Moolenaar9c102382006-05-03 21:26:49 +00007084 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007085 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007086#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 if (spin->si_conv.vc_type != CONV_NONE)
7088 {
7089 pc = string_convert(&spin->si_conv, rline, NULL);
7090 if (pc == NULL)
7091 {
7092 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7093 fname, lnum, rline);
7094 continue;
7095 }
7096 line = pc;
7097 }
7098 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007099#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007100 {
7101 pc = NULL;
7102 line = rline;
7103 }
7104
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007105 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007106 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007107 ++line;
7108 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007109 {
7110 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007111 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7112 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007113 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007114 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7115 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007116 else
7117 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007118#ifdef FEAT_MBYTE
7119 char_u *enc;
7120
Bram Moolenaar51485f02005-06-04 21:55:20 +00007121 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007122 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007123 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007124 if (enc != NULL && !spin->si_ascii
7125 && convert_setup(&spin->si_conv, enc,
7126 p_enc) == FAIL)
7127 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007128 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007129 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007130 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007131#else
7132 smsg((char_u *)_("Conversion in %s not supported"), fname);
7133#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007134 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007135 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007136 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007137
Bram Moolenaar3982c542005-06-08 21:56:31 +00007138 if (STRNCMP(line, "regions=", 8) == 0)
7139 {
7140 if (spin->si_region_count > 1)
7141 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7142 fname, lnum, line);
7143 else
7144 {
7145 line += 8;
7146 if (STRLEN(line) > 16)
7147 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7148 fname, lnum, line);
7149 else
7150 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007151 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007152 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007153
7154 /* Adjust the mask for a word valid in all regions. */
7155 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007156 }
7157 }
7158 continue;
7159 }
7160
Bram Moolenaar7887d882005-07-01 22:33:52 +00007161 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7162 fname, lnum, line - 1);
7163 continue;
7164 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007165
Bram Moolenaar7887d882005-07-01 22:33:52 +00007166 flags = 0;
7167 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007168
Bram Moolenaar7887d882005-07-01 22:33:52 +00007169 /* Check for flags and region after a slash. */
7170 p = vim_strchr(line, '/');
7171 if (p != NULL)
7172 {
7173 *p++ = NUL;
7174 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007175 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007176 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007177 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007178 else if (*p == '!') /* Bad, bad, wicked word. */
7179 flags |= WF_BANNED;
7180 else if (*p == '?') /* Rare word. */
7181 flags |= WF_RARE;
7182 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007183 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007184 if ((flags & WF_REGION) == 0) /* first one */
7185 regionmask = 0;
7186 flags |= WF_REGION;
7187
7188 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007189 if (l > spin->si_region_count)
7190 {
7191 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007192 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007193 break;
7194 }
7195 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007196 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007197 else
7198 {
7199 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7200 fname, lnum, p);
7201 break;
7202 }
7203 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007204 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007205 }
7206
7207 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7208 if (spin->si_ascii && has_non_ascii(line))
7209 {
7210 ++non_ascii;
7211 continue;
7212 }
7213
7214 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007215 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007216 {
7217 retval = FAIL;
7218 break;
7219 }
7220 did_word = TRUE;
7221 }
7222
7223 vim_free(pc);
7224 fclose(fd);
7225
Bram Moolenaar4770d092006-01-12 23:22:24 +00007226 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007227 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007228 vim_snprintf((char *)IObuff, IOSIZE,
7229 _("Ignored %d words with non-ASCII characters"), non_ascii);
7230 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007231 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007232
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233 return retval;
7234}
7235
7236/*
7237 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007238 * This avoids calling free() for every little struct we use (and keeping
7239 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007240 * The memory is cleared to all zeros.
7241 * Returns NULL when out of memory.
7242 */
7243 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007244getroom(spin, len, align)
7245 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007246 size_t len; /* length needed */
7247 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007248{
7249 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007250 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007251
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007252 if (align && bl != NULL)
7253 /* Round size up for alignment. On some systems structures need to be
7254 * aligned to the size of a pointer (e.g., SPARC). */
7255 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7256 & ~(sizeof(char *) - 1);
7257
Bram Moolenaar51485f02005-06-04 21:55:20 +00007258 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7259 {
7260 /* Allocate a block of memory. This is not freed until much later. */
7261 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7262 if (bl == NULL)
7263 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007264 bl->sb_next = spin->si_blocks;
7265 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007266 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007267 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007268 }
7269
7270 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007271 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007272
7273 return p;
7274}
7275
7276/*
7277 * Make a copy of a string into memory allocated with getroom().
7278 */
7279 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007280getroom_save(spin, s)
7281 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007282 char_u *s;
7283{
7284 char_u *sc;
7285
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007286 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007287 if (sc != NULL)
7288 STRCPY(sc, s);
7289 return sc;
7290}
7291
7292
7293/*
7294 * Free the list of allocated sblock_T.
7295 */
7296 static void
7297free_blocks(bl)
7298 sblock_T *bl;
7299{
7300 sblock_T *next;
7301
7302 while (bl != NULL)
7303 {
7304 next = bl->sb_next;
7305 vim_free(bl);
7306 bl = next;
7307 }
7308}
7309
7310/*
7311 * Allocate the root of a word tree.
7312 */
7313 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007314wordtree_alloc(spin)
7315 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007316{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007317 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007318}
7319
7320/*
7321 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007322 * Always store it in the case-folded tree. For a keep-case word this is
7323 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7324 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007325 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007326 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7327 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007328 */
7329 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007330store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007331 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007332 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007333 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007334 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007335 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007336 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007337{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007338 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007339 int ct = captype(word, word + len);
7340 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007341 int res = OK;
7342 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007344 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007345 for (p = pfxlist; res == OK; ++p)
7346 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007347 if (!need_affix || (p != NULL && *p != NUL))
7348 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007349 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007350 if (p == NULL || *p == NUL)
7351 break;
7352 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007353 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007354
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007355 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007356 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007357 for (p = pfxlist; res == OK; ++p)
7358 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007359 if (!need_affix || (p != NULL && *p != NUL))
7360 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007361 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007362 if (p == NULL || *p == NUL)
7363 break;
7364 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007365 ++spin->si_keepwcount;
7366 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007367 return res;
7368}
7369
7370/*
7371 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007372 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007373 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007374 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007375 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007376 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007377tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007378 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007379 char_u *word;
7380 wordnode_T *root;
7381 int flags;
7382 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007383 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007384{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007385 wordnode_T *node = root;
7386 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007387 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007388 wordnode_T **prev = NULL;
7389 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007390
Bram Moolenaar51485f02005-06-04 21:55:20 +00007391 /* Add each byte of the word to the tree, including the NUL at the end. */
7392 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007393 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007394 /* When there is more than one reference to this node we need to make
7395 * a copy, so that we can modify it. Copy the whole list of siblings
7396 * (we don't optimize for a partly shared list of siblings). */
7397 if (node != NULL && node->wn_refs > 1)
7398 {
7399 --node->wn_refs;
7400 copyprev = prev;
7401 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7402 {
7403 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007404 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007405 if (np == NULL)
7406 return FAIL;
7407 np->wn_child = copyp->wn_child;
7408 if (np->wn_child != NULL)
7409 ++np->wn_child->wn_refs; /* child gets extra ref */
7410 np->wn_byte = copyp->wn_byte;
7411 if (np->wn_byte == NUL)
7412 {
7413 np->wn_flags = copyp->wn_flags;
7414 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007415 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007416 }
7417
7418 /* Link the new node in the list, there will be one ref. */
7419 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007420 if (copyprev != NULL)
7421 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007422 copyprev = &np->wn_sibling;
7423
7424 /* Let "node" point to the head of the copied list. */
7425 if (copyp == node)
7426 node = np;
7427 }
7428 }
7429
Bram Moolenaar51485f02005-06-04 21:55:20 +00007430 /* Look for the sibling that has the same character. They are sorted
7431 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007432 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007433 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007434 while (node != NULL
7435 && (node->wn_byte < word[i]
7436 || (node->wn_byte == NUL
7437 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007438 ? node->wn_affixID < (unsigned)affixID
7439 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007440 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007441 && (spin->si_sugtree
7442 ? (node->wn_region & 0xffff) < region
7443 : node->wn_affixID
7444 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007445 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007446 prev = &node->wn_sibling;
7447 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007448 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007449 if (node == NULL
7450 || node->wn_byte != word[i]
7451 || (word[i] == NUL
7452 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007453 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007454 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007455 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007456 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007457 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007458 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007459 if (np == NULL)
7460 return FAIL;
7461 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007462
7463 /* If "node" is NULL this is a new child or the end of the sibling
7464 * list: ref count is one. Otherwise use ref count of sibling and
7465 * make ref count of sibling one (matters when inserting in front
7466 * of the list of siblings). */
7467 if (node == NULL)
7468 np->wn_refs = 1;
7469 else
7470 {
7471 np->wn_refs = node->wn_refs;
7472 node->wn_refs = 1;
7473 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007474 *prev = np;
7475 np->wn_sibling = node;
7476 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007477 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007478
Bram Moolenaar51485f02005-06-04 21:55:20 +00007479 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007480 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007481 node->wn_flags = flags;
7482 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007483 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007484 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007485 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007486 prev = &node->wn_child;
7487 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007488 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007489#ifdef SPELL_PRINTTREE
7490 smsg("Added \"%s\"", word);
7491 spell_print_tree(root->wn_sibling);
7492#endif
7493
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007494 /* count nr of words added since last message */
7495 ++spin->si_msg_count;
7496
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007497 if (spin->si_compress_cnt > 1)
7498 {
7499 if (--spin->si_compress_cnt == 1)
7500 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007501 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007502 }
7503
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007504 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007505 * When we have allocated lots of memory we need to compress the word tree
7506 * to free up some room. But compression is slow, and we might actually
7507 * need that room, thus only compress in the following situations:
7508 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007509 * "compress_start" blocks.
7510 * 2. When compressed before and used "compress_inc" blocks before
7511 * adding "compress_added" words (si_compress_cnt > 1).
7512 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007513 * (si_compress_cnt == 1) and the number of free nodes drops below the
7514 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007515 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007516#ifndef SPELL_PRINTTREE
7517 if (spin->si_compress_cnt == 1
7518 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007519 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007520#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007521 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007522 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007523 * when the freed up room has been used and another "compress_inc"
7524 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007525 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007526 spin->si_blocks_cnt -= compress_inc;
7527 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007528
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007529 if (spin->si_verbose)
7530 {
7531 msg_start();
7532 msg_puts((char_u *)_(msg_compressing));
7533 msg_clr_eos();
7534 msg_didout = FALSE;
7535 msg_col = 0;
7536 out_flush();
7537 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007538
7539 /* Compress both trees. Either they both have many nodes, which makes
7540 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007541 * compression goes fast. But when filling the souldfold word tree
7542 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007543 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007544 if (affixID >= 0)
7545 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007546 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007547
7548 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007549}
7550
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007551/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007552 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7553 * Sets "sps_flags".
7554 */
7555 int
7556spell_check_msm()
7557{
7558 char_u *p = p_msm;
7559 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007560 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007561 long added = 0;
7562
7563 if (!VIM_ISDIGIT(*p))
7564 return FAIL;
7565 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7566 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7567 if (*p != ',')
7568 return FAIL;
7569 ++p;
7570 if (!VIM_ISDIGIT(*p))
7571 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007572 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007573 if (*p != ',')
7574 return FAIL;
7575 ++p;
7576 if (!VIM_ISDIGIT(*p))
7577 return FAIL;
7578 added = getdigits(&p) * 1024;
7579 if (*p != NUL)
7580 return FAIL;
7581
Bram Moolenaar89d40322006-08-29 15:30:07 +00007582 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007583 return FAIL;
7584
7585 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007586 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007587 compress_added = added;
7588 return OK;
7589}
7590
7591
7592/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007593 * Get a wordnode_T, either from the list of previously freed nodes or
7594 * allocate a new one.
7595 */
7596 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007597get_wordnode(spin)
7598 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007599{
7600 wordnode_T *n;
7601
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007602 if (spin->si_first_free == NULL)
7603 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007604 else
7605 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007606 n = spin->si_first_free;
7607 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007608 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007609 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007610 }
7611#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007612 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007613#endif
7614 return n;
7615}
7616
7617/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007618 * Decrement the reference count on a node (which is the head of a list of
7619 * siblings). If the reference count becomes zero free the node and its
7620 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007621 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007622 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007623 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007624deref_wordnode(spin, node)
7625 spellinfo_T *spin;
7626 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007627{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007628 wordnode_T *np;
7629 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007630
7631 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007632 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007633 for (np = node; np != NULL; np = np->wn_sibling)
7634 {
7635 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007636 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007637 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007638 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007639 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007640 ++cnt; /* length field */
7641 }
7642 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007643}
7644
7645/*
7646 * Free a wordnode_T for re-use later.
7647 * Only the "wn_child" field becomes invalid.
7648 */
7649 static void
7650free_wordnode(spin, n)
7651 spellinfo_T *spin;
7652 wordnode_T *n;
7653{
7654 n->wn_child = spin->si_first_free;
7655 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007656 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007657}
7658
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007659/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007660 * Compress a tree: find tails that are identical and can be shared.
7661 */
7662 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007663wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007664 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007665 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007666{
7667 hashtab_T ht;
7668 int n;
7669 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007670 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007671
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007672 /* Skip the root itself, it's not actually used. The first sibling is the
7673 * start of the tree. */
7674 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007675 {
7676 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007677 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007678
7679#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007680 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007681#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007682 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007683 if (tot > 1000000)
7684 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007685 else if (tot == 0)
7686 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007687 else
7688 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007689 vim_snprintf((char *)IObuff, IOSIZE,
7690 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7691 n, tot, tot - n, perc);
7692 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007693 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007694#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007695 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007696#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007697 hash_clear(&ht);
7698 }
7699}
7700
7701/*
7702 * Compress a node, its siblings and its children, depth first.
7703 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007704 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007705 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007706node_compress(spin, node, ht, tot)
7707 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007708 wordnode_T *node;
7709 hashtab_T *ht;
7710 int *tot; /* total count of nodes before compressing,
7711 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007712{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007713 wordnode_T *np;
7714 wordnode_T *tp;
7715 wordnode_T *child;
7716 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007717 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007718 int len = 0;
7719 unsigned nr, n;
7720 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007721
Bram Moolenaar51485f02005-06-04 21:55:20 +00007722 /*
7723 * Go through the list of siblings. Compress each child and then try
7724 * finding an identical child to replace it.
7725 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007726 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007727 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007728 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007729 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007730 ++len;
7731 if ((child = np->wn_child) != NULL)
7732 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007733 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007734 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007735
7736 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007737 hash = hash_hash(child->wn_u1.hashkey);
7738 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007739 if (!HASHITEM_EMPTY(hi))
7740 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007741 /* There are children we encountered before with a hash value
7742 * identical to the current child. Now check if there is one
7743 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007744 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007745 if (node_equal(child, tp))
7746 {
7747 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007748 * current one. This means the current child and all
7749 * its siblings is unlinked from the tree. */
7750 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007751 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007752 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007753 break;
7754 }
7755 if (tp == NULL)
7756 {
7757 /* No other child with this hash value equals the child of
7758 * the node, add it to the linked list after the first
7759 * item. */
7760 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007761 child->wn_u2.next = tp->wn_u2.next;
7762 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007763 }
7764 }
7765 else
7766 /* No other child has this hash value, add it to the
7767 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007768 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007769 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007770 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007771 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007772
7773 /*
7774 * Make a hash key for the node and its siblings, so that we can quickly
7775 * find a lookalike node. This must be done after compressing the sibling
7776 * list, otherwise the hash key would become invalid by the compression.
7777 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007778 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007779 nr = 0;
7780 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007781 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007782 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007783 /* end node: use wn_flags, wn_region and wn_affixID */
7784 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007785 else
7786 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007787 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007788 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007789 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007790
7791 /* Avoid NUL bytes, it terminates the hash key. */
7792 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007793 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007794 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007795 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007796 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007797 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007798 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007799 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7800 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007801
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007802 /* Check for CTRL-C pressed now and then. */
7803 fast_breakcheck();
7804
Bram Moolenaar51485f02005-06-04 21:55:20 +00007805 return compressed;
7806}
7807
7808/*
7809 * Return TRUE when two nodes have identical siblings and children.
7810 */
7811 static int
7812node_equal(n1, n2)
7813 wordnode_T *n1;
7814 wordnode_T *n2;
7815{
7816 wordnode_T *p1;
7817 wordnode_T *p2;
7818
7819 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7820 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7821 if (p1->wn_byte != p2->wn_byte
7822 || (p1->wn_byte == NUL
7823 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007824 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007825 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007826 : (p1->wn_child != p2->wn_child)))
7827 break;
7828
7829 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007830}
7831
7832/*
7833 * Write a number to file "fd", MSB first, in "len" bytes.
7834 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007835 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007836put_bytes(fd, nr, len)
7837 FILE *fd;
7838 long_u nr;
7839 int len;
7840{
7841 int i;
7842
7843 for (i = len - 1; i >= 0; --i)
7844 putc((int)(nr >> (i * 8)), fd);
7845}
7846
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007847#ifdef _MSC_VER
7848# if (_MSC_VER <= 1200)
7849/* This line is required for VC6 without the service pack. Also see the
7850 * matching #pragma below. */
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007851 # pragma optimize("", off)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007852# endif
7853#endif
7854
Bram Moolenaar4770d092006-01-12 23:22:24 +00007855/*
7856 * Write spin->si_sugtime to file "fd".
7857 */
7858 static void
7859put_sugtime(spin, fd)
7860 spellinfo_T *spin;
7861 FILE *fd;
7862{
7863 int c;
7864 int i;
7865
7866 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7867 * can't use put_bytes() here. */
7868 for (i = 7; i >= 0; --i)
7869 if (i + 1 > sizeof(time_t))
7870 /* ">>" doesn't work well when shifting more bits than avail */
7871 putc(0, fd);
7872 else
7873 {
7874 c = (unsigned)spin->si_sugtime >> (i * 8);
7875 putc(c, fd);
7876 }
7877}
7878
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007879#ifdef _MSC_VER
7880# if (_MSC_VER <= 1200)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007881 # pragma optimize("", on)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007882# endif
7883#endif
7884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885static int
7886#ifdef __BORLANDC__
7887_RTLENTRYF
7888#endif
7889rep_compare __ARGS((const void *s1, const void *s2));
7890
7891/*
7892 * Function given to qsort() to sort the REP items on "from" string.
7893 */
7894 static int
7895#ifdef __BORLANDC__
7896_RTLENTRYF
7897#endif
7898rep_compare(s1, s2)
7899 const void *s1;
7900 const void *s2;
7901{
7902 fromto_T *p1 = (fromto_T *)s1;
7903 fromto_T *p2 = (fromto_T *)s2;
7904
7905 return STRCMP(p1->ft_from, p2->ft_from);
7906}
7907
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007908/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007909 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007910 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007911 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007912 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007913write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007914 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007915 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007916{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007917 FILE *fd;
7918 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007919 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007920 wordnode_T *tree;
7921 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007922 int i;
7923 int l;
7924 garray_T *gap;
7925 fromto_T *ftp;
7926 char_u *p;
7927 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007928 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007929 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007930 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007931
Bram Moolenaarb765d632005-06-07 21:00:02 +00007932 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007933 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007934 {
7935 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007936 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007937 }
7938
Bram Moolenaar5195e452005-08-19 20:32:47 +00007939 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007940 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007941 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007942 if (fwv != (size_t)1)
7943 /* Catch first write error, don't try writing more. */
7944 goto theend;
7945
Bram Moolenaar5195e452005-08-19 20:32:47 +00007946 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007947
Bram Moolenaar5195e452005-08-19 20:32:47 +00007948 /*
7949 * <SECTIONS>: <section> ... <sectionend>
7950 */
7951
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007952 /* SN_INFO: <infotext> */
7953 if (spin->si_info != NULL)
7954 {
7955 putc(SN_INFO, fd); /* <sectionID> */
7956 putc(0, fd); /* <sectionflags> */
7957
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007958 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007959 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007960 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007961 }
7962
Bram Moolenaar5195e452005-08-19 20:32:47 +00007963 /* SN_REGION: <regionname> ...
7964 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007965 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007966 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007967 putc(SN_REGION, fd); /* <sectionID> */
7968 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7969 l = spin->si_region_count * 2;
7970 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007971 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007972 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007973 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007974 }
7975 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007976 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007977
Bram Moolenaar5195e452005-08-19 20:32:47 +00007978 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7979 *
7980 * The table with character flags and the table for case folding.
7981 * This makes sure the same characters are recognized as word characters
7982 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007983 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007984 * 'encoding'.
7985 * Also skip this for an .add.spl file, the main spell file must contain
7986 * the table (avoids that it conflicts). File is shorter too.
7987 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007988 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007989 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007990 char_u folchars[128 * 8];
7991 int flags;
7992
Bram Moolenaard12a1322005-08-21 22:08:24 +00007993 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007994 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7995
7996 /* Form the <folchars> string first, we need to know its length. */
7997 l = 0;
7998 for (i = 128; i < 256; ++i)
7999 {
8000#ifdef FEAT_MBYTE
8001 if (has_mbyte)
8002 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8003 else
8004#endif
8005 folchars[l++] = spelltab.st_fold[i];
8006 }
8007 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8008
8009 fputc(128, fd); /* <charflagslen> */
8010 for (i = 128; i < 256; ++i)
8011 {
8012 flags = 0;
8013 if (spelltab.st_isw[i])
8014 flags |= CF_WORD;
8015 if (spelltab.st_isu[i])
8016 flags |= CF_UPPER;
8017 fputc(flags, fd); /* <charflags> */
8018 }
8019
8020 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008021 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008022 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008023
Bram Moolenaar5195e452005-08-19 20:32:47 +00008024 /* SN_MIDWORD: <midword> */
8025 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008026 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008027 putc(SN_MIDWORD, fd); /* <sectionID> */
8028 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8029
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008030 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008031 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008032 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8033 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008034 }
8035
Bram Moolenaar5195e452005-08-19 20:32:47 +00008036 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8037 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008038 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008039 putc(SN_PREFCOND, fd); /* <sectionID> */
8040 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8041
8042 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8043 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8044
8045 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008046 }
8047
Bram Moolenaar5195e452005-08-19 20:32:47 +00008048 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008049 * SN_SAL: <salflags> <salcount> <sal> ...
8050 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008051
Bram Moolenaar5195e452005-08-19 20:32:47 +00008052 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008053 * round 2: SN_SAL section (unless SN_SOFO is used)
8054 * round 3: SN_REPSAL section */
8055 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008056 {
8057 if (round == 1)
8058 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008059 else if (round == 2)
8060 {
8061 /* Don't write SN_SAL when using a SN_SOFO section */
8062 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8063 continue;
8064 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008067 gap = &spin->si_repsal;
8068
8069 /* Don't write the section if there are no items. */
8070 if (gap->ga_len == 0)
8071 continue;
8072
8073 /* Sort the REP/REPSAL items. */
8074 if (round != 2)
8075 qsort(gap->ga_data, (size_t)gap->ga_len,
8076 sizeof(fromto_T), rep_compare);
8077
8078 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8079 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008080
Bram Moolenaar5195e452005-08-19 20:32:47 +00008081 /* This is for making suggestions, section is not required. */
8082 putc(0, fd); /* <sectionflags> */
8083
8084 /* Compute the length of what follows. */
8085 l = 2; /* count <repcount> or <salcount> */
8086 for (i = 0; i < gap->ga_len; ++i)
8087 {
8088 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008089 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8090 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008091 }
8092 if (round == 2)
8093 ++l; /* count <salflags> */
8094 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8095
8096 if (round == 2)
8097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008098 i = 0;
8099 if (spin->si_followup)
8100 i |= SAL_F0LLOWUP;
8101 if (spin->si_collapse)
8102 i |= SAL_COLLAPSE;
8103 if (spin->si_rem_accents)
8104 i |= SAL_REM_ACCENTS;
8105 putc(i, fd); /* <salflags> */
8106 }
8107
8108 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8109 for (i = 0; i < gap->ga_len; ++i)
8110 {
8111 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8112 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8113 ftp = &((fromto_T *)gap->ga_data)[i];
8114 for (rr = 1; rr <= 2; ++rr)
8115 {
8116 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008117 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008119 if (l > 0)
8120 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008121 }
8122 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008124 }
8125
Bram Moolenaar5195e452005-08-19 20:32:47 +00008126 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8127 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008128 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8129 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008130 putc(SN_SOFO, fd); /* <sectionID> */
8131 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008132
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008133 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008134 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8135 /* <sectionlen> */
8136
8137 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008138 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008140 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008141 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008142 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008143 }
8144
Bram Moolenaar4770d092006-01-12 23:22:24 +00008145 /* SN_WORDS: <word> ...
8146 * This is for making suggestions, section is not required. */
8147 if (spin->si_commonwords.ht_used > 0)
8148 {
8149 putc(SN_WORDS, fd); /* <sectionID> */
8150 putc(0, fd); /* <sectionflags> */
8151
8152 /* round 1: count the bytes
8153 * round 2: write the bytes */
8154 for (round = 1; round <= 2; ++round)
8155 {
8156 int todo;
8157 int len = 0;
8158 hashitem_T *hi;
8159
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008160 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008161 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8162 if (!HASHITEM_EMPTY(hi))
8163 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008164 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008165 len += l;
8166 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008167 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008168 --todo;
8169 }
8170 if (round == 1)
8171 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8172 }
8173 }
8174
Bram Moolenaar5195e452005-08-19 20:32:47 +00008175 /* SN_MAP: <mapstr>
8176 * This is for making suggestions, section is not required. */
8177 if (spin->si_map.ga_len > 0)
8178 {
8179 putc(SN_MAP, fd); /* <sectionID> */
8180 putc(0, fd); /* <sectionflags> */
8181 l = spin->si_map.ga_len;
8182 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008183 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008184 /* <mapstr> */
8185 }
8186
Bram Moolenaar4770d092006-01-12 23:22:24 +00008187 /* SN_SUGFILE: <timestamp>
8188 * This is used to notify that a .sug file may be available and at the
8189 * same time allows for checking that a .sug file that is found matches
8190 * with this .spl file. That's because the word numbers must be exactly
8191 * right. */
8192 if (!spin->si_nosugfile
8193 && (spin->si_sal.ga_len > 0
8194 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8195 {
8196 putc(SN_SUGFILE, fd); /* <sectionID> */
8197 putc(0, fd); /* <sectionflags> */
8198 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8199
8200 /* Set si_sugtime and write it to the file. */
8201 spin->si_sugtime = time(NULL);
8202 put_sugtime(spin, fd); /* <timestamp> */
8203 }
8204
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008205 /* SN_NOSPLITSUGS: nothing
8206 * This is used to notify that no suggestions with word splits are to be
8207 * made. */
8208 if (spin->si_nosplitsugs)
8209 {
8210 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8211 putc(0, fd); /* <sectionflags> */
8212 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8213 }
8214
Bram Moolenaar5195e452005-08-19 20:32:47 +00008215 /* SN_COMPOUND: compound info.
8216 * We don't mark it required, when not supported all compound words will
8217 * be bad words. */
8218 if (spin->si_compflags != NULL)
8219 {
8220 putc(SN_COMPOUND, fd); /* <sectionID> */
8221 putc(0, fd); /* <sectionflags> */
8222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008223 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008224 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008225 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008226 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8227
Bram Moolenaar5195e452005-08-19 20:32:47 +00008228 putc(spin->si_compmax, fd); /* <compmax> */
8229 putc(spin->si_compminlen, fd); /* <compminlen> */
8230 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008231 putc(0, fd); /* for Vim 7.0b compatibility */
8232 putc(spin->si_compoptions, fd); /* <compoptions> */
8233 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8234 /* <comppatcount> */
8235 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8236 {
8237 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008238 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008239 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8240 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008241 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008242 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008243 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008244 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008245 }
8246
Bram Moolenaar78622822005-08-23 21:00:13 +00008247 /* SN_NOBREAK: NOBREAK flag */
8248 if (spin->si_nobreak)
8249 {
8250 putc(SN_NOBREAK, fd); /* <sectionID> */
8251 putc(0, fd); /* <sectionflags> */
8252
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008253 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008254 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8255 }
8256
Bram Moolenaar5195e452005-08-19 20:32:47 +00008257 /* SN_SYLLABLE: syllable info.
8258 * We don't mark it required, when not supported syllables will not be
8259 * counted. */
8260 if (spin->si_syllable != NULL)
8261 {
8262 putc(SN_SYLLABLE, fd); /* <sectionID> */
8263 putc(0, fd); /* <sectionflags> */
8264
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008265 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008266 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008267 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8268 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008269 }
8270
8271 /* end of <SECTIONS> */
8272 putc(SN_END, fd); /* <sectionend> */
8273
Bram Moolenaar50cde822005-06-05 21:54:54 +00008274
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008275 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008276 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008277 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008278 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008279 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008280 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008281 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008282 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008283 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008284 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008285 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008286 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008287
Bram Moolenaar0c405862005-06-22 22:26:26 +00008288 /* Clear the index and wnode fields in the tree. */
8289 clear_node(tree);
8290
Bram Moolenaar51485f02005-06-04 21:55:20 +00008291 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008292 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008293 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008294 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008295
Bram Moolenaar51485f02005-06-04 21:55:20 +00008296 /* number of nodes in 4 bytes */
8297 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008298 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008299
Bram Moolenaar51485f02005-06-04 21:55:20 +00008300 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008301 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008302 }
8303
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008304 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008305 if (putc(0, fd) == EOF)
8306 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008307theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008308 if (fclose(fd) == EOF)
8309 retval = FAIL;
8310
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008311 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008312 retval = FAIL;
8313 if (retval == FAIL)
8314 EMSG(_(e_write));
8315
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008316 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008317}
8318
8319/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008320 * Clear the index and wnode fields of "node", it siblings and its
8321 * children. This is needed because they are a union with other items to save
8322 * space.
8323 */
8324 static void
8325clear_node(node)
8326 wordnode_T *node;
8327{
8328 wordnode_T *np;
8329
8330 if (node != NULL)
8331 for (np = node; np != NULL; np = np->wn_sibling)
8332 {
8333 np->wn_u1.index = 0;
8334 np->wn_u2.wnode = NULL;
8335
8336 if (np->wn_byte != NUL)
8337 clear_node(np->wn_child);
8338 }
8339}
8340
8341
8342/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008343 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008344 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008345 * This first writes the list of possible bytes (siblings). Then for each
8346 * byte recursively write the children.
8347 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008348 * NOTE: The code here must match the code in read_tree_node(), since
8349 * assumptions are made about the indexes (so that we don't have to write them
8350 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008351 *
8352 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008353 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008354 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008355put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008356 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008357 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008358 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008359 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008360 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008361{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008362 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008363 int siblingcount = 0;
8364 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008365 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008366
Bram Moolenaar51485f02005-06-04 21:55:20 +00008367 /* If "node" is zero the tree is empty. */
8368 if (node == NULL)
8369 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008370
Bram Moolenaar51485f02005-06-04 21:55:20 +00008371 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008372 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008373
8374 /* Count the number of siblings. */
8375 for (np = node; np != NULL; np = np->wn_sibling)
8376 ++siblingcount;
8377
8378 /* Write the sibling count. */
8379 if (fd != NULL)
8380 putc(siblingcount, fd); /* <siblingcount> */
8381
8382 /* Write each sibling byte and optionally extra info. */
8383 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008384 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008385 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008386 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008387 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008388 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008389 /* For a NUL byte (end of word) write the flags etc. */
8390 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008391 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008392 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008393 * associated condition nr (stored in wn_region). The
8394 * byte value is misused to store the "rare" and "not
8395 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008396 if (np->wn_flags == (short_u)PFX_FLAGS)
8397 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008398 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008399 {
8400 putc(BY_FLAGS, fd); /* <byte> */
8401 putc(np->wn_flags, fd); /* <pflags> */
8402 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008403 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008404 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008405 }
8406 else
8407 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008408 /* For word trees we write the flag/region items. */
8409 flags = np->wn_flags;
8410 if (regionmask != 0 && np->wn_region != regionmask)
8411 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008412 if (np->wn_affixID != 0)
8413 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008414 if (flags == 0)
8415 {
8416 /* word without flags or region */
8417 putc(BY_NOFLAGS, fd); /* <byte> */
8418 }
8419 else
8420 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008421 if (np->wn_flags >= 0x100)
8422 {
8423 putc(BY_FLAGS2, fd); /* <byte> */
8424 putc(flags, fd); /* <flags> */
8425 putc((unsigned)flags >> 8, fd); /* <flags2> */
8426 }
8427 else
8428 {
8429 putc(BY_FLAGS, fd); /* <byte> */
8430 putc(flags, fd); /* <flags> */
8431 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008432 if (flags & WF_REGION)
8433 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008434 if (flags & WF_AFX)
8435 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008436 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008437 }
8438 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008439 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008440 else
8441 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008442 if (np->wn_child->wn_u1.index != 0
8443 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008444 {
8445 /* The child is written elsewhere, write the reference. */
8446 if (fd != NULL)
8447 {
8448 putc(BY_INDEX, fd); /* <byte> */
8449 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008450 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008451 }
8452 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008453 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008454 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008455 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008456
Bram Moolenaar51485f02005-06-04 21:55:20 +00008457 if (fd != NULL)
8458 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8459 {
8460 EMSG(_(e_write));
8461 return 0;
8462 }
8463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008464 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008465
8466 /* Space used in the array when reading: one for each sibling and one for
8467 * the count. */
8468 newindex += siblingcount + 1;
8469
8470 /* Recursively dump the children of each sibling. */
8471 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008472 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8473 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008474 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008475
8476 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008477}
8478
8479
8480/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008481 * ":mkspell [-ascii] outfile infile ..."
8482 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008483 */
8484 void
8485ex_mkspell(eap)
8486 exarg_T *eap;
8487{
8488 int fcount;
8489 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008490 char_u *arg = eap->arg;
8491 int ascii = FALSE;
8492
8493 if (STRNCMP(arg, "-ascii", 6) == 0)
8494 {
8495 ascii = TRUE;
8496 arg = skipwhite(arg + 6);
8497 }
8498
8499 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8500 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008502 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008503 FreeWild(fcount, fnames);
8504 }
8505}
8506
8507/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008508 * Create the .sug file.
8509 * Uses the soundfold info in "spin".
8510 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8511 */
8512 static void
8513spell_make_sugfile(spin, wfname)
8514 spellinfo_T *spin;
8515 char_u *wfname;
8516{
8517 char_u fname[MAXPATHL];
8518 int len;
8519 slang_T *slang;
8520 int free_slang = FALSE;
8521
8522 /*
8523 * Read back the .spl file that was written. This fills the required
8524 * info for soundfolding. This also uses less memory than the
8525 * pointer-linked version of the trie. And it avoids having two versions
8526 * of the code for the soundfolding stuff.
8527 * It might have been done already by spell_reload_one().
8528 */
8529 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8530 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8531 break;
8532 if (slang == NULL)
8533 {
8534 spell_message(spin, (char_u *)_("Reading back spell file..."));
8535 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8536 if (slang == NULL)
8537 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008538 free_slang = TRUE;
8539 }
8540
8541 /*
8542 * Clear the info in "spin" that is used.
8543 */
8544 spin->si_blocks = NULL;
8545 spin->si_blocks_cnt = 0;
8546 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8547 spin->si_free_count = 0;
8548 spin->si_first_free = NULL;
8549 spin->si_foldwcount = 0;
8550
8551 /*
8552 * Go through the trie of good words, soundfold each word and add it to
8553 * the soundfold trie.
8554 */
8555 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8556 if (sug_filltree(spin, slang) == FAIL)
8557 goto theend;
8558
8559 /*
8560 * Create the table which links each soundfold word with a list of the
8561 * good words it may come from. Creates buffer "spin->si_spellbuf".
8562 * This also removes the wordnr from the NUL byte entries to make
8563 * compression possible.
8564 */
8565 if (sug_maketable(spin) == FAIL)
8566 goto theend;
8567
8568 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8569 (long)spin->si_spellbuf->b_ml.ml_line_count);
8570
8571 /*
8572 * Compress the soundfold trie.
8573 */
8574 spell_message(spin, (char_u *)_(msg_compressing));
8575 wordtree_compress(spin, spin->si_foldroot);
8576
8577 /*
8578 * Write the .sug file.
8579 * Make the file name by changing ".spl" to ".sug".
8580 */
8581 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008582 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008583 fname[len - 2] = 'u';
8584 fname[len - 1] = 'g';
8585 sug_write(spin, fname);
8586
8587theend:
8588 if (free_slang)
8589 slang_free(slang);
8590 free_blocks(spin->si_blocks);
8591 close_spellbuf(spin->si_spellbuf);
8592}
8593
8594/*
8595 * Build the soundfold trie for language "slang".
8596 */
8597 static int
8598sug_filltree(spin, slang)
8599 spellinfo_T *spin;
8600 slang_T *slang;
8601{
8602 char_u *byts;
8603 idx_T *idxs;
8604 int depth;
8605 idx_T arridx[MAXWLEN];
8606 int curi[MAXWLEN];
8607 char_u tword[MAXWLEN];
8608 char_u tsalword[MAXWLEN];
8609 int c;
8610 idx_T n;
8611 unsigned words_done = 0;
8612 int wordcount[MAXWLEN];
8613
8614 /* We use si_foldroot for the souldfolded trie. */
8615 spin->si_foldroot = wordtree_alloc(spin);
8616 if (spin->si_foldroot == NULL)
8617 return FAIL;
8618
8619 /* let tree_add_word() know we're adding to the soundfolded tree */
8620 spin->si_sugtree = TRUE;
8621
8622 /*
8623 * Go through the whole case-folded tree, soundfold each word and put it
8624 * in the trie.
8625 */
8626 byts = slang->sl_fbyts;
8627 idxs = slang->sl_fidxs;
8628
8629 arridx[0] = 0;
8630 curi[0] = 1;
8631 wordcount[0] = 0;
8632
8633 depth = 0;
8634 while (depth >= 0 && !got_int)
8635 {
8636 if (curi[depth] > byts[arridx[depth]])
8637 {
8638 /* Done all bytes at this node, go up one level. */
8639 idxs[arridx[depth]] = wordcount[depth];
8640 if (depth > 0)
8641 wordcount[depth - 1] += wordcount[depth];
8642
8643 --depth;
8644 line_breakcheck();
8645 }
8646 else
8647 {
8648
8649 /* Do one more byte at this node. */
8650 n = arridx[depth] + curi[depth];
8651 ++curi[depth];
8652
8653 c = byts[n];
8654 if (c == 0)
8655 {
8656 /* Sound-fold the word. */
8657 tword[depth] = NUL;
8658 spell_soundfold(slang, tword, TRUE, tsalword);
8659
8660 /* We use the "flags" field for the MSB of the wordnr,
8661 * "region" for the LSB of the wordnr. */
8662 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8663 words_done >> 16, words_done & 0xffff,
8664 0) == FAIL)
8665 return FAIL;
8666
8667 ++words_done;
8668 ++wordcount[depth];
8669
8670 /* Reset the block count each time to avoid compression
8671 * kicking in. */
8672 spin->si_blocks_cnt = 0;
8673
8674 /* Skip over any other NUL bytes (same word with different
8675 * flags). */
8676 while (byts[n + 1] == 0)
8677 {
8678 ++n;
8679 ++curi[depth];
8680 }
8681 }
8682 else
8683 {
8684 /* Normal char, go one level deeper. */
8685 tword[depth++] = c;
8686 arridx[depth] = idxs[n];
8687 curi[depth] = 1;
8688 wordcount[depth] = 0;
8689 }
8690 }
8691 }
8692
8693 smsg((char_u *)_("Total number of words: %d"), words_done);
8694
8695 return OK;
8696}
8697
8698/*
8699 * Make the table that links each word in the soundfold trie to the words it
8700 * can be produced from.
8701 * This is not unlike lines in a file, thus use a memfile to be able to access
8702 * the table efficiently.
8703 * Returns FAIL when out of memory.
8704 */
8705 static int
8706sug_maketable(spin)
8707 spellinfo_T *spin;
8708{
8709 garray_T ga;
8710 int res = OK;
8711
8712 /* Allocate a buffer, open a memline for it and create the swap file
8713 * (uses a temp file, not a .swp file). */
8714 spin->si_spellbuf = open_spellbuf();
8715 if (spin->si_spellbuf == NULL)
8716 return FAIL;
8717
8718 /* Use a buffer to store the line info, avoids allocating many small
8719 * pieces of memory. */
8720 ga_init2(&ga, 1, 100);
8721
8722 /* recursively go through the tree */
8723 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8724 res = FAIL;
8725
8726 ga_clear(&ga);
8727 return res;
8728}
8729
8730/*
8731 * Fill the table for one node and its children.
8732 * Returns the wordnr at the start of the node.
8733 * Returns -1 when out of memory.
8734 */
8735 static int
8736sug_filltable(spin, node, startwordnr, gap)
8737 spellinfo_T *spin;
8738 wordnode_T *node;
8739 int startwordnr;
8740 garray_T *gap; /* place to store line of numbers */
8741{
8742 wordnode_T *p, *np;
8743 int wordnr = startwordnr;
8744 int nr;
8745 int prev_nr;
8746
8747 for (p = node; p != NULL; p = p->wn_sibling)
8748 {
8749 if (p->wn_byte == NUL)
8750 {
8751 gap->ga_len = 0;
8752 prev_nr = 0;
8753 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8754 {
8755 if (ga_grow(gap, 10) == FAIL)
8756 return -1;
8757
8758 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8759 /* Compute the offset from the previous nr and store the
8760 * offset in a way that it takes a minimum number of bytes.
8761 * It's a bit like utf-8, but without the need to mark
8762 * following bytes. */
8763 nr -= prev_nr;
8764 prev_nr += nr;
8765 gap->ga_len += offset2bytes(nr,
8766 (char_u *)gap->ga_data + gap->ga_len);
8767 }
8768
8769 /* add the NUL byte */
8770 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8771
8772 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8773 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8774 return -1;
8775 ++wordnr;
8776
8777 /* Remove extra NUL entries, we no longer need them. We don't
8778 * bother freeing the nodes, the won't be reused anyway. */
8779 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8780 p->wn_sibling = p->wn_sibling->wn_sibling;
8781
8782 /* Clear the flags on the remaining NUL node, so that compression
8783 * works a lot better. */
8784 p->wn_flags = 0;
8785 p->wn_region = 0;
8786 }
8787 else
8788 {
8789 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8790 if (wordnr == -1)
8791 return -1;
8792 }
8793 }
8794 return wordnr;
8795}
8796
8797/*
8798 * Convert an offset into a minimal number of bytes.
8799 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8800 * bytes.
8801 */
8802 static int
8803offset2bytes(nr, buf)
8804 int nr;
8805 char_u *buf;
8806{
8807 int rem;
8808 int b1, b2, b3, b4;
8809
8810 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8811 b1 = nr % 255 + 1;
8812 rem = nr / 255;
8813 b2 = rem % 255 + 1;
8814 rem = rem / 255;
8815 b3 = rem % 255 + 1;
8816 b4 = rem / 255 + 1;
8817
8818 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8819 {
8820 buf[0] = 0xe0 + b4;
8821 buf[1] = b3;
8822 buf[2] = b2;
8823 buf[3] = b1;
8824 return 4;
8825 }
8826 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8827 {
8828 buf[0] = 0xc0 + b3;
8829 buf[1] = b2;
8830 buf[2] = b1;
8831 return 3;
8832 }
8833 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8834 {
8835 buf[0] = 0x80 + b2;
8836 buf[1] = b1;
8837 return 2;
8838 }
8839 /* 1 byte */
8840 buf[0] = b1;
8841 return 1;
8842}
8843
8844/*
8845 * Opposite of offset2bytes().
8846 * "pp" points to the bytes and is advanced over it.
8847 * Returns the offset.
8848 */
8849 static int
8850bytes2offset(pp)
8851 char_u **pp;
8852{
8853 char_u *p = *pp;
8854 int nr;
8855 int c;
8856
8857 c = *p++;
8858 if ((c & 0x80) == 0x00) /* 1 byte */
8859 {
8860 nr = c - 1;
8861 }
8862 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8863 {
8864 nr = (c & 0x3f) - 1;
8865 nr = nr * 255 + (*p++ - 1);
8866 }
8867 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8868 {
8869 nr = (c & 0x1f) - 1;
8870 nr = nr * 255 + (*p++ - 1);
8871 nr = nr * 255 + (*p++ - 1);
8872 }
8873 else /* 4 bytes */
8874 {
8875 nr = (c & 0x0f) - 1;
8876 nr = nr * 255 + (*p++ - 1);
8877 nr = nr * 255 + (*p++ - 1);
8878 nr = nr * 255 + (*p++ - 1);
8879 }
8880
8881 *pp = p;
8882 return nr;
8883}
8884
8885/*
8886 * Write the .sug file in "fname".
8887 */
8888 static void
8889sug_write(spin, fname)
8890 spellinfo_T *spin;
8891 char_u *fname;
8892{
8893 FILE *fd;
8894 wordnode_T *tree;
8895 int nodecount;
8896 int wcount;
8897 char_u *line;
8898 linenr_T lnum;
8899 int len;
8900
8901 /* Create the file. Note that an existing file is silently overwritten! */
8902 fd = mch_fopen((char *)fname, "w");
8903 if (fd == NULL)
8904 {
8905 EMSG2(_(e_notopen), fname);
8906 return;
8907 }
8908
8909 vim_snprintf((char *)IObuff, IOSIZE,
8910 _("Writing suggestion file %s ..."), fname);
8911 spell_message(spin, IObuff);
8912
8913 /*
8914 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8915 */
8916 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8917 {
8918 EMSG(_(e_write));
8919 goto theend;
8920 }
8921 putc(VIMSUGVERSION, fd); /* <versionnr> */
8922
8923 /* Write si_sugtime to the file. */
8924 put_sugtime(spin, fd); /* <timestamp> */
8925
8926 /*
8927 * <SUGWORDTREE>
8928 */
8929 spin->si_memtot = 0;
8930 tree = spin->si_foldroot->wn_sibling;
8931
8932 /* Clear the index and wnode fields in the tree. */
8933 clear_node(tree);
8934
8935 /* Count the number of nodes. Needed to be able to allocate the
8936 * memory when reading the nodes. Also fills in index for shared
8937 * nodes. */
8938 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8939
8940 /* number of nodes in 4 bytes */
8941 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8942 spin->si_memtot += nodecount + nodecount * sizeof(int);
8943
8944 /* Write the nodes. */
8945 (void)put_node(fd, tree, 0, 0, FALSE);
8946
8947 /*
8948 * <SUGTABLE>: <sugwcount> <sugline> ...
8949 */
8950 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8951 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8952
8953 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8954 {
8955 /* <sugline>: <sugnr> ... NUL */
8956 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008957 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008958 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8959 {
8960 EMSG(_(e_write));
8961 goto theend;
8962 }
8963 spin->si_memtot += len;
8964 }
8965
8966 /* Write another byte to check for errors. */
8967 if (putc(0, fd) == EOF)
8968 EMSG(_(e_write));
8969
8970 vim_snprintf((char *)IObuff, IOSIZE,
8971 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8972 spell_message(spin, IObuff);
8973
8974theend:
8975 /* close the file */
8976 fclose(fd);
8977}
8978
8979/*
8980 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8981 * list and only contains text lines. Can use a swapfile to reduce memory
8982 * use.
8983 * Most other fields are invalid! Esp. watch out for string options being
8984 * NULL and there is no undo info.
8985 * Returns NULL when out of memory.
8986 */
8987 static buf_T *
8988open_spellbuf()
8989{
8990 buf_T *buf;
8991
8992 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8993 if (buf != NULL)
8994 {
8995 buf->b_spell = TRUE;
8996 buf->b_p_swf = TRUE; /* may create a swap file */
8997 ml_open(buf);
8998 ml_open_file(buf); /* create swap file now */
8999 }
9000 return buf;
9001}
9002
9003/*
9004 * Close the buffer used for spell info.
9005 */
9006 static void
9007close_spellbuf(buf)
9008 buf_T *buf;
9009{
9010 if (buf != NULL)
9011 {
9012 ml_close(buf, TRUE);
9013 vim_free(buf);
9014 }
9015}
9016
9017
9018/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009019 * Create a Vim spell file from one or more word lists.
9020 * "fnames[0]" is the output file name.
9021 * "fnames[fcount - 1]" is the last input file name.
9022 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9023 * and ".spl" is appended to make the output file name.
9024 */
9025 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009026mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009027 int fcount;
9028 char_u **fnames;
9029 int ascii; /* -ascii argument given */
9030 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009032{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009033 char_u fname[MAXPATHL];
9034 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009035 char_u **innames;
9036 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009037 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009038 int i;
9039 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009040 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009041 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009042 spellinfo_T spin;
9043
9044 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009046 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 spin.si_followup = TRUE;
9048 spin.si_rem_accents = TRUE;
9049 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009050 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9052 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009053 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009054 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009055 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009056 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009057
Bram Moolenaarb765d632005-06-07 21:00:02 +00009058 /* default: fnames[0] is output file, following are input files */
9059 innames = &fnames[1];
9060 incount = fcount - 1;
9061
9062 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009063 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009064 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009065 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9066 {
9067 /* For ":mkspell path/en.latin1.add" output file is
9068 * "path/en.latin1.add.spl". */
9069 innames = &fnames[0];
9070 incount = 1;
9071 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9072 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009073 else if (fcount == 1)
9074 {
9075 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9076 innames = &fnames[0];
9077 incount = 1;
9078 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9079 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9080 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009081 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9082 {
9083 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009085 }
9086 else
9087 /* Name should be language, make the file name from it. */
9088 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9089 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9090
9091 /* Check for .ascii.spl. */
9092 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9093 spin.si_ascii = TRUE;
9094
9095 /* Check for .add.spl. */
9096 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9097 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009098 }
9099
Bram Moolenaarb765d632005-06-07 21:00:02 +00009100 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009101 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009102 else if (vim_strchr(gettail(wfname), '_') != NULL)
9103 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009104 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009105 EMSG(_("E754: Only up to 8 regions supported"));
9106 else
9107 {
9108 /* Check for overwriting before doing things that may take a lot of
9109 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009110 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009111 {
9112 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009113 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009114 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009115 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009116 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009117 EMSG2(_(e_isadir2), wfname);
9118 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009119 }
9120
9121 /*
9122 * Init the aff and dic pointers.
9123 * Get the region names if there are more than 2 arguments.
9124 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009125 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009126 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009127 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009128
Bram Moolenaar3982c542005-06-08 21:56:31 +00009129 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009130 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009131 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009132 if (STRLEN(gettail(innames[i])) < 5
9133 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009135 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9136 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009137 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009138 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9139 spin.si_region_name[i * 2 + 1] =
9140 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009141 }
9142 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009143 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009144
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009145 spin.si_foldroot = wordtree_alloc(&spin);
9146 spin.si_keeproot = wordtree_alloc(&spin);
9147 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009148 if (spin.si_foldroot == NULL
9149 || spin.si_keeproot == NULL
9150 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009151 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009152 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009153 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009154 }
9155
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009156 /* When not producing a .add.spl file clear the character table when
9157 * we encounter one in the .aff file. This means we dump the current
9158 * one in the .spl file if the .aff file doesn't define one. That's
9159 * better than guessing the contents, the table will match a
9160 * previously loaded spell file. */
9161 if (!spin.si_add)
9162 spin.si_clear_chartab = TRUE;
9163
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009164 /*
9165 * Read all the .aff and .dic files.
9166 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009167 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009168 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009170 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009171 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009172 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009173
Bram Moolenaarb765d632005-06-07 21:00:02 +00009174 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009175 if (mch_stat((char *)fname, &st) >= 0)
9176 {
9177 /* Read the .aff file. Will init "spin->si_conv" based on the
9178 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009179 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009180 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009181 error = TRUE;
9182 else
9183 {
9184 /* Read the .dic file and store the words in the trees. */
9185 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009186 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009187 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009188 error = TRUE;
9189 }
9190 }
9191 else
9192 {
9193 /* No .aff file, try reading the file as a word list. Store
9194 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009195 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009196 error = TRUE;
9197 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009198
Bram Moolenaarb765d632005-06-07 21:00:02 +00009199#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009200 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009201 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009202#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009203 }
9204
Bram Moolenaar78622822005-08-23 21:00:13 +00009205 if (spin.si_compflags != NULL && spin.si_nobreak)
9206 MSG(_("Warning: both compounding and NOBREAK specified"));
9207
Bram Moolenaar4770d092006-01-12 23:22:24 +00009208 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009209 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009210 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009211 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009212 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009213 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009214 wordtree_compress(&spin, spin.si_foldroot);
9215 wordtree_compress(&spin, spin.si_keeproot);
9216 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009217 }
9218
Bram Moolenaar4770d092006-01-12 23:22:24 +00009219 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009220 {
9221 /*
9222 * Write the info in the spell file.
9223 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009224 vim_snprintf((char *)IObuff, IOSIZE,
9225 _("Writing spell file %s ..."), wfname);
9226 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009227
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009228 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009229
Bram Moolenaar4770d092006-01-12 23:22:24 +00009230 spell_message(&spin, (char_u *)_("Done!"));
9231 vim_snprintf((char *)IObuff, IOSIZE,
9232 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9233 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009234
Bram Moolenaar4770d092006-01-12 23:22:24 +00009235 /*
9236 * If the file is loaded need to reload it.
9237 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009238 if (!error)
9239 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009240 }
9241
9242 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009244 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 ga_clear(&spin.si_sal);
9246 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009247 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009248 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009249 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009250
9251 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009252 for (i = 0; i < incount; ++i)
9253 if (afile[i] != NULL)
9254 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009255
9256 /* Free all the bits and pieces at once. */
9257 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009258
9259 /*
9260 * If there is soundfolding info and no NOSUGFILE item create the
9261 * .sug file with the soundfolded word trie.
9262 */
9263 if (spin.si_sugtime != 0 && !error && !got_int)
9264 spell_make_sugfile(&spin, wfname);
9265
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009266 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009267}
9268
Bram Moolenaar4770d092006-01-12 23:22:24 +00009269/*
9270 * Display a message for spell file processing when 'verbose' is set or using
9271 * ":mkspell". "str" can be IObuff.
9272 */
9273 static void
9274spell_message(spin, str)
9275 spellinfo_T *spin;
9276 char_u *str;
9277{
9278 if (spin->si_verbose || p_verbose > 2)
9279 {
9280 if (!spin->si_verbose)
9281 verbose_enter();
9282 MSG(str);
9283 out_flush();
9284 if (!spin->si_verbose)
9285 verbose_leave();
9286 }
9287}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009288
9289/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009290 * ":[count]spellgood {word}"
9291 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009292 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009293 */
9294 void
9295ex_spell(eap)
9296 exarg_T *eap;
9297{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009298 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009299 eap->forceit ? 0 : (int)eap->line2,
9300 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009301}
9302
9303/*
9304 * Add "word[len]" to 'spellfile' as a good or bad word.
9305 */
9306 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009307spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009308 char_u *word;
9309 int len;
9310 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009311 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009312 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009313 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009314{
Bram Moolenaara3917072006-09-14 08:48:14 +00009315 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009316 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009317 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009318 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009319 char_u fnamebuf[MAXPATHL];
9320 char_u line[MAXWLEN * 2];
9321 long fpos, fpos_next = 0;
9322 int i;
9323 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009324
Bram Moolenaar89d40322006-08-29 15:30:07 +00009325 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009326 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009327 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009328 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009329 int_wordlist = vim_tempname('s');
9330 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009331 return;
9332 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009333 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009334 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009335 else
9336 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009337 /* If 'spellfile' isn't set figure out a good default value. */
9338 if (*curbuf->b_p_spf == NUL)
9339 {
9340 init_spellfile();
9341 new_spf = TRUE;
9342 }
9343
9344 if (*curbuf->b_p_spf == NUL)
9345 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009346 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009347 return;
9348 }
9349
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009350 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9351 {
9352 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009353 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009354 break;
9355 if (*spf == NUL)
9356 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009357 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009358 return;
9359 }
9360 }
9361
Bram Moolenaarb765d632005-06-07 21:00:02 +00009362 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009363 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009364 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9365 buf = NULL;
9366 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009367 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009368 EMSG(_(e_bufloaded));
9369 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009370 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009371
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009372 fname = fnamebuf;
9373 }
9374
Bram Moolenaard0131a82006-03-04 21:46:13 +00009375 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009376 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009377 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009378 * since its flags sort before the one with WF_BANNED. */
9379 fd = mch_fopen((char *)fname, "r");
9380 if (fd != NULL)
9381 {
9382 while (!vim_fgets(line, MAXWLEN * 2, fd))
9383 {
9384 fpos = fpos_next;
9385 fpos_next = ftell(fd);
9386 if (STRNCMP(word, line, len) == 0
9387 && (line[len] == '/' || line[len] < ' '))
9388 {
9389 /* Found duplicate word. Remove it by writing a '#' at
9390 * the start of the line. Mixing reading and writing
9391 * doesn't work for all systems, close the file first. */
9392 fclose(fd);
9393 fd = mch_fopen((char *)fname, "r+");
9394 if (fd == NULL)
9395 break;
9396 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009397 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009398 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009399 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009400 {
9401 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009402 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009403 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009404 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009405 fseek(fd, fpos_next, SEEK_SET);
9406 }
9407 }
9408 fclose(fd);
9409 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009410 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009411
9412 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009413 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009414 fd = mch_fopen((char *)fname, "a");
9415 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009416 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009417 char_u *p;
9418
Bram Moolenaard0131a82006-03-04 21:46:13 +00009419 /* We just initialized the 'spellfile' option and can't open the
9420 * file. We may need to create the "spell" directory first. We
9421 * already checked the runtime directory is writable in
9422 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009423 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009424 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009425 int c = *p;
9426
Bram Moolenaard0131a82006-03-04 21:46:13 +00009427 /* The directory doesn't exist. Try creating it and opening
9428 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009429 *p = NUL;
9430 vim_mkdir(fname, 0755);
9431 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009432 fd = mch_fopen((char *)fname, "a");
9433 }
9434 }
9435
9436 if (fd == NULL)
9437 EMSG2(_(e_notopen), fname);
9438 else
9439 {
9440 if (bad)
9441 fprintf(fd, "%.*s/!\n", len, word);
9442 else
9443 fprintf(fd, "%.*s\n", len, word);
9444 fclose(fd);
9445
9446 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9447 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009448 }
9449 }
9450
Bram Moolenaard0131a82006-03-04 21:46:13 +00009451 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009452 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009453 /* Update the .add.spl file. */
9454 mkspell(1, &fname, FALSE, TRUE, TRUE);
9455
9456 /* If the .add file is edited somewhere, reload it. */
9457 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009458 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009459
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009460 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009461 }
9462}
9463
9464/*
9465 * Initialize 'spellfile' for the current buffer.
9466 */
9467 static void
9468init_spellfile()
9469{
9470 char_u buf[MAXPATHL];
9471 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009472 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009473 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009474 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009475 int aspath = FALSE;
9476 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009477
9478 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9479 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009480 /* Find the end of the language name. Exclude the region. If there
9481 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009482 for (lend = curbuf->b_p_spl; *lend != NUL
9483 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009484 if (vim_ispathsep(*lend))
9485 {
9486 aspath = TRUE;
9487 lstart = lend + 1;
9488 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009489
9490 /* Loop over all entries in 'runtimepath'. Use the first one where we
9491 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009492 rtp = p_rtp;
9493 while (*rtp != NUL)
9494 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009495 if (aspath)
9496 /* Use directory of an entry with path, e.g., for
9497 * "/dir/lg.utf-8.spl" use "/dir". */
9498 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9499 else
9500 /* Copy the path from 'runtimepath' to buf[]. */
9501 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009502 if (filewritable(buf) == 2)
9503 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009504 /* Use the first language name from 'spelllang' and the
9505 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009506 if (aspath)
9507 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9508 else
9509 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009510 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009511 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009512 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9513 if (!filewritable(buf) != 2)
9514 vim_mkdir(buf, 0755);
9515
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009516 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009517 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009518 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009519 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009520 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009521 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9522 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9523 fname != NULL
9524 && strstr((char *)gettail(fname), ".ascii.") != NULL
9525 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009526 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9527 break;
9528 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009529 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009530 }
9531 }
9532}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009533
Bram Moolenaar51485f02005-06-04 21:55:20 +00009534
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009535/*
9536 * Init the chartab used for spelling for ASCII.
9537 * EBCDIC is not supported!
9538 */
9539 static void
9540clear_spell_chartab(sp)
9541 spelltab_T *sp;
9542{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009543 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009544
9545 /* Init everything to FALSE. */
9546 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9547 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9548 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009549 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009550 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009551 sp->st_upper[i] = i;
9552 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009553
9554 /* We include digits. A word shouldn't start with a digit, but handling
9555 * that is done separately. */
9556 for (i = '0'; i <= '9'; ++i)
9557 sp->st_isw[i] = TRUE;
9558 for (i = 'A'; i <= 'Z'; ++i)
9559 {
9560 sp->st_isw[i] = TRUE;
9561 sp->st_isu[i] = TRUE;
9562 sp->st_fold[i] = i + 0x20;
9563 }
9564 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009565 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009566 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009567 sp->st_upper[i] = i - 0x20;
9568 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009569}
9570
9571/*
9572 * Init the chartab used for spelling. Only depends on 'encoding'.
9573 * Called once while starting up and when 'encoding' changes.
9574 * The default is to use isalpha(), but the spell file should define the word
9575 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009576 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009577 */
9578 void
9579init_spell_chartab()
9580{
9581 int i;
9582
9583 did_set_spelltab = FALSE;
9584 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009585#ifdef FEAT_MBYTE
9586 if (enc_dbcs)
9587 {
9588 /* DBCS: assume double-wide characters are word characters. */
9589 for (i = 128; i <= 255; ++i)
9590 if (MB_BYTE2LEN(i) == 2)
9591 spelltab.st_isw[i] = TRUE;
9592 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009593 else if (enc_utf8)
9594 {
9595 for (i = 128; i < 256; ++i)
9596 {
9597 spelltab.st_isu[i] = utf_isupper(i);
9598 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9599 spelltab.st_fold[i] = utf_fold(i);
9600 spelltab.st_upper[i] = utf_toupper(i);
9601 }
9602 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009603 else
9604#endif
9605 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009606 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009607 for (i = 128; i < 256; ++i)
9608 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009609 if (MB_ISUPPER(i))
9610 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009611 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009612 spelltab.st_isu[i] = TRUE;
9613 spelltab.st_fold[i] = MB_TOLOWER(i);
9614 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009615 else if (MB_ISLOWER(i))
9616 {
9617 spelltab.st_isw[i] = TRUE;
9618 spelltab.st_upper[i] = MB_TOUPPER(i);
9619 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009620 }
9621 }
9622}
9623
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009624/*
9625 * Set the spell character tables from strings in the affix file.
9626 */
9627 static int
9628set_spell_chartab(fol, low, upp)
9629 char_u *fol;
9630 char_u *low;
9631 char_u *upp;
9632{
9633 /* We build the new tables here first, so that we can compare with the
9634 * previous one. */
9635 spelltab_T new_st;
9636 char_u *pf = fol, *pl = low, *pu = upp;
9637 int f, l, u;
9638
9639 clear_spell_chartab(&new_st);
9640
9641 while (*pf != NUL)
9642 {
9643 if (*pl == NUL || *pu == NUL)
9644 {
9645 EMSG(_(e_affform));
9646 return FAIL;
9647 }
9648#ifdef FEAT_MBYTE
9649 f = mb_ptr2char_adv(&pf);
9650 l = mb_ptr2char_adv(&pl);
9651 u = mb_ptr2char_adv(&pu);
9652#else
9653 f = *pf++;
9654 l = *pl++;
9655 u = *pu++;
9656#endif
9657 /* Every character that appears is a word character. */
9658 if (f < 256)
9659 new_st.st_isw[f] = TRUE;
9660 if (l < 256)
9661 new_st.st_isw[l] = TRUE;
9662 if (u < 256)
9663 new_st.st_isw[u] = TRUE;
9664
9665 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9666 * case-folding */
9667 if (l < 256 && l != f)
9668 {
9669 if (f >= 256)
9670 {
9671 EMSG(_(e_affrange));
9672 return FAIL;
9673 }
9674 new_st.st_fold[l] = f;
9675 }
9676
9677 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009678 * case-folding, it's upper case and the "UPP" is the upper case of
9679 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009680 if (u < 256 && u != f)
9681 {
9682 if (f >= 256)
9683 {
9684 EMSG(_(e_affrange));
9685 return FAIL;
9686 }
9687 new_st.st_fold[u] = f;
9688 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009689 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009690 }
9691 }
9692
9693 if (*pl != NUL || *pu != NUL)
9694 {
9695 EMSG(_(e_affform));
9696 return FAIL;
9697 }
9698
9699 return set_spell_finish(&new_st);
9700}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009701
9702/*
9703 * Set the spell character tables from strings in the .spl file.
9704 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009705 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009706set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009707 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009708 int cnt; /* length of "flags" */
9709 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009710{
9711 /* We build the new tables here first, so that we can compare with the
9712 * previous one. */
9713 spelltab_T new_st;
9714 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009715 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009716 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009717
9718 clear_spell_chartab(&new_st);
9719
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009720 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009721 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009722 if (i < cnt)
9723 {
9724 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9725 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9726 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009727
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009728 if (*p != NUL)
9729 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009730#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009731 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009732#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009733 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009734#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009735 new_st.st_fold[i + 128] = c;
9736 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9737 new_st.st_upper[c] = i + 128;
9738 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009739 }
9740
Bram Moolenaar5195e452005-08-19 20:32:47 +00009741 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009742}
9743
9744 static int
9745set_spell_finish(new_st)
9746 spelltab_T *new_st;
9747{
9748 int i;
9749
9750 if (did_set_spelltab)
9751 {
9752 /* check that it's the same table */
9753 for (i = 0; i < 256; ++i)
9754 {
9755 if (spelltab.st_isw[i] != new_st->st_isw[i]
9756 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009757 || spelltab.st_fold[i] != new_st->st_fold[i]
9758 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009759 {
9760 EMSG(_("E763: Word characters differ between spell files"));
9761 return FAIL;
9762 }
9763 }
9764 }
9765 else
9766 {
9767 /* copy the new spelltab into the one being used */
9768 spelltab = *new_st;
9769 did_set_spelltab = TRUE;
9770 }
9771
9772 return OK;
9773}
9774
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009775/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009776 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009777 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009778 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009779 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009780 */
9781 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009782spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009783 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009784 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009785{
Bram Moolenaarea408852005-06-25 22:49:46 +00009786#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009787 char_u *s;
9788 int l;
9789 int c;
9790
9791 if (has_mbyte)
9792 {
9793 l = MB_BYTE2LEN(*p);
9794 s = p;
9795 if (l == 1)
9796 {
9797 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009798 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009799 {
9800 s = p + 1; /* skip a mid-word character */
9801 l = MB_BYTE2LEN(*s);
9802 }
9803 }
9804 else
9805 {
9806 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009807 if (c < 256 ? buf->b_spell_ismw[c]
9808 : (buf->b_spell_ismw_mb != NULL
9809 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009810 {
9811 s = p + l;
9812 l = MB_BYTE2LEN(*s);
9813 }
9814 }
9815
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009816 c = mb_ptr2char(s);
9817 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009818 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009819 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009820 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009821#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009822
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009823 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9824}
9825
9826/*
9827 * Return TRUE if "p" points to a word character.
9828 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9829 */
9830 static int
9831spell_iswordp_nmw(p)
9832 char_u *p;
9833{
9834#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009835 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009836
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009837 if (has_mbyte)
9838 {
9839 c = mb_ptr2char(p);
9840 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009841 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009842 return spelltab.st_isw[c];
9843 }
9844#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009845 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009846}
9847
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009848#ifdef FEAT_MBYTE
9849/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009850 * Return TRUE if word class indicates a word character.
9851 * Only for characters above 255.
9852 * Unicode subscript and superscript are not considered word characters.
9853 */
9854 static int
9855spell_mb_isword_class(cl)
9856 int cl;
9857{
9858 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9859}
9860
9861/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009862 * Return TRUE if "p" points to a word character.
9863 * Wide version of spell_iswordp().
9864 */
9865 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009866spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009867 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009868 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009869{
9870 int *s;
9871
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009872 if (*p < 256 ? buf->b_spell_ismw[*p]
9873 : (buf->b_spell_ismw_mb != NULL
9874 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009875 s = p + 1;
9876 else
9877 s = p;
9878
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009879 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009880 {
9881 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009882 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009883 if (enc_dbcs)
9884 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9885 return 0;
9886 }
9887 return spelltab.st_isw[*s];
9888}
9889#endif
9890
Bram Moolenaarea408852005-06-25 22:49:46 +00009891/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009892 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009893 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009894 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009895 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009896write_spell_prefcond(fd, gap)
9897 FILE *fd;
9898 garray_T *gap;
9899{
9900 int i;
9901 char_u *p;
9902 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009903 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009904 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009905
Bram Moolenaar5195e452005-08-19 20:32:47 +00009906 if (fd != NULL)
9907 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9908
9909 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009910
9911 for (i = 0; i < gap->ga_len; ++i)
9912 {
9913 /* <prefcond> : <condlen> <condstr> */
9914 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009915 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009916 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009917 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009918 if (fd != NULL)
9919 {
9920 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009921 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009922 }
9923 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009924 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009925 else if (fd != NULL)
9926 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009927 }
9928
Bram Moolenaar5195e452005-08-19 20:32:47 +00009929 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009930}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009931
9932/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009933 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9934 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009935 * When using a multi-byte 'encoding' the length may change!
9936 * Returns FAIL when something wrong.
9937 */
9938 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009939spell_casefold(str, len, buf, buflen)
9940 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009941 int len;
9942 char_u *buf;
9943 int buflen;
9944{
9945 int i;
9946
9947 if (len >= buflen)
9948 {
9949 buf[0] = NUL;
9950 return FAIL; /* result will not fit */
9951 }
9952
9953#ifdef FEAT_MBYTE
9954 if (has_mbyte)
9955 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009956 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009957 char_u *p;
9958 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009959
9960 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009961 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009962 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009963 if (outi + MB_MAXBYTES > buflen)
9964 {
9965 buf[outi] = NUL;
9966 return FAIL;
9967 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009968 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009969 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009970 }
9971 buf[outi] = NUL;
9972 }
9973 else
9974#endif
9975 {
9976 /* Be quick for non-multibyte encodings. */
9977 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009978 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009979 buf[i] = NUL;
9980 }
9981
9982 return OK;
9983}
9984
Bram Moolenaar4770d092006-01-12 23:22:24 +00009985/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009986#define SPS_BEST 1
9987#define SPS_FAST 2
9988#define SPS_DOUBLE 4
9989
Bram Moolenaar4770d092006-01-12 23:22:24 +00009990static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9991static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009992
9993/*
9994 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009995 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009996 */
9997 int
9998spell_check_sps()
9999{
10000 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010001 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010002 char_u buf[MAXPATHL];
10003 int f;
10004
10005 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010006 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010007
10008 for (p = p_sps; *p != NUL; )
10009 {
10010 copy_option_part(&p, buf, MAXPATHL, ",");
10011
10012 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010013 if (VIM_ISDIGIT(*buf))
10014 {
10015 s = buf;
10016 sps_limit = getdigits(&s);
10017 if (*s != NUL && !VIM_ISDIGIT(*s))
10018 f = -1;
10019 }
10020 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010021 f = SPS_BEST;
10022 else if (STRCMP(buf, "fast") == 0)
10023 f = SPS_FAST;
10024 else if (STRCMP(buf, "double") == 0)
10025 f = SPS_DOUBLE;
10026 else if (STRNCMP(buf, "expr:", 5) != 0
10027 && STRNCMP(buf, "file:", 5) != 0)
10028 f = -1;
10029
10030 if (f == -1 || (sps_flags != 0 && f != 0))
10031 {
10032 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010033 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010034 return FAIL;
10035 }
10036 if (f != 0)
10037 sps_flags = f;
10038 }
10039
10040 if (sps_flags == 0)
10041 sps_flags = SPS_BEST;
10042
10043 return OK;
10044}
10045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010046/*
10047 * "z?": Find badly spelled word under or after the cursor.
10048 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010049 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010050 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010051 */
10052 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010053spell_suggest(count)
10054 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055{
10056 char_u *line;
10057 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010058 char_u wcopy[MAXWLEN + 2];
10059 char_u *p;
10060 int i;
10061 int c;
10062 suginfo_T sug;
10063 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010064 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010065 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010066 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010067 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010068 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010070 if (no_spell_checking(curwin))
10071 return;
10072
10073#ifdef FEAT_VISUAL
10074 if (VIsual_active)
10075 {
10076 /* Use the Visually selected text as the bad word. But reject
10077 * a multi-line selection. */
10078 if (curwin->w_cursor.lnum != VIsual.lnum)
10079 {
10080 vim_beep();
10081 return;
10082 }
10083 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10084 if (badlen < 0)
10085 badlen = -badlen;
10086 else
10087 curwin->w_cursor.col = VIsual.col;
10088 ++badlen;
10089 end_visual_mode();
10090 }
10091 else
10092#endif
10093 /* Find the start of the badly spelled word. */
10094 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010095 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010097 /* No bad word or it starts after the cursor: use the word under the
10098 * cursor. */
10099 curwin->w_cursor = prev_cursor;
10100 line = ml_get_curline();
10101 p = line + curwin->w_cursor.col;
10102 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010103 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010104 mb_ptr_back(line, p);
10105 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010106 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010107 mb_ptr_adv(p);
10108
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010109 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010110 {
10111 beep_flush();
10112 return;
10113 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010114 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 }
10116
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010117 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010119 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010120 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010121
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010122 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010123
Bram Moolenaar5195e452005-08-19 20:32:47 +000010124 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10125 * 'spellsuggest', whatever is smaller. */
10126 if (sps_limit > (int)Rows - 2)
10127 limit = (int)Rows - 2;
10128 else
10129 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010130 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010131 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132
10133 if (sug.su_ga.ga_len == 0)
10134 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010135 else if (count > 0)
10136 {
10137 if (count > sug.su_ga.ga_len)
10138 smsg((char_u *)_("Sorry, only %ld suggestions"),
10139 (long)sug.su_ga.ga_len);
10140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 else
10142 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010143 vim_free(repl_from);
10144 repl_from = NULL;
10145 vim_free(repl_to);
10146 repl_to = NULL;
10147
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010148#ifdef FEAT_RIGHTLEFT
10149 /* When 'rightleft' is set the list is drawn right-left. */
10150 cmdmsg_rl = curwin->w_p_rl;
10151 if (cmdmsg_rl)
10152 msg_col = Columns - 1;
10153#endif
10154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010155 /* List the suggestions. */
10156 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010157 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010158 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10160 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010161#ifdef FEAT_RIGHTLEFT
10162 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10163 {
10164 /* And now the rabbit from the high hat: Avoid showing the
10165 * untranslated message rightleft. */
10166 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10167 sug.su_badlen, sug.su_badptr);
10168 }
10169#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 msg_puts(IObuff);
10171 msg_clr_eos();
10172 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010174 msg_scroll = TRUE;
10175 for (i = 0; i < sug.su_ga.ga_len; ++i)
10176 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010177 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178
10179 /* The suggested word may replace only part of the bad word, add
10180 * the not replaced part. */
10181 STRCPY(wcopy, stp->st_word);
10182 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010183 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 sug.su_badptr + stp->st_orglen,
10185 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010186 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10187#ifdef FEAT_RIGHTLEFT
10188 if (cmdmsg_rl)
10189 rl_mirror(IObuff);
10190#endif
10191 msg_puts(IObuff);
10192
10193 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010194 msg_puts(IObuff);
10195
10196 /* The word may replace more than "su_badlen". */
10197 if (sug.su_badlen < stp->st_orglen)
10198 {
10199 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10200 stp->st_orglen, sug.su_badptr);
10201 msg_puts(IObuff);
10202 }
10203
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010204 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010205 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010206 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010207 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010208 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010209 stp->st_salscore ? "s " : "",
10210 stp->st_score, stp->st_altscore);
10211 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010212 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010213 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010214#ifdef FEAT_RIGHTLEFT
10215 if (cmdmsg_rl)
10216 /* Mirror the numbers, but keep the leading space. */
10217 rl_mirror(IObuff + 1);
10218#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010219 msg_advance(30);
10220 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 msg_putchar('\n');
10223 }
10224
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010225#ifdef FEAT_RIGHTLEFT
10226 cmdmsg_rl = FALSE;
10227 msg_col = 0;
10228#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010230 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010231 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010232 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010233 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 }
10235
Bram Moolenaard12a1322005-08-21 22:08:24 +000010236 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10237 {
10238 /* Save the from and to text for :spellrepall. */
10239 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010240 if (sug.su_badlen > stp->st_orglen)
10241 {
10242 /* Replacing less than "su_badlen", append the remainder to
10243 * repl_to. */
10244 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10245 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10246 sug.su_badlen - stp->st_orglen,
10247 sug.su_badptr + stp->st_orglen);
10248 repl_to = vim_strsave(IObuff);
10249 }
10250 else
10251 {
10252 /* Replacing su_badlen or more, use the whole word. */
10253 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10254 repl_to = vim_strsave(stp->st_word);
10255 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010256
10257 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010258 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010259 if (p != NULL)
10260 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010261 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010262 mch_memmove(p, line, c);
10263 STRCPY(p + c, stp->st_word);
10264 STRCAT(p, sug.su_badptr + stp->st_orglen);
10265 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10266 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010267
10268 /* For redo we use a change-word command. */
10269 ResetRedobuff();
10270 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010271 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010272 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010273 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010274
10275 /* After this "p" may be invalid. */
10276 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010277 }
10278 }
10279 else
10280 curwin->w_cursor = prev_cursor;
10281
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010282 spell_find_cleanup(&sug);
10283}
10284
10285/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010286 * Check if the word at line "lnum" column "col" is required to start with a
10287 * capital. This uses 'spellcapcheck' of the current buffer.
10288 */
10289 static int
10290check_need_cap(lnum, col)
10291 linenr_T lnum;
10292 colnr_T col;
10293{
10294 int need_cap = FALSE;
10295 char_u *line;
10296 char_u *line_copy = NULL;
10297 char_u *p;
10298 colnr_T endcol;
10299 regmatch_T regmatch;
10300
10301 if (curbuf->b_cap_prog == NULL)
10302 return FALSE;
10303
10304 line = ml_get_curline();
10305 endcol = 0;
10306 if ((int)(skipwhite(line) - line) >= (int)col)
10307 {
10308 /* At start of line, check if previous line is empty or sentence
10309 * ends there. */
10310 if (lnum == 1)
10311 need_cap = TRUE;
10312 else
10313 {
10314 line = ml_get(lnum - 1);
10315 if (*skipwhite(line) == NUL)
10316 need_cap = TRUE;
10317 else
10318 {
10319 /* Append a space in place of the line break. */
10320 line_copy = concat_str(line, (char_u *)" ");
10321 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010322 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010323 }
10324 }
10325 }
10326 else
10327 endcol = col;
10328
10329 if (endcol > 0)
10330 {
10331 /* Check if sentence ends before the bad word. */
10332 regmatch.regprog = curbuf->b_cap_prog;
10333 regmatch.rm_ic = FALSE;
10334 p = line + endcol;
10335 for (;;)
10336 {
10337 mb_ptr_back(line, p);
10338 if (p == line || spell_iswordp_nmw(p))
10339 break;
10340 if (vim_regexec(&regmatch, p, 0)
10341 && regmatch.endp[0] == line + endcol)
10342 {
10343 need_cap = TRUE;
10344 break;
10345 }
10346 }
10347 }
10348
10349 vim_free(line_copy);
10350
10351 return need_cap;
10352}
10353
10354
10355/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010356 * ":spellrepall"
10357 */
10358/*ARGSUSED*/
10359 void
10360ex_spellrepall(eap)
10361 exarg_T *eap;
10362{
10363 pos_T pos = curwin->w_cursor;
10364 char_u *frompat;
10365 int addlen;
10366 char_u *line;
10367 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010368 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010369 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010370
10371 if (repl_from == NULL || repl_to == NULL)
10372 {
10373 EMSG(_("E752: No previous spell replacement"));
10374 return;
10375 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010376 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010377
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010378 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010379 if (frompat == NULL)
10380 return;
10381 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10382 p_ws = FALSE;
10383
Bram Moolenaar5195e452005-08-19 20:32:47 +000010384 sub_nsubs = 0;
10385 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010386 curwin->w_cursor.lnum = 0;
10387 while (!got_int)
10388 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010389 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010390 || u_save_cursor() == FAIL)
10391 break;
10392
10393 /* Only replace when the right word isn't there yet. This happens
10394 * when changing "etc" to "etc.". */
10395 line = ml_get_curline();
10396 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10397 repl_to, STRLEN(repl_to)) != 0)
10398 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010399 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010400 if (p == NULL)
10401 break;
10402 mch_memmove(p, line, curwin->w_cursor.col);
10403 STRCPY(p + curwin->w_cursor.col, repl_to);
10404 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10405 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10406 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010407
10408 if (curwin->w_cursor.lnum != prev_lnum)
10409 {
10410 ++sub_nlines;
10411 prev_lnum = curwin->w_cursor.lnum;
10412 }
10413 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010414 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010415 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010416 }
10417
10418 p_ws = save_ws;
10419 curwin->w_cursor = pos;
10420 vim_free(frompat);
10421
Bram Moolenaar5195e452005-08-19 20:32:47 +000010422 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010423 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010424 else
10425 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010426}
10427
10428/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010429 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10430 * a list of allocated strings.
10431 */
10432 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010433spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010434 garray_T *gap;
10435 char_u *word;
10436 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010437 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010438 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010439{
10440 suginfo_T sug;
10441 int i;
10442 suggest_T *stp;
10443 char_u *wcopy;
10444
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010445 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010446
10447 /* Make room in "gap". */
10448 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010449 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010450 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010451 for (i = 0; i < sug.su_ga.ga_len; ++i)
10452 {
10453 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010454
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010455 /* The suggested word may replace only part of "word", add the not
10456 * replaced part. */
10457 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010458 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010459 if (wcopy == NULL)
10460 break;
10461 STRCPY(wcopy, stp->st_word);
10462 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10463 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10464 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010465 }
10466
10467 spell_find_cleanup(&sug);
10468}
10469
10470/*
10471 * Find spell suggestions for the word at the start of "badptr".
10472 * Return the suggestions in "su->su_ga".
10473 * The maximum number of suggestions is "maxcount".
10474 * Note: does use info for the current window.
10475 * This is based on the mechanisms of Aspell, but completely reimplemented.
10476 */
10477 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010478spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010479 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010480 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010481 suginfo_T *su;
10482 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010483 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010484 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010485 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010486{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010487 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010488 char_u buf[MAXPATHL];
10489 char_u *p;
10490 int do_combine = FALSE;
10491 char_u *sps_copy;
10492#ifdef FEAT_EVAL
10493 static int expr_busy = FALSE;
10494#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010495 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010496 int i;
10497 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010498
10499 /*
10500 * Set the info in "*su".
10501 */
10502 vim_memset(su, 0, sizeof(suginfo_T));
10503 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10504 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010505 if (*badptr == NUL)
10506 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010507 hash_init(&su->su_banned);
10508
10509 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010510 if (badlen != 0)
10511 su->su_badlen = badlen;
10512 else
10513 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010514 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010515 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010516
10517 if (su->su_badlen >= MAXWLEN)
10518 su->su_badlen = MAXWLEN - 1; /* just in case */
10519 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10520 (void)spell_casefold(su->su_badptr, su->su_badlen,
10521 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010522 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010523 su->su_badflags = badword_captype(su->su_badptr,
10524 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010525 if (need_cap)
10526 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010527
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010528 /* Find the default language for sound folding. We simply use the first
10529 * one in 'spelllang' that supports sound folding. That's good for when
10530 * using multiple files for one language, it's not that bad when mixing
10531 * languages (e.g., "pl,en"). */
10532 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10533 {
10534 lp = LANGP_ENTRY(curbuf->b_langp, i);
10535 if (lp->lp_sallang != NULL)
10536 {
10537 su->su_sallang = lp->lp_sallang;
10538 break;
10539 }
10540 }
10541
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010542 /* Soundfold the bad word with the default sound folding, so that we don't
10543 * have to do this many times. */
10544 if (su->su_sallang != NULL)
10545 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10546 su->su_sal_badword);
10547
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010548 /* If the word is not capitalised and spell_check() doesn't consider the
10549 * word to be bad then it might need to be capitalised. Add a suggestion
10550 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010551 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010552 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010553 {
10554 make_case_word(su->su_badword, buf, WF_ONECAP);
10555 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010556 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010557 }
10558
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010559 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010560 if (banbadword)
10561 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010562
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010563 /* Make a copy of 'spellsuggest', because the expression may change it. */
10564 sps_copy = vim_strsave(p_sps);
10565 if (sps_copy == NULL)
10566 return;
10567
10568 /* Loop over the items in 'spellsuggest'. */
10569 for (p = sps_copy; *p != NUL; )
10570 {
10571 copy_option_part(&p, buf, MAXPATHL, ",");
10572
10573 if (STRNCMP(buf, "expr:", 5) == 0)
10574 {
10575#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010576 /* Evaluate an expression. Skip this when called recursively,
10577 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010578 if (!expr_busy)
10579 {
10580 expr_busy = TRUE;
10581 spell_suggest_expr(su, buf + 5);
10582 expr_busy = FALSE;
10583 }
10584#endif
10585 }
10586 else if (STRNCMP(buf, "file:", 5) == 0)
10587 /* Use list of suggestions in a file. */
10588 spell_suggest_file(su, buf + 5);
10589 else
10590 {
10591 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010592 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010593 if (sps_flags & SPS_DOUBLE)
10594 do_combine = TRUE;
10595 }
10596 }
10597
10598 vim_free(sps_copy);
10599
10600 if (do_combine)
10601 /* Combine the two list of suggestions. This must be done last,
10602 * because sorting changes the order again. */
10603 score_combine(su);
10604}
10605
10606#ifdef FEAT_EVAL
10607/*
10608 * Find suggestions by evaluating expression "expr".
10609 */
10610 static void
10611spell_suggest_expr(su, expr)
10612 suginfo_T *su;
10613 char_u *expr;
10614{
10615 list_T *list;
10616 listitem_T *li;
10617 int score;
10618 char_u *p;
10619
10620 /* The work is split up in a few parts to avoid having to export
10621 * suginfo_T.
10622 * First evaluate the expression and get the resulting list. */
10623 list = eval_spell_expr(su->su_badword, expr);
10624 if (list != NULL)
10625 {
10626 /* Loop over the items in the list. */
10627 for (li = list->lv_first; li != NULL; li = li->li_next)
10628 if (li->li_tv.v_type == VAR_LIST)
10629 {
10630 /* Get the word and the score from the items. */
10631 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010632 if (score >= 0 && score <= su->su_maxscore)
10633 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10634 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010635 }
10636 list_unref(list);
10637 }
10638
Bram Moolenaar4770d092006-01-12 23:22:24 +000010639 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10640 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010641 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10642}
10643#endif
10644
10645/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010646 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010647 */
10648 static void
10649spell_suggest_file(su, fname)
10650 suginfo_T *su;
10651 char_u *fname;
10652{
10653 FILE *fd;
10654 char_u line[MAXWLEN * 2];
10655 char_u *p;
10656 int len;
10657 char_u cword[MAXWLEN];
10658
10659 /* Open the file. */
10660 fd = mch_fopen((char *)fname, "r");
10661 if (fd == NULL)
10662 {
10663 EMSG2(_(e_notopen), fname);
10664 return;
10665 }
10666
10667 /* Read it line by line. */
10668 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10669 {
10670 line_breakcheck();
10671
10672 p = vim_strchr(line, '/');
10673 if (p == NULL)
10674 continue; /* No Tab found, just skip the line. */
10675 *p++ = NUL;
10676 if (STRICMP(su->su_badword, line) == 0)
10677 {
10678 /* Match! Isolate the good word, until CR or NL. */
10679 for (len = 0; p[len] >= ' '; ++len)
10680 ;
10681 p[len] = NUL;
10682
10683 /* If the suggestion doesn't have specific case duplicate the case
10684 * of the bad word. */
10685 if (captype(p, NULL) == 0)
10686 {
10687 make_case_word(p, cword, su->su_badflags);
10688 p = cword;
10689 }
10690
10691 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010692 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010693 }
10694 }
10695
10696 fclose(fd);
10697
Bram Moolenaar4770d092006-01-12 23:22:24 +000010698 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10699 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010700 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10701}
10702
10703/*
10704 * Find suggestions for the internal method indicated by "sps_flags".
10705 */
10706 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010707spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010708 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010709 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010710{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010711 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010712 * Load the .sug file(s) that are available and not done yet.
10713 */
10714 suggest_load_files();
10715
10716 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010717 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010718 *
10719 * Set a maximum score to limit the combination of operations that is
10720 * tried.
10721 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010722 suggest_try_special(su);
10723
10724 /*
10725 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10726 * from the .aff file and inserting a space (split the word).
10727 */
10728 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010729
10730 /* For the resulting top-scorers compute the sound-a-like score. */
10731 if (sps_flags & SPS_DOUBLE)
10732 score_comp_sal(su);
10733
10734 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010735 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010736 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010737 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010738 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010739 if (sps_flags & SPS_BEST)
10740 /* Adjust the word score for the suggestions found so far for how
10741 * they sounds like. */
10742 rescore_suggestions(su);
10743
10744 /*
10745 * While going throught the soundfold tree "su_maxscore" is the score
10746 * for the soundfold word, limits the changes that are being tried,
10747 * and "su_sfmaxscore" the rescored score, which is set by
10748 * cleanup_suggestions().
10749 * First find words with a small edit distance, because this is much
10750 * faster and often already finds the top-N suggestions. If we didn't
10751 * find many suggestions try again with a higher edit distance.
10752 * "sl_sounddone" is used to avoid doing the same word twice.
10753 */
10754 suggest_try_soundalike_prep();
10755 su->su_maxscore = SCORE_SFMAX1;
10756 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010757 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010758 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10759 {
10760 /* We didn't find enough matches, try again, allowing more
10761 * changes to the soundfold word. */
10762 su->su_maxscore = SCORE_SFMAX2;
10763 suggest_try_soundalike(su);
10764 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10765 {
10766 /* Still didn't find enough matches, try again, allowing even
10767 * more changes to the soundfold word. */
10768 su->su_maxscore = SCORE_SFMAX3;
10769 suggest_try_soundalike(su);
10770 }
10771 }
10772 su->su_maxscore = su->su_sfmaxscore;
10773 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010774 }
10775
Bram Moolenaar4770d092006-01-12 23:22:24 +000010776 /* When CTRL-C was hit while searching do show the results. Only clear
10777 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010778 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010779 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010780 {
10781 (void)vgetc();
10782 got_int = FALSE;
10783 }
10784
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010785 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010786 {
10787 if (sps_flags & SPS_BEST)
10788 /* Adjust the word score for how it sounds like. */
10789 rescore_suggestions(su);
10790
Bram Moolenaar4770d092006-01-12 23:22:24 +000010791 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10792 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010793 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010794 }
10795}
10796
10797/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010798 * Load the .sug files for languages that have one and weren't loaded yet.
10799 */
10800 static void
10801suggest_load_files()
10802{
10803 langp_T *lp;
10804 int lpi;
10805 slang_T *slang;
10806 char_u *dotp;
10807 FILE *fd;
10808 char_u buf[MAXWLEN];
10809 int i;
10810 time_t timestamp;
10811 int wcount;
10812 int wordnr;
10813 garray_T ga;
10814 int c;
10815
10816 /* Do this for all languages that support sound folding. */
10817 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10818 {
10819 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10820 slang = lp->lp_slang;
10821 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10822 {
10823 /* Change ".spl" to ".sug" and open the file. When the file isn't
10824 * found silently skip it. Do set "sl_sugloaded" so that we
10825 * don't try again and again. */
10826 slang->sl_sugloaded = TRUE;
10827
10828 dotp = vim_strrchr(slang->sl_fname, '.');
10829 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10830 continue;
10831 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010832 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010833 if (fd == NULL)
10834 goto nextone;
10835
10836 /*
10837 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10838 */
10839 for (i = 0; i < VIMSUGMAGICL; ++i)
10840 buf[i] = getc(fd); /* <fileID> */
10841 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10842 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010843 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010844 slang->sl_fname);
10845 goto nextone;
10846 }
10847 c = getc(fd); /* <versionnr> */
10848 if (c < VIMSUGVERSION)
10849 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010850 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010851 slang->sl_fname);
10852 goto nextone;
10853 }
10854 else if (c > VIMSUGVERSION)
10855 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010856 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010857 slang->sl_fname);
10858 goto nextone;
10859 }
10860
10861 /* Check the timestamp, it must be exactly the same as the one in
10862 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010863 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010864 if (timestamp != slang->sl_sugtime)
10865 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010866 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010867 slang->sl_fname);
10868 goto nextone;
10869 }
10870
10871 /*
10872 * <SUGWORDTREE>: <wordtree>
10873 * Read the trie with the soundfolded words.
10874 */
10875 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10876 FALSE, 0) != 0)
10877 {
10878someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010879 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010880 slang->sl_fname);
10881 slang_clear_sug(slang);
10882 goto nextone;
10883 }
10884
10885 /*
10886 * <SUGTABLE>: <sugwcount> <sugline> ...
10887 *
10888 * Read the table with word numbers. We use a file buffer for
10889 * this, because it's so much like a file with lines. Makes it
10890 * possible to swap the info and save on memory use.
10891 */
10892 slang->sl_sugbuf = open_spellbuf();
10893 if (slang->sl_sugbuf == NULL)
10894 goto someerror;
10895 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010896 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010897 if (wcount < 0)
10898 goto someerror;
10899
10900 /* Read all the wordnr lists into the buffer, one NUL terminated
10901 * list per line. */
10902 ga_init2(&ga, 1, 100);
10903 for (wordnr = 0; wordnr < wcount; ++wordnr)
10904 {
10905 ga.ga_len = 0;
10906 for (;;)
10907 {
10908 c = getc(fd); /* <sugline> */
10909 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10910 goto someerror;
10911 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10912 if (c == NUL)
10913 break;
10914 }
10915 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10916 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10917 goto someerror;
10918 }
10919 ga_clear(&ga);
10920
10921 /*
10922 * Need to put word counts in the word tries, so that we can find
10923 * a word by its number.
10924 */
10925 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10926 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10927
10928nextone:
10929 if (fd != NULL)
10930 fclose(fd);
10931 STRCPY(dotp, ".spl");
10932 }
10933 }
10934}
10935
10936
10937/*
10938 * Fill in the wordcount fields for a trie.
10939 * Returns the total number of words.
10940 */
10941 static void
10942tree_count_words(byts, idxs)
10943 char_u *byts;
10944 idx_T *idxs;
10945{
10946 int depth;
10947 idx_T arridx[MAXWLEN];
10948 int curi[MAXWLEN];
10949 int c;
10950 idx_T n;
10951 int wordcount[MAXWLEN];
10952
10953 arridx[0] = 0;
10954 curi[0] = 1;
10955 wordcount[0] = 0;
10956 depth = 0;
10957 while (depth >= 0 && !got_int)
10958 {
10959 if (curi[depth] > byts[arridx[depth]])
10960 {
10961 /* Done all bytes at this node, go up one level. */
10962 idxs[arridx[depth]] = wordcount[depth];
10963 if (depth > 0)
10964 wordcount[depth - 1] += wordcount[depth];
10965
10966 --depth;
10967 fast_breakcheck();
10968 }
10969 else
10970 {
10971 /* Do one more byte at this node. */
10972 n = arridx[depth] + curi[depth];
10973 ++curi[depth];
10974
10975 c = byts[n];
10976 if (c == 0)
10977 {
10978 /* End of word, count it. */
10979 ++wordcount[depth];
10980
10981 /* Skip over any other NUL bytes (same word with different
10982 * flags). */
10983 while (byts[n + 1] == 0)
10984 {
10985 ++n;
10986 ++curi[depth];
10987 }
10988 }
10989 else
10990 {
10991 /* Normal char, go one level deeper to count the words. */
10992 ++depth;
10993 arridx[depth] = idxs[n];
10994 curi[depth] = 1;
10995 wordcount[depth] = 0;
10996 }
10997 }
10998 }
10999}
11000
11001/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011002 * Free the info put in "*su" by spell_find_suggest().
11003 */
11004 static void
11005spell_find_cleanup(su)
11006 suginfo_T *su;
11007{
11008 int i;
11009
11010 /* Free the suggestions. */
11011 for (i = 0; i < su->su_ga.ga_len; ++i)
11012 vim_free(SUG(su->su_ga, i).st_word);
11013 ga_clear(&su->su_ga);
11014 for (i = 0; i < su->su_sga.ga_len; ++i)
11015 vim_free(SUG(su->su_sga, i).st_word);
11016 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017
11018 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011019 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020}
11021
11022/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011023 * Make a copy of "word", with the first letter upper or lower cased, to
11024 * "wcopy[MAXWLEN]". "word" must not be empty.
11025 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 */
11027 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011028onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030 char_u *wcopy;
11031 int upper; /* TRUE: first letter made upper case */
11032{
11033 char_u *p;
11034 int c;
11035 int l;
11036
11037 p = word;
11038#ifdef FEAT_MBYTE
11039 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011040 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 else
11042#endif
11043 c = *p++;
11044 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011045 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011047 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048#ifdef FEAT_MBYTE
11049 if (has_mbyte)
11050 l = mb_char2bytes(c, wcopy);
11051 else
11052#endif
11053 {
11054 l = 1;
11055 wcopy[0] = c;
11056 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011057 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058}
11059
11060/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011061 * Make a copy of "word" with all the letters upper cased into
11062 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 */
11064 static void
11065allcap_copy(word, wcopy)
11066 char_u *word;
11067 char_u *wcopy;
11068{
11069 char_u *s;
11070 char_u *d;
11071 int c;
11072
11073 d = wcopy;
11074 for (s = word; *s != NUL; )
11075 {
11076#ifdef FEAT_MBYTE
11077 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011078 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 else
11080#endif
11081 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011082
11083#ifdef FEAT_MBYTE
11084 /* We only change ß to SS when we are certain latin1 is used. It
11085 * would cause weird errors in other 8-bit encodings. */
11086 if (enc_latin1like && c == 0xdf)
11087 {
11088 c = 'S';
11089 if (d - wcopy >= MAXWLEN - 1)
11090 break;
11091 *d++ = c;
11092 }
11093 else
11094#endif
11095 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096
11097#ifdef FEAT_MBYTE
11098 if (has_mbyte)
11099 {
11100 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11101 break;
11102 d += mb_char2bytes(c, d);
11103 }
11104 else
11105#endif
11106 {
11107 if (d - wcopy >= MAXWLEN - 1)
11108 break;
11109 *d++ = c;
11110 }
11111 }
11112 *d = NUL;
11113}
11114
11115/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011116 * Try finding suggestions by recognizing specific situations.
11117 */
11118 static void
11119suggest_try_special(su)
11120 suginfo_T *su;
11121{
11122 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011123 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011124 int c;
11125 char_u word[MAXWLEN];
11126
11127 /*
11128 * Recognize a word that is repeated: "the the".
11129 */
11130 p = skiptowhite(su->su_fbadword);
11131 len = p - su->su_fbadword;
11132 p = skipwhite(p);
11133 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11134 {
11135 /* Include badflags: if the badword is onecap or allcap
11136 * use that for the goodword too: "The the" -> "The". */
11137 c = su->su_fbadword[len];
11138 su->su_fbadword[len] = NUL;
11139 make_case_word(su->su_fbadword, word, su->su_badflags);
11140 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011141
11142 /* Give a soundalike score of 0, compute the score as if deleting one
11143 * character. */
11144 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011145 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011146 }
11147}
11148
11149/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 * Try finding suggestions by adding/removing/swapping letters.
11151 */
11152 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011153suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 suginfo_T *su;
11155{
11156 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011157 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011159 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011160 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011161
11162 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011163 * to find matches (esp. REP items). Append some more text, changing
11164 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011166 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011167 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011168 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011170 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011172 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011173
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011174 /* If reloading a spell file fails it's still in the list but
11175 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011176 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011177 continue;
11178
Bram Moolenaar4770d092006-01-12 23:22:24 +000011179 /* Try it for this language. Will add possible suggestions. */
11180 suggest_trie_walk(su, lp, fword, FALSE);
11181 }
11182}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183
Bram Moolenaar4770d092006-01-12 23:22:24 +000011184/* Check the maximum score, if we go over it we won't try this change. */
11185#define TRY_DEEPER(su, stack, depth, add) \
11186 (stack[depth].ts_score + (add) < su->su_maxscore)
11187
11188/*
11189 * Try finding suggestions by adding/removing/swapping letters.
11190 *
11191 * This uses a state machine. At each node in the tree we try various
11192 * operations. When trying if an operation works "depth" is increased and the
11193 * stack[] is used to store info. This allows combinations, thus insert one
11194 * character, replace one and delete another. The number of changes is
11195 * limited by su->su_maxscore.
11196 *
11197 * After implementing this I noticed an article by Kemal Oflazer that
11198 * describes something similar: "Error-tolerant Finite State Recognition with
11199 * Applications to Morphological Analysis and Spelling Correction" (1996).
11200 * The implementation in the article is simplified and requires a stack of
11201 * unknown depth. The implementation here only needs a stack depth equal to
11202 * the length of the word.
11203 *
11204 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11205 * The mechanism is the same, but we find a match with a sound-folded word
11206 * that comes from one or more original words. Each of these words may be
11207 * added, this is done by add_sound_suggest().
11208 * Don't use:
11209 * the prefix tree or the keep-case tree
11210 * "su->su_badlen"
11211 * anything to do with upper and lower case
11212 * anything to do with word or non-word characters ("spell_iswordp()")
11213 * banned words
11214 * word flags (rare, region, compounding)
11215 * word splitting for now
11216 * "similar_chars()"
11217 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11218 */
11219 static void
11220suggest_trie_walk(su, lp, fword, soundfold)
11221 suginfo_T *su;
11222 langp_T *lp;
11223 char_u *fword;
11224 int soundfold;
11225{
11226 char_u tword[MAXWLEN]; /* good word collected so far */
11227 trystate_T stack[MAXWLEN];
11228 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11229 * concatanation of prefix compound
11230 * words and split word. NUL terminated
11231 * when going deeper but not when coming
11232 * back. */
11233 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11234 trystate_T *sp;
11235 int newscore;
11236 int score;
11237 char_u *byts, *fbyts, *pbyts;
11238 idx_T *idxs, *fidxs, *pidxs;
11239 int depth;
11240 int c, c2, c3;
11241 int n = 0;
11242 int flags;
11243 garray_T *gap;
11244 idx_T arridx;
11245 int len;
11246 char_u *p;
11247 fromto_T *ftp;
11248 int fl = 0, tl;
11249 int repextra = 0; /* extra bytes in fword[] from REP item */
11250 slang_T *slang = lp->lp_slang;
11251 int fword_ends;
11252 int goodword_ends;
11253#ifdef DEBUG_TRIEWALK
11254 /* Stores the name of the change made at each level. */
11255 char_u changename[MAXWLEN][80];
11256#endif
11257 int breakcheckcount = 1000;
11258 int compound_ok;
11259
11260 /*
11261 * Go through the whole case-fold tree, try changes at each node.
11262 * "tword[]" contains the word collected from nodes in the tree.
11263 * "fword[]" the word we are trying to match with (initially the bad
11264 * word).
11265 */
11266 depth = 0;
11267 sp = &stack[0];
11268 vim_memset(sp, 0, sizeof(trystate_T));
11269 sp->ts_curi = 1;
11270
11271 if (soundfold)
11272 {
11273 /* Going through the soundfold tree. */
11274 byts = fbyts = slang->sl_sbyts;
11275 idxs = fidxs = slang->sl_sidxs;
11276 pbyts = NULL;
11277 pidxs = NULL;
11278 sp->ts_prefixdepth = PFD_NOPREFIX;
11279 sp->ts_state = STATE_START;
11280 }
11281 else
11282 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011283 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011284 * When there are postponed prefixes we need to use these first. At
11285 * the end of the prefix we continue in the case-fold tree.
11286 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011287 fbyts = slang->sl_fbyts;
11288 fidxs = slang->sl_fidxs;
11289 pbyts = slang->sl_pbyts;
11290 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011291 if (pbyts != NULL)
11292 {
11293 byts = pbyts;
11294 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011295 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011296 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11297 }
11298 else
11299 {
11300 byts = fbyts;
11301 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011302 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011303 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011304 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011305 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011306
Bram Moolenaar4770d092006-01-12 23:22:24 +000011307 /*
11308 * Loop to find all suggestions. At each round we either:
11309 * - For the current state try one operation, advance "ts_curi",
11310 * increase "depth".
11311 * - When a state is done go to the next, set "ts_state".
11312 * - When all states are tried decrease "depth".
11313 */
11314 while (depth >= 0 && !got_int)
11315 {
11316 sp = &stack[depth];
11317 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011319 case STATE_START:
11320 case STATE_NOPREFIX:
11321 /*
11322 * Start of node: Deal with NUL bytes, which means
11323 * tword[] may end here.
11324 */
11325 arridx = sp->ts_arridx; /* current node in the tree */
11326 len = byts[arridx]; /* bytes in this node */
11327 arridx += sp->ts_curi; /* index of current byte */
11328
11329 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011331 /* Skip over the NUL bytes, we use them later. */
11332 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11333 ;
11334 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335
Bram Moolenaar4770d092006-01-12 23:22:24 +000011336 /* Always past NUL bytes now. */
11337 n = (int)sp->ts_state;
11338 sp->ts_state = STATE_ENDNUL;
11339 sp->ts_save_badflags = su->su_badflags;
11340
11341 /* At end of a prefix or at start of prefixtree: check for
11342 * following word. */
11343 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011344 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011345 /* Set su->su_badflags to the caps type at this position.
11346 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011347#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011348 if (has_mbyte)
11349 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11350 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011351#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011352 n = sp->ts_fidx;
11353 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11354 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011355 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011356#ifdef DEBUG_TRIEWALK
11357 sprintf(changename[depth], "prefix");
11358#endif
11359 go_deeper(stack, depth, 0);
11360 ++depth;
11361 sp = &stack[depth];
11362 sp->ts_prefixdepth = depth - 1;
11363 byts = fbyts;
11364 idxs = fidxs;
11365 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011366
Bram Moolenaar4770d092006-01-12 23:22:24 +000011367 /* Move the prefix to preword[] with the right case
11368 * and make find_keepcap_word() works. */
11369 tword[sp->ts_twordlen] = NUL;
11370 make_case_word(tword + sp->ts_splitoff,
11371 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011372 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011373 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011374 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011375 break;
11376 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011377
Bram Moolenaar4770d092006-01-12 23:22:24 +000011378 if (sp->ts_curi > len || byts[arridx] != 0)
11379 {
11380 /* Past bytes in node and/or past NUL bytes. */
11381 sp->ts_state = STATE_ENDNUL;
11382 sp->ts_save_badflags = su->su_badflags;
11383 break;
11384 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011385
Bram Moolenaar4770d092006-01-12 23:22:24 +000011386 /*
11387 * End of word in tree.
11388 */
11389 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390
Bram Moolenaar4770d092006-01-12 23:22:24 +000011391 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011392
11393 /* Skip words with the NOSUGGEST flag. */
11394 if (flags & WF_NOSUGGEST)
11395 break;
11396
Bram Moolenaar4770d092006-01-12 23:22:24 +000011397 fword_ends = (fword[sp->ts_fidx] == NUL
11398 || (soundfold
11399 ? vim_iswhite(fword[sp->ts_fidx])
11400 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11401 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402
Bram Moolenaar4770d092006-01-12 23:22:24 +000011403 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011404 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011405 {
11406 /* There was a prefix before the word. Check that the prefix
11407 * can be used with this word. */
11408 /* Count the length of the NULs in the prefix. If there are
11409 * none this must be the first try without a prefix. */
11410 n = stack[sp->ts_prefixdepth].ts_arridx;
11411 len = pbyts[n++];
11412 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11413 ;
11414 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011415 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011416 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011417 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011418 if (c == 0)
11419 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011420
Bram Moolenaar4770d092006-01-12 23:22:24 +000011421 /* Use the WF_RARE flag for a rare prefix. */
11422 if (c & WF_RAREPFX)
11423 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011424
Bram Moolenaar4770d092006-01-12 23:22:24 +000011425 /* Tricky: when checking for both prefix and compounding
11426 * we run into the prefix flag first.
11427 * Remember that it's OK, so that we accept the prefix
11428 * when arriving at a compound flag. */
11429 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011430 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011431 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011432
Bram Moolenaar4770d092006-01-12 23:22:24 +000011433 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11434 * appending another compound word below. */
11435 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011436 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011437 goodword_ends = FALSE;
11438 else
11439 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011440
Bram Moolenaar4770d092006-01-12 23:22:24 +000011441 p = NULL;
11442 compound_ok = TRUE;
11443 if (sp->ts_complen > sp->ts_compsplit)
11444 {
11445 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011446 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011447 /* There was a word before this word. When there was no
11448 * change in this word (it was correct) add the first word
11449 * as a suggestion. If this word was corrected too, we
11450 * need to check if a correct word follows. */
11451 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011452 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011453 && STRNCMP(fword + sp->ts_splitfidx,
11454 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011455 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011456 {
11457 preword[sp->ts_prewordlen] = NUL;
11458 newscore = score_wordcount_adj(slang, sp->ts_score,
11459 preword + sp->ts_prewordlen,
11460 sp->ts_prewordlen > 0);
11461 /* Add the suggestion if the score isn't too bad. */
11462 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011463 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011464 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 newscore, 0, FALSE,
11466 lp->lp_sallang, FALSE);
11467 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011468 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011469 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011470 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011471 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011472 /* There was a compound word before this word. If this
11473 * word does not support compounding then give up
11474 * (splitting is tried for the word without compound
11475 * flag). */
11476 if (((unsigned)flags >> 24) == 0
11477 || sp->ts_twordlen - sp->ts_splitoff
11478 < slang->sl_compminlen)
11479 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011480#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011481 /* For multi-byte chars check character length against
11482 * COMPOUNDMIN. */
11483 if (has_mbyte
11484 && slang->sl_compminlen > 0
11485 && mb_charlen(tword + sp->ts_splitoff)
11486 < slang->sl_compminlen)
11487 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011488#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011489
Bram Moolenaar4770d092006-01-12 23:22:24 +000011490 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11491 compflags[sp->ts_complen + 1] = NUL;
11492 vim_strncpy(preword + sp->ts_prewordlen,
11493 tword + sp->ts_splitoff,
11494 sp->ts_twordlen - sp->ts_splitoff);
11495 p = preword;
11496 while (*skiptowhite(p) != NUL)
11497 p = skipwhite(skiptowhite(p));
11498 if (fword_ends && !can_compound(slang, p,
11499 compflags + sp->ts_compsplit))
11500 /* Compound is not allowed. But it may still be
11501 * possible if we add another (short) word. */
11502 compound_ok = FALSE;
11503
11504 /* Get pointer to last char of previous word. */
11505 p = preword + sp->ts_prewordlen;
11506 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011507 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011509
Bram Moolenaar4770d092006-01-12 23:22:24 +000011510 /*
11511 * Form the word with proper case in preword.
11512 * If there is a word from a previous split, append.
11513 * For the soundfold tree don't change the case, simply append.
11514 */
11515 if (soundfold)
11516 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11517 else if (flags & WF_KEEPCAP)
11518 /* Must find the word in the keep-case tree. */
11519 find_keepcap_word(slang, tword + sp->ts_splitoff,
11520 preword + sp->ts_prewordlen);
11521 else
11522 {
11523 /* Include badflags: If the badword is onecap or allcap
11524 * use that for the goodword too. But if the badword is
11525 * allcap and it's only one char long use onecap. */
11526 c = su->su_badflags;
11527 if ((c & WF_ALLCAP)
11528#ifdef FEAT_MBYTE
11529 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11530#else
11531 && su->su_badlen == 1
11532#endif
11533 )
11534 c = WF_ONECAP;
11535 c |= flags;
11536
11537 /* When appending a compound word after a word character don't
11538 * use Onecap. */
11539 if (p != NULL && spell_iswordp_nmw(p))
11540 c &= ~WF_ONECAP;
11541 make_case_word(tword + sp->ts_splitoff,
11542 preword + sp->ts_prewordlen, c);
11543 }
11544
11545 if (!soundfold)
11546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011547 /* Don't use a banned word. It may appear again as a good
11548 * word, thus remember it. */
11549 if (flags & WF_BANNED)
11550 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011551 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552 break;
11553 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011554 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011555 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11556 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011557 {
11558 if (slang->sl_compprog == NULL)
11559 break;
11560 /* the word so far was banned but we may try compounding */
11561 goodword_ends = FALSE;
11562 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564
Bram Moolenaar4770d092006-01-12 23:22:24 +000011565 newscore = 0;
11566 if (!soundfold) /* soundfold words don't have flags */
11567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011569 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011570 newscore += SCORE_REGION;
11571 if (flags & WF_RARE)
11572 newscore += SCORE_RARE;
11573
Bram Moolenaar0c405862005-06-22 22:26:26 +000011574 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011575 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011576 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578
Bram Moolenaar4770d092006-01-12 23:22:24 +000011579 /* TODO: how about splitting in the soundfold tree? */
11580 if (fword_ends
11581 && goodword_ends
11582 && sp->ts_fidx >= sp->ts_fidxtry
11583 && compound_ok)
11584 {
11585 /* The badword also ends: add suggestions. */
11586#ifdef DEBUG_TRIEWALK
11587 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011588 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011589 int j;
11590
11591 /* print the stack of changes that brought us here */
11592 smsg("------ %s -------", fword);
11593 for (j = 0; j < depth; ++j)
11594 smsg("%s", changename[j]);
11595 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011596#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011597 if (soundfold)
11598 {
11599 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011600 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011601 add_sound_suggest(su, preword, sp->ts_score, lp);
11602 }
11603 else
11604 {
11605 /* Give a penalty when changing non-word char to word
11606 * char, e.g., "thes," -> "these". */
11607 p = fword + sp->ts_fidx;
11608 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011609 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011610 {
11611 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011612 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011613 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011614 newscore += SCORE_NONWORD;
11615 }
11616
Bram Moolenaar4770d092006-01-12 23:22:24 +000011617 /* Give a bonus to words seen before. */
11618 score = score_wordcount_adj(slang,
11619 sp->ts_score + newscore,
11620 preword + sp->ts_prewordlen,
11621 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011622
Bram Moolenaar4770d092006-01-12 23:22:24 +000011623 /* Add the suggestion if the score isn't too bad. */
11624 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011625 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011626 add_suggestion(su, &su->su_ga, preword,
11627 sp->ts_fidx - repextra,
11628 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011629
11630 if (su->su_badflags & WF_MIXCAP)
11631 {
11632 /* We really don't know if the word should be
11633 * upper or lower case, add both. */
11634 c = captype(preword, NULL);
11635 if (c == 0 || c == WF_ALLCAP)
11636 {
11637 make_case_word(tword + sp->ts_splitoff,
11638 preword + sp->ts_prewordlen,
11639 c == 0 ? WF_ALLCAP : 0);
11640
11641 add_suggestion(su, &su->su_ga, preword,
11642 sp->ts_fidx - repextra,
11643 score + SCORE_ICASE, 0, FALSE,
11644 lp->lp_sallang, FALSE);
11645 }
11646 }
11647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011648 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011649 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011650
Bram Moolenaar4770d092006-01-12 23:22:24 +000011651 /*
11652 * Try word split and/or compounding.
11653 */
11654 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011655#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011656 /* Don't split halfway a character. */
11657 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011658#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011659 )
11660 {
11661 int try_compound;
11662 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011663
Bram Moolenaar4770d092006-01-12 23:22:24 +000011664 /* If past the end of the bad word don't try a split.
11665 * Otherwise try changing the next word. E.g., find
11666 * suggestions for "the the" where the second "the" is
11667 * different. It's done like a split.
11668 * TODO: word split for soundfold words */
11669 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11670 && !soundfold;
11671
11672 /* Get here in several situations:
11673 * 1. The word in the tree ends:
11674 * If the word allows compounding try that. Otherwise try
11675 * a split by inserting a space. For both check that a
11676 * valid words starts at fword[sp->ts_fidx].
11677 * For NOBREAK do like compounding to be able to check if
11678 * the next word is valid.
11679 * 2. The badword does end, but it was due to a change (e.g.,
11680 * a swap). No need to split, but do check that the
11681 * following word is valid.
11682 * 3. The badword and the word in the tree end. It may still
11683 * be possible to compound another (short) word.
11684 */
11685 try_compound = FALSE;
11686 if (!soundfold
11687 && slang->sl_compprog != NULL
11688 && ((unsigned)flags >> 24) != 0
11689 && sp->ts_twordlen - sp->ts_splitoff
11690 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011691#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011692 && (!has_mbyte
11693 || slang->sl_compminlen == 0
11694 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011695 >= slang->sl_compminlen)
11696#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011697 && (slang->sl_compsylmax < MAXWLEN
11698 || sp->ts_complen + 1 - sp->ts_compsplit
11699 < slang->sl_compmax)
11700 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11701 ? slang->sl_compstartflags
11702 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011703 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011704 {
11705 try_compound = TRUE;
11706 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11707 compflags[sp->ts_complen + 1] = NUL;
11708 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011709
Bram Moolenaar4770d092006-01-12 23:22:24 +000011710 /* For NOBREAK we never try splitting, it won't make any word
11711 * valid. */
11712 if (slang->sl_nobreak)
11713 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011714
Bram Moolenaar4770d092006-01-12 23:22:24 +000011715 /* If we could add a compound word, and it's also possible to
11716 * split at this point, do the split first and set
11717 * TSF_DIDSPLIT to avoid doing it again. */
11718 else if (!fword_ends
11719 && try_compound
11720 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11721 {
11722 try_compound = FALSE;
11723 sp->ts_flags |= TSF_DIDSPLIT;
11724 --sp->ts_curi; /* do the same NUL again */
11725 compflags[sp->ts_complen] = NUL;
11726 }
11727 else
11728 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011729
Bram Moolenaar4770d092006-01-12 23:22:24 +000011730 if (try_split || try_compound)
11731 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011732 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011733 {
11734 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011735 * words so far are valid for compounding. If there
11736 * is only one word it must not have the NEEDCOMPOUND
11737 * flag. */
11738 if (sp->ts_complen == sp->ts_compsplit
11739 && (flags & WF_NEEDCOMP))
11740 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011741 p = preword;
11742 while (*skiptowhite(p) != NUL)
11743 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011744 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011745 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011746 compflags + sp->ts_compsplit))
11747 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011748
11749 if (slang->sl_nosplitsugs)
11750 newscore += SCORE_SPLIT_NO;
11751 else
11752 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011753
11754 /* Give a bonus to words seen before. */
11755 newscore = score_wordcount_adj(slang, newscore,
11756 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011757 }
11758
Bram Moolenaar4770d092006-01-12 23:22:24 +000011759 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011761 go_deeper(stack, depth, newscore);
11762#ifdef DEBUG_TRIEWALK
11763 if (!try_compound && !fword_ends)
11764 sprintf(changename[depth], "%.*s-%s: split",
11765 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11766 else
11767 sprintf(changename[depth], "%.*s-%s: compound",
11768 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11769#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011770 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011771 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011772 sp->ts_state = STATE_SPLITUNDO;
11773
11774 ++depth;
11775 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011777 /* Append a space to preword when splitting. */
11778 if (!try_compound && !fword_ends)
11779 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011780 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011781 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011782 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011783
11784 /* If the badword has a non-word character at this
11785 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011786 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011787 * character when the word ends. But only when the
11788 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011789 if (((!try_compound && !spell_iswordp_nmw(fword
11790 + sp->ts_fidx))
11791 || fword_ends)
11792 && fword[sp->ts_fidx] != NUL
11793 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011794 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011795 int l;
11796
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011797#ifdef FEAT_MBYTE
11798 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011799 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011800 else
11801#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011802 l = 1;
11803 if (fword_ends)
11804 {
11805 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011806 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011807 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011808 sp->ts_prewordlen += l;
11809 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011810 }
11811 else
11812 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11813 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011814 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011815
Bram Moolenaard12a1322005-08-21 22:08:24 +000011816 /* When compounding include compound flag in
11817 * compflags[] (already set above). When splitting we
11818 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011819 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011820 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011821 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011822 sp->ts_compsplit = sp->ts_complen;
11823 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011824
Bram Moolenaar53805d12005-08-01 07:08:33 +000011825 /* set su->su_badflags to the caps type at this
11826 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011827#ifdef FEAT_MBYTE
11828 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011829 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011830 else
11831#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011832 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011833 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011834 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011837 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011838
11839 /* If there are postponed prefixes, try these too. */
11840 if (pbyts != NULL)
11841 {
11842 byts = pbyts;
11843 idxs = pidxs;
11844 sp->ts_prefixdepth = PFD_PREFIXTREE;
11845 sp->ts_state = STATE_NOPREFIX;
11846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 }
11848 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011849 }
11850 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851
Bram Moolenaar4770d092006-01-12 23:22:24 +000011852 case STATE_SPLITUNDO:
11853 /* Undo the changes done for word split or compound word. */
11854 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855
Bram Moolenaar4770d092006-01-12 23:22:24 +000011856 /* Continue looking for NUL bytes. */
11857 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011858
Bram Moolenaar4770d092006-01-12 23:22:24 +000011859 /* In case we went into the prefix tree. */
11860 byts = fbyts;
11861 idxs = fidxs;
11862 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863
Bram Moolenaar4770d092006-01-12 23:22:24 +000011864 case STATE_ENDNUL:
11865 /* Past the NUL bytes in the node. */
11866 su->su_badflags = sp->ts_save_badflags;
11867 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011868#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011869 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011870#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011871 )
11872 {
11873 /* The badword ends, can't use STATE_PLAIN. */
11874 sp->ts_state = STATE_DEL;
11875 break;
11876 }
11877 sp->ts_state = STATE_PLAIN;
11878 /*FALLTHROUGH*/
11879
11880 case STATE_PLAIN:
11881 /*
11882 * Go over all possible bytes at this node, add each to tword[]
11883 * and use child node. "ts_curi" is the index.
11884 */
11885 arridx = sp->ts_arridx;
11886 if (sp->ts_curi > byts[arridx])
11887 {
11888 /* Done all bytes at this node, do next state. When still at
11889 * already changed bytes skip the other tricks. */
11890 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011893 sp->ts_state = STATE_FINAL;
11894 }
11895 else
11896 {
11897 arridx += sp->ts_curi++;
11898 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011899
Bram Moolenaar4770d092006-01-12 23:22:24 +000011900 /* Normal byte, go one level deeper. If it's not equal to the
11901 * byte in the bad word adjust the score. But don't even try
11902 * when the byte was already changed. And don't try when we
11903 * just deleted this byte, accepting it is always cheaper then
11904 * delete + substitute. */
11905 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011906#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011907 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011908#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011909 )
11910 newscore = 0;
11911 else
11912 newscore = SCORE_SUBST;
11913 if ((newscore == 0
11914 || (sp->ts_fidx >= sp->ts_fidxtry
11915 && ((sp->ts_flags & TSF_DIDDEL) == 0
11916 || c != fword[sp->ts_delidx])))
11917 && TRY_DEEPER(su, stack, depth, newscore))
11918 {
11919 go_deeper(stack, depth, newscore);
11920#ifdef DEBUG_TRIEWALK
11921 if (newscore > 0)
11922 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11923 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11924 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011925 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011926 sprintf(changename[depth], "%.*s-%s: accept %c",
11927 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11928 fword[sp->ts_fidx]);
11929#endif
11930 ++depth;
11931 sp = &stack[depth];
11932 ++sp->ts_fidx;
11933 tword[sp->ts_twordlen++] = c;
11934 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011935#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011936 if (newscore == SCORE_SUBST)
11937 sp->ts_isdiff = DIFF_YES;
11938 if (has_mbyte)
11939 {
11940 /* Multi-byte characters are a bit complicated to
11941 * handle: They differ when any of the bytes differ
11942 * and then their length may also differ. */
11943 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011944 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011945 /* First byte. */
11946 sp->ts_tcharidx = 0;
11947 sp->ts_tcharlen = MB_BYTE2LEN(c);
11948 sp->ts_fcharstart = sp->ts_fidx - 1;
11949 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011950 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011951 }
11952 else if (sp->ts_isdiff == DIFF_INSERT)
11953 /* When inserting trail bytes don't advance in the
11954 * bad word. */
11955 --sp->ts_fidx;
11956 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11957 {
11958 /* Last byte of character. */
11959 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011960 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011961 /* Correct ts_fidx for the byte length of the
11962 * character (we didn't check that before). */
11963 sp->ts_fidx = sp->ts_fcharstart
11964 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011965 fword[sp->ts_fcharstart]);
11966
Bram Moolenaar4770d092006-01-12 23:22:24 +000011967 /* For changing a composing character adjust
11968 * the score from SCORE_SUBST to
11969 * SCORE_SUBCOMP. */
11970 if (enc_utf8
11971 && utf_iscomposing(
11972 mb_ptr2char(tword
11973 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011974 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011975 && utf_iscomposing(
11976 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011977 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011978 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011979 SCORE_SUBST - SCORE_SUBCOMP;
11980
Bram Moolenaar4770d092006-01-12 23:22:24 +000011981 /* For a similar character adjust score from
11982 * SCORE_SUBST to SCORE_SIMILAR. */
11983 else if (!soundfold
11984 && slang->sl_has_map
11985 && similar_chars(slang,
11986 mb_ptr2char(tword
11987 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011988 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011989 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011990 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011991 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011992 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011993 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011994 else if (sp->ts_isdiff == DIFF_INSERT
11995 && sp->ts_twordlen > sp->ts_tcharlen)
11996 {
11997 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11998 c = mb_ptr2char(p);
11999 if (enc_utf8 && utf_iscomposing(c))
12000 {
12001 /* Inserting a composing char doesn't
12002 * count that much. */
12003 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12004 }
12005 else
12006 {
12007 /* If the previous character was the same,
12008 * thus doubling a character, give a bonus
12009 * to the score. Also for the soundfold
12010 * tree (might seem illogical but does
12011 * give better scores). */
12012 mb_ptr_back(tword, p);
12013 if (c == mb_ptr2char(p))
12014 sp->ts_score -= SCORE_INS
12015 - SCORE_INSDUP;
12016 }
12017 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018
Bram Moolenaar4770d092006-01-12 23:22:24 +000012019 /* Starting a new char, reset the length. */
12020 sp->ts_tcharlen = 0;
12021 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012022 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012023 else
12024#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012025 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012026 /* If we found a similar char adjust the score.
12027 * We do this after calling go_deeper() because
12028 * it's slow. */
12029 if (newscore != 0
12030 && !soundfold
12031 && slang->sl_has_map
12032 && similar_chars(slang,
12033 c, fword[sp->ts_fidx - 1]))
12034 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012037 }
12038 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039
Bram Moolenaar4770d092006-01-12 23:22:24 +000012040 case STATE_DEL:
12041#ifdef FEAT_MBYTE
12042 /* When past the first byte of a multi-byte char don't try
12043 * delete/insert/swap a character. */
12044 if (has_mbyte && sp->ts_tcharlen > 0)
12045 {
12046 sp->ts_state = STATE_FINAL;
12047 break;
12048 }
12049#endif
12050 /*
12051 * Try skipping one character in the bad word (delete it).
12052 */
12053 sp->ts_state = STATE_INS_PREP;
12054 sp->ts_curi = 1;
12055 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12056 /* Deleting a vowel at the start of a word counts less, see
12057 * soundalike_score(). */
12058 newscore = 2 * SCORE_DEL / 3;
12059 else
12060 newscore = SCORE_DEL;
12061 if (fword[sp->ts_fidx] != NUL
12062 && TRY_DEEPER(su, stack, depth, newscore))
12063 {
12064 go_deeper(stack, depth, newscore);
12065#ifdef DEBUG_TRIEWALK
12066 sprintf(changename[depth], "%.*s-%s: delete %c",
12067 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12068 fword[sp->ts_fidx]);
12069#endif
12070 ++depth;
12071
12072 /* Remember what character we deleted, so that we can avoid
12073 * inserting it again. */
12074 stack[depth].ts_flags |= TSF_DIDDEL;
12075 stack[depth].ts_delidx = sp->ts_fidx;
12076
12077 /* Advance over the character in fword[]. Give a bonus to the
12078 * score if the same character is following "nn" -> "n". It's
12079 * a bit illogical for soundfold tree but it does give better
12080 * results. */
12081#ifdef FEAT_MBYTE
12082 if (has_mbyte)
12083 {
12084 c = mb_ptr2char(fword + sp->ts_fidx);
12085 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12086 if (enc_utf8 && utf_iscomposing(c))
12087 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12088 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12089 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12090 }
12091 else
12092#endif
12093 {
12094 ++stack[depth].ts_fidx;
12095 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12096 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12097 }
12098 break;
12099 }
12100 /*FALLTHROUGH*/
12101
12102 case STATE_INS_PREP:
12103 if (sp->ts_flags & TSF_DIDDEL)
12104 {
12105 /* If we just deleted a byte then inserting won't make sense,
12106 * a substitute is always cheaper. */
12107 sp->ts_state = STATE_SWAP;
12108 break;
12109 }
12110
12111 /* skip over NUL bytes */
12112 n = sp->ts_arridx;
12113 for (;;)
12114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012115 if (sp->ts_curi > byts[n])
12116 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012117 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012118 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012119 break;
12120 }
12121 if (byts[n + sp->ts_curi] != NUL)
12122 {
12123 /* Found a byte to insert. */
12124 sp->ts_state = STATE_INS;
12125 break;
12126 }
12127 ++sp->ts_curi;
12128 }
12129 break;
12130
12131 /*FALLTHROUGH*/
12132
12133 case STATE_INS:
12134 /* Insert one byte. Repeat this for each possible byte at this
12135 * node. */
12136 n = sp->ts_arridx;
12137 if (sp->ts_curi > byts[n])
12138 {
12139 /* Done all bytes at this node, go to next state. */
12140 sp->ts_state = STATE_SWAP;
12141 break;
12142 }
12143
12144 /* Do one more byte at this node, but:
12145 * - Skip NUL bytes.
12146 * - Skip the byte if it's equal to the byte in the word,
12147 * accepting that byte is always better.
12148 */
12149 n += sp->ts_curi++;
12150 c = byts[n];
12151 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12152 /* Inserting a vowel at the start of a word counts less,
12153 * see soundalike_score(). */
12154 newscore = 2 * SCORE_INS / 3;
12155 else
12156 newscore = SCORE_INS;
12157 if (c != fword[sp->ts_fidx]
12158 && TRY_DEEPER(su, stack, depth, newscore))
12159 {
12160 go_deeper(stack, depth, newscore);
12161#ifdef DEBUG_TRIEWALK
12162 sprintf(changename[depth], "%.*s-%s: insert %c",
12163 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12164 c);
12165#endif
12166 ++depth;
12167 sp = &stack[depth];
12168 tword[sp->ts_twordlen++] = c;
12169 sp->ts_arridx = idxs[n];
12170#ifdef FEAT_MBYTE
12171 if (has_mbyte)
12172 {
12173 fl = MB_BYTE2LEN(c);
12174 if (fl > 1)
12175 {
12176 /* There are following bytes for the same character.
12177 * We must find all bytes before trying
12178 * delete/insert/swap/etc. */
12179 sp->ts_tcharlen = fl;
12180 sp->ts_tcharidx = 1;
12181 sp->ts_isdiff = DIFF_INSERT;
12182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012183 }
12184 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012185 fl = 1;
12186 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012187#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012188 {
12189 /* If the previous character was the same, thus doubling a
12190 * character, give a bonus to the score. Also for
12191 * soundfold words (illogical but does give a better
12192 * score). */
12193 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012194 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012195 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012196 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012197 }
12198 break;
12199
12200 case STATE_SWAP:
12201 /*
12202 * Swap two bytes in the bad word: "12" -> "21".
12203 * We change "fword" here, it's changed back afterwards at
12204 * STATE_UNSWAP.
12205 */
12206 p = fword + sp->ts_fidx;
12207 c = *p;
12208 if (c == NUL)
12209 {
12210 /* End of word, can't swap or replace. */
12211 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012212 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012214
Bram Moolenaar4770d092006-01-12 23:22:24 +000012215 /* Don't swap if the first character is not a word character.
12216 * SWAP3 etc. also don't make sense then. */
12217 if (!soundfold && !spell_iswordp(p, curbuf))
12218 {
12219 sp->ts_state = STATE_REP_INI;
12220 break;
12221 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012222
Bram Moolenaar4770d092006-01-12 23:22:24 +000012223#ifdef FEAT_MBYTE
12224 if (has_mbyte)
12225 {
12226 n = mb_cptr2len(p);
12227 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012228 if (p[n] == NUL)
12229 c2 = NUL;
12230 else if (!soundfold && !spell_iswordp(p + n, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012231 c2 = c; /* don't swap non-word char */
12232 else
12233 c2 = mb_ptr2char(p + n);
12234 }
12235 else
12236#endif
12237 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012238 if (p[1] == NUL)
12239 c2 = NUL;
12240 else if (!soundfold && !spell_iswordp(p + 1, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012241 c2 = c; /* don't swap non-word char */
12242 else
12243 c2 = p[1];
12244 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012245
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012246 /* When the second character is NUL we can't swap. */
12247 if (c2 == NUL)
12248 {
12249 sp->ts_state = STATE_REP_INI;
12250 break;
12251 }
12252
Bram Moolenaar4770d092006-01-12 23:22:24 +000012253 /* When characters are identical, swap won't do anything.
12254 * Also get here if the second char is not a word character. */
12255 if (c == c2)
12256 {
12257 sp->ts_state = STATE_SWAP3;
12258 break;
12259 }
12260 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12261 {
12262 go_deeper(stack, depth, SCORE_SWAP);
12263#ifdef DEBUG_TRIEWALK
12264 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12265 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12266 c, c2);
12267#endif
12268 sp->ts_state = STATE_UNSWAP;
12269 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012270#ifdef FEAT_MBYTE
12271 if (has_mbyte)
12272 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012273 fl = mb_char2len(c2);
12274 mch_memmove(p, p + n, fl);
12275 mb_char2bytes(c, p + fl);
12276 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012277 }
12278 else
12279#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012280 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012281 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012282 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012283 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012284 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012285 }
12286 else
12287 /* If this swap doesn't work then SWAP3 won't either. */
12288 sp->ts_state = STATE_REP_INI;
12289 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012290
Bram Moolenaar4770d092006-01-12 23:22:24 +000012291 case STATE_UNSWAP:
12292 /* Undo the STATE_SWAP swap: "21" -> "12". */
12293 p = fword + sp->ts_fidx;
12294#ifdef FEAT_MBYTE
12295 if (has_mbyte)
12296 {
12297 n = MB_BYTE2LEN(*p);
12298 c = mb_ptr2char(p + n);
12299 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12300 mb_char2bytes(c, p);
12301 }
12302 else
12303#endif
12304 {
12305 c = *p;
12306 *p = p[1];
12307 p[1] = c;
12308 }
12309 /*FALLTHROUGH*/
12310
12311 case STATE_SWAP3:
12312 /* Swap two bytes, skipping one: "123" -> "321". We change
12313 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12314 p = fword + sp->ts_fidx;
12315#ifdef FEAT_MBYTE
12316 if (has_mbyte)
12317 {
12318 n = mb_cptr2len(p);
12319 c = mb_ptr2char(p);
12320 fl = mb_cptr2len(p + n);
12321 c2 = mb_ptr2char(p + n);
12322 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12323 c3 = c; /* don't swap non-word char */
12324 else
12325 c3 = mb_ptr2char(p + n + fl);
12326 }
12327 else
12328#endif
12329 {
12330 c = *p;
12331 c2 = p[1];
12332 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12333 c3 = c; /* don't swap non-word char */
12334 else
12335 c3 = p[2];
12336 }
12337
12338 /* When characters are identical: "121" then SWAP3 result is
12339 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12340 * same as SWAP on next char: "112". Thus skip all swapping.
12341 * Also skip when c3 is NUL.
12342 * Also get here when the third character is not a word character.
12343 * Second character may any char: "a.b" -> "b.a" */
12344 if (c == c3 || c3 == NUL)
12345 {
12346 sp->ts_state = STATE_REP_INI;
12347 break;
12348 }
12349 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12350 {
12351 go_deeper(stack, depth, SCORE_SWAP3);
12352#ifdef DEBUG_TRIEWALK
12353 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12354 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12355 c, c3);
12356#endif
12357 sp->ts_state = STATE_UNSWAP3;
12358 ++depth;
12359#ifdef FEAT_MBYTE
12360 if (has_mbyte)
12361 {
12362 tl = mb_char2len(c3);
12363 mch_memmove(p, p + n + fl, tl);
12364 mb_char2bytes(c2, p + tl);
12365 mb_char2bytes(c, p + fl + tl);
12366 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12367 }
12368 else
12369#endif
12370 {
12371 p[0] = p[2];
12372 p[2] = c;
12373 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12374 }
12375 }
12376 else
12377 sp->ts_state = STATE_REP_INI;
12378 break;
12379
12380 case STATE_UNSWAP3:
12381 /* Undo STATE_SWAP3: "321" -> "123" */
12382 p = fword + sp->ts_fidx;
12383#ifdef FEAT_MBYTE
12384 if (has_mbyte)
12385 {
12386 n = MB_BYTE2LEN(*p);
12387 c2 = mb_ptr2char(p + n);
12388 fl = MB_BYTE2LEN(p[n]);
12389 c = mb_ptr2char(p + n + fl);
12390 tl = MB_BYTE2LEN(p[n + fl]);
12391 mch_memmove(p + fl + tl, p, n);
12392 mb_char2bytes(c, p);
12393 mb_char2bytes(c2, p + tl);
12394 p = p + tl;
12395 }
12396 else
12397#endif
12398 {
12399 c = *p;
12400 *p = p[2];
12401 p[2] = c;
12402 ++p;
12403 }
12404
12405 if (!soundfold && !spell_iswordp(p, curbuf))
12406 {
12407 /* Middle char is not a word char, skip the rotate. First and
12408 * third char were already checked at swap and swap3. */
12409 sp->ts_state = STATE_REP_INI;
12410 break;
12411 }
12412
12413 /* Rotate three characters left: "123" -> "231". We change
12414 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12415 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12416 {
12417 go_deeper(stack, depth, SCORE_SWAP3);
12418#ifdef DEBUG_TRIEWALK
12419 p = fword + sp->ts_fidx;
12420 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12421 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12422 p[0], p[1], p[2]);
12423#endif
12424 sp->ts_state = STATE_UNROT3L;
12425 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012426 p = fword + sp->ts_fidx;
12427#ifdef FEAT_MBYTE
12428 if (has_mbyte)
12429 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012430 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012431 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012432 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012433 fl += mb_cptr2len(p + n + fl);
12434 mch_memmove(p, p + n, fl);
12435 mb_char2bytes(c, p + fl);
12436 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012437 }
12438 else
12439#endif
12440 {
12441 c = *p;
12442 *p = p[1];
12443 p[1] = p[2];
12444 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012445 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012446 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012447 }
12448 else
12449 sp->ts_state = STATE_REP_INI;
12450 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012451
Bram Moolenaar4770d092006-01-12 23:22:24 +000012452 case STATE_UNROT3L:
12453 /* Undo ROT3L: "231" -> "123" */
12454 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012455#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012456 if (has_mbyte)
12457 {
12458 n = MB_BYTE2LEN(*p);
12459 n += MB_BYTE2LEN(p[n]);
12460 c = mb_ptr2char(p + n);
12461 tl = MB_BYTE2LEN(p[n]);
12462 mch_memmove(p + tl, p, n);
12463 mb_char2bytes(c, p);
12464 }
12465 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012466#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012467 {
12468 c = p[2];
12469 p[2] = p[1];
12470 p[1] = *p;
12471 *p = c;
12472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012473
Bram Moolenaar4770d092006-01-12 23:22:24 +000012474 /* Rotate three bytes right: "123" -> "312". We change "fword"
12475 * here, it's changed back afterwards at STATE_UNROT3R. */
12476 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12477 {
12478 go_deeper(stack, depth, SCORE_SWAP3);
12479#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012480 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012481 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12482 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12483 p[0], p[1], p[2]);
12484#endif
12485 sp->ts_state = STATE_UNROT3R;
12486 ++depth;
12487 p = fword + sp->ts_fidx;
12488#ifdef FEAT_MBYTE
12489 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012490 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012491 n = mb_cptr2len(p);
12492 n += mb_cptr2len(p + n);
12493 c = mb_ptr2char(p + n);
12494 tl = mb_cptr2len(p + n);
12495 mch_memmove(p + tl, p, n);
12496 mb_char2bytes(c, p);
12497 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012498 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012499 else
12500#endif
12501 {
12502 c = p[2];
12503 p[2] = p[1];
12504 p[1] = *p;
12505 *p = c;
12506 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12507 }
12508 }
12509 else
12510 sp->ts_state = STATE_REP_INI;
12511 break;
12512
12513 case STATE_UNROT3R:
12514 /* Undo ROT3R: "312" -> "123" */
12515 p = fword + sp->ts_fidx;
12516#ifdef FEAT_MBYTE
12517 if (has_mbyte)
12518 {
12519 c = mb_ptr2char(p);
12520 tl = MB_BYTE2LEN(*p);
12521 n = MB_BYTE2LEN(p[tl]);
12522 n += MB_BYTE2LEN(p[tl + n]);
12523 mch_memmove(p, p + tl, n);
12524 mb_char2bytes(c, p + n);
12525 }
12526 else
12527#endif
12528 {
12529 c = *p;
12530 *p = p[1];
12531 p[1] = p[2];
12532 p[2] = c;
12533 }
12534 /*FALLTHROUGH*/
12535
12536 case STATE_REP_INI:
12537 /* Check if matching with REP items from the .aff file would work.
12538 * Quickly skip if:
12539 * - there are no REP items and we are not in the soundfold trie
12540 * - the score is going to be too high anyway
12541 * - already applied a REP item or swapped here */
12542 if ((lp->lp_replang == NULL && !soundfold)
12543 || sp->ts_score + SCORE_REP >= su->su_maxscore
12544 || sp->ts_fidx < sp->ts_fidxtry)
12545 {
12546 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012548 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549
Bram Moolenaar4770d092006-01-12 23:22:24 +000012550 /* Use the first byte to quickly find the first entry that may
12551 * match. If the index is -1 there is none. */
12552 if (soundfold)
12553 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12554 else
12555 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012556
Bram Moolenaar4770d092006-01-12 23:22:24 +000012557 if (sp->ts_curi < 0)
12558 {
12559 sp->ts_state = STATE_FINAL;
12560 break;
12561 }
12562
12563 sp->ts_state = STATE_REP;
12564 /*FALLTHROUGH*/
12565
12566 case STATE_REP:
12567 /* Try matching with REP items from the .aff file. For each match
12568 * replace the characters and check if the resulting word is
12569 * valid. */
12570 p = fword + sp->ts_fidx;
12571
12572 if (soundfold)
12573 gap = &slang->sl_repsal;
12574 else
12575 gap = &lp->lp_replang->sl_rep;
12576 while (sp->ts_curi < gap->ga_len)
12577 {
12578 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12579 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012580 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012581 /* past possible matching entries */
12582 sp->ts_curi = gap->ga_len;
12583 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012584 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012585 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12586 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12587 {
12588 go_deeper(stack, depth, SCORE_REP);
12589#ifdef DEBUG_TRIEWALK
12590 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12591 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12592 ftp->ft_from, ftp->ft_to);
12593#endif
12594 /* Need to undo this afterwards. */
12595 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012596
Bram Moolenaar4770d092006-01-12 23:22:24 +000012597 /* Change the "from" to the "to" string. */
12598 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012599 fl = (int)STRLEN(ftp->ft_from);
12600 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012601 if (fl != tl)
12602 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012603 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012604 repextra += tl - fl;
12605 }
12606 mch_memmove(p, ftp->ft_to, tl);
12607 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12608#ifdef FEAT_MBYTE
12609 stack[depth].ts_tcharlen = 0;
12610#endif
12611 break;
12612 }
12613 }
12614
12615 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12616 /* No (more) matches. */
12617 sp->ts_state = STATE_FINAL;
12618
12619 break;
12620
12621 case STATE_REP_UNDO:
12622 /* Undo a REP replacement and continue with the next one. */
12623 if (soundfold)
12624 gap = &slang->sl_repsal;
12625 else
12626 gap = &lp->lp_replang->sl_rep;
12627 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012628 fl = (int)STRLEN(ftp->ft_from);
12629 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012630 p = fword + sp->ts_fidx;
12631 if (fl != tl)
12632 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012633 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012634 repextra -= tl - fl;
12635 }
12636 mch_memmove(p, ftp->ft_from, fl);
12637 sp->ts_state = STATE_REP;
12638 break;
12639
12640 default:
12641 /* Did all possible states at this level, go up one level. */
12642 --depth;
12643
12644 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12645 {
12646 /* Continue in or go back to the prefix tree. */
12647 byts = pbyts;
12648 idxs = pidxs;
12649 }
12650
12651 /* Don't check for CTRL-C too often, it takes time. */
12652 if (--breakcheckcount == 0)
12653 {
12654 ui_breakcheck();
12655 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012656 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012657 }
12658 }
12659}
12660
Bram Moolenaar4770d092006-01-12 23:22:24 +000012661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012662/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012663 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012665 static void
12666go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012667 trystate_T *stack;
12668 int depth;
12669 int score_add;
12670{
Bram Moolenaarea424162005-06-16 21:51:00 +000012671 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012673 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012675 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012676}
12677
Bram Moolenaar53805d12005-08-01 07:08:33 +000012678#ifdef FEAT_MBYTE
12679/*
12680 * Case-folding may change the number of bytes: Count nr of chars in
12681 * fword[flen] and return the byte length of that many chars in "word".
12682 */
12683 static int
12684nofold_len(fword, flen, word)
12685 char_u *fword;
12686 int flen;
12687 char_u *word;
12688{
12689 char_u *p;
12690 int i = 0;
12691
12692 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12693 ++i;
12694 for (p = word; i > 0; mb_ptr_adv(p))
12695 --i;
12696 return (int)(p - word);
12697}
12698#endif
12699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700/*
12701 * "fword" is a good word with case folded. Find the matching keep-case
12702 * words and put it in "kword".
12703 * Theoretically there could be several keep-case words that result in the
12704 * same case-folded word, but we only find one...
12705 */
12706 static void
12707find_keepcap_word(slang, fword, kword)
12708 slang_T *slang;
12709 char_u *fword;
12710 char_u *kword;
12711{
12712 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12713 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012714 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012715
12716 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012717 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 int round[MAXWLEN];
12719 int fwordidx[MAXWLEN];
12720 int uwordidx[MAXWLEN];
12721 int kwordlen[MAXWLEN];
12722
12723 int flen, ulen;
12724 int l;
12725 int len;
12726 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012727 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 char_u *p;
12729 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012730 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731
12732 if (byts == NULL)
12733 {
12734 /* array is empty: "cannot happen" */
12735 *kword = NUL;
12736 return;
12737 }
12738
12739 /* Make an all-cap version of "fword". */
12740 allcap_copy(fword, uword);
12741
12742 /*
12743 * Each character needs to be tried both case-folded and upper-case.
12744 * All this gets very complicated if we keep in mind that changing case
12745 * may change the byte length of a multi-byte character...
12746 */
12747 depth = 0;
12748 arridx[0] = 0;
12749 round[0] = 0;
12750 fwordidx[0] = 0;
12751 uwordidx[0] = 0;
12752 kwordlen[0] = 0;
12753 while (depth >= 0)
12754 {
12755 if (fword[fwordidx[depth]] == NUL)
12756 {
12757 /* We are at the end of "fword". If the tree allows a word to end
12758 * here we have found a match. */
12759 if (byts[arridx[depth] + 1] == 0)
12760 {
12761 kword[kwordlen[depth]] = NUL;
12762 return;
12763 }
12764
12765 /* kword is getting too long, continue one level up */
12766 --depth;
12767 }
12768 else if (++round[depth] > 2)
12769 {
12770 /* tried both fold-case and upper-case character, continue one
12771 * level up */
12772 --depth;
12773 }
12774 else
12775 {
12776 /*
12777 * round[depth] == 1: Try using the folded-case character.
12778 * round[depth] == 2: Try using the upper-case character.
12779 */
12780#ifdef FEAT_MBYTE
12781 if (has_mbyte)
12782 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012783 flen = mb_cptr2len(fword + fwordidx[depth]);
12784 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012785 }
12786 else
12787#endif
12788 ulen = flen = 1;
12789 if (round[depth] == 1)
12790 {
12791 p = fword + fwordidx[depth];
12792 l = flen;
12793 }
12794 else
12795 {
12796 p = uword + uwordidx[depth];
12797 l = ulen;
12798 }
12799
12800 for (tryidx = arridx[depth]; l > 0; --l)
12801 {
12802 /* Perform a binary search in the list of accepted bytes. */
12803 len = byts[tryidx++];
12804 c = *p++;
12805 lo = tryidx;
12806 hi = tryidx + len - 1;
12807 while (lo < hi)
12808 {
12809 m = (lo + hi) / 2;
12810 if (byts[m] > c)
12811 hi = m - 1;
12812 else if (byts[m] < c)
12813 lo = m + 1;
12814 else
12815 {
12816 lo = hi = m;
12817 break;
12818 }
12819 }
12820
12821 /* Stop if there is no matching byte. */
12822 if (hi < lo || byts[lo] != c)
12823 break;
12824
12825 /* Continue at the child (if there is one). */
12826 tryidx = idxs[lo];
12827 }
12828
12829 if (l == 0)
12830 {
12831 /*
12832 * Found the matching char. Copy it to "kword" and go a
12833 * level deeper.
12834 */
12835 if (round[depth] == 1)
12836 {
12837 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12838 flen);
12839 kwordlen[depth + 1] = kwordlen[depth] + flen;
12840 }
12841 else
12842 {
12843 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12844 ulen);
12845 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12846 }
12847 fwordidx[depth + 1] = fwordidx[depth] + flen;
12848 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12849
12850 ++depth;
12851 arridx[depth] = tryidx;
12852 round[depth] = 0;
12853 }
12854 }
12855 }
12856
12857 /* Didn't find it: "cannot happen". */
12858 *kword = NUL;
12859}
12860
12861/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012862 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12863 * su->su_sga.
12864 */
12865 static void
12866score_comp_sal(su)
12867 suginfo_T *su;
12868{
12869 langp_T *lp;
12870 char_u badsound[MAXWLEN];
12871 int i;
12872 suggest_T *stp;
12873 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012874 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012875 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012876
12877 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12878 return;
12879
12880 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012881 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012882 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012883 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012884 if (lp->lp_slang->sl_sal.ga_len > 0)
12885 {
12886 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012887 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012888
12889 for (i = 0; i < su->su_ga.ga_len; ++i)
12890 {
12891 stp = &SUG(su->su_ga, i);
12892
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012893 /* Case-fold the suggested word, sound-fold it and compute the
12894 * sound-a-like score. */
12895 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012896 if (score < SCORE_MAXMAX)
12897 {
12898 /* Add the suggestion. */
12899 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12900 sstp->st_word = vim_strsave(stp->st_word);
12901 if (sstp->st_word != NULL)
12902 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012903 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012904 sstp->st_score = score;
12905 sstp->st_altscore = 0;
12906 sstp->st_orglen = stp->st_orglen;
12907 ++su->su_sga.ga_len;
12908 }
12909 }
12910 }
12911 break;
12912 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012913 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012914}
12915
12916/*
12917 * Combine the list of suggestions in su->su_ga and su->su_sga.
12918 * They are intwined.
12919 */
12920 static void
12921score_combine(su)
12922 suginfo_T *su;
12923{
12924 int i;
12925 int j;
12926 garray_T ga;
12927 garray_T *gap;
12928 langp_T *lp;
12929 suggest_T *stp;
12930 char_u *p;
12931 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012932 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012933 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012934 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012935
12936 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012937 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012938 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012939 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012940 if (lp->lp_slang->sl_sal.ga_len > 0)
12941 {
12942 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012943 slang = lp->lp_slang;
12944 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012945
12946 for (i = 0; i < su->su_ga.ga_len; ++i)
12947 {
12948 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012949 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012950 if (stp->st_altscore == SCORE_MAXMAX)
12951 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12952 else
12953 stp->st_score = (stp->st_score * 3
12954 + stp->st_altscore) / 4;
12955 stp->st_salscore = FALSE;
12956 }
12957 break;
12958 }
12959 }
12960
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012961 if (slang == NULL) /* Using "double" without sound folding. */
12962 {
12963 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12964 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012965 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012966 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012967
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012968 /* Add the alternate score to su_sga. */
12969 for (i = 0; i < su->su_sga.ga_len; ++i)
12970 {
12971 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012972 stp->st_altscore = spell_edit_score(slang,
12973 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012974 if (stp->st_score == SCORE_MAXMAX)
12975 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12976 else
12977 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12978 stp->st_salscore = TRUE;
12979 }
12980
Bram Moolenaar4770d092006-01-12 23:22:24 +000012981 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12982 * for both lists. */
12983 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012984 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012985 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012986 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12987
12988 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12989 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12990 return;
12991
12992 stp = &SUG(ga, 0);
12993 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12994 {
12995 /* round 1: get a suggestion from su_ga
12996 * round 2: get a suggestion from su_sga */
12997 for (round = 1; round <= 2; ++round)
12998 {
12999 gap = round == 1 ? &su->su_ga : &su->su_sga;
13000 if (i < gap->ga_len)
13001 {
13002 /* Don't add a word if it's already there. */
13003 p = SUG(*gap, i).st_word;
13004 for (j = 0; j < ga.ga_len; ++j)
13005 if (STRCMP(stp[j].st_word, p) == 0)
13006 break;
13007 if (j == ga.ga_len)
13008 stp[ga.ga_len++] = SUG(*gap, i);
13009 else
13010 vim_free(p);
13011 }
13012 }
13013 }
13014
13015 ga_clear(&su->su_ga);
13016 ga_clear(&su->su_sga);
13017
13018 /* Truncate the list to the number of suggestions that will be displayed. */
13019 if (ga.ga_len > su->su_maxcount)
13020 {
13021 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13022 vim_free(stp[i].st_word);
13023 ga.ga_len = su->su_maxcount;
13024 }
13025
13026 su->su_ga = ga;
13027}
13028
13029/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013030 * For the goodword in "stp" compute the soundalike score compared to the
13031 * badword.
13032 */
13033 static int
13034stp_sal_score(stp, su, slang, badsound)
13035 suggest_T *stp;
13036 suginfo_T *su;
13037 slang_T *slang;
13038 char_u *badsound; /* sound-folded badword */
13039{
13040 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013041 char_u *pbad;
13042 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013043 char_u badsound2[MAXWLEN];
13044 char_u fword[MAXWLEN];
13045 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013046 char_u goodword[MAXWLEN];
13047 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013048
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013049 lendiff = (int)(su->su_badlen - stp->st_orglen);
13050 if (lendiff >= 0)
13051 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013052 else
13053 {
13054 /* soundfold the bad word with more characters following */
13055 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13056
13057 /* When joining two words the sound often changes a lot. E.g., "t he"
13058 * sounds like "t h" while "the" sounds like "@". Avoid that by
13059 * removing the space. Don't do it when the good word also contains a
13060 * space. */
13061 if (vim_iswhite(su->su_badptr[su->su_badlen])
13062 && *skiptowhite(stp->st_word) == NUL)
13063 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013064 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013065
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013066 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013067 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013068 }
13069
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013070 if (lendiff > 0)
13071 {
13072 /* Add part of the bad word to the good word, so that we soundfold
13073 * what replaces the bad word. */
13074 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013075 vim_strncpy(goodword + stp->st_wordlen,
13076 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013077 pgood = goodword;
13078 }
13079 else
13080 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013081
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013082 /* Sound-fold the word and compute the score for the difference. */
13083 spell_soundfold(slang, pgood, FALSE, goodsound);
13084
13085 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013086}
13087
Bram Moolenaar4770d092006-01-12 23:22:24 +000013088/* structure used to store soundfolded words that add_sound_suggest() has
13089 * handled already. */
13090typedef struct
13091{
13092 short sft_score; /* lowest score used */
13093 char_u sft_word[1]; /* soundfolded word, actually longer */
13094} sftword_T;
13095
13096static sftword_T dumsft;
13097#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13098#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13099
13100/*
13101 * Prepare for calling suggest_try_soundalike().
13102 */
13103 static void
13104suggest_try_soundalike_prep()
13105{
13106 langp_T *lp;
13107 int lpi;
13108 slang_T *slang;
13109
13110 /* Do this for all languages that support sound folding and for which a
13111 * .sug file has been loaded. */
13112 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13113 {
13114 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13115 slang = lp->lp_slang;
13116 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13117 /* prepare the hashtable used by add_sound_suggest() */
13118 hash_init(&slang->sl_sounddone);
13119 }
13120}
13121
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013122/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013123 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013124 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 */
13126 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013127suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013128 suginfo_T *su;
13129{
13130 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013131 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013132 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013133 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013134
Bram Moolenaar4770d092006-01-12 23:22:24 +000013135 /* Do this for all languages that support sound folding and for which a
13136 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013137 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013138 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013139 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13140 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013141 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142 {
13143 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013144 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013145
Bram Moolenaar4770d092006-01-12 23:22:24 +000013146 /* try all kinds of inserts/deletes/swaps/etc. */
13147 /* TODO: also soundfold the next words, so that we can try joining
13148 * and splitting */
13149 suggest_trie_walk(su, lp, salword, TRUE);
13150 }
13151 }
13152}
13153
13154/*
13155 * Finish up after calling suggest_try_soundalike().
13156 */
13157 static void
13158suggest_try_soundalike_finish()
13159{
13160 langp_T *lp;
13161 int lpi;
13162 slang_T *slang;
13163 int todo;
13164 hashitem_T *hi;
13165
13166 /* Do this for all languages that support sound folding and for which a
13167 * .sug file has been loaded. */
13168 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13169 {
13170 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13171 slang = lp->lp_slang;
13172 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13173 {
13174 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013175 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013176 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13177 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013179 vim_free(HI2SFT(hi));
13180 --todo;
13181 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013182
13183 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013184 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013185 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013186 }
13187 }
13188}
13189
13190/*
13191 * A match with a soundfolded word is found. Add the good word(s) that
13192 * produce this soundfolded word.
13193 */
13194 static void
13195add_sound_suggest(su, goodword, score, lp)
13196 suginfo_T *su;
13197 char_u *goodword;
13198 int score; /* soundfold score */
13199 langp_T *lp;
13200{
13201 slang_T *slang = lp->lp_slang; /* language for sound folding */
13202 int sfwordnr;
13203 char_u *nrline;
13204 int orgnr;
13205 char_u theword[MAXWLEN];
13206 int i;
13207 int wlen;
13208 char_u *byts;
13209 idx_T *idxs;
13210 int n;
13211 int wordcount;
13212 int wc;
13213 int goodscore;
13214 hash_T hash;
13215 hashitem_T *hi;
13216 sftword_T *sft;
13217 int bc, gc;
13218 int limit;
13219
13220 /*
13221 * It's very well possible that the same soundfold word is found several
13222 * times with different scores. Since the following is quite slow only do
13223 * the words that have a better score than before. Use a hashtable to
13224 * remember the words that have been done.
13225 */
13226 hash = hash_hash(goodword);
13227 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13228 if (HASHITEM_EMPTY(hi))
13229 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013230 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13231 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013232 if (sft != NULL)
13233 {
13234 sft->sft_score = score;
13235 STRCPY(sft->sft_word, goodword);
13236 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13237 }
13238 }
13239 else
13240 {
13241 sft = HI2SFT(hi);
13242 if (score >= sft->sft_score)
13243 return;
13244 sft->sft_score = score;
13245 }
13246
13247 /*
13248 * Find the word nr in the soundfold tree.
13249 */
13250 sfwordnr = soundfold_find(slang, goodword);
13251 if (sfwordnr < 0)
13252 {
13253 EMSG2(_(e_intern2), "add_sound_suggest()");
13254 return;
13255 }
13256
13257 /*
13258 * go over the list of good words that produce this soundfold word
13259 */
13260 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13261 orgnr = 0;
13262 while (*nrline != NUL)
13263 {
13264 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13265 * previous wordnr. */
13266 orgnr += bytes2offset(&nrline);
13267
13268 byts = slang->sl_fbyts;
13269 idxs = slang->sl_fidxs;
13270
13271 /* Lookup the word "orgnr" one of the two tries. */
13272 n = 0;
13273 wlen = 0;
13274 wordcount = 0;
13275 for (;;)
13276 {
13277 i = 1;
13278 if (wordcount == orgnr && byts[n + 1] == NUL)
13279 break; /* found end of word */
13280
13281 if (byts[n + 1] == NUL)
13282 ++wordcount;
13283
13284 /* skip over the NUL bytes */
13285 for ( ; byts[n + i] == NUL; ++i)
13286 if (i > byts[n]) /* safety check */
13287 {
13288 STRCPY(theword + wlen, "BAD");
13289 goto badword;
13290 }
13291
13292 /* One of the siblings must have the word. */
13293 for ( ; i < byts[n]; ++i)
13294 {
13295 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13296 if (wordcount + wc > orgnr)
13297 break;
13298 wordcount += wc;
13299 }
13300
13301 theword[wlen++] = byts[n + i];
13302 n = idxs[n + i];
13303 }
13304badword:
13305 theword[wlen] = NUL;
13306
13307 /* Go over the possible flags and regions. */
13308 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13309 {
13310 char_u cword[MAXWLEN];
13311 char_u *p;
13312 int flags = (int)idxs[n + i];
13313
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013314 /* Skip words with the NOSUGGEST flag */
13315 if (flags & WF_NOSUGGEST)
13316 continue;
13317
Bram Moolenaar4770d092006-01-12 23:22:24 +000013318 if (flags & WF_KEEPCAP)
13319 {
13320 /* Must find the word in the keep-case tree. */
13321 find_keepcap_word(slang, theword, cword);
13322 p = cword;
13323 }
13324 else
13325 {
13326 flags |= su->su_badflags;
13327 if ((flags & WF_CAPMASK) != 0)
13328 {
13329 /* Need to fix case according to "flags". */
13330 make_case_word(theword, cword, flags);
13331 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 }
13333 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013334 p = theword;
13335 }
13336
13337 /* Add the suggestion. */
13338 if (sps_flags & SPS_DOUBLE)
13339 {
13340 /* Add the suggestion if the score isn't too bad. */
13341 if (score <= su->su_maxscore)
13342 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13343 score, 0, FALSE, slang, FALSE);
13344 }
13345 else
13346 {
13347 /* Add a penalty for words in another region. */
13348 if ((flags & WF_REGION)
13349 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13350 goodscore = SCORE_REGION;
13351 else
13352 goodscore = 0;
13353
13354 /* Add a small penalty for changing the first letter from
13355 * lower to upper case. Helps for "tath" -> "Kath", which is
13356 * less common thatn "tath" -> "path". Don't do it when the
13357 * letter is the same, that has already been counted. */
13358 gc = PTR2CHAR(p);
13359 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013361 bc = PTR2CHAR(su->su_badword);
13362 if (!SPELL_ISUPPER(bc)
13363 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13364 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013365 }
13366
Bram Moolenaar4770d092006-01-12 23:22:24 +000013367 /* Compute the score for the good word. This only does letter
13368 * insert/delete/swap/replace. REP items are not considered,
13369 * which may make the score a bit higher.
13370 * Use a limit for the score to make it work faster. Use
13371 * MAXSCORE(), because RESCORE() will change the score.
13372 * If the limit is very high then the iterative method is
13373 * inefficient, using an array is quicker. */
13374 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13375 if (limit > SCORE_LIMITMAX)
13376 goodscore += spell_edit_score(slang, su->su_badword, p);
13377 else
13378 goodscore += spell_edit_score_limit(slang, su->su_badword,
13379 p, limit);
13380
13381 /* When going over the limit don't bother to do the rest. */
13382 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013384 /* Give a bonus to words seen before. */
13385 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386
Bram Moolenaar4770d092006-01-12 23:22:24 +000013387 /* Add the suggestion if the score isn't too bad. */
13388 goodscore = RESCORE(goodscore, score);
13389 if (goodscore <= su->su_sfmaxscore)
13390 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13391 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013393 }
13394 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013395 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013396 }
13397}
13398
13399/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013400 * Find word "word" in fold-case tree for "slang" and return the word number.
13401 */
13402 static int
13403soundfold_find(slang, word)
13404 slang_T *slang;
13405 char_u *word;
13406{
13407 idx_T arridx = 0;
13408 int len;
13409 int wlen = 0;
13410 int c;
13411 char_u *ptr = word;
13412 char_u *byts;
13413 idx_T *idxs;
13414 int wordnr = 0;
13415
13416 byts = slang->sl_sbyts;
13417 idxs = slang->sl_sidxs;
13418
13419 for (;;)
13420 {
13421 /* First byte is the number of possible bytes. */
13422 len = byts[arridx++];
13423
13424 /* If the first possible byte is a zero the word could end here.
13425 * If the word ends we found the word. If not skip the NUL bytes. */
13426 c = ptr[wlen];
13427 if (byts[arridx] == NUL)
13428 {
13429 if (c == NUL)
13430 break;
13431
13432 /* Skip over the zeros, there can be several. */
13433 while (len > 0 && byts[arridx] == NUL)
13434 {
13435 ++arridx;
13436 --len;
13437 }
13438 if (len == 0)
13439 return -1; /* no children, word should have ended here */
13440 ++wordnr;
13441 }
13442
13443 /* If the word ends we didn't find it. */
13444 if (c == NUL)
13445 return -1;
13446
13447 /* Perform a binary search in the list of accepted bytes. */
13448 if (c == TAB) /* <Tab> is handled like <Space> */
13449 c = ' ';
13450 while (byts[arridx] < c)
13451 {
13452 /* The word count is in the first idxs[] entry of the child. */
13453 wordnr += idxs[idxs[arridx]];
13454 ++arridx;
13455 if (--len == 0) /* end of the bytes, didn't find it */
13456 return -1;
13457 }
13458 if (byts[arridx] != c) /* didn't find the byte */
13459 return -1;
13460
13461 /* Continue at the child (if there is one). */
13462 arridx = idxs[arridx];
13463 ++wlen;
13464
13465 /* One space in the good word may stand for several spaces in the
13466 * checked word. */
13467 if (c == ' ')
13468 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13469 ++wlen;
13470 }
13471
13472 return wordnr;
13473}
13474
13475/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013476 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 */
13478 static void
13479make_case_word(fword, cword, flags)
13480 char_u *fword;
13481 char_u *cword;
13482 int flags;
13483{
13484 if (flags & WF_ALLCAP)
13485 /* Make it all upper-case */
13486 allcap_copy(fword, cword);
13487 else if (flags & WF_ONECAP)
13488 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013489 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013490 else
13491 /* Use goodword as-is. */
13492 STRCPY(cword, fword);
13493}
13494
Bram Moolenaarea424162005-06-16 21:51:00 +000013495/*
13496 * Use map string "map" for languages "lp".
13497 */
13498 static void
13499set_map_str(lp, map)
13500 slang_T *lp;
13501 char_u *map;
13502{
13503 char_u *p;
13504 int headc = 0;
13505 int c;
13506 int i;
13507
13508 if (*map == NUL)
13509 {
13510 lp->sl_has_map = FALSE;
13511 return;
13512 }
13513 lp->sl_has_map = TRUE;
13514
Bram Moolenaar4770d092006-01-12 23:22:24 +000013515 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013516 for (i = 0; i < 256; ++i)
13517 lp->sl_map_array[i] = 0;
13518#ifdef FEAT_MBYTE
13519 hash_init(&lp->sl_map_hash);
13520#endif
13521
13522 /*
13523 * The similar characters are stored separated with slashes:
13524 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13525 * before the same slash. For characters above 255 sl_map_hash is used.
13526 */
13527 for (p = map; *p != NUL; )
13528 {
13529#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013530 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013531#else
13532 c = *p++;
13533#endif
13534 if (c == '/')
13535 headc = 0;
13536 else
13537 {
13538 if (headc == 0)
13539 headc = c;
13540
13541#ifdef FEAT_MBYTE
13542 /* Characters above 255 don't fit in sl_map_array[], put them in
13543 * the hash table. Each entry is the char, a NUL the headchar and
13544 * a NUL. */
13545 if (c >= 256)
13546 {
13547 int cl = mb_char2len(c);
13548 int headcl = mb_char2len(headc);
13549 char_u *b;
13550 hash_T hash;
13551 hashitem_T *hi;
13552
13553 b = alloc((unsigned)(cl + headcl + 2));
13554 if (b == NULL)
13555 return;
13556 mb_char2bytes(c, b);
13557 b[cl] = NUL;
13558 mb_char2bytes(headc, b + cl + 1);
13559 b[cl + 1 + headcl] = NUL;
13560 hash = hash_hash(b);
13561 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13562 if (HASHITEM_EMPTY(hi))
13563 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13564 else
13565 {
13566 /* This should have been checked when generating the .spl
13567 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013568 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013569 vim_free(b);
13570 }
13571 }
13572 else
13573#endif
13574 lp->sl_map_array[c] = headc;
13575 }
13576 }
13577}
13578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579/*
13580 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13581 * lines in the .aff file.
13582 */
13583 static int
13584similar_chars(slang, c1, c2)
13585 slang_T *slang;
13586 int c1;
13587 int c2;
13588{
Bram Moolenaarea424162005-06-16 21:51:00 +000013589 int m1, m2;
13590#ifdef FEAT_MBYTE
13591 char_u buf[MB_MAXBYTES];
13592 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593
Bram Moolenaarea424162005-06-16 21:51:00 +000013594 if (c1 >= 256)
13595 {
13596 buf[mb_char2bytes(c1, buf)] = 0;
13597 hi = hash_find(&slang->sl_map_hash, buf);
13598 if (HASHITEM_EMPTY(hi))
13599 m1 = 0;
13600 else
13601 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13602 }
13603 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013604#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013605 m1 = slang->sl_map_array[c1];
13606 if (m1 == 0)
13607 return FALSE;
13608
13609
13610#ifdef FEAT_MBYTE
13611 if (c2 >= 256)
13612 {
13613 buf[mb_char2bytes(c2, buf)] = 0;
13614 hi = hash_find(&slang->sl_map_hash, buf);
13615 if (HASHITEM_EMPTY(hi))
13616 m2 = 0;
13617 else
13618 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13619 }
13620 else
13621#endif
13622 m2 = slang->sl_map_array[c2];
13623
13624 return m1 == m2;
13625}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626
13627/*
13628 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013629 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 */
13631 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013632add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13633 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013635 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013637 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013638 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013639 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013640 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013641 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013642 int maxsf; /* su_maxscore applies to soundfold score,
13643 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013645 int goodlen; /* len of goodword changed */
13646 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013647 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013648 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013649 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013650 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013652 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13653 * "thee the" is added next to changing the first "the" the "thee". */
13654 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013655 pbad = su->su_badptr + badlenarg;
13656 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013657 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013658 goodlen = (int)(pgood - goodword);
13659 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013660 if (goodlen <= 0 || badlen <= 0)
13661 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013662 mb_ptr_back(goodword, pgood);
13663 mb_ptr_back(su->su_badptr, pbad);
13664#ifdef FEAT_MBYTE
13665 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013666 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013667 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13668 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013669 }
13670 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013671#endif
13672 if (*pgood != *pbad)
13673 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013674 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013675
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013676 if (badlen == 0 && goodlen == 0)
13677 /* goodword doesn't change anything; may happen for "the the" changing
13678 * the first "the" to itself. */
13679 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013680
Bram Moolenaar89d40322006-08-29 15:30:07 +000013681 if (gap->ga_len == 0)
13682 i = -1;
13683 else
13684 {
13685 /* Check if the word is already there. Also check the length that is
13686 * being replaced "thes," -> "these" is a different suggestion from
13687 * "thes" -> "these". */
13688 stp = &SUG(*gap, 0);
13689 for (i = gap->ga_len; --i >= 0; ++stp)
13690 if (stp->st_wordlen == goodlen
13691 && stp->st_orglen == badlen
13692 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013694 /*
13695 * Found it. Remember the word with the lowest score.
13696 */
13697 if (stp->st_slang == NULL)
13698 stp->st_slang = slang;
13699
13700 new_sug.st_score = score;
13701 new_sug.st_altscore = altscore;
13702 new_sug.st_had_bonus = had_bonus;
13703
13704 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013705 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013706 /* Only one of the two had the soundalike score computed.
13707 * Need to do that for the other one now, otherwise the
13708 * scores can't be compared. This happens because
13709 * suggest_try_change() doesn't compute the soundalike
13710 * word to keep it fast, while some special methods set
13711 * the soundalike score to zero. */
13712 if (had_bonus)
13713 rescore_one(su, stp);
13714 else
13715 {
13716 new_sug.st_word = stp->st_word;
13717 new_sug.st_wordlen = stp->st_wordlen;
13718 new_sug.st_slang = stp->st_slang;
13719 new_sug.st_orglen = badlen;
13720 rescore_one(su, &new_sug);
13721 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013723
Bram Moolenaar89d40322006-08-29 15:30:07 +000013724 if (stp->st_score > new_sug.st_score)
13725 {
13726 stp->st_score = new_sug.st_score;
13727 stp->st_altscore = new_sug.st_altscore;
13728 stp->st_had_bonus = new_sug.st_had_bonus;
13729 }
13730 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013731 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013733
Bram Moolenaar4770d092006-01-12 23:22:24 +000013734 if (i < 0 && ga_grow(gap, 1) == OK)
13735 {
13736 /* Add a suggestion. */
13737 stp = &SUG(*gap, gap->ga_len);
13738 stp->st_word = vim_strnsave(goodword, goodlen);
13739 if (stp->st_word != NULL)
13740 {
13741 stp->st_wordlen = goodlen;
13742 stp->st_score = score;
13743 stp->st_altscore = altscore;
13744 stp->st_had_bonus = had_bonus;
13745 stp->st_orglen = badlen;
13746 stp->st_slang = slang;
13747 ++gap->ga_len;
13748
13749 /* If we have too many suggestions now, sort the list and keep
13750 * the best suggestions. */
13751 if (gap->ga_len > SUG_MAX_COUNT(su))
13752 {
13753 if (maxsf)
13754 su->su_sfmaxscore = cleanup_suggestions(gap,
13755 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13756 else
13757 {
13758 i = su->su_maxscore;
13759 su->su_maxscore = cleanup_suggestions(gap,
13760 su->su_maxscore, SUG_CLEAN_COUNT(su));
13761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013762 }
13763 }
13764 }
13765}
13766
13767/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013768 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13769 * for split words, such as "the the". Remove these from the list here.
13770 */
13771 static void
13772check_suggestions(su, gap)
13773 suginfo_T *su;
13774 garray_T *gap; /* either su_ga or su_sga */
13775{
13776 suggest_T *stp;
13777 int i;
13778 char_u longword[MAXWLEN + 1];
13779 int len;
13780 hlf_T attr;
13781
13782 stp = &SUG(*gap, 0);
13783 for (i = gap->ga_len - 1; i >= 0; --i)
13784 {
13785 /* Need to append what follows to check for "the the". */
13786 STRCPY(longword, stp[i].st_word);
13787 len = stp[i].st_wordlen;
13788 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13789 MAXWLEN - len);
13790 attr = HLF_COUNT;
13791 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13792 if (attr != HLF_COUNT)
13793 {
13794 /* Remove this entry. */
13795 vim_free(stp[i].st_word);
13796 --gap->ga_len;
13797 if (i < gap->ga_len)
13798 mch_memmove(stp + i, stp + i + 1,
13799 sizeof(suggest_T) * (gap->ga_len - i));
13800 }
13801 }
13802}
13803
13804
13805/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 * Add a word to be banned.
13807 */
13808 static void
13809add_banned(su, word)
13810 suginfo_T *su;
13811 char_u *word;
13812{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013813 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013814 hash_T hash;
13815 hashitem_T *hi;
13816
Bram Moolenaar4770d092006-01-12 23:22:24 +000013817 hash = hash_hash(word);
13818 hi = hash_lookup(&su->su_banned, word, hash);
13819 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013821 s = vim_strsave(word);
13822 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 hash_add_item(&su->su_banned, hi, s, hash);
13824 }
13825}
13826
13827/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013828 * Recompute the score for all suggestions if sound-folding is possible. This
13829 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013830 */
13831 static void
13832rescore_suggestions(su)
13833 suginfo_T *su;
13834{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013835 int i;
13836
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013837 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013838 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013839 rescore_one(su, &SUG(su->su_ga, i));
13840}
13841
13842/*
13843 * Recompute the score for one suggestion if sound-folding is possible.
13844 */
13845 static void
13846rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013847 suginfo_T *su;
13848 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013849{
13850 slang_T *slang = stp->st_slang;
13851 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013852 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013853
13854 /* Only rescore suggestions that have no sal score yet and do have a
13855 * language. */
13856 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13857 {
13858 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013859 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013860 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013861 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013862 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013863 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013864 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013865
13866 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013867 if (stp->st_altscore == SCORE_MAXMAX)
13868 stp->st_altscore = SCORE_BIG;
13869 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13870 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013871 }
13872}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013874static int
13875#ifdef __BORLANDC__
13876_RTLENTRYF
13877#endif
13878sug_compare __ARGS((const void *s1, const void *s2));
13879
13880/*
13881 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013882 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883 */
13884 static int
13885#ifdef __BORLANDC__
13886_RTLENTRYF
13887#endif
13888sug_compare(s1, s2)
13889 const void *s1;
13890 const void *s2;
13891{
13892 suggest_T *p1 = (suggest_T *)s1;
13893 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013894 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013896 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013897 {
13898 n = p1->st_altscore - p2->st_altscore;
13899 if (n == 0)
13900 n = STRICMP(p1->st_word, p2->st_word);
13901 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013902 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013903}
13904
13905/*
13906 * Cleanup the suggestions:
13907 * - Sort on score.
13908 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013909 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013910 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013911 static int
13912cleanup_suggestions(gap, maxscore, keep)
13913 garray_T *gap;
13914 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013915 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013917 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918 int i;
13919
13920 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013921 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922
13923 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013924 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013926 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013928 gap->ga_len = keep;
13929 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013930 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013931 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013932}
13933
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013934#if defined(FEAT_EVAL) || defined(PROTO)
13935/*
13936 * Soundfold a string, for soundfold().
13937 * Result is in allocated memory, NULL for an error.
13938 */
13939 char_u *
13940eval_soundfold(word)
13941 char_u *word;
13942{
13943 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013944 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013945 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013946
13947 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13948 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013949 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013950 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013951 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013952 if (lp->lp_slang->sl_sal.ga_len > 0)
13953 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013954 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013955 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013956 return vim_strsave(sound);
13957 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013958 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013959
13960 /* No language with sound folding, return word as-is. */
13961 return vim_strsave(word);
13962}
13963#endif
13964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965/*
13966 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013967 *
13968 * There are many ways to turn a word into a sound-a-like representation. The
13969 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13970 * swedish name matching - survey and test of different algorithms" by Klas
13971 * Erikson.
13972 *
13973 * We support two methods:
13974 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13975 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 */
13977 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013978spell_soundfold(slang, inword, folded, res)
13979 slang_T *slang;
13980 char_u *inword;
13981 int folded; /* "inword" is already case-folded */
13982 char_u *res;
13983{
13984 char_u fword[MAXWLEN];
13985 char_u *word;
13986
13987 if (slang->sl_sofo)
13988 /* SOFOFROM and SOFOTO used */
13989 spell_soundfold_sofo(slang, inword, res);
13990 else
13991 {
13992 /* SAL items used. Requires the word to be case-folded. */
13993 if (folded)
13994 word = inword;
13995 else
13996 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013997 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013998 word = fword;
13999 }
14000
14001#ifdef FEAT_MBYTE
14002 if (has_mbyte)
14003 spell_soundfold_wsal(slang, word, res);
14004 else
14005#endif
14006 spell_soundfold_sal(slang, word, res);
14007 }
14008}
14009
14010/*
14011 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14012 * SOFOTO lines.
14013 */
14014 static void
14015spell_soundfold_sofo(slang, inword, res)
14016 slang_T *slang;
14017 char_u *inword;
14018 char_u *res;
14019{
14020 char_u *s;
14021 int ri = 0;
14022 int c;
14023
14024#ifdef FEAT_MBYTE
14025 if (has_mbyte)
14026 {
14027 int prevc = 0;
14028 int *ip;
14029
14030 /* The sl_sal_first[] table contains the translation for chars up to
14031 * 255, sl_sal the rest. */
14032 for (s = inword; *s != NUL; )
14033 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014034 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014035 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14036 c = ' ';
14037 else if (c < 256)
14038 c = slang->sl_sal_first[c];
14039 else
14040 {
14041 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14042 if (ip == NULL) /* empty list, can't match */
14043 c = NUL;
14044 else
14045 for (;;) /* find "c" in the list */
14046 {
14047 if (*ip == 0) /* not found */
14048 {
14049 c = NUL;
14050 break;
14051 }
14052 if (*ip == c) /* match! */
14053 {
14054 c = ip[1];
14055 break;
14056 }
14057 ip += 2;
14058 }
14059 }
14060
14061 if (c != NUL && c != prevc)
14062 {
14063 ri += mb_char2bytes(c, res + ri);
14064 if (ri + MB_MAXBYTES > MAXWLEN)
14065 break;
14066 prevc = c;
14067 }
14068 }
14069 }
14070 else
14071#endif
14072 {
14073 /* The sl_sal_first[] table contains the translation. */
14074 for (s = inword; (c = *s) != NUL; ++s)
14075 {
14076 if (vim_iswhite(c))
14077 c = ' ';
14078 else
14079 c = slang->sl_sal_first[c];
14080 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14081 res[ri++] = c;
14082 }
14083 }
14084
14085 res[ri] = NUL;
14086}
14087
14088 static void
14089spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 slang_T *slang;
14091 char_u *inword;
14092 char_u *res;
14093{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014094 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014095 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014096 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014098 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014100 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 int n, k = 0;
14102 int z0;
14103 int k0;
14104 int n0;
14105 int c;
14106 int pri;
14107 int p0 = -333;
14108 int c0;
14109
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014110 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014111 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 if (slang->sl_rem_accents)
14113 {
14114 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014115 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014117 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014118 {
14119 *t++ = ' ';
14120 s = skipwhite(s);
14121 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014122 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014123 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014124 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 *t++ = *s;
14126 ++s;
14127 }
14128 }
14129 *t = NUL;
14130 }
14131 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014132 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014134 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135
14136 /*
14137 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014138 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014140 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 while ((c = word[i]) != NUL)
14142 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014143 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 n = slang->sl_sal_first[c];
14145 z0 = 0;
14146
14147 if (n >= 0)
14148 {
14149 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014150 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014152 /* Quickly skip entries that don't match the word. Most
14153 * entries are less then three chars, optimize for that. */
14154 k = smp[n].sm_leadlen;
14155 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014157 if (word[i + 1] != s[1])
14158 continue;
14159 if (k > 2)
14160 {
14161 for (j = 2; j < k; ++j)
14162 if (word[i + j] != s[j])
14163 break;
14164 if (j < k)
14165 continue;
14166 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 }
14168
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014169 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014171 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014172 while (*pf != NUL && *pf != word[i + k])
14173 ++pf;
14174 if (*pf == NUL)
14175 continue;
14176 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014178 s = smp[n].sm_rules;
14179 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180
14181 p0 = *s;
14182 k0 = k;
14183 while (*s == '-' && k > 1)
14184 {
14185 k--;
14186 s++;
14187 }
14188 if (*s == '<')
14189 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014190 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 {
14192 /* determine priority */
14193 pri = *s - '0';
14194 s++;
14195 }
14196 if (*s == '^' && *(s + 1) == '^')
14197 s++;
14198
14199 if (*s == NUL
14200 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014201 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014202 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014204 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014206 && spell_iswordp(word + i - 1, curbuf)
14207 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 {
14209 /* search for followup rules, if: */
14210 /* followup and k > 1 and NO '-' in searchstring */
14211 c0 = word[i + k - 1];
14212 n0 = slang->sl_sal_first[c0];
14213
14214 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014215 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 {
14217 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014218 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014220 /* Quickly skip entries that don't match the word.
14221 * */
14222 k0 = smp[n0].sm_leadlen;
14223 if (k0 > 1)
14224 {
14225 if (word[i + k] != s[1])
14226 continue;
14227 if (k0 > 2)
14228 {
14229 pf = word + i + k + 1;
14230 for (j = 2; j < k0; ++j)
14231 if (*pf++ != s[j])
14232 break;
14233 if (j < k0)
14234 continue;
14235 }
14236 }
14237 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014239 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014240 {
14241 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014242 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014243 while (*pf != NUL && *pf != word[i + k0])
14244 ++pf;
14245 if (*pf == NUL)
14246 continue;
14247 ++k0;
14248 }
14249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014251 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 while (*s == '-')
14253 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014254 /* "k0" gets NOT reduced because
14255 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 s++;
14257 }
14258 if (*s == '<')
14259 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014260 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 {
14262 p0 = *s - '0';
14263 s++;
14264 }
14265
14266 if (*s == NUL
14267 /* *s == '^' cuts */
14268 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014269 && !spell_iswordp(word + i + k0,
14270 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 {
14272 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275
14276 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014277 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 /* rule fits; stop search */
14280 break;
14281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 }
14283
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014284 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 }
14287
14288 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014289 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014290 if (s == NULL)
14291 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014292 pf = smp[n].sm_rules;
14293 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 if (p0 == 1 && z == 0)
14295 {
14296 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014297 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14298 || res[reslen - 1] == *s))
14299 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 z0 = 1;
14301 z = 1;
14302 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014303 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 {
14305 word[i + k0] = *s;
14306 k0++;
14307 s++;
14308 }
14309 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014310 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311
14312 /* new "actual letter" */
14313 c = word[i];
14314 }
14315 else
14316 {
14317 /* no '<' rule used */
14318 i += k - 1;
14319 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014320 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014322 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014323 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014324 s++;
14325 }
14326 /* new "actual letter" */
14327 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014328 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 {
14330 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014331 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014332 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 i = 0;
14334 z0 = 1;
14335 }
14336 }
14337 break;
14338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 }
14340 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014341 else if (vim_iswhite(c))
14342 {
14343 c = ' ';
14344 k = 1;
14345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346
14347 if (z0 == 0)
14348 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014349 if (k && !p0 && reslen < MAXWLEN && c != NUL
14350 && (!slang->sl_collapse || reslen == 0
14351 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014353 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354
14355 i++;
14356 z = 0;
14357 k = 0;
14358 }
14359 }
14360
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014361 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362}
14363
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014364#ifdef FEAT_MBYTE
14365/*
14366 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14367 * Multi-byte version of spell_soundfold().
14368 */
14369 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014370spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014371 slang_T *slang;
14372 char_u *inword;
14373 char_u *res;
14374{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014375 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014376 int word[MAXWLEN];
14377 int wres[MAXWLEN];
14378 int l;
14379 char_u *s;
14380 int *ws;
14381 char_u *t;
14382 int *pf;
14383 int i, j, z;
14384 int reslen;
14385 int n, k = 0;
14386 int z0;
14387 int k0;
14388 int n0;
14389 int c;
14390 int pri;
14391 int p0 = -333;
14392 int c0;
14393 int did_white = FALSE;
14394
14395 /*
14396 * Convert the multi-byte string to a wide-character string.
14397 * Remove accents, if wanted. We actually remove all non-word characters.
14398 * But keep white space.
14399 */
14400 n = 0;
14401 for (s = inword; *s != NUL; )
14402 {
14403 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014404 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014405 if (slang->sl_rem_accents)
14406 {
14407 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14408 {
14409 if (did_white)
14410 continue;
14411 c = ' ';
14412 did_white = TRUE;
14413 }
14414 else
14415 {
14416 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014417 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014418 continue;
14419 }
14420 }
14421 word[n++] = c;
14422 }
14423 word[n] = NUL;
14424
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014425 /*
14426 * This comes from Aspell phonet.cpp.
14427 * Converted from C++ to C. Added support for multi-byte chars.
14428 * Changed to keep spaces.
14429 */
14430 i = reslen = z = 0;
14431 while ((c = word[i]) != NUL)
14432 {
14433 /* Start with the first rule that has the character in the word. */
14434 n = slang->sl_sal_first[c & 0xff];
14435 z0 = 0;
14436
14437 if (n >= 0)
14438 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014439 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014440 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14441 {
14442 /* Quickly skip entries that don't match the word. Most
14443 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014444 if (c != ws[0])
14445 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014446 k = smp[n].sm_leadlen;
14447 if (k > 1)
14448 {
14449 if (word[i + 1] != ws[1])
14450 continue;
14451 if (k > 2)
14452 {
14453 for (j = 2; j < k; ++j)
14454 if (word[i + j] != ws[j])
14455 break;
14456 if (j < k)
14457 continue;
14458 }
14459 }
14460
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014461 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014462 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014463 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014464 while (*pf != NUL && *pf != word[i + k])
14465 ++pf;
14466 if (*pf == NUL)
14467 continue;
14468 ++k;
14469 }
14470 s = smp[n].sm_rules;
14471 pri = 5; /* default priority */
14472
14473 p0 = *s;
14474 k0 = k;
14475 while (*s == '-' && k > 1)
14476 {
14477 k--;
14478 s++;
14479 }
14480 if (*s == '<')
14481 s++;
14482 if (VIM_ISDIGIT(*s))
14483 {
14484 /* determine priority */
14485 pri = *s - '0';
14486 s++;
14487 }
14488 if (*s == '^' && *(s + 1) == '^')
14489 s++;
14490
14491 if (*s == NUL
14492 || (*s == '^'
14493 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014494 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014495 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014496 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014497 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014498 && spell_iswordp_w(word + i - 1, curbuf)
14499 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014500 {
14501 /* search for followup rules, if: */
14502 /* followup and k > 1 and NO '-' in searchstring */
14503 c0 = word[i + k - 1];
14504 n0 = slang->sl_sal_first[c0 & 0xff];
14505
14506 if (slang->sl_followup && k > 1 && n0 >= 0
14507 && p0 != '-' && word[i + k] != NUL)
14508 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014509 /* Test follow-up rule for "word[i + k]"; loop over
14510 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014511 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14512 == (c0 & 0xff); ++n0)
14513 {
14514 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014515 */
14516 if (c0 != ws[0])
14517 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014518 k0 = smp[n0].sm_leadlen;
14519 if (k0 > 1)
14520 {
14521 if (word[i + k] != ws[1])
14522 continue;
14523 if (k0 > 2)
14524 {
14525 pf = word + i + k + 1;
14526 for (j = 2; j < k0; ++j)
14527 if (*pf++ != ws[j])
14528 break;
14529 if (j < k0)
14530 continue;
14531 }
14532 }
14533 k0 += k - 1;
14534
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014535 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014536 {
14537 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014538 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014539 while (*pf != NUL && *pf != word[i + k0])
14540 ++pf;
14541 if (*pf == NUL)
14542 continue;
14543 ++k0;
14544 }
14545
14546 p0 = 5;
14547 s = smp[n0].sm_rules;
14548 while (*s == '-')
14549 {
14550 /* "k0" gets NOT reduced because
14551 * "if (k0 == k)" */
14552 s++;
14553 }
14554 if (*s == '<')
14555 s++;
14556 if (VIM_ISDIGIT(*s))
14557 {
14558 p0 = *s - '0';
14559 s++;
14560 }
14561
14562 if (*s == NUL
14563 /* *s == '^' cuts */
14564 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014565 && !spell_iswordp_w(word + i + k0,
14566 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014567 {
14568 if (k0 == k)
14569 /* this is just a piece of the string */
14570 continue;
14571
14572 if (p0 < pri)
14573 /* priority too low */
14574 continue;
14575 /* rule fits; stop search */
14576 break;
14577 }
14578 }
14579
14580 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14581 == (c0 & 0xff))
14582 continue;
14583 }
14584
14585 /* replace string */
14586 ws = smp[n].sm_to_w;
14587 s = smp[n].sm_rules;
14588 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14589 if (p0 == 1 && z == 0)
14590 {
14591 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014592 if (reslen > 0 && ws != NULL && *ws != NUL
14593 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014594 || wres[reslen - 1] == *ws))
14595 reslen--;
14596 z0 = 1;
14597 z = 1;
14598 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014599 if (ws != NULL)
14600 while (*ws != NUL && word[i + k0] != NUL)
14601 {
14602 word[i + k0] = *ws;
14603 k0++;
14604 ws++;
14605 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014606 if (k > k0)
14607 mch_memmove(word + i + k0, word + i + k,
14608 sizeof(int) * (STRLEN(word + i + k) + 1));
14609
14610 /* new "actual letter" */
14611 c = word[i];
14612 }
14613 else
14614 {
14615 /* no '<' rule used */
14616 i += k - 1;
14617 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014618 if (ws != NULL)
14619 while (*ws != NUL && ws[1] != NUL
14620 && reslen < MAXWLEN)
14621 {
14622 if (reslen == 0 || wres[reslen - 1] != *ws)
14623 wres[reslen++] = *ws;
14624 ws++;
14625 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014626 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014627 if (ws == NULL)
14628 c = NUL;
14629 else
14630 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014631 if (strstr((char *)s, "^^") != NULL)
14632 {
14633 if (c != NUL)
14634 wres[reslen++] = c;
14635 mch_memmove(word, word + i + 1,
14636 sizeof(int) * (STRLEN(word + i + 1) + 1));
14637 i = 0;
14638 z0 = 1;
14639 }
14640 }
14641 break;
14642 }
14643 }
14644 }
14645 else if (vim_iswhite(c))
14646 {
14647 c = ' ';
14648 k = 1;
14649 }
14650
14651 if (z0 == 0)
14652 {
14653 if (k && !p0 && reslen < MAXWLEN && c != NUL
14654 && (!slang->sl_collapse || reslen == 0
14655 || wres[reslen - 1] != c))
14656 /* condense only double letters */
14657 wres[reslen++] = c;
14658
14659 i++;
14660 z = 0;
14661 k = 0;
14662 }
14663 }
14664
14665 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14666 l = 0;
14667 for (n = 0; n < reslen; ++n)
14668 {
14669 l += mb_char2bytes(wres[n], res + l);
14670 if (l + MB_MAXBYTES > MAXWLEN)
14671 break;
14672 }
14673 res[l] = NUL;
14674}
14675#endif
14676
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014677/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014678 * Compute a score for two sound-a-like words.
14679 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14680 * Instead of a generic loop we write out the code. That keeps it fast by
14681 * avoiding checks that will not be possible.
14682 */
14683 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014684soundalike_score(goodstart, badstart)
14685 char_u *goodstart; /* sound-folded good word */
14686 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014687{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014688 char_u *goodsound = goodstart;
14689 char_u *badsound = badstart;
14690 int goodlen;
14691 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014692 int n;
14693 char_u *pl, *ps;
14694 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014695 int score = 0;
14696
14697 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14698 * counted so much, vowels halfway the word aren't counted at all. */
14699 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14700 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014701 if (badsound[1] == goodsound[1]
14702 || (badsound[1] != NUL
14703 && goodsound[1] != NUL
14704 && badsound[2] == goodsound[2]))
14705 {
14706 /* handle like a substitute */
14707 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014708 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014709 {
14710 score = 2 * SCORE_DEL / 3;
14711 if (*badsound == '*')
14712 ++badsound;
14713 else
14714 ++goodsound;
14715 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014716 }
14717
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014718 goodlen = (int)STRLEN(goodsound);
14719 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014720
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014721 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014722 * changes. */
14723 n = goodlen - badlen;
14724 if (n < -2 || n > 2)
14725 return SCORE_MAXMAX;
14726
14727 if (n > 0)
14728 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014729 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014730 ps = badsound;
14731 }
14732 else
14733 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014734 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014735 ps = goodsound;
14736 }
14737
14738 /* Skip over the identical part. */
14739 while (*pl == *ps && *pl != NUL)
14740 {
14741 ++pl;
14742 ++ps;
14743 }
14744
14745 switch (n)
14746 {
14747 case -2:
14748 case 2:
14749 /*
14750 * Must delete two characters from "pl".
14751 */
14752 ++pl; /* first delete */
14753 while (*pl == *ps)
14754 {
14755 ++pl;
14756 ++ps;
14757 }
14758 /* strings must be equal after second delete */
14759 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014760 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014761
14762 /* Failed to compare. */
14763 break;
14764
14765 case -1:
14766 case 1:
14767 /*
14768 * Minimal one delete from "pl" required.
14769 */
14770
14771 /* 1: delete */
14772 pl2 = pl + 1;
14773 ps2 = ps;
14774 while (*pl2 == *ps2)
14775 {
14776 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014777 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014778 ++pl2;
14779 ++ps2;
14780 }
14781
14782 /* 2: delete then swap, then rest must be equal */
14783 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14784 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014785 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014786
14787 /* 3: delete then substitute, then the rest must be equal */
14788 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014789 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014790
14791 /* 4: first swap then delete */
14792 if (pl[0] == ps[1] && pl[1] == ps[0])
14793 {
14794 pl2 = pl + 2; /* swap, skip two chars */
14795 ps2 = ps + 2;
14796 while (*pl2 == *ps2)
14797 {
14798 ++pl2;
14799 ++ps2;
14800 }
14801 /* delete a char and then strings must be equal */
14802 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014803 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014804 }
14805
14806 /* 5: first substitute then delete */
14807 pl2 = pl + 1; /* substitute, skip one char */
14808 ps2 = ps + 1;
14809 while (*pl2 == *ps2)
14810 {
14811 ++pl2;
14812 ++ps2;
14813 }
14814 /* delete a char and then strings must be equal */
14815 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014816 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014817
14818 /* Failed to compare. */
14819 break;
14820
14821 case 0:
14822 /*
14823 * Lenghts are equal, thus changes must result in same length: An
14824 * insert is only possible in combination with a delete.
14825 * 1: check if for identical strings
14826 */
14827 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014828 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014829
14830 /* 2: swap */
14831 if (pl[0] == ps[1] && pl[1] == ps[0])
14832 {
14833 pl2 = pl + 2; /* swap, skip two chars */
14834 ps2 = ps + 2;
14835 while (*pl2 == *ps2)
14836 {
14837 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014838 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014839 ++pl2;
14840 ++ps2;
14841 }
14842 /* 3: swap and swap again */
14843 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14844 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014845 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014846
14847 /* 4: swap and substitute */
14848 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014849 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014850 }
14851
14852 /* 5: substitute */
14853 pl2 = pl + 1;
14854 ps2 = ps + 1;
14855 while (*pl2 == *ps2)
14856 {
14857 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014858 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014859 ++pl2;
14860 ++ps2;
14861 }
14862
14863 /* 6: substitute and swap */
14864 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14865 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014866 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014867
14868 /* 7: substitute and substitute */
14869 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014870 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014871
14872 /* 8: insert then delete */
14873 pl2 = pl;
14874 ps2 = ps + 1;
14875 while (*pl2 == *ps2)
14876 {
14877 ++pl2;
14878 ++ps2;
14879 }
14880 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014881 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014882
14883 /* 9: delete then insert */
14884 pl2 = pl + 1;
14885 ps2 = ps;
14886 while (*pl2 == *ps2)
14887 {
14888 ++pl2;
14889 ++ps2;
14890 }
14891 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014892 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014893
14894 /* Failed to compare. */
14895 break;
14896 }
14897
14898 return SCORE_MAXMAX;
14899}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901/*
14902 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014903 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014904 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014905 * The algorithm is described by Du and Chang, 1992.
14906 * The implementation of the algorithm comes from Aspell editdist.cpp,
14907 * edit_distance(). It has been converted from C++ to C and modified to
14908 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014909 */
14910 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014911spell_edit_score(slang, badword, goodword)
14912 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014913 char_u *badword;
14914 char_u *goodword;
14915{
14916 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014917 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 int j, i;
14919 int t;
14920 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014921 int pbc, pgc;
14922#ifdef FEAT_MBYTE
14923 char_u *p;
14924 int wbadword[MAXWLEN];
14925 int wgoodword[MAXWLEN];
14926
14927 if (has_mbyte)
14928 {
14929 /* Get the characters from the multi-byte strings and put them in an
14930 * int array for easy access. */
14931 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014932 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014933 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014934 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014935 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014936 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014937 }
14938 else
14939#endif
14940 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014941 badlen = (int)STRLEN(badword) + 1;
14942 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944
14945 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14946#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014947 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14948 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014949 if (cnt == NULL)
14950 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014951
14952 CNT(0, 0) = 0;
14953 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014954 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955
14956 for (i = 1; i <= badlen; ++i)
14957 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014958 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014959 for (j = 1; j <= goodlen; ++j)
14960 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014961#ifdef FEAT_MBYTE
14962 if (has_mbyte)
14963 {
14964 bc = wbadword[i - 1];
14965 gc = wgoodword[j - 1];
14966 }
14967 else
14968#endif
14969 {
14970 bc = badword[i - 1];
14971 gc = goodword[j - 1];
14972 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 if (bc == gc)
14974 CNT(i, j) = CNT(i - 1, j - 1);
14975 else
14976 {
14977 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014978 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014979 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14980 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014981 {
14982 /* For a similar character use SCORE_SIMILAR. */
14983 if (slang != NULL
14984 && slang->sl_has_map
14985 && similar_chars(slang, gc, bc))
14986 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14987 else
14988 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14989 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014990
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014991 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014993#ifdef FEAT_MBYTE
14994 if (has_mbyte)
14995 {
14996 pbc = wbadword[i - 2];
14997 pgc = wgoodword[j - 2];
14998 }
14999 else
15000#endif
15001 {
15002 pbc = badword[i - 2];
15003 pgc = goodword[j - 2];
15004 }
15005 if (bc == pgc && pbc == gc)
15006 {
15007 t = SCORE_SWAP + CNT(i - 2, j - 2);
15008 if (t < CNT(i, j))
15009 CNT(i, j) = t;
15010 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015011 }
15012 t = SCORE_DEL + CNT(i - 1, j);
15013 if (t < CNT(i, j))
15014 CNT(i, j) = t;
15015 t = SCORE_INS + CNT(i, j - 1);
15016 if (t < CNT(i, j))
15017 CNT(i, j) = t;
15018 }
15019 }
15020 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015021
15022 i = CNT(badlen - 1, goodlen - 1);
15023 vim_free(cnt);
15024 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015025}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015026
Bram Moolenaar4770d092006-01-12 23:22:24 +000015027typedef struct
15028{
15029 int badi;
15030 int goodi;
15031 int score;
15032} limitscore_T;
15033
15034/*
15035 * Like spell_edit_score(), but with a limit on the score to make it faster.
15036 * May return SCORE_MAXMAX when the score is higher than "limit".
15037 *
15038 * This uses a stack for the edits still to be tried.
15039 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15040 * for multi-byte characters.
15041 */
15042 static int
15043spell_edit_score_limit(slang, badword, goodword, limit)
15044 slang_T *slang;
15045 char_u *badword;
15046 char_u *goodword;
15047 int limit;
15048{
15049 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15050 int stackidx;
15051 int bi, gi;
15052 int bi2, gi2;
15053 int bc, gc;
15054 int score;
15055 int score_off;
15056 int minscore;
15057 int round;
15058
15059#ifdef FEAT_MBYTE
15060 /* Multi-byte characters require a bit more work, use a different function
15061 * to avoid testing "has_mbyte" quite often. */
15062 if (has_mbyte)
15063 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15064#endif
15065
15066 /*
15067 * The idea is to go from start to end over the words. So long as
15068 * characters are equal just continue, this always gives the lowest score.
15069 * When there is a difference try several alternatives. Each alternative
15070 * increases "score" for the edit distance. Some of the alternatives are
15071 * pushed unto a stack and tried later, some are tried right away. At the
15072 * end of the word the score for one alternative is known. The lowest
15073 * possible score is stored in "minscore".
15074 */
15075 stackidx = 0;
15076 bi = 0;
15077 gi = 0;
15078 score = 0;
15079 minscore = limit + 1;
15080
15081 for (;;)
15082 {
15083 /* Skip over an equal part, score remains the same. */
15084 for (;;)
15085 {
15086 bc = badword[bi];
15087 gc = goodword[gi];
15088 if (bc != gc) /* stop at a char that's different */
15089 break;
15090 if (bc == NUL) /* both words end */
15091 {
15092 if (score < minscore)
15093 minscore = score;
15094 goto pop; /* do next alternative */
15095 }
15096 ++bi;
15097 ++gi;
15098 }
15099
15100 if (gc == NUL) /* goodword ends, delete badword chars */
15101 {
15102 do
15103 {
15104 if ((score += SCORE_DEL) >= minscore)
15105 goto pop; /* do next alternative */
15106 } while (badword[++bi] != NUL);
15107 minscore = score;
15108 }
15109 else if (bc == NUL) /* badword ends, insert badword chars */
15110 {
15111 do
15112 {
15113 if ((score += SCORE_INS) >= minscore)
15114 goto pop; /* do next alternative */
15115 } while (goodword[++gi] != NUL);
15116 minscore = score;
15117 }
15118 else /* both words continue */
15119 {
15120 /* If not close to the limit, perform a change. Only try changes
15121 * that may lead to a lower score than "minscore".
15122 * round 0: try deleting a char from badword
15123 * round 1: try inserting a char in badword */
15124 for (round = 0; round <= 1; ++round)
15125 {
15126 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15127 if (score_off < minscore)
15128 {
15129 if (score_off + SCORE_EDIT_MIN >= minscore)
15130 {
15131 /* Near the limit, rest of the words must match. We
15132 * can check that right now, no need to push an item
15133 * onto the stack. */
15134 bi2 = bi + 1 - round;
15135 gi2 = gi + round;
15136 while (goodword[gi2] == badword[bi2])
15137 {
15138 if (goodword[gi2] == NUL)
15139 {
15140 minscore = score_off;
15141 break;
15142 }
15143 ++bi2;
15144 ++gi2;
15145 }
15146 }
15147 else
15148 {
15149 /* try deleting/inserting a character later */
15150 stack[stackidx].badi = bi + 1 - round;
15151 stack[stackidx].goodi = gi + round;
15152 stack[stackidx].score = score_off;
15153 ++stackidx;
15154 }
15155 }
15156 }
15157
15158 if (score + SCORE_SWAP < minscore)
15159 {
15160 /* If swapping two characters makes a match then the
15161 * substitution is more expensive, thus there is no need to
15162 * try both. */
15163 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15164 {
15165 /* Swap two characters, that is: skip them. */
15166 gi += 2;
15167 bi += 2;
15168 score += SCORE_SWAP;
15169 continue;
15170 }
15171 }
15172
15173 /* Substitute one character for another which is the same
15174 * thing as deleting a character from both goodword and badword.
15175 * Use a better score when there is only a case difference. */
15176 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15177 score += SCORE_ICASE;
15178 else
15179 {
15180 /* For a similar character use SCORE_SIMILAR. */
15181 if (slang != NULL
15182 && slang->sl_has_map
15183 && similar_chars(slang, gc, bc))
15184 score += SCORE_SIMILAR;
15185 else
15186 score += SCORE_SUBST;
15187 }
15188
15189 if (score < minscore)
15190 {
15191 /* Do the substitution. */
15192 ++gi;
15193 ++bi;
15194 continue;
15195 }
15196 }
15197pop:
15198 /*
15199 * Get here to try the next alternative, pop it from the stack.
15200 */
15201 if (stackidx == 0) /* stack is empty, finished */
15202 break;
15203
15204 /* pop an item from the stack */
15205 --stackidx;
15206 gi = stack[stackidx].goodi;
15207 bi = stack[stackidx].badi;
15208 score = stack[stackidx].score;
15209 }
15210
15211 /* When the score goes over "limit" it may actually be much higher.
15212 * Return a very large number to avoid going below the limit when giving a
15213 * bonus. */
15214 if (minscore > limit)
15215 return SCORE_MAXMAX;
15216 return minscore;
15217}
15218
15219#ifdef FEAT_MBYTE
15220/*
15221 * Multi-byte version of spell_edit_score_limit().
15222 * Keep it in sync with the above!
15223 */
15224 static int
15225spell_edit_score_limit_w(slang, badword, goodword, limit)
15226 slang_T *slang;
15227 char_u *badword;
15228 char_u *goodword;
15229 int limit;
15230{
15231 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15232 int stackidx;
15233 int bi, gi;
15234 int bi2, gi2;
15235 int bc, gc;
15236 int score;
15237 int score_off;
15238 int minscore;
15239 int round;
15240 char_u *p;
15241 int wbadword[MAXWLEN];
15242 int wgoodword[MAXWLEN];
15243
15244 /* Get the characters from the multi-byte strings and put them in an
15245 * int array for easy access. */
15246 bi = 0;
15247 for (p = badword; *p != NUL; )
15248 wbadword[bi++] = mb_cptr2char_adv(&p);
15249 wbadword[bi++] = 0;
15250 gi = 0;
15251 for (p = goodword; *p != NUL; )
15252 wgoodword[gi++] = mb_cptr2char_adv(&p);
15253 wgoodword[gi++] = 0;
15254
15255 /*
15256 * The idea is to go from start to end over the words. So long as
15257 * characters are equal just continue, this always gives the lowest score.
15258 * When there is a difference try several alternatives. Each alternative
15259 * increases "score" for the edit distance. Some of the alternatives are
15260 * pushed unto a stack and tried later, some are tried right away. At the
15261 * end of the word the score for one alternative is known. The lowest
15262 * possible score is stored in "minscore".
15263 */
15264 stackidx = 0;
15265 bi = 0;
15266 gi = 0;
15267 score = 0;
15268 minscore = limit + 1;
15269
15270 for (;;)
15271 {
15272 /* Skip over an equal part, score remains the same. */
15273 for (;;)
15274 {
15275 bc = wbadword[bi];
15276 gc = wgoodword[gi];
15277
15278 if (bc != gc) /* stop at a char that's different */
15279 break;
15280 if (bc == NUL) /* both words end */
15281 {
15282 if (score < minscore)
15283 minscore = score;
15284 goto pop; /* do next alternative */
15285 }
15286 ++bi;
15287 ++gi;
15288 }
15289
15290 if (gc == NUL) /* goodword ends, delete badword chars */
15291 {
15292 do
15293 {
15294 if ((score += SCORE_DEL) >= minscore)
15295 goto pop; /* do next alternative */
15296 } while (wbadword[++bi] != NUL);
15297 minscore = score;
15298 }
15299 else if (bc == NUL) /* badword ends, insert badword chars */
15300 {
15301 do
15302 {
15303 if ((score += SCORE_INS) >= minscore)
15304 goto pop; /* do next alternative */
15305 } while (wgoodword[++gi] != NUL);
15306 minscore = score;
15307 }
15308 else /* both words continue */
15309 {
15310 /* If not close to the limit, perform a change. Only try changes
15311 * that may lead to a lower score than "minscore".
15312 * round 0: try deleting a char from badword
15313 * round 1: try inserting a char in badword */
15314 for (round = 0; round <= 1; ++round)
15315 {
15316 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15317 if (score_off < minscore)
15318 {
15319 if (score_off + SCORE_EDIT_MIN >= minscore)
15320 {
15321 /* Near the limit, rest of the words must match. We
15322 * can check that right now, no need to push an item
15323 * onto the stack. */
15324 bi2 = bi + 1 - round;
15325 gi2 = gi + round;
15326 while (wgoodword[gi2] == wbadword[bi2])
15327 {
15328 if (wgoodword[gi2] == NUL)
15329 {
15330 minscore = score_off;
15331 break;
15332 }
15333 ++bi2;
15334 ++gi2;
15335 }
15336 }
15337 else
15338 {
15339 /* try deleting a character from badword later */
15340 stack[stackidx].badi = bi + 1 - round;
15341 stack[stackidx].goodi = gi + round;
15342 stack[stackidx].score = score_off;
15343 ++stackidx;
15344 }
15345 }
15346 }
15347
15348 if (score + SCORE_SWAP < minscore)
15349 {
15350 /* If swapping two characters makes a match then the
15351 * substitution is more expensive, thus there is no need to
15352 * try both. */
15353 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15354 {
15355 /* Swap two characters, that is: skip them. */
15356 gi += 2;
15357 bi += 2;
15358 score += SCORE_SWAP;
15359 continue;
15360 }
15361 }
15362
15363 /* Substitute one character for another which is the same
15364 * thing as deleting a character from both goodword and badword.
15365 * Use a better score when there is only a case difference. */
15366 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15367 score += SCORE_ICASE;
15368 else
15369 {
15370 /* For a similar character use SCORE_SIMILAR. */
15371 if (slang != NULL
15372 && slang->sl_has_map
15373 && similar_chars(slang, gc, bc))
15374 score += SCORE_SIMILAR;
15375 else
15376 score += SCORE_SUBST;
15377 }
15378
15379 if (score < minscore)
15380 {
15381 /* Do the substitution. */
15382 ++gi;
15383 ++bi;
15384 continue;
15385 }
15386 }
15387pop:
15388 /*
15389 * Get here to try the next alternative, pop it from the stack.
15390 */
15391 if (stackidx == 0) /* stack is empty, finished */
15392 break;
15393
15394 /* pop an item from the stack */
15395 --stackidx;
15396 gi = stack[stackidx].goodi;
15397 bi = stack[stackidx].badi;
15398 score = stack[stackidx].score;
15399 }
15400
15401 /* When the score goes over "limit" it may actually be much higher.
15402 * Return a very large number to avoid going below the limit when giving a
15403 * bonus. */
15404 if (minscore > limit)
15405 return SCORE_MAXMAX;
15406 return minscore;
15407}
15408#endif
15409
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015410/*
15411 * ":spellinfo"
15412 */
15413/*ARGSUSED*/
15414 void
15415ex_spellinfo(eap)
15416 exarg_T *eap;
15417{
15418 int lpi;
15419 langp_T *lp;
15420 char_u *p;
15421
15422 if (no_spell_checking(curwin))
15423 return;
15424
15425 msg_start();
15426 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15427 {
15428 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15429 msg_puts((char_u *)"file: ");
15430 msg_puts(lp->lp_slang->sl_fname);
15431 msg_putchar('\n');
15432 p = lp->lp_slang->sl_info;
15433 if (p != NULL)
15434 {
15435 msg_puts(p);
15436 msg_putchar('\n');
15437 }
15438 }
15439 msg_end();
15440}
15441
Bram Moolenaar4770d092006-01-12 23:22:24 +000015442#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15443#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015444#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015445#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15446#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015447
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015448/*
15449 * ":spelldump"
15450 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015451 void
15452ex_spelldump(eap)
15453 exarg_T *eap;
15454{
15455 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015456
15457 if (no_spell_checking(curwin))
15458 return;
15459
15460 /* Create a new empty buffer by splitting the window. */
15461 do_cmdline_cmd((char_u *)"new");
15462 if (!bufempty() || !buf_valid(buf))
15463 return;
15464
15465 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15466
15467 /* Delete the empty line that we started with. */
15468 if (curbuf->b_ml.ml_line_count > 1)
15469 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15470
15471 redraw_later(NOT_VALID);
15472}
15473
15474/*
15475 * Go through all possible words and:
15476 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15477 * "ic" and "dir" are not used.
15478 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15479 */
15480 void
15481spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15482 buf_T *buf; /* buffer with spell checking */
15483 char_u *pat; /* leading part of the word */
15484 int ic; /* ignore case */
15485 int *dir; /* direction for adding matches */
15486 int dumpflags_arg; /* DUMPFLAG_* */
15487{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015488 langp_T *lp;
15489 slang_T *slang;
15490 idx_T arridx[MAXWLEN];
15491 int curi[MAXWLEN];
15492 char_u word[MAXWLEN];
15493 int c;
15494 char_u *byts;
15495 idx_T *idxs;
15496 linenr_T lnum = 0;
15497 int round;
15498 int depth;
15499 int n;
15500 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015501 char_u *region_names = NULL; /* region names being used */
15502 int do_region = TRUE; /* dump region names and numbers */
15503 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015504 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015505 int dumpflags = dumpflags_arg;
15506 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015507
Bram Moolenaard0131a82006-03-04 21:46:13 +000015508 /* When ignoring case or when the pattern starts with capital pass this on
15509 * to dump_word(). */
15510 if (pat != NULL)
15511 {
15512 if (ic)
15513 dumpflags |= DUMPFLAG_ICASE;
15514 else
15515 {
15516 n = captype(pat, NULL);
15517 if (n == WF_ONECAP)
15518 dumpflags |= DUMPFLAG_ONECAP;
15519 else if (n == WF_ALLCAP
15520#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015521 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015522#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015523 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015524#endif
15525 )
15526 dumpflags |= DUMPFLAG_ALLCAP;
15527 }
15528 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015529
Bram Moolenaar7887d882005-07-01 22:33:52 +000015530 /* Find out if we can support regions: All languages must support the same
15531 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015532 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015533 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015534 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015535 p = lp->lp_slang->sl_regions;
15536 if (p[0] != 0)
15537 {
15538 if (region_names == NULL) /* first language with regions */
15539 region_names = p;
15540 else if (STRCMP(region_names, p) != 0)
15541 {
15542 do_region = FALSE; /* region names are different */
15543 break;
15544 }
15545 }
15546 }
15547
15548 if (do_region && region_names != NULL)
15549 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015550 if (pat == NULL)
15551 {
15552 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15553 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15554 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015555 }
15556 else
15557 do_region = FALSE;
15558
15559 /*
15560 * Loop over all files loaded for the entries in 'spelllang'.
15561 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015562 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015563 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015564 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015565 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015566 if (slang->sl_fbyts == NULL) /* reloading failed */
15567 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015568
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015569 if (pat == NULL)
15570 {
15571 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15572 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15573 }
15574
15575 /* When matching with a pattern and there are no prefixes only use
15576 * parts of the tree that match "pat". */
15577 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015578 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015579 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015580 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015581
15582 /* round 1: case-folded tree
15583 * round 2: keep-case tree */
15584 for (round = 1; round <= 2; ++round)
15585 {
15586 if (round == 1)
15587 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015588 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015589 byts = slang->sl_fbyts;
15590 idxs = slang->sl_fidxs;
15591 }
15592 else
15593 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015594 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015595 byts = slang->sl_kbyts;
15596 idxs = slang->sl_kidxs;
15597 }
15598 if (byts == NULL)
15599 continue; /* array is empty */
15600
15601 depth = 0;
15602 arridx[0] = 0;
15603 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015604 while (depth >= 0 && !got_int
15605 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015606 {
15607 if (curi[depth] > byts[arridx[depth]])
15608 {
15609 /* Done all bytes at this node, go up one level. */
15610 --depth;
15611 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015612 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015613 }
15614 else
15615 {
15616 /* Do one more byte at this node. */
15617 n = arridx[depth] + curi[depth];
15618 ++curi[depth];
15619 c = byts[n];
15620 if (c == 0)
15621 {
15622 /* End of word, deal with the word.
15623 * Don't use keep-case words in the fold-case tree,
15624 * they will appear in the keep-case tree.
15625 * Only use the word when the region matches. */
15626 flags = (int)idxs[n];
15627 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015628 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015629 && (do_region
15630 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015631 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015632 & lp->lp_region) != 0))
15633 {
15634 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015635 if (!do_region)
15636 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015637
15638 /* Dump the basic word if there is no prefix or
15639 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015640 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015641 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015642 {
15643 dump_word(slang, word, pat, dir,
15644 dumpflags, flags, lnum);
15645 if (pat == NULL)
15646 ++lnum;
15647 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015648
15649 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015650 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015651 lnum = dump_prefixes(slang, word, pat, dir,
15652 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015653 }
15654 }
15655 else
15656 {
15657 /* Normal char, go one level deeper. */
15658 word[depth++] = c;
15659 arridx[depth] = idxs[n];
15660 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015661
15662 /* Check if this characters matches with the pattern.
15663 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015664 * Always ignore case here, dump_word() will check
15665 * proper case later. This isn't exactly right when
15666 * length changes for multi-byte characters with
15667 * ignore case... */
15668 if (depth <= patlen
15669 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015670 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015671 }
15672 }
15673 }
15674 }
15675 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015676}
15677
15678/*
15679 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015680 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015681 */
15682 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015683dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015684 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015685 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015686 char_u *pat;
15687 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015688 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015689 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015690 linenr_T lnum;
15691{
15692 int keepcap = FALSE;
15693 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015694 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015695 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015696 char_u badword[MAXWLEN + 10];
15697 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015698 int flags = wordflags;
15699
15700 if (dumpflags & DUMPFLAG_ONECAP)
15701 flags |= WF_ONECAP;
15702 if (dumpflags & DUMPFLAG_ALLCAP)
15703 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015704
Bram Moolenaar4770d092006-01-12 23:22:24 +000015705 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015706 {
15707 /* Need to fix case according to "flags". */
15708 make_case_word(word, cword, flags);
15709 p = cword;
15710 }
15711 else
15712 {
15713 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015714 if ((dumpflags & DUMPFLAG_KEEPCASE)
15715 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015716 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015717 keepcap = TRUE;
15718 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015719 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015720
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015721 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015722 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015723 /* Add flags and regions after a slash. */
15724 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015725 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015726 STRCPY(badword, p);
15727 STRCAT(badword, "/");
15728 if (keepcap)
15729 STRCAT(badword, "=");
15730 if (flags & WF_BANNED)
15731 STRCAT(badword, "!");
15732 else if (flags & WF_RARE)
15733 STRCAT(badword, "?");
15734 if (flags & WF_REGION)
15735 for (i = 0; i < 7; ++i)
15736 if (flags & (0x10000 << i))
15737 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15738 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015739 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015740
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015741 if (dumpflags & DUMPFLAG_COUNT)
15742 {
15743 hashitem_T *hi;
15744
15745 /* Include the word count for ":spelldump!". */
15746 hi = hash_find(&slang->sl_wordcount, tw);
15747 if (!HASHITEM_EMPTY(hi))
15748 {
15749 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15750 tw, HI2WC(hi)->wc_count);
15751 p = IObuff;
15752 }
15753 }
15754
15755 ml_append(lnum, p, (colnr_T)0, FALSE);
15756 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015757 else if (((dumpflags & DUMPFLAG_ICASE)
15758 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15759 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015760 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015761 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015762 /* if dir was BACKWARD then honor it just once */
15763 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015764}
15765
15766/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015767 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15768 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015769 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015770 * Return the updated line number.
15771 */
15772 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015773dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015774 slang_T *slang;
15775 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015776 char_u *pat;
15777 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015778 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015779 int flags; /* flags with prefix ID */
15780 linenr_T startlnum;
15781{
15782 idx_T arridx[MAXWLEN];
15783 int curi[MAXWLEN];
15784 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015785 char_u word_up[MAXWLEN];
15786 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015787 int c;
15788 char_u *byts;
15789 idx_T *idxs;
15790 linenr_T lnum = startlnum;
15791 int depth;
15792 int n;
15793 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015794 int i;
15795
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015796 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015797 * upper-case letter in word_up[]. */
15798 c = PTR2CHAR(word);
15799 if (SPELL_TOUPPER(c) != c)
15800 {
15801 onecap_copy(word, word_up, TRUE);
15802 has_word_up = TRUE;
15803 }
15804
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015805 byts = slang->sl_pbyts;
15806 idxs = slang->sl_pidxs;
15807 if (byts != NULL) /* array not is empty */
15808 {
15809 /*
15810 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015811 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015812 */
15813 depth = 0;
15814 arridx[0] = 0;
15815 curi[0] = 1;
15816 while (depth >= 0 && !got_int)
15817 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015818 n = arridx[depth];
15819 len = byts[n];
15820 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015821 {
15822 /* Done all bytes at this node, go up one level. */
15823 --depth;
15824 line_breakcheck();
15825 }
15826 else
15827 {
15828 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015829 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015830 ++curi[depth];
15831 c = byts[n];
15832 if (c == 0)
15833 {
15834 /* End of prefix, find out how many IDs there are. */
15835 for (i = 1; i < len; ++i)
15836 if (byts[n + i] != 0)
15837 break;
15838 curi[depth] += i - 1;
15839
Bram Moolenaar53805d12005-08-01 07:08:33 +000015840 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15841 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015842 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015843 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015844 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015845 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015846 : flags, lnum);
15847 if (lnum != 0)
15848 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015849 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015850
15851 /* Check for prefix that matches the word when the
15852 * first letter is upper-case, but only if the prefix has
15853 * a condition. */
15854 if (has_word_up)
15855 {
15856 c = valid_word_prefix(i, n, flags, word_up, slang,
15857 TRUE);
15858 if (c != 0)
15859 {
15860 vim_strncpy(prefix + depth, word_up,
15861 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015862 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015863 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015864 : flags, lnum);
15865 if (lnum != 0)
15866 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015867 }
15868 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015869 }
15870 else
15871 {
15872 /* Normal char, go one level deeper. */
15873 prefix[depth++] = c;
15874 arridx[depth] = idxs[n];
15875 curi[depth] = 1;
15876 }
15877 }
15878 }
15879 }
15880
15881 return lnum;
15882}
15883
Bram Moolenaar95529562005-08-25 21:21:38 +000015884/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015885 * Move "p" to the end of word "start".
15886 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015887 */
15888 char_u *
15889spell_to_word_end(start, buf)
15890 char_u *start;
15891 buf_T *buf;
15892{
15893 char_u *p = start;
15894
15895 while (*p != NUL && spell_iswordp(p, buf))
15896 mb_ptr_adv(p);
15897 return p;
15898}
15899
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015900#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015901/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015902 * For Insert mode completion CTRL-X s:
15903 * Find start of the word in front of column "startcol".
15904 * We don't check if it is badly spelled, with completion we can only change
15905 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015906 * Returns the column number of the word.
15907 */
15908 int
15909spell_word_start(startcol)
15910 int startcol;
15911{
15912 char_u *line;
15913 char_u *p;
15914 int col = 0;
15915
Bram Moolenaar95529562005-08-25 21:21:38 +000015916 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015917 return startcol;
15918
15919 /* Find a word character before "startcol". */
15920 line = ml_get_curline();
15921 for (p = line + startcol; p > line; )
15922 {
15923 mb_ptr_back(line, p);
15924 if (spell_iswordp_nmw(p))
15925 break;
15926 }
15927
15928 /* Go back to start of the word. */
15929 while (p > line)
15930 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015931 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015932 mb_ptr_back(line, p);
15933 if (!spell_iswordp(p, curbuf))
15934 break;
15935 col = 0;
15936 }
15937
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015938 return col;
15939}
15940
15941/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015942 * Need to check for 'spellcapcheck' now, the word is removed before
15943 * expand_spelling() is called. Therefore the ugly global variable.
15944 */
15945static int spell_expand_need_cap;
15946
15947 void
15948spell_expand_check_cap(col)
15949 colnr_T col;
15950{
15951 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15952}
15953
15954/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015955 * Get list of spelling suggestions.
15956 * Used for Insert mode completion CTRL-X ?.
15957 * Returns the number of matches. The matches are in "matchp[]", array of
15958 * allocated strings.
15959 */
15960/*ARGSUSED*/
15961 int
15962expand_spelling(lnum, col, pat, matchp)
15963 linenr_T lnum;
15964 int col;
15965 char_u *pat;
15966 char_u ***matchp;
15967{
15968 garray_T ga;
15969
Bram Moolenaar4770d092006-01-12 23:22:24 +000015970 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015971 *matchp = ga.ga_data;
15972 return ga.ga_len;
15973}
15974#endif
15975
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015976#endif /* FEAT_SPELL */