blob: a11372ab3d67419c3823e6651554a4b47f77f8fb [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
80 * compute the the maximum word score that can be used.
81 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaare19defe2005-03-21 08:23:33 +0000308#endif
309
310#include "vim.h"
311
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000312#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000313
314#ifdef HAVE_FCNTL_H
315# include <fcntl.h>
316#endif
317
Bram Moolenaar4770d092006-01-12 23:22:24 +0000318#ifndef UNIX /* it's in os_unix.h for Unix */
319# include <time.h> /* for time_t */
320#endif
321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000322#define MAXWLEN 250 /* Assume max. word len is this many bytes.
323 Some places assume a word length fits in a
324 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000325
Bram Moolenaare52325c2005-08-22 22:54:29 +0000326/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000327 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000328#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000329typedef int idx_T;
330#else
331typedef long idx_T;
332#endif
333
334/* Flags used for a word. Only the lowest byte can be used, the region byte
335 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336#define WF_REGION 0x01 /* region byte follows */
337#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
338#define WF_ALLCAP 0x04 /* word must be all capitals */
339#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000340#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000341#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000342#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000345/* for <flags2>, shifted up one byte to be used in wn_flags */
346#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000347#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000348#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000349#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000350#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
351#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000352
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000353/* only used for su_badflags */
354#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
355
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000356#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000357
Bram Moolenaar53805d12005-08-01 07:08:33 +0000358/* flags for <pflags> */
359#define WFP_RARE 0x01 /* rare prefix */
360#define WFP_NC 0x02 /* prefix is not combining */
361#define WFP_UP 0x04 /* to-upper prefix */
362
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000363/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000364 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000365#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
366 * postponed prefix */
367#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
368 * postponed prefix */
369#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
370 * postponed prefix */
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;
586#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
587#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
628/* Number of suggestions kept when cleaning up. we need to keep more than
629 * 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
752static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
753#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000754static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000755
756/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000757 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000758 */
759typedef enum
760{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 STATE_START = 0, /* At start of node check for NUL bytes (goodword
762 * ends); if badword ends there is a match, otherwise
763 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000764 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000766 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
767 STATE_PLAIN, /* Use each byte of the node. */
768 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000769 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 STATE_INS, /* Insert a byte in the bad word. */
771 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000772 STATE_UNSWAP, /* Undo swap two characters. */
773 STATE_SWAP3, /* Swap two characters over three. */
774 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000775 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000777 STATE_REP_INI, /* Prepare for using REP items. */
778 STATE_REP, /* Use matching REP items from the .aff file. */
779 STATE_REP_UNDO, /* Undo a REP item replacement. */
780 STATE_FINAL /* End of this node. */
781} state_T;
782
783/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000784 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785 */
786typedef struct trystate_S
787{
Bram Moolenaarea424162005-06-16 21:51:00 +0000788 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000790 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000791 short ts_curi; /* index in list of child nodes */
792 char_u ts_fidx; /* index in fword[], case-folded bad word */
793 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
794 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000795 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000796 * PFD_PREFIXTREE or PFD_NOPREFIX */
797 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000798#ifdef FEAT_MBYTE
799 char_u ts_tcharlen; /* number of bytes in tword character */
800 char_u ts_tcharidx; /* current byte index in tword character */
801 char_u ts_isdiff; /* DIFF_ values */
802 char_u ts_fcharstart; /* index in fword where badword char started */
803#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000804 char_u ts_prewordlen; /* length of word in "preword[]" */
805 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000806 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000807 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000808 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000809 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000810 char_u ts_delidx; /* index in fword for char that was deleted,
811 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000812} trystate_T;
813
Bram Moolenaarea424162005-06-16 21:51:00 +0000814/* values for ts_isdiff */
815#define DIFF_NONE 0 /* no different byte (yet) */
816#define DIFF_YES 1 /* different byte found */
817#define DIFF_INSERT 2 /* inserting character */
818
Bram Moolenaard12a1322005-08-21 22:08:24 +0000819/* values for ts_flags */
820#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
821#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000822#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000823
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000824/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000825#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000826#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000827#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000828
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000829/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000830#define FIND_FOLDWORD 0 /* find word case-folded */
831#define FIND_KEEPWORD 1 /* find keep-case word */
832#define FIND_PREFIX 2 /* find word after prefix */
833#define FIND_COMPOUND 3 /* find case-folded compound word */
834#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000835
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836static slang_T *slang_alloc __ARGS((char_u *lang));
837static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000838static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000839static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000840static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000841static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000842static 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 +0000843static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000845static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000846static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000847static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000848static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000849static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000850static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000851static 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 +0000852static int get2c __ARGS((FILE *fd));
853static int get3c __ARGS((FILE *fd));
854static int get4c __ARGS((FILE *fd));
855static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000856static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000857static char_u *read_string __ARGS((FILE *fd, int cnt));
858static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
859static int read_charflags_section __ARGS((FILE *fd));
860static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000861static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000862static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000863static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
864static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
865static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
867static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000868static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000869static int init_syl_tab __ARGS((slang_T *slang));
870static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000871static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
872static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000873#ifdef FEAT_MBYTE
874static int *mb_str2wide __ARGS((char_u *s));
875#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000876static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
877static 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 +0000878static void clear_midword __ARGS((buf_T *buf));
879static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880static int find_region __ARGS((char_u *rp, char_u *region));
881static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000883static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000884static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000885static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000887static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000888static 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 +0000889#ifdef FEAT_EVAL
890static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
891#endif
892static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000893static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
894static void suggest_load_files __ARGS((void));
895static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000896static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000897static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000898static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000899static void suggest_try_special __ARGS((suginfo_T *su));
900static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
902static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000903#ifdef FEAT_MBYTE
904static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000906static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000907static void score_comp_sal __ARGS((suginfo_T *su));
908static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000909static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000910static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000911static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000912static void suggest_try_soundalike_finish __ARGS((void));
913static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
914static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000916static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static 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));
919static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000920static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000921static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000922static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000923static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000924static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
925static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
926static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000927#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000929#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000931static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
932static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
933#ifdef FEAT_MBYTE
934static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
935#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000936static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
937static 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 +0000938static buf_T *open_spellbuf __ARGS((void));
939static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000940
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000941/*
942 * Use our own character-case definitions, because the current locale may
943 * differ from what the .spl file uses.
944 * These must not be called with negative number!
945 */
946#ifndef FEAT_MBYTE
947/* Non-multi-byte implementation. */
948# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
949# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
950# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
951#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000952# if defined(HAVE_WCHAR_H)
953# include <wchar.h> /* for towupper() and towlower() */
954# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000955/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
956 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
957 * the "w" library function for characters above 255 if available. */
958# ifdef HAVE_TOWLOWER
959# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
960 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
961# else
962# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
963 : (c) < 256 ? spelltab.st_fold[c] : (c))
964# endif
965
966# ifdef HAVE_TOWUPPER
967# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
968 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
969# else
970# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
971 : (c) < 256 ? spelltab.st_upper[c] : (c))
972# endif
973
974# ifdef HAVE_ISWUPPER
975# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
976 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
977# else
978# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000979 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000980# endif
981#endif
982
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000983
984static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000985static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000986static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000987static char *e_affname = N_("Affix name too long in %s line %d: %s");
988static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
989static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000990static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000992/* Remember what "z?" replaced. */
993static char_u *repl_from = NULL;
994static char_u *repl_to = NULL;
995
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000996/*
997 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000998 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000999 * "*attrp" is set to the highlight index for a badly spelled word. For a
1000 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001002 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001003 * "capcol" is used to check for a Capitalised word after the end of a
1004 * sentence. If it's zero then perform the check. Return the column where to
1005 * check next, or -1 when no sentence end was found. If it's NULL then don't
1006 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001007 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 * Returns the length of the word in bytes, also when it's OK, so that the
1009 * caller can skip over the word.
1010 */
1011 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001015 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001016 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001017 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018{
1019 matchinf_T mi; /* Most things are put in "mi" so that it can
1020 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001021 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001023 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001024 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001025 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001026
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001027 /* A word never starts at a space or a control character. Return quickly
1028 * then, skipping over the character. */
1029 if (*ptr <= ' ')
1030 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001031
1032 /* Return here when loading language files failed. */
1033 if (wp->w_buffer->b_langp.ga_len == 0)
1034 return 1;
1035
Bram Moolenaar5195e452005-08-19 20:32:47 +00001036 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001038 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001039 * 0X99FF. But always do check spelling to find "3GPP" and "11
1040 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001043 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1044 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045 else
1046 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +00001047 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001049
Bram Moolenaar0c405862005-06-22 22:26:26 +00001050 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001052 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001053 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001057 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001058 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001059
1060 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1061 {
1062 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001063 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001064 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001065 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001066 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001067 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (capcol != NULL)
1069 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001070
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 /* We always use the characters up to the next non-word character,
1072 * also for bad words. */
1073 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001076 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001077
Bram Moolenaar5195e452005-08-19 20:32:47 +00001078 /* case-fold the word with one non-word character, so that we can check
1079 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001080 if (*mi.mi_fend != NUL)
1081 mb_ptr_adv(mi.mi_fend);
1082
1083 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1084 MAXWLEN + 1);
1085 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1086
1087 /* The word is bad unless we recognize it. */
1088 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001089 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090
1091 /*
1092 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001093 * We check them all, because a word may be matched longer in another
1094 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001095 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001096 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001097 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001098 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1099
1100 /* If reloading fails the language is still in the list but everything
1101 * has been cleared. */
1102 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1103 continue;
1104
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001105 /* Check for a matching word in case-folded words. */
1106 find_word(&mi, FIND_FOLDWORD);
1107
1108 /* Check for a matching word in keep-case words. */
1109 find_word(&mi, FIND_KEEPWORD);
1110
1111 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001112 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001113
1114 /* For a NOBREAK language, may want to use a word without a following
1115 * word as a backup. */
1116 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1117 && mi.mi_result2 != SP_BAD)
1118 {
1119 mi.mi_result = mi.mi_result2;
1120 mi.mi_end = mi.mi_end2;
1121 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001122
1123 /* Count the word in the first language where it's found to be OK. */
1124 if (count_word && mi.mi_result == SP_OK)
1125 {
1126 count_common_word(mi.mi_lp->lp_slang, ptr,
1127 (int)(mi.mi_end - ptr), 1);
1128 count_word = FALSE;
1129 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001130 }
1131
1132 if (mi.mi_result != SP_OK)
1133 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001134 /* If we found a number skip over it. Allows for "42nd". Do flag
1135 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001136 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001137 {
1138 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1139 return nrlen;
1140 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001141
1142 /* When we are at a non-word character there is no error, just
1143 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001144 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001145 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001146 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1147 {
1148 regmatch_T regmatch;
1149
1150 /* Check for end of sentence. */
1151 regmatch.regprog = wp->w_buffer->b_cap_prog;
1152 regmatch.rm_ic = FALSE;
1153 if (vim_regexec(&regmatch, ptr, 0))
1154 *capcol = (int)(regmatch.endp[0] - ptr);
1155 }
1156
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001157#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001159 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001160#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001161 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001162 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001163 else if (mi.mi_end == ptr)
1164 /* Always include at least one character. Required for when there
1165 * is a mixup in "midword". */
1166 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001167 else if (mi.mi_result == SP_BAD
1168 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1169 {
1170 char_u *p, *fp;
1171 int save_result = mi.mi_result;
1172
1173 /* First language in 'spelllang' is NOBREAK. Find first position
1174 * at which any word would be valid. */
1175 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001176 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001177 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001178 p = mi.mi_word;
1179 fp = mi.mi_fword;
1180 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001182 mb_ptr_adv(p);
1183 mb_ptr_adv(fp);
1184 if (p >= mi.mi_end)
1185 break;
1186 mi.mi_compoff = fp - mi.mi_fword;
1187 find_word(&mi, FIND_COMPOUND);
1188 if (mi.mi_result != SP_BAD)
1189 {
1190 mi.mi_end = p;
1191 break;
1192 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001193 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001194 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001195 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001196 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001197
1198 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001199 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001200 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001201 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001202 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001204 }
1205
Bram Moolenaar5195e452005-08-19 20:32:47 +00001206 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1207 {
1208 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001209 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 return wrongcaplen;
1211 }
1212
Bram Moolenaar51485f02005-06-04 21:55:20 +00001213 return (int)(mi.mi_end - ptr);
1214}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215
Bram Moolenaar51485f02005-06-04 21:55:20 +00001216/*
1217 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001218 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1219 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1220 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1221 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001222 *
1223 * For a match mip->mi_result is updated.
1224 */
1225 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001226find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001227 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001230 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001232 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 int endidxcnt = 0;
1234 int len;
1235 int wlen = 0;
1236 int flen;
1237 int c;
1238 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001239 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001240#ifdef FEAT_MBYTE
1241 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001243 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001244 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001245 slang_T *slang = mip->mi_lp->lp_slang;
1246 unsigned flags;
1247 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001248 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001249 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001250 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001251 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001252
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001253 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 /* Check for word with matching case in keep-case tree. */
1256 ptr = mip->mi_word;
1257 flen = 9999; /* no case folding, always enough bytes */
1258 byts = slang->sl_kbyts;
1259 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001260
1261 if (mode == FIND_KEEPCOMPOUND)
1262 /* Skip over the previously found word(s). */
1263 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001264 }
1265 else
1266 {
1267 /* Check for case-folded in case-folded tree. */
1268 ptr = mip->mi_fword;
1269 flen = mip->mi_fwordlen; /* available case-folded bytes */
1270 byts = slang->sl_fbyts;
1271 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001272
1273 if (mode == FIND_PREFIX)
1274 {
1275 /* Skip over the prefix. */
1276 wlen = mip->mi_prefixlen;
1277 flen -= mip->mi_prefixlen;
1278 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001279 else if (mode == FIND_COMPOUND)
1280 {
1281 /* Skip over the previously found word(s). */
1282 wlen = mip->mi_compoff;
1283 flen -= mip->mi_compoff;
1284 }
1285
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001286 }
1287
Bram Moolenaar51485f02005-06-04 21:55:20 +00001288 if (byts == NULL)
1289 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290
Bram Moolenaar51485f02005-06-04 21:55:20 +00001291 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001292 * Repeat advancing in the tree until:
1293 * - there is a byte that doesn't match,
1294 * - we reach the end of the tree,
1295 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001296 */
1297 for (;;)
1298 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001299 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001300 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001301
1302 len = byts[arridx++];
1303
1304 /* If the first possible byte is a zero the word could end here.
1305 * Remember this index, we first check for the longest word. */
1306 if (byts[arridx] == 0)
1307 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001308 if (endidxcnt == MAXWLEN)
1309 {
1310 /* Must be a corrupted spell file. */
1311 EMSG(_(e_format));
1312 return;
1313 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001314 endlen[endidxcnt] = wlen;
1315 endidx[endidxcnt++] = arridx++;
1316 --len;
1317
1318 /* Skip over the zeros, there can be several flag/region
1319 * combinations. */
1320 while (len > 0 && byts[arridx] == 0)
1321 {
1322 ++arridx;
1323 --len;
1324 }
1325 if (len == 0)
1326 break; /* no children, word must end here */
1327 }
1328
1329 /* Stop looking at end of the line. */
1330 if (ptr[wlen] == NUL)
1331 break;
1332
1333 /* Perform a binary search in the list of accepted bytes. */
1334 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001335 if (c == TAB) /* <Tab> is handled like <Space> */
1336 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001337 lo = arridx;
1338 hi = arridx + len - 1;
1339 while (lo < hi)
1340 {
1341 m = (lo + hi) / 2;
1342 if (byts[m] > c)
1343 hi = m - 1;
1344 else if (byts[m] < c)
1345 lo = m + 1;
1346 else
1347 {
1348 lo = hi = m;
1349 break;
1350 }
1351 }
1352
1353 /* Stop if there is no matching byte. */
1354 if (hi < lo || byts[lo] != c)
1355 break;
1356
1357 /* Continue at the child (if there is one). */
1358 arridx = idxs[lo];
1359 ++wlen;
1360 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001361
1362 /* One space in the good word may stand for several spaces in the
1363 * checked word. */
1364 if (c == ' ')
1365 {
1366 for (;;)
1367 {
1368 if (flen <= 0 && *mip->mi_fend != NUL)
1369 flen = fold_more(mip);
1370 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1371 break;
1372 ++wlen;
1373 --flen;
1374 }
1375 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001376 }
1377
1378 /*
1379 * Verify that one of the possible endings is valid. Try the longest
1380 * first.
1381 */
1382 while (endidxcnt > 0)
1383 {
1384 --endidxcnt;
1385 arridx = endidx[endidxcnt];
1386 wlen = endlen[endidxcnt];
1387
1388#ifdef FEAT_MBYTE
1389 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1390 continue; /* not at first byte of character */
1391#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001392 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001393 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001394 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001395 continue; /* next char is a word character */
1396 word_ends = FALSE;
1397 }
1398 else
1399 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001400 /* The prefix flag is before compound flags. Once a valid prefix flag
1401 * has been found we try compound flags. */
1402 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001403
1404#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001405 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001406 {
1407 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001408 * when folding case. This can be slow, take a shortcut when the
1409 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001411 if (STRNCMP(ptr, p, wlen) != 0)
1412 {
1413 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1414 mb_ptr_adv(p);
1415 wlen = p - mip->mi_word;
1416 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001417 }
1418#endif
1419
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 /* Check flags and region. For FIND_PREFIX check the condition and
1421 * prefix ID.
1422 * Repeat this if there are more flags/region alternatives until there
1423 * is a match. */
1424 res = SP_BAD;
1425 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1426 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001427 {
1428 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001429
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 /* For the fold-case tree check that the case of the checked word
1431 * matches with what the word in the tree requires.
1432 * For keep-case tree the case is always right. For prefixes we
1433 * don't bother to check. */
1434 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001435 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001436 if (mip->mi_cend != mip->mi_word + wlen)
1437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001438 /* mi_capflags was set for a different word length, need
1439 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001441 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001442 }
1443
Bram Moolenaar0c405862005-06-22 22:26:26 +00001444 if (mip->mi_capflags == WF_KEEPCAP
1445 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001446 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001447 }
1448
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001449 /* When mode is FIND_PREFIX the word must support the prefix:
1450 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001451 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001452 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001453 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001454 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001455 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001456 mip->mi_word + mip->mi_cprefixlen, slang,
1457 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001458 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001459 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001460
1461 /* Use the WF_RARE flag for a rare prefix. */
1462 if (c & WF_RAREPFX)
1463 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001464 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001465 }
1466
Bram Moolenaar78622822005-08-23 21:00:13 +00001467 if (slang->sl_nobreak)
1468 {
1469 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1470 && (flags & WF_BANNED) == 0)
1471 {
1472 /* NOBREAK: found a valid following word. That's all we
1473 * need to know, so return. */
1474 mip->mi_result = SP_OK;
1475 break;
1476 }
1477 }
1478
1479 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1480 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001481 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001482 /* If there is no flag or the word is shorter than
1483 * COMPOUNDMIN reject it quickly.
1484 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001485 * that's too short... Myspell compatibility requires this
1486 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001487 if (((unsigned)flags >> 24) == 0
1488 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001490#ifdef FEAT_MBYTE
1491 /* For multi-byte chars check character length against
1492 * COMPOUNDMIN. */
1493 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001494 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001495 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1496 wlen - mip->mi_compoff) < slang->sl_compminlen)
1497 continue;
1498#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001499
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001500 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001501 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001502 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1503 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001504 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001505 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001506
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001507 /* Don't allow compounding on a side where an affix was added,
1508 * unless COMPOUNDPERMITFLAG was used. */
1509 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1510 continue;
1511 if (!word_ends && (flags & WF_NOCOMPAFT))
1512 continue;
1513
Bram Moolenaard12a1322005-08-21 22:08:24 +00001514 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001515 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001516 ? slang->sl_compstartflags
1517 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001518 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001519 continue;
1520
Bram Moolenaare52325c2005-08-22 22:54:29 +00001521 if (mode == FIND_COMPOUND)
1522 {
1523 int capflags;
1524
1525 /* Need to check the caps type of the appended compound
1526 * word. */
1527#ifdef FEAT_MBYTE
1528 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1529 mip->mi_compoff) != 0)
1530 {
1531 /* case folding may have changed the length */
1532 p = mip->mi_word;
1533 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1534 mb_ptr_adv(p);
1535 }
1536 else
1537#endif
1538 p = mip->mi_word + mip->mi_compoff;
1539 capflags = captype(p, mip->mi_word + wlen);
1540 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1541 && (flags & WF_FIXCAP) != 0))
1542 continue;
1543
1544 if (capflags != WF_ALLCAP)
1545 {
1546 /* When the character before the word is a word
1547 * character we do not accept a Onecap word. We do
1548 * accept a no-caps word, even when the dictionary
1549 * word specifies ONECAP. */
1550 mb_ptr_back(mip->mi_word, p);
1551 if (spell_iswordp_nmw(p)
1552 ? capflags == WF_ONECAP
1553 : (flags & WF_ONECAP) != 0
1554 && capflags != WF_ONECAP)
1555 continue;
1556 }
1557 }
1558
Bram Moolenaar5195e452005-08-19 20:32:47 +00001559 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001560 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001561 * the number of syllables must not be too large. */
1562 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1563 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1564 if (word_ends)
1565 {
1566 char_u fword[MAXWLEN];
1567
1568 if (slang->sl_compsylmax < MAXWLEN)
1569 {
1570 /* "fword" is only needed for checking syllables. */
1571 if (ptr == mip->mi_word)
1572 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1573 else
1574 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1575 }
1576 if (!can_compound(slang, fword, mip->mi_compflags))
1577 continue;
1578 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001579 }
1580
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001581 /* Check NEEDCOMPOUND: can't use word without compounding. */
1582 else if (flags & WF_NEEDCOMP)
1583 continue;
1584
Bram Moolenaar78622822005-08-23 21:00:13 +00001585 nobreak_result = SP_OK;
1586
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001587 if (!word_ends)
1588 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 int save_result = mip->mi_result;
1590 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001591 langp_T *save_lp = mip->mi_lp;
1592 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001593
1594 /* Check that a valid word follows. If there is one and we
1595 * are compounding, it will set "mi_result", thus we are
1596 * always finished here. For NOBREAK we only check that a
1597 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001598 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001599 if (slang->sl_nobreak)
1600 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001601
1602 /* Find following word in case-folded tree. */
1603 mip->mi_compoff = endlen[endidxcnt];
1604#ifdef FEAT_MBYTE
1605 if (has_mbyte && mode == FIND_KEEPWORD)
1606 {
1607 /* Compute byte length in case-folded word from "wlen":
1608 * byte length in keep-case word. Length may change when
1609 * folding case. This can be slow, take a shortcut when
1610 * the case-folded word is equal to the keep-case word. */
1611 p = mip->mi_fword;
1612 if (STRNCMP(ptr, p, wlen) != 0)
1613 {
1614 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1615 mb_ptr_adv(p);
1616 mip->mi_compoff = p - mip->mi_fword;
1617 }
1618 }
1619#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001620 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001621 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001622 if (flags & WF_COMPROOT)
1623 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001624
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001625 /* For NOBREAK we need to try all NOBREAK languages, at least
1626 * to find the ".add" file(s). */
1627 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001628 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001629 if (slang->sl_nobreak)
1630 {
1631 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1632 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1633 || !mip->mi_lp->lp_slang->sl_nobreak)
1634 continue;
1635 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001636
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001637 find_word(mip, FIND_COMPOUND);
1638
1639 /* When NOBREAK any word that matches is OK. Otherwise we
1640 * need to find the longest match, thus try with keep-case
1641 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001642 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1643 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001644 /* Find following word in keep-case tree. */
1645 mip->mi_compoff = wlen;
1646 find_word(mip, FIND_KEEPCOMPOUND);
1647
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001648#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1649 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1650 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001651 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1652 {
1653 /* Check for following word with prefix. */
1654 mip->mi_compoff = c;
1655 find_prefix(mip, FIND_COMPOUND);
1656 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001657#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001658 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001659
1660 if (!slang->sl_nobreak)
1661 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001663 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001664 if (flags & WF_COMPROOT)
1665 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001666 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001667
Bram Moolenaar78622822005-08-23 21:00:13 +00001668 if (slang->sl_nobreak)
1669 {
1670 nobreak_result = mip->mi_result;
1671 mip->mi_result = save_result;
1672 mip->mi_end = save_end;
1673 }
1674 else
1675 {
1676 if (mip->mi_result == SP_OK)
1677 break;
1678 continue;
1679 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001680 }
1681
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001682 if (flags & WF_BANNED)
1683 res = SP_BANNED;
1684 else if (flags & WF_REGION)
1685 {
1686 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001687 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001688 res = SP_OK;
1689 else
1690 res = SP_LOCAL;
1691 }
1692 else if (flags & WF_RARE)
1693 res = SP_RARE;
1694 else
1695 res = SP_OK;
1696
Bram Moolenaar78622822005-08-23 21:00:13 +00001697 /* Always use the longest match and the best result. For NOBREAK
1698 * we separately keep the longest match without a following good
1699 * word as a fall-back. */
1700 if (nobreak_result == SP_BAD)
1701 {
1702 if (mip->mi_result2 > res)
1703 {
1704 mip->mi_result2 = res;
1705 mip->mi_end2 = mip->mi_word + wlen;
1706 }
1707 else if (mip->mi_result2 == res
1708 && mip->mi_end2 < mip->mi_word + wlen)
1709 mip->mi_end2 = mip->mi_word + wlen;
1710 }
1711 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001712 {
1713 mip->mi_result = res;
1714 mip->mi_end = mip->mi_word + wlen;
1715 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001716 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001717 mip->mi_end = mip->mi_word + wlen;
1718
Bram Moolenaar78622822005-08-23 21:00:13 +00001719 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001720 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001721 }
1722
Bram Moolenaar78622822005-08-23 21:00:13 +00001723 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001724 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001726}
1727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001728/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001729 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1730 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001731 */
1732 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001733can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001734 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001735 char_u *word;
1736 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001737{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001739#ifdef FEAT_MBYTE
1740 char_u uflags[MAXWLEN * 2];
1741 int i;
1742#endif
1743 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001744
1745 if (slang->sl_compprog == NULL)
1746 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001747#ifdef FEAT_MBYTE
1748 if (enc_utf8)
1749 {
1750 /* Need to convert the single byte flags to utf8 characters. */
1751 p = uflags;
1752 for (i = 0; flags[i] != NUL; ++i)
1753 p += mb_char2bytes(flags[i], p);
1754 *p = NUL;
1755 p = uflags;
1756 }
1757 else
1758#endif
1759 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001760 regmatch.regprog = slang->sl_compprog;
1761 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001762 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001763 return FALSE;
1764
Bram Moolenaare52325c2005-08-22 22:54:29 +00001765 /* Count the number of syllables. This may be slow, do it last. If there
1766 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001767 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001768 if (slang->sl_compsylmax < MAXWLEN
1769 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001770 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001771 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001772}
1773
1774/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001775 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1776 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001777 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001778 */
1779 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001780valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001781 int totprefcnt; /* nr of prefix IDs */
1782 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001783 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001784 char_u *word;
1785 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001786 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001787{
1788 int prefcnt;
1789 int pidx;
1790 regprog_T *rp;
1791 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001792 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001793
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001794 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001795 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1796 {
1797 pidx = slang->sl_pidxs[arridx + prefcnt];
1798
1799 /* Check the prefix ID. */
1800 if (prefid != (pidx & 0xff))
1801 continue;
1802
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001803 /* Check if the prefix doesn't combine and the word already has a
1804 * suffix. */
1805 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1806 continue;
1807
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001808 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001809 * stored in the two bytes above the prefix ID byte. */
1810 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001811 if (rp != NULL)
1812 {
1813 regmatch.regprog = rp;
1814 regmatch.rm_ic = FALSE;
1815 if (!vim_regexec(&regmatch, word, 0))
1816 continue;
1817 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001818 else if (cond_req)
1819 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001820
Bram Moolenaar53805d12005-08-01 07:08:33 +00001821 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001822 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001823 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001824 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001825}
1826
1827/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001828 * Check if the word at "mip->mi_word" has a matching prefix.
1829 * If it does, then check the following word.
1830 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001831 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1832 * prefix in a compound word.
1833 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001834 * For a match mip->mi_result is updated.
1835 */
1836 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001837find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001839 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001840{
1841 idx_T arridx = 0;
1842 int len;
1843 int wlen = 0;
1844 int flen;
1845 int c;
1846 char_u *ptr;
1847 idx_T lo, hi, m;
1848 slang_T *slang = mip->mi_lp->lp_slang;
1849 char_u *byts;
1850 idx_T *idxs;
1851
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001852 byts = slang->sl_pbyts;
1853 if (byts == NULL)
1854 return; /* array is empty */
1855
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001856 /* We use the case-folded word here, since prefixes are always
1857 * case-folded. */
1858 ptr = mip->mi_fword;
1859 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001860 if (mode == FIND_COMPOUND)
1861 {
1862 /* Skip over the previously found word(s). */
1863 ptr += mip->mi_compoff;
1864 flen -= mip->mi_compoff;
1865 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001866 idxs = slang->sl_pidxs;
1867
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001868 /*
1869 * Repeat advancing in the tree until:
1870 * - there is a byte that doesn't match,
1871 * - we reach the end of the tree,
1872 * - or we reach the end of the line.
1873 */
1874 for (;;)
1875 {
1876 if (flen == 0 && *mip->mi_fend != NUL)
1877 flen = fold_more(mip);
1878
1879 len = byts[arridx++];
1880
1881 /* If the first possible byte is a zero the prefix could end here.
1882 * Check if the following word matches and supports the prefix. */
1883 if (byts[arridx] == 0)
1884 {
1885 /* There can be several prefixes with different conditions. We
1886 * try them all, since we don't know which one will give the
1887 * longest match. The word is the same each time, pass the list
1888 * of possible prefixes to find_word(). */
1889 mip->mi_prefarridx = arridx;
1890 mip->mi_prefcnt = len;
1891 while (len > 0 && byts[arridx] == 0)
1892 {
1893 ++arridx;
1894 --len;
1895 }
1896 mip->mi_prefcnt -= len;
1897
1898 /* Find the word that comes after the prefix. */
1899 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001900 if (mode == FIND_COMPOUND)
1901 /* Skip over the previously found word(s). */
1902 mip->mi_prefixlen += mip->mi_compoff;
1903
Bram Moolenaar53805d12005-08-01 07:08:33 +00001904#ifdef FEAT_MBYTE
1905 if (has_mbyte)
1906 {
1907 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001908 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1909 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001910 }
1911 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001912 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001913#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001914 find_word(mip, FIND_PREFIX);
1915
1916
1917 if (len == 0)
1918 break; /* no children, word must end here */
1919 }
1920
1921 /* Stop looking at end of the line. */
1922 if (ptr[wlen] == NUL)
1923 break;
1924
1925 /* Perform a binary search in the list of accepted bytes. */
1926 c = ptr[wlen];
1927 lo = arridx;
1928 hi = arridx + len - 1;
1929 while (lo < hi)
1930 {
1931 m = (lo + hi) / 2;
1932 if (byts[m] > c)
1933 hi = m - 1;
1934 else if (byts[m] < c)
1935 lo = m + 1;
1936 else
1937 {
1938 lo = hi = m;
1939 break;
1940 }
1941 }
1942
1943 /* Stop if there is no matching byte. */
1944 if (hi < lo || byts[lo] != c)
1945 break;
1946
1947 /* Continue at the child (if there is one). */
1948 arridx = idxs[lo];
1949 ++wlen;
1950 --flen;
1951 }
1952}
1953
1954/*
1955 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001956 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001957 * Return the length of the folded chars in bytes.
1958 */
1959 static int
1960fold_more(mip)
1961 matchinf_T *mip;
1962{
1963 int flen;
1964 char_u *p;
1965
1966 p = mip->mi_fend;
1967 do
1968 {
1969 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001970 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001972 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001973 if (*mip->mi_fend != NUL)
1974 mb_ptr_adv(mip->mi_fend);
1975
1976 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1977 mip->mi_fword + mip->mi_fwordlen,
1978 MAXWLEN - mip->mi_fwordlen);
1979 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1980 mip->mi_fwordlen += flen;
1981 return flen;
1982}
1983
1984/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001985 * Check case flags for a word. Return TRUE if the word has the requested
1986 * case.
1987 */
1988 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001989spell_valid_case(wordflags, treeflags)
1990 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001991 int treeflags; /* flags for the word in the spell tree */
1992{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001993 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001994 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001995 && ((treeflags & WF_ONECAP) == 0
1996 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001997}
1998
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001999/*
2000 * Return TRUE if spell checking is not enabled.
2001 */
2002 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002003no_spell_checking(wp)
2004 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002005{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002006 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2007 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002008 {
2009 EMSG(_("E756: Spell checking is not enabled"));
2010 return TRUE;
2011 }
2012 return FALSE;
2013}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002014
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002015/*
2016 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002017 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2018 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002019 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2020 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002021 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002022 */
2023 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002024spell_move_to(wp, dir, allwords, curline, attrp)
2025 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002026 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002027 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002028 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002029 hlf_T *attrp; /* return: attributes of bad word or NULL
2030 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002031{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002032 linenr_T lnum;
2033 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002034 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035 char_u *line;
2036 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002037 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002038 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002040# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002041 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002042 int col;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002043# endif
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002044 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002045 char_u *buf = NULL;
2046 int buflen = 0;
2047 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002048 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002049 int found_one = FALSE;
2050 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002051
Bram Moolenaar95529562005-08-25 21:21:38 +00002052 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002053 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002054
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002055 /*
2056 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002057 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002058 *
2059 * When searching backwards, we continue in the line to find the last
2060 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002061 *
2062 * We concatenate the start of the next line, so that wrapped words work
2063 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2064 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002065 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002066 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002067 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002068
2069 while (!got_int)
2070 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002071 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002072
Bram Moolenaar0c405862005-06-22 22:26:26 +00002073 len = STRLEN(line);
2074 if (buflen < len + MAXWLEN + 2)
2075 {
2076 vim_free(buf);
2077 buflen = len + MAXWLEN + 2;
2078 buf = alloc(buflen);
2079 if (buf == NULL)
2080 break;
2081 }
2082
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002083 /* In first line check first word for Capital. */
2084 if (lnum == 1)
2085 capcol = 0;
2086
2087 /* For checking first word with a capital skip white space. */
2088 if (capcol == 0)
2089 capcol = skipwhite(line) - line;
2090
Bram Moolenaar0c405862005-06-22 22:26:26 +00002091 /* Copy the line into "buf" and append the start of the next line if
2092 * possible. */
2093 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002094 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002095 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2096
2097 p = buf + skip;
2098 endp = buf + len;
2099 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002100 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002101 /* When searching backward don't search after the cursor. Unless
2102 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002103 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002104 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002105 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002106 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002107 break;
2108
2109 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002110 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002111 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002112
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002113 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002114 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002115 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002116 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002117 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002118 found_one = TRUE;
2119
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 /* When searching forward only accept a bad word after
2121 * the cursor. */
2122 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002123 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002124 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002125 && (wrapped
2126 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002127 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002128 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002130# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002132 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002133 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002134 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002135 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002136 }
2137 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002138#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002139 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002140
Bram Moolenaar51485f02005-06-04 21:55:20 +00002141 if (can_spell)
2142 {
2143 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002144 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002145#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002146 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002147#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 if (dir == FORWARD)
2149 {
2150 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002151 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002152 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002153 if (attrp != NULL)
2154 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002155 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002157 else if (curline)
2158 /* Insert mode completion: put cursor after
2159 * the bad word. */
2160 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002161 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002162 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 }
2166
Bram Moolenaar51485f02005-06-04 21:55:20 +00002167 /* advance to character after the word */
2168 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002169 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002170 }
2171
Bram Moolenaar5195e452005-08-19 20:32:47 +00002172 if (dir == BACKWARD && found_pos.lnum != 0)
2173 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002174 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002175 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002176 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002177 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002178 }
2179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002181 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002184 if (dir == BACKWARD)
2185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002186 /* If we are back at the starting line and searched it again there
2187 * is no match, give up. */
2188 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002189 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002190
2191 if (lnum > 1)
2192 --lnum;
2193 else if (!p_ws)
2194 break; /* at first line and 'nowrapscan' */
2195 else
2196 {
2197 /* Wrap around to the end of the buffer. May search the
2198 * starting line again and accept the last match. */
2199 lnum = wp->w_buffer->b_ml.ml_line_count;
2200 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002201 if (!shortmess(SHM_SEARCH))
2202 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002203 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002204 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002205 }
2206 else
2207 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002208 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2209 ++lnum;
2210 else if (!p_ws)
2211 break; /* at first line and 'nowrapscan' */
2212 else
2213 {
2214 /* Wrap around to the start of the buffer. May search the
2215 * starting line again and accept the first match. */
2216 lnum = 1;
2217 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002218 if (!shortmess(SHM_SEARCH))
2219 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002220 }
2221
2222 /* If we are back at the starting line and there is no match then
2223 * give up. */
2224 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002225 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002226
2227 /* Skip the characters at the start of the next line that were
2228 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002229 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002230 skip = p - endp;
2231 else
2232 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002233
2234 /* Capscol skips over the inserted space. */
2235 --capcol;
2236
2237 /* But after empty line check first word in next line */
2238 if (*skipwhite(line) == NUL)
2239 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002240 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002241
2242 line_breakcheck();
2243 }
2244
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002246 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002247}
2248
2249/*
2250 * For spell checking: concatenate the start of the following line "line" into
2251 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2252 */
2253 void
2254spell_cat_line(buf, line, maxlen)
2255 char_u *buf;
2256 char_u *line;
2257 int maxlen;
2258{
2259 char_u *p;
2260 int n;
2261
2262 p = skipwhite(line);
2263 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2264 p = skipwhite(p + 1);
2265
2266 if (*p != NUL)
2267 {
2268 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002269 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002270 n = p - line;
2271 if (n >= maxlen)
2272 n = maxlen - 1;
2273 vim_memset(buf + 1, ' ', n);
2274 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275}
2276
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002277/*
2278 * Structure used for the cookie argument of do_in_runtimepath().
2279 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002280typedef struct spelload_S
2281{
2282 char_u sl_lang[MAXWLEN + 1]; /* language name */
2283 slang_T *sl_slang; /* resulting slang_T struct */
2284 int sl_nobreak; /* NOBREAK language found */
2285} spelload_T;
2286
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002288 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002289 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002290 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002291 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002292spell_load_lang(lang)
2293 char_u *lang;
2294{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002295 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002296 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002297 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002298#ifdef FEAT_AUTOCMD
2299 int round;
2300#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002301
Bram Moolenaarb765d632005-06-07 21:00:02 +00002302 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002303 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002304 STRCPY(sl.sl_lang, lang);
2305 sl.sl_slang = NULL;
2306 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002307
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002308#ifdef FEAT_AUTOCMD
2309 /* We may retry when no spell file is found for the language, an
2310 * autocommand may load it then. */
2311 for (round = 1; round <= 2; ++round)
2312#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002313 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002314 /*
2315 * Find the first spell file for "lang" in 'runtimepath' and load it.
2316 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002317 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002318 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002319 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002320
2321 if (r == FAIL && *sl.sl_lang != NUL)
2322 {
2323 /* Try loading the ASCII version. */
2324 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2325 "spell/%s.ascii.spl", lang);
2326 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2327
2328#ifdef FEAT_AUTOCMD
2329 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2330 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2331 curbuf->b_fname, FALSE, curbuf))
2332 continue;
2333 break;
2334#endif
2335 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002336#ifdef FEAT_AUTOCMD
2337 break;
2338#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002339 }
2340
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002341 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002342 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002343 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2344 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002345 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002346 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002347 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002348 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002349 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002350 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002351 }
2352}
2353
2354/*
2355 * Return the encoding used for spell checking: Use 'encoding', except that we
2356 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2357 */
2358 static char_u *
2359spell_enc()
2360{
2361
2362#ifdef FEAT_MBYTE
2363 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2364 return p_enc;
2365#endif
2366 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002367}
2368
2369/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002370 * Get the name of the .spl file for the internal wordlist into
2371 * "fname[MAXPATHL]".
2372 */
2373 static void
2374int_wordlist_spl(fname)
2375 char_u *fname;
2376{
2377 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2378 int_wordlist, spell_enc());
2379}
2380
2381/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002382 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383 * Caller must fill "sl_next".
2384 */
2385 static slang_T *
2386slang_alloc(lang)
2387 char_u *lang;
2388{
2389 slang_T *lp;
2390
Bram Moolenaar51485f02005-06-04 21:55:20 +00002391 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002392 if (lp != NULL)
2393 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002394 if (lang != NULL)
2395 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002397 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002398 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002399 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002400 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002401 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002402
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403 return lp;
2404}
2405
2406/*
2407 * Free the contents of an slang_T and the structure itself.
2408 */
2409 static void
2410slang_free(lp)
2411 slang_T *lp;
2412{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002413 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002414 vim_free(lp->sl_fname);
2415 slang_clear(lp);
2416 vim_free(lp);
2417}
2418
2419/*
2420 * Clear an slang_T so that the file can be reloaded.
2421 */
2422 static void
2423slang_clear(lp)
2424 slang_T *lp;
2425{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002426 garray_T *gap;
2427 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002428 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002429 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002430 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431
Bram Moolenaar51485f02005-06-04 21:55:20 +00002432 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002433 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002434 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002435 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002436 vim_free(lp->sl_pbyts);
2437 lp->sl_pbyts = NULL;
2438
Bram Moolenaar51485f02005-06-04 21:55:20 +00002439 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002440 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002441 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002442 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002443 vim_free(lp->sl_pidxs);
2444 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445
Bram Moolenaar4770d092006-01-12 23:22:24 +00002446 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002448 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2449 while (gap->ga_len > 0)
2450 {
2451 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2452 vim_free(ftp->ft_from);
2453 vim_free(ftp->ft_to);
2454 }
2455 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002457
2458 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002459 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002460 {
2461 /* "ga_len" is set to 1 without adding an item for latin1 */
2462 if (gap->ga_data != NULL)
2463 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2464 for (i = 0; i < gap->ga_len; ++i)
2465 vim_free(((int **)gap->ga_data)[i]);
2466 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002467 else
2468 /* SAL items: free salitem_T items */
2469 while (gap->ga_len > 0)
2470 {
2471 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2472 vim_free(smp->sm_lead);
2473 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2474 vim_free(smp->sm_to);
2475#ifdef FEAT_MBYTE
2476 vim_free(smp->sm_lead_w);
2477 vim_free(smp->sm_oneof_w);
2478 vim_free(smp->sm_to_w);
2479#endif
2480 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002481 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002483 for (i = 0; i < lp->sl_prefixcnt; ++i)
2484 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002485 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002486 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002487 lp->sl_prefprog = NULL;
2488
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002489 vim_free(lp->sl_info);
2490 lp->sl_info = NULL;
2491
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002492 vim_free(lp->sl_midword);
2493 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002494
Bram Moolenaar5195e452005-08-19 20:32:47 +00002495 vim_free(lp->sl_compprog);
2496 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002497 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002498 lp->sl_compprog = NULL;
2499 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002500 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002501
2502 vim_free(lp->sl_syllable);
2503 lp->sl_syllable = NULL;
2504 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002505
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002506 ga_clear_strings(&lp->sl_comppat);
2507
Bram Moolenaar4770d092006-01-12 23:22:24 +00002508 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2509 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002510
Bram Moolenaar4770d092006-01-12 23:22:24 +00002511#ifdef FEAT_MBYTE
2512 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002513#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002514
Bram Moolenaar4770d092006-01-12 23:22:24 +00002515 /* Clear info from .sug file. */
2516 slang_clear_sug(lp);
2517
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002519 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002520 lp->sl_compsylmax = MAXWLEN;
2521 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002522}
2523
2524/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002525 * Clear the info from the .sug file in "lp".
2526 */
2527 static void
2528slang_clear_sug(lp)
2529 slang_T *lp;
2530{
2531 vim_free(lp->sl_sbyts);
2532 lp->sl_sbyts = NULL;
2533 vim_free(lp->sl_sidxs);
2534 lp->sl_sidxs = NULL;
2535 close_spellbuf(lp->sl_sugbuf);
2536 lp->sl_sugbuf = NULL;
2537 lp->sl_sugloaded = FALSE;
2538 lp->sl_sugtime = 0;
2539}
2540
2541/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002542 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 * Invoked through do_in_runtimepath().
2544 */
2545 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002546spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002548 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002549{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002550 spelload_T *slp = (spelload_T *)cookie;
2551 slang_T *slang;
2552
2553 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2554 if (slang != NULL)
2555 {
2556 /* When a previously loaded file has NOBREAK also use it for the
2557 * ".add" files. */
2558 if (slp->sl_nobreak && slang->sl_add)
2559 slang->sl_nobreak = TRUE;
2560 else if (slang->sl_nobreak)
2561 slp->sl_nobreak = TRUE;
2562
2563 slp->sl_slang = slang;
2564 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002565}
2566
2567/*
2568 * Load one spell file and store the info into a slang_T.
2569 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002570 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002571 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2572 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2573 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2574 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002575 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002576 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2577 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002578 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002579 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002580 static slang_T *
2581spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002582 char_u *fname;
2583 char_u *lang;
2584 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002586{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002587 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002588 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002589 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002591 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002592 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002593 char_u *save_sourcing_name = sourcing_name;
2594 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002595 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002596 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002597 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598
Bram Moolenaarb765d632005-06-07 21:00:02 +00002599 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600 if (fd == NULL)
2601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002602 if (!silent)
2603 EMSG2(_(e_notopen), fname);
2604 else if (p_verbose > 2)
2605 {
2606 verbose_enter();
2607 smsg((char_u *)e_notopen, fname);
2608 verbose_leave();
2609 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002610 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 if (p_verbose > 2)
2613 {
2614 verbose_enter();
2615 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2616 verbose_leave();
2617 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002618
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 if (old_lp == NULL)
2620 {
2621 lp = slang_alloc(lang);
2622 if (lp == NULL)
2623 goto endFAIL;
2624
2625 /* Remember the file name, used to reload the file when it's updated. */
2626 lp->sl_fname = vim_strsave(fname);
2627 if (lp->sl_fname == NULL)
2628 goto endFAIL;
2629
2630 /* Check for .add.spl. */
2631 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2632 }
2633 else
2634 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002635
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002636 /* Set sourcing_name, so that error messages mention the file name. */
2637 sourcing_name = fname;
2638 sourcing_lnum = 0;
2639
Bram Moolenaar4770d092006-01-12 23:22:24 +00002640 /*
2641 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002642 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002643 for (i = 0; i < VIMSPELLMAGICL; ++i)
2644 buf[i] = getc(fd); /* <fileID> */
2645 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2646 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002647 EMSG(_("E757: This does not look like a spell file"));
2648 goto endFAIL;
2649 }
2650 c = getc(fd); /* <versionnr> */
2651 if (c < VIMSPELLVERSION)
2652 {
2653 EMSG(_("E771: Old spell file, needs to be updated"));
2654 goto endFAIL;
2655 }
2656 else if (c > VIMSPELLVERSION)
2657 {
2658 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002659 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002660 }
2661
Bram Moolenaar5195e452005-08-19 20:32:47 +00002662
2663 /*
2664 * <SECTIONS>: <section> ... <sectionend>
2665 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2666 */
2667 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002668 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002669 n = getc(fd); /* <sectionID> or <sectionend> */
2670 if (n == SN_END)
2671 break;
2672 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002673 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002674 if (len < 0)
2675 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002676
Bram Moolenaar5195e452005-08-19 20:32:47 +00002677 res = 0;
2678 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002679 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002680 case SN_INFO:
2681 lp->sl_info = read_string(fd, len); /* <infotext> */
2682 if (lp->sl_info == NULL)
2683 goto endFAIL;
2684 break;
2685
Bram Moolenaar5195e452005-08-19 20:32:47 +00002686 case SN_REGION:
2687 res = read_region_section(fd, lp, len);
2688 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002689
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690 case SN_CHARFLAGS:
2691 res = read_charflags_section(fd);
2692 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002693
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 case SN_MIDWORD:
2695 lp->sl_midword = read_string(fd, len); /* <midword> */
2696 if (lp->sl_midword == NULL)
2697 goto endFAIL;
2698 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002699
Bram Moolenaar5195e452005-08-19 20:32:47 +00002700 case SN_PREFCOND:
2701 res = read_prefcond_section(fd, lp);
2702 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002703
Bram Moolenaar5195e452005-08-19 20:32:47 +00002704 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002705 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2706 break;
2707
2708 case SN_REPSAL:
2709 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002710 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002711
Bram Moolenaar5195e452005-08-19 20:32:47 +00002712 case SN_SAL:
2713 res = read_sal_section(fd, lp);
2714 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002715
Bram Moolenaar5195e452005-08-19 20:32:47 +00002716 case SN_SOFO:
2717 res = read_sofo_section(fd, lp);
2718 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002719
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 case SN_MAP:
2721 p = read_string(fd, len); /* <mapstr> */
2722 if (p == NULL)
2723 goto endFAIL;
2724 set_map_str(lp, p);
2725 vim_free(p);
2726 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002727
Bram Moolenaar4770d092006-01-12 23:22:24 +00002728 case SN_WORDS:
2729 res = read_words_section(fd, lp, len);
2730 break;
2731
2732 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002733 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002734 break;
2735
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002736 case SN_NOSPLITSUGS:
2737 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2738 break;
2739
Bram Moolenaar5195e452005-08-19 20:32:47 +00002740 case SN_COMPOUND:
2741 res = read_compound(fd, lp, len);
2742 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002743
Bram Moolenaar78622822005-08-23 21:00:13 +00002744 case SN_NOBREAK:
2745 lp->sl_nobreak = TRUE;
2746 break;
2747
Bram Moolenaar5195e452005-08-19 20:32:47 +00002748 case SN_SYLLABLE:
2749 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2750 if (lp->sl_syllable == NULL)
2751 goto endFAIL;
2752 if (init_syl_tab(lp) == FAIL)
2753 goto endFAIL;
2754 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002755
Bram Moolenaar5195e452005-08-19 20:32:47 +00002756 default:
2757 /* Unsupported section. When it's required give an error
2758 * message. When it's not required skip the contents. */
2759 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002760 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002761 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002762 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002763 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002764 while (--len >= 0)
2765 if (getc(fd) < 0)
2766 goto truncerr;
2767 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002768 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002769someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002770 if (res == SP_FORMERROR)
2771 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002772 EMSG(_(e_format));
2773 goto endFAIL;
2774 }
2775 if (res == SP_TRUNCERROR)
2776 {
2777truncerr:
2778 EMSG(_(e_spell_trunc));
2779 goto endFAIL;
2780 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002781 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002782 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002784
Bram Moolenaar4770d092006-01-12 23:22:24 +00002785 /* <LWORDTREE> */
2786 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2787 if (res != 0)
2788 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002789
Bram Moolenaar4770d092006-01-12 23:22:24 +00002790 /* <KWORDTREE> */
2791 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2792 if (res != 0)
2793 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002794
Bram Moolenaar4770d092006-01-12 23:22:24 +00002795 /* <PREFIXTREE> */
2796 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2797 lp->sl_prefixcnt);
2798 if (res != 0)
2799 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002800
Bram Moolenaarb765d632005-06-07 21:00:02 +00002801 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002802 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002803 {
2804 lp->sl_next = first_lang;
2805 first_lang = lp;
2806 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002807
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002808 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002809
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002810endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002811 if (lang != NULL)
2812 /* truncating the name signals the error to spell_load_lang() */
2813 *lang = NUL;
2814 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002815 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002816 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002817
2818endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002819 if (fd != NULL)
2820 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002821 sourcing_name = save_sourcing_name;
2822 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002823
2824 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002825}
2826
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002827/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002828 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2829 */
2830 static int
2831get2c(fd)
2832 FILE *fd;
2833{
2834 long n;
2835
2836 n = getc(fd);
2837 n = (n << 8) + getc(fd);
2838 return n;
2839}
2840
2841/*
2842 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2843 */
2844 static int
2845get3c(fd)
2846 FILE *fd;
2847{
2848 long n;
2849
2850 n = getc(fd);
2851 n = (n << 8) + getc(fd);
2852 n = (n << 8) + getc(fd);
2853 return n;
2854}
2855
2856/*
2857 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2858 */
2859 static int
2860get4c(fd)
2861 FILE *fd;
2862{
2863 long n;
2864
2865 n = getc(fd);
2866 n = (n << 8) + getc(fd);
2867 n = (n << 8) + getc(fd);
2868 n = (n << 8) + getc(fd);
2869 return n;
2870}
2871
2872/*
2873 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2874 */
2875 static time_t
2876get8c(fd)
2877 FILE *fd;
2878{
2879 time_t n = 0;
2880 int i;
2881
2882 for (i = 0; i < 8; ++i)
2883 n = (n << 8) + getc(fd);
2884 return n;
2885}
2886
2887/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002888 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002889 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002890 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002891 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2892 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002893 */
2894 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002895read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002896 FILE *fd;
2897 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002898 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002899{
2900 int cnt = 0;
2901 int i;
2902 char_u *str;
2903
2904 /* read the length bytes, MSB first */
2905 for (i = 0; i < cnt_bytes; ++i)
2906 cnt = (cnt << 8) + getc(fd);
2907 if (cnt < 0)
2908 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002909 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002910 return NULL;
2911 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002912 *cntp = cnt;
2913 if (cnt == 0)
2914 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002915
Bram Moolenaar5195e452005-08-19 20:32:47 +00002916 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002917 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002918 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002919 return str;
2920}
2921
Bram Moolenaar7887d882005-07-01 22:33:52 +00002922/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002923 * Read a string of length "cnt" from "fd" into allocated memory.
2924 * Returns NULL when out of memory.
2925 */
2926 static char_u *
2927read_string(fd, cnt)
2928 FILE *fd;
2929 int cnt;
2930{
2931 char_u *str;
2932 int i;
2933
2934 /* allocate memory */
2935 str = alloc((unsigned)cnt + 1);
2936 if (str != NULL)
2937 {
2938 /* Read the string. Doesn't check for truncated file. */
2939 for (i = 0; i < cnt; ++i)
2940 str[i] = getc(fd);
2941 str[i] = NUL;
2942 }
2943 return str;
2944}
2945
2946/*
2947 * Read SN_REGION: <regionname> ...
2948 * Return SP_*ERROR flags.
2949 */
2950 static int
2951read_region_section(fd, lp, len)
2952 FILE *fd;
2953 slang_T *lp;
2954 int len;
2955{
2956 int i;
2957
2958 if (len > 16)
2959 return SP_FORMERROR;
2960 for (i = 0; i < len; ++i)
2961 lp->sl_regions[i] = getc(fd); /* <regionname> */
2962 lp->sl_regions[len] = NUL;
2963 return 0;
2964}
2965
2966/*
2967 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2968 * <folcharslen> <folchars>
2969 * Return SP_*ERROR flags.
2970 */
2971 static int
2972read_charflags_section(fd)
2973 FILE *fd;
2974{
2975 char_u *flags;
2976 char_u *fol;
2977 int flagslen, follen;
2978
2979 /* <charflagslen> <charflags> */
2980 flags = read_cnt_string(fd, 1, &flagslen);
2981 if (flagslen < 0)
2982 return flagslen;
2983
2984 /* <folcharslen> <folchars> */
2985 fol = read_cnt_string(fd, 2, &follen);
2986 if (follen < 0)
2987 {
2988 vim_free(flags);
2989 return follen;
2990 }
2991
2992 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2993 if (flags != NULL && fol != NULL)
2994 set_spell_charflags(flags, flagslen, fol);
2995
2996 vim_free(flags);
2997 vim_free(fol);
2998
2999 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3000 if ((flags == NULL) != (fol == NULL))
3001 return SP_FORMERROR;
3002 return 0;
3003}
3004
3005/*
3006 * Read SN_PREFCOND section.
3007 * Return SP_*ERROR flags.
3008 */
3009 static int
3010read_prefcond_section(fd, lp)
3011 FILE *fd;
3012 slang_T *lp;
3013{
3014 int cnt;
3015 int i;
3016 int n;
3017 char_u *p;
3018 char_u buf[MAXWLEN + 1];
3019
3020 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003021 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003022 if (cnt <= 0)
3023 return SP_FORMERROR;
3024
3025 lp->sl_prefprog = (regprog_T **)alloc_clear(
3026 (unsigned)sizeof(regprog_T *) * cnt);
3027 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003028 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003029 lp->sl_prefixcnt = cnt;
3030
3031 for (i = 0; i < cnt; ++i)
3032 {
3033 /* <prefcond> : <condlen> <condstr> */
3034 n = getc(fd); /* <condlen> */
3035 if (n < 0 || n >= MAXWLEN)
3036 return SP_FORMERROR;
3037
3038 /* When <condlen> is zero we have an empty condition. Otherwise
3039 * compile the regexp program used to check for the condition. */
3040 if (n > 0)
3041 {
3042 buf[0] = '^'; /* always match at one position only */
3043 p = buf + 1;
3044 while (n-- > 0)
3045 *p++ = getc(fd); /* <condstr> */
3046 *p = NUL;
3047 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3048 }
3049 }
3050 return 0;
3051}
3052
3053/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003054 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003055 * Return SP_*ERROR flags.
3056 */
3057 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003058read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003059 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003060 garray_T *gap;
3061 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003062{
3063 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003064 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003065 int i;
3066
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003067 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003068 if (cnt < 0)
3069 return SP_TRUNCERROR;
3070
Bram Moolenaar5195e452005-08-19 20:32:47 +00003071 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003072 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003073
3074 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3075 for (; gap->ga_len < cnt; ++gap->ga_len)
3076 {
3077 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3078 ftp->ft_from = read_cnt_string(fd, 1, &i);
3079 if (i < 0)
3080 return i;
3081 if (i == 0)
3082 return SP_FORMERROR;
3083 ftp->ft_to = read_cnt_string(fd, 1, &i);
3084 if (i <= 0)
3085 {
3086 vim_free(ftp->ft_from);
3087 if (i < 0)
3088 return i;
3089 return SP_FORMERROR;
3090 }
3091 }
3092
3093 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003094 for (i = 0; i < 256; ++i)
3095 first[i] = -1;
3096 for (i = 0; i < gap->ga_len; ++i)
3097 {
3098 ftp = &((fromto_T *)gap->ga_data)[i];
3099 if (first[*ftp->ft_from] == -1)
3100 first[*ftp->ft_from] = i;
3101 }
3102 return 0;
3103}
3104
3105/*
3106 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3107 * Return SP_*ERROR flags.
3108 */
3109 static int
3110read_sal_section(fd, slang)
3111 FILE *fd;
3112 slang_T *slang;
3113{
3114 int i;
3115 int cnt;
3116 garray_T *gap;
3117 salitem_T *smp;
3118 int ccnt;
3119 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003120 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003121
3122 slang->sl_sofo = FALSE;
3123
3124 i = getc(fd); /* <salflags> */
3125 if (i & SAL_F0LLOWUP)
3126 slang->sl_followup = TRUE;
3127 if (i & SAL_COLLAPSE)
3128 slang->sl_collapse = TRUE;
3129 if (i & SAL_REM_ACCENTS)
3130 slang->sl_rem_accents = TRUE;
3131
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003132 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003133 if (cnt < 0)
3134 return SP_TRUNCERROR;
3135
3136 gap = &slang->sl_sal;
3137 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003138 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003139 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003140
3141 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3142 for (; gap->ga_len < cnt; ++gap->ga_len)
3143 {
3144 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3145 ccnt = getc(fd); /* <salfromlen> */
3146 if (ccnt < 0)
3147 return SP_TRUNCERROR;
3148 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003149 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150 smp->sm_lead = p;
3151
3152 /* Read up to the first special char into sm_lead. */
3153 for (i = 0; i < ccnt; ++i)
3154 {
3155 c = getc(fd); /* <salfrom> */
3156 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3157 break;
3158 *p++ = c;
3159 }
3160 smp->sm_leadlen = p - smp->sm_lead;
3161 *p++ = NUL;
3162
3163 /* Put (abc) chars in sm_oneof, if any. */
3164 if (c == '(')
3165 {
3166 smp->sm_oneof = p;
3167 for (++i; i < ccnt; ++i)
3168 {
3169 c = getc(fd); /* <salfrom> */
3170 if (c == ')')
3171 break;
3172 *p++ = c;
3173 }
3174 *p++ = NUL;
3175 if (++i < ccnt)
3176 c = getc(fd);
3177 }
3178 else
3179 smp->sm_oneof = NULL;
3180
3181 /* Any following chars go in sm_rules. */
3182 smp->sm_rules = p;
3183 if (i < ccnt)
3184 /* store the char we got while checking for end of sm_lead */
3185 *p++ = c;
3186 for (++i; i < ccnt; ++i)
3187 *p++ = getc(fd); /* <salfrom> */
3188 *p++ = NUL;
3189
3190 /* <saltolen> <salto> */
3191 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3192 if (ccnt < 0)
3193 {
3194 vim_free(smp->sm_lead);
3195 return ccnt;
3196 }
3197
3198#ifdef FEAT_MBYTE
3199 if (has_mbyte)
3200 {
3201 /* convert the multi-byte strings to wide char strings */
3202 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3203 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3204 if (smp->sm_oneof == NULL)
3205 smp->sm_oneof_w = NULL;
3206 else
3207 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3208 if (smp->sm_to == NULL)
3209 smp->sm_to_w = NULL;
3210 else
3211 smp->sm_to_w = mb_str2wide(smp->sm_to);
3212 if (smp->sm_lead_w == NULL
3213 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3214 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3215 {
3216 vim_free(smp->sm_lead);
3217 vim_free(smp->sm_to);
3218 vim_free(smp->sm_lead_w);
3219 vim_free(smp->sm_oneof_w);
3220 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003221 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003222 }
3223 }
3224#endif
3225 }
3226
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003227 if (gap->ga_len > 0)
3228 {
3229 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3230 * that we need to check the index every time. */
3231 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3232 if ((p = alloc(1)) == NULL)
3233 return SP_OTHERERROR;
3234 p[0] = NUL;
3235 smp->sm_lead = p;
3236 smp->sm_leadlen = 0;
3237 smp->sm_oneof = NULL;
3238 smp->sm_rules = p;
3239 smp->sm_to = NULL;
3240#ifdef FEAT_MBYTE
3241 if (has_mbyte)
3242 {
3243 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3244 smp->sm_leadlen = 0;
3245 smp->sm_oneof_w = NULL;
3246 smp->sm_to_w = NULL;
3247 }
3248#endif
3249 ++gap->ga_len;
3250 }
3251
Bram Moolenaar5195e452005-08-19 20:32:47 +00003252 /* Fill the first-index table. */
3253 set_sal_first(slang);
3254
3255 return 0;
3256}
3257
3258/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003259 * Read SN_WORDS: <word> ...
3260 * Return SP_*ERROR flags.
3261 */
3262 static int
3263read_words_section(fd, lp, len)
3264 FILE *fd;
3265 slang_T *lp;
3266 int len;
3267{
3268 int done = 0;
3269 int i;
3270 char_u word[MAXWLEN];
3271
3272 while (done < len)
3273 {
3274 /* Read one word at a time. */
3275 for (i = 0; ; ++i)
3276 {
3277 word[i] = getc(fd);
3278 if (word[i] == NUL)
3279 break;
3280 if (i == MAXWLEN - 1)
3281 return SP_FORMERROR;
3282 }
3283
3284 /* Init the count to 10. */
3285 count_common_word(lp, word, -1, 10);
3286 done += i + 1;
3287 }
3288 return 0;
3289}
3290
3291/*
3292 * Add a word to the hashtable of common words.
3293 * If it's already there then the counter is increased.
3294 */
3295 static void
3296count_common_word(lp, word, len, count)
3297 slang_T *lp;
3298 char_u *word;
3299 int len; /* word length, -1 for upto NUL */
3300 int count; /* 1 to count once, 10 to init */
3301{
3302 hash_T hash;
3303 hashitem_T *hi;
3304 wordcount_T *wc;
3305 char_u buf[MAXWLEN];
3306 char_u *p;
3307
3308 if (len == -1)
3309 p = word;
3310 else
3311 {
3312 vim_strncpy(buf, word, len);
3313 p = buf;
3314 }
3315
3316 hash = hash_hash(p);
3317 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3318 if (HASHITEM_EMPTY(hi))
3319 {
3320 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3321 if (wc == NULL)
3322 return;
3323 STRCPY(wc->wc_word, p);
3324 wc->wc_count = count;
3325 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3326 }
3327 else
3328 {
3329 wc = HI2WC(hi);
3330 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3331 wc->wc_count = MAXWORDCOUNT;
3332 }
3333}
3334
3335/*
3336 * Adjust the score of common words.
3337 */
3338 static int
3339score_wordcount_adj(slang, score, word, split)
3340 slang_T *slang;
3341 int score;
3342 char_u *word;
3343 int split; /* word was split, less bonus */
3344{
3345 hashitem_T *hi;
3346 wordcount_T *wc;
3347 int bonus;
3348 int newscore;
3349
3350 hi = hash_find(&slang->sl_wordcount, word);
3351 if (!HASHITEM_EMPTY(hi))
3352 {
3353 wc = HI2WC(hi);
3354 if (wc->wc_count < SCORE_THRES2)
3355 bonus = SCORE_COMMON1;
3356 else if (wc->wc_count < SCORE_THRES3)
3357 bonus = SCORE_COMMON2;
3358 else
3359 bonus = SCORE_COMMON3;
3360 if (split)
3361 newscore = score - bonus / 2;
3362 else
3363 newscore = score - bonus;
3364 if (newscore < 0)
3365 return 0;
3366 return newscore;
3367 }
3368 return score;
3369}
3370
3371/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003372 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3373 * Return SP_*ERROR flags.
3374 */
3375 static int
3376read_sofo_section(fd, slang)
3377 FILE *fd;
3378 slang_T *slang;
3379{
3380 int cnt;
3381 char_u *from, *to;
3382 int res;
3383
3384 slang->sl_sofo = TRUE;
3385
3386 /* <sofofromlen> <sofofrom> */
3387 from = read_cnt_string(fd, 2, &cnt);
3388 if (cnt < 0)
3389 return cnt;
3390
3391 /* <sofotolen> <sofoto> */
3392 to = read_cnt_string(fd, 2, &cnt);
3393 if (cnt < 0)
3394 {
3395 vim_free(from);
3396 return cnt;
3397 }
3398
3399 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3400 if (from != NULL && to != NULL)
3401 res = set_sofo(slang, from, to);
3402 else if (from != NULL || to != NULL)
3403 res = SP_FORMERROR; /* only one of two strings is an error */
3404 else
3405 res = 0;
3406
3407 vim_free(from);
3408 vim_free(to);
3409 return res;
3410}
3411
3412/*
3413 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003414 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003415 * Returns SP_*ERROR flags.
3416 */
3417 static int
3418read_compound(fd, slang, len)
3419 FILE *fd;
3420 slang_T *slang;
3421 int len;
3422{
3423 int todo = len;
3424 int c;
3425 int atstart;
3426 char_u *pat;
3427 char_u *pp;
3428 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003429 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003430 int cnt;
3431 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003432
3433 if (todo < 2)
3434 return SP_FORMERROR; /* need at least two bytes */
3435
3436 --todo;
3437 c = getc(fd); /* <compmax> */
3438 if (c < 2)
3439 c = MAXWLEN;
3440 slang->sl_compmax = c;
3441
3442 --todo;
3443 c = getc(fd); /* <compminlen> */
3444 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003445 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003446 slang->sl_compminlen = c;
3447
3448 --todo;
3449 c = getc(fd); /* <compsylmax> */
3450 if (c < 1)
3451 c = MAXWLEN;
3452 slang->sl_compsylmax = c;
3453
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003454 c = getc(fd); /* <compoptions> */
3455 if (c != 0)
3456 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3457 else
3458 {
3459 --todo;
3460 c = getc(fd); /* only use the lower byte for now */
3461 --todo;
3462 slang->sl_compoptions = c;
3463
3464 gap = &slang->sl_comppat;
3465 c = get2c(fd); /* <comppatcount> */
3466 todo -= 2;
3467 ga_init2(gap, sizeof(char_u *), c);
3468 if (ga_grow(gap, c) == OK)
3469 while (--c >= 0)
3470 {
3471 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3472 read_cnt_string(fd, 1, &cnt);
3473 /* <comppatlen> <comppattext> */
3474 if (cnt < 0)
3475 return cnt;
3476 todo -= cnt + 2;
3477 }
3478 }
3479
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003480 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003481 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003482 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3483 * Conversion to utf-8 may double the size. */
3484 c = todo * 2 + 7;
3485#ifdef FEAT_MBYTE
3486 if (enc_utf8)
3487 c += todo * 2;
3488#endif
3489 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003490 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003491 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003492
Bram Moolenaard12a1322005-08-21 22:08:24 +00003493 /* We also need a list of all flags that can appear at the start and one
3494 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003495 cp = alloc(todo + 1);
3496 if (cp == NULL)
3497 {
3498 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003499 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003500 }
3501 slang->sl_compstartflags = cp;
3502 *cp = NUL;
3503
Bram Moolenaard12a1322005-08-21 22:08:24 +00003504 ap = alloc(todo + 1);
3505 if (ap == NULL)
3506 {
3507 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003508 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003509 }
3510 slang->sl_compallflags = ap;
3511 *ap = NUL;
3512
Bram Moolenaar5195e452005-08-19 20:32:47 +00003513 pp = pat;
3514 *pp++ = '^';
3515 *pp++ = '\\';
3516 *pp++ = '(';
3517
3518 atstart = 1;
3519 while (todo-- > 0)
3520 {
3521 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003522
3523 /* Add all flags to "sl_compallflags". */
3524 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003525 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003526 {
3527 *ap++ = c;
3528 *ap = NUL;
3529 }
3530
Bram Moolenaar5195e452005-08-19 20:32:47 +00003531 if (atstart != 0)
3532 {
3533 /* At start of item: copy flags to "sl_compstartflags". For a
3534 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3535 if (c == '[')
3536 atstart = 2;
3537 else if (c == ']')
3538 atstart = 0;
3539 else
3540 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003541 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003542 {
3543 *cp++ = c;
3544 *cp = NUL;
3545 }
3546 if (atstart == 1)
3547 atstart = 0;
3548 }
3549 }
3550 if (c == '/') /* slash separates two items */
3551 {
3552 *pp++ = '\\';
3553 *pp++ = '|';
3554 atstart = 1;
3555 }
3556 else /* normal char, "[abc]" and '*' are copied as-is */
3557 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003558 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003559 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003560#ifdef FEAT_MBYTE
3561 if (enc_utf8)
3562 pp += mb_char2bytes(c, pp);
3563 else
3564#endif
3565 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003566 }
3567 }
3568
3569 *pp++ = '\\';
3570 *pp++ = ')';
3571 *pp++ = '$';
3572 *pp = NUL;
3573
3574 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3575 vim_free(pat);
3576 if (slang->sl_compprog == NULL)
3577 return SP_FORMERROR;
3578
3579 return 0;
3580}
3581
Bram Moolenaar6de68532005-08-24 22:08:48 +00003582/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003583 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003584 * Like strchr() but independent of locale.
3585 */
3586 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003587byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003588 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003589 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003590{
3591 char_u *p;
3592
3593 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003594 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003595 return TRUE;
3596 return FALSE;
3597}
3598
Bram Moolenaar5195e452005-08-19 20:32:47 +00003599#define SY_MAXLEN 30
3600typedef struct syl_item_S
3601{
3602 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3603 int sy_len;
3604} syl_item_T;
3605
3606/*
3607 * Truncate "slang->sl_syllable" at the first slash and put the following items
3608 * in "slang->sl_syl_items".
3609 */
3610 static int
3611init_syl_tab(slang)
3612 slang_T *slang;
3613{
3614 char_u *p;
3615 char_u *s;
3616 int l;
3617 syl_item_T *syl;
3618
3619 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3620 p = vim_strchr(slang->sl_syllable, '/');
3621 while (p != NULL)
3622 {
3623 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003624 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003625 break;
3626 s = p;
3627 p = vim_strchr(p, '/');
3628 if (p == NULL)
3629 l = STRLEN(s);
3630 else
3631 l = p - s;
3632 if (l >= SY_MAXLEN)
3633 return SP_FORMERROR;
3634 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003635 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003636 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3637 + slang->sl_syl_items.ga_len++;
3638 vim_strncpy(syl->sy_chars, s, l);
3639 syl->sy_len = l;
3640 }
3641 return OK;
3642}
3643
3644/*
3645 * Count the number of syllables in "word".
3646 * When "word" contains spaces the syllables after the last space are counted.
3647 * Returns zero if syllables are not defines.
3648 */
3649 static int
3650count_syllables(slang, word)
3651 slang_T *slang;
3652 char_u *word;
3653{
3654 int cnt = 0;
3655 int skip = FALSE;
3656 char_u *p;
3657 int len;
3658 int i;
3659 syl_item_T *syl;
3660 int c;
3661
3662 if (slang->sl_syllable == NULL)
3663 return 0;
3664
3665 for (p = word; *p != NUL; p += len)
3666 {
3667 /* When running into a space reset counter. */
3668 if (*p == ' ')
3669 {
3670 len = 1;
3671 cnt = 0;
3672 continue;
3673 }
3674
3675 /* Find longest match of syllable items. */
3676 len = 0;
3677 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3678 {
3679 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3680 if (syl->sy_len > len
3681 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3682 len = syl->sy_len;
3683 }
3684 if (len != 0) /* found a match, count syllable */
3685 {
3686 ++cnt;
3687 skip = FALSE;
3688 }
3689 else
3690 {
3691 /* No recognized syllable item, at least a syllable char then? */
3692#ifdef FEAT_MBYTE
3693 c = mb_ptr2char(p);
3694 len = (*mb_ptr2len)(p);
3695#else
3696 c = *p;
3697 len = 1;
3698#endif
3699 if (vim_strchr(slang->sl_syllable, c) == NULL)
3700 skip = FALSE; /* No, search for next syllable */
3701 else if (!skip)
3702 {
3703 ++cnt; /* Yes, count it */
3704 skip = TRUE; /* don't count following syllable chars */
3705 }
3706 }
3707 }
3708 return cnt;
3709}
3710
3711/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003712 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003713 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003714 */
3715 static int
3716set_sofo(lp, from, to)
3717 slang_T *lp;
3718 char_u *from;
3719 char_u *to;
3720{
3721 int i;
3722
3723#ifdef FEAT_MBYTE
3724 garray_T *gap;
3725 char_u *s;
3726 char_u *p;
3727 int c;
3728 int *inp;
3729
3730 if (has_mbyte)
3731 {
3732 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3733 * characters. The index is the low byte of the character.
3734 * The list contains from-to pairs with a terminating NUL.
3735 * sl_sal_first[] is used for latin1 "from" characters. */
3736 gap = &lp->sl_sal;
3737 ga_init2(gap, sizeof(int *), 1);
3738 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003739 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003740 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3741 gap->ga_len = 256;
3742
3743 /* First count the number of items for each list. Temporarily use
3744 * sl_sal_first[] for this. */
3745 for (p = from, s = to; *p != NUL && *s != NUL; )
3746 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003747 c = mb_cptr2char_adv(&p);
3748 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003749 if (c >= 256)
3750 ++lp->sl_sal_first[c & 0xff];
3751 }
3752 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003753 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003754
3755 /* Allocate the lists. */
3756 for (i = 0; i < 256; ++i)
3757 if (lp->sl_sal_first[i] > 0)
3758 {
3759 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3760 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003761 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003762 ((int **)gap->ga_data)[i] = (int *)p;
3763 *(int *)p = 0;
3764 }
3765
3766 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3767 * list. */
3768 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3769 for (p = from, s = to; *p != NUL && *s != NUL; )
3770 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003771 c = mb_cptr2char_adv(&p);
3772 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003773 if (c >= 256)
3774 {
3775 /* Append the from-to chars at the end of the list with
3776 * the low byte. */
3777 inp = ((int **)gap->ga_data)[c & 0xff];
3778 while (*inp != 0)
3779 ++inp;
3780 *inp++ = c; /* from char */
3781 *inp++ = i; /* to char */
3782 *inp++ = NUL; /* NUL at the end */
3783 }
3784 else
3785 /* mapping byte to char is done in sl_sal_first[] */
3786 lp->sl_sal_first[c] = i;
3787 }
3788 }
3789 else
3790#endif
3791 {
3792 /* mapping bytes to bytes is done in sl_sal_first[] */
3793 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003794 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003795
3796 for (i = 0; to[i] != NUL; ++i)
3797 lp->sl_sal_first[from[i]] = to[i];
3798 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3799 }
3800
Bram Moolenaar5195e452005-08-19 20:32:47 +00003801 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003802}
3803
3804/*
3805 * Fill the first-index table for "lp".
3806 */
3807 static void
3808set_sal_first(lp)
3809 slang_T *lp;
3810{
3811 salfirst_T *sfirst;
3812 int i;
3813 salitem_T *smp;
3814 int c;
3815 garray_T *gap = &lp->sl_sal;
3816
3817 sfirst = lp->sl_sal_first;
3818 for (i = 0; i < 256; ++i)
3819 sfirst[i] = -1;
3820 smp = (salitem_T *)gap->ga_data;
3821 for (i = 0; i < gap->ga_len; ++i)
3822 {
3823#ifdef FEAT_MBYTE
3824 if (has_mbyte)
3825 /* Use the lowest byte of the first character. For latin1 it's
3826 * the character, for other encodings it should differ for most
3827 * characters. */
3828 c = *smp[i].sm_lead_w & 0xff;
3829 else
3830#endif
3831 c = *smp[i].sm_lead;
3832 if (sfirst[c] == -1)
3833 {
3834 sfirst[c] = i;
3835#ifdef FEAT_MBYTE
3836 if (has_mbyte)
3837 {
3838 int n;
3839
3840 /* Make sure all entries with this byte are following each
3841 * other. Move the ones that are in the wrong position. Do
3842 * keep the same ordering! */
3843 while (i + 1 < gap->ga_len
3844 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3845 /* Skip over entry with same index byte. */
3846 ++i;
3847
3848 for (n = 1; i + n < gap->ga_len; ++n)
3849 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3850 {
3851 salitem_T tsal;
3852
3853 /* Move entry with same index byte after the entries
3854 * we already found. */
3855 ++i;
3856 --n;
3857 tsal = smp[i + n];
3858 mch_memmove(smp + i + 1, smp + i,
3859 sizeof(salitem_T) * n);
3860 smp[i] = tsal;
3861 }
3862 }
3863#endif
3864 }
3865 }
3866}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003867
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003868#ifdef FEAT_MBYTE
3869/*
3870 * Turn a multi-byte string into a wide character string.
3871 * Return it in allocated memory (NULL for out-of-memory)
3872 */
3873 static int *
3874mb_str2wide(s)
3875 char_u *s;
3876{
3877 int *res;
3878 char_u *p;
3879 int i = 0;
3880
3881 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3882 if (res != NULL)
3883 {
3884 for (p = s; *p != NUL; )
3885 res[i++] = mb_ptr2char_adv(&p);
3886 res[i] = NUL;
3887 }
3888 return res;
3889}
3890#endif
3891
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003893 * Read a tree from the .spl or .sug file.
3894 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3895 * This is skipped when the tree has zero length.
3896 * Returns zero when OK, SP_ value for an error.
3897 */
3898 static int
3899spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3900 FILE *fd;
3901 char_u **bytsp;
3902 idx_T **idxsp;
3903 int prefixtree; /* TRUE for the prefix tree */
3904 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3905{
3906 int len;
3907 int idx;
3908 char_u *bp;
3909 idx_T *ip;
3910
3911 /* The tree size was computed when writing the file, so that we can
3912 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003913 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003914 if (len < 0)
3915 return SP_TRUNCERROR;
3916 if (len > 0)
3917 {
3918 /* Allocate the byte array. */
3919 bp = lalloc((long_u)len, TRUE);
3920 if (bp == NULL)
3921 return SP_OTHERERROR;
3922 *bytsp = bp;
3923
3924 /* Allocate the index array. */
3925 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3926 if (ip == NULL)
3927 return SP_OTHERERROR;
3928 *idxsp = ip;
3929
3930 /* Recursively read the tree and store it in the array. */
3931 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3932 if (idx < 0)
3933 return idx;
3934 }
3935 return 0;
3936}
3937
3938/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939 * Read one row of siblings from the spell file and store it in the byte array
3940 * "byts" and index array "idxs". Recursively read the children.
3941 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003942 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003943 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003944 * Returns the index (>= 0) following the siblings.
3945 * Returns SP_TRUNCERROR if the file is shorter than expected.
3946 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003947 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003948 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003949read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003950 FILE *fd;
3951 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003952 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003953 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003954 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003955 int prefixtree; /* TRUE for reading PREFIXTREE */
3956 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003957{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003958 int len;
3959 int i;
3960 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003961 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003962 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003963 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003964#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003965
Bram Moolenaar51485f02005-06-04 21:55:20 +00003966 len = getc(fd); /* <siblingcount> */
3967 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003968 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003969
3970 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003971 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003972 byts[idx++] = len;
3973
3974 /* Read the byte values, flag/region bytes and shared indexes. */
3975 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003977 c = getc(fd); /* <byte> */
3978 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003979 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003980 if (c <= BY_SPECIAL)
3981 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003982 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 {
3984 /* No flags, all regions. */
3985 idxs[idx] = 0;
3986 c = 0;
3987 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003988 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003990 if (prefixtree)
3991 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003992 /* Read the optional pflags byte, the prefix ID and the
3993 * condition nr. In idxs[] store the prefix ID in the low
3994 * byte, the condition index shifted up 8 bits, the flags
3995 * shifted up 24 bits. */
3996 if (c == BY_FLAGS)
3997 c = getc(fd) << 24; /* <pflags> */
3998 else
3999 c = 0;
4000
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004001 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004002
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004003 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004004 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004005 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004006 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004007 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004008 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004009 {
4010 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004011 * idxs[] the flags go in the low two bytes, region above
4012 * that and prefix ID above the region. */
4013 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004014 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004015 if (c2 == BY_FLAGS2)
4016 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004017 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004018 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004019 if (c & WF_AFX)
4020 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004021 }
4022
Bram Moolenaar51485f02005-06-04 21:55:20 +00004023 idxs[idx] = c;
4024 c = 0;
4025 }
4026 else /* c == BY_INDEX */
4027 {
4028 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004029 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004030 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004031 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004032 idxs[idx] = n + SHARED_MASK;
4033 c = getc(fd); /* <xbyte> */
4034 }
4035 }
4036 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004037 }
4038
Bram Moolenaar51485f02005-06-04 21:55:20 +00004039 /* Recursively read the children for non-shared siblings.
4040 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4041 * remove SHARED_MASK) */
4042 for (i = 1; i <= len; ++i)
4043 if (byts[startidx + i] != 0)
4044 {
4045 if (idxs[startidx + i] & SHARED_MASK)
4046 idxs[startidx + i] &= ~SHARED_MASK;
4047 else
4048 {
4049 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004050 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004051 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 if (idx < 0)
4053 break;
4054 }
4055 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004056
Bram Moolenaar51485f02005-06-04 21:55:20 +00004057 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058}
4059
4060/*
4061 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004062 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063 */
4064 char_u *
4065did_set_spelllang(buf)
4066 buf_T *buf;
4067{
4068 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004069 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004070 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004071 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004072 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004073 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004074 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004076 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004078 int len;
4079 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004080 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004081 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004082 char_u *use_region = NULL;
4083 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004084 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004085 int i, j;
4086 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087
4088 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004089 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004090
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004091 /* loop over comma separated language names. */
4092 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004094 /* Get one language name. */
4095 copy_option_part(&splp, lang, MAXWLEN, ",");
4096
Bram Moolenaar5482f332005-04-17 20:18:43 +00004097 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004098 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004099
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004100 /* If the name ends in ".spl" use it as the name of the spell file.
4101 * If there is a region name let "region" point to it and remove it
4102 * from the name. */
4103 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4104 {
4105 filename = TRUE;
4106
Bram Moolenaarb6356332005-07-18 21:40:44 +00004107 /* Locate a region and remove it from the file name. */
4108 p = vim_strchr(gettail(lang), '_');
4109 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4110 && !ASCII_ISALPHA(p[3]))
4111 {
4112 vim_strncpy(region_cp, p + 1, 2);
4113 mch_memmove(p, p + 3, len - (p - lang) - 2);
4114 len -= 3;
4115 region = region_cp;
4116 }
4117 else
4118 dont_use_region = TRUE;
4119
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004120 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004121 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4122 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004123 break;
4124 }
4125 else
4126 {
4127 filename = FALSE;
4128 if (len > 3 && lang[len - 3] == '_')
4129 {
4130 region = lang + len - 2;
4131 len -= 3;
4132 lang[len] = NUL;
4133 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004134 else
4135 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004136
4137 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004138 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4139 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004140 break;
4141 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004142
Bram Moolenaarb6356332005-07-18 21:40:44 +00004143 if (region != NULL)
4144 {
4145 /* If the region differs from what was used before then don't
4146 * use it for 'spellfile'. */
4147 if (use_region != NULL && STRCMP(region, use_region) != 0)
4148 dont_use_region = TRUE;
4149 use_region = region;
4150 }
4151
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004152 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004153 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004154 {
4155 if (filename)
4156 (void)spell_load_file(lang, lang, NULL, FALSE);
4157 else
4158 spell_load_lang(lang);
4159 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004160
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004161 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004162 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004163 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004164 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4165 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4166 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004168 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004169 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004170 {
4171 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004172 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004173 if (c == REGION_ALL)
4174 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004175 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004176 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004177 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004178 /* This addition file is for other regions. */
4179 region_mask = 0;
4180 }
4181 else
4182 /* This is probably an error. Give a warning and
4183 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004184 smsg((char_u *)
4185 _("Warning: region %s not supported"),
4186 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004187 }
4188 else
4189 region_mask = 1 << c;
4190 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004191
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004192 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004193 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004194 if (ga_grow(&ga, 1) == FAIL)
4195 {
4196 ga_clear(&ga);
4197 return e_outofmem;
4198 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004199 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004200 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4201 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004202 use_midword(slang, buf);
4203 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004204 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004205 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004206 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004207 }
4208
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004209 /* round 0: load int_wordlist, if possible.
4210 * round 1: load first name in 'spellfile'.
4211 * round 2: load second name in 'spellfile.
4212 * etc. */
4213 spf = curbuf->b_p_spf;
4214 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004216 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004217 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004218 /* Internal wordlist, if there is one. */
4219 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004220 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004221 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004222 }
4223 else
4224 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004225 /* One entry in 'spellfile'. */
4226 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4227 STRCAT(spf_name, ".spl");
4228
4229 /* If it was already found above then skip it. */
4230 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004231 {
4232 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4233 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004234 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004235 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004236 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004237 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004238 }
4239
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004240 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004241 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4242 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004244 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004246 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004247 * region name, the region is ignored otherwise. for int_wordlist
4248 * use an arbitrary name. */
4249 if (round == 0)
4250 STRCPY(lang, "internal wordlist");
4251 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004252 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004253 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004254 p = vim_strchr(lang, '.');
4255 if (p != NULL)
4256 *p = NUL; /* truncate at ".encoding.add" */
4257 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004258 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004259
4260 /* If one of the languages has NOBREAK we assume the addition
4261 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 if (slang != NULL && nobreak)
4263 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004265 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004267 region_mask = REGION_ALL;
4268 if (use_region != NULL && !dont_use_region)
4269 {
4270 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004271 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004272 if (c != REGION_ALL)
4273 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004274 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004275 /* This spell file is for other regions. */
4276 region_mask = 0;
4277 }
4278
4279 if (region_mask != 0)
4280 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004281 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4282 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4283 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004284 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4285 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004286 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 }
4289 }
4290
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291 /* Everything is fine, store the new b_langp value. */
4292 ga_clear(&buf->b_langp);
4293 buf->b_langp = ga;
4294
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 /* For each language figure out what language to use for sound folding and
4296 * REP items. If the language doesn't support it itself use another one
4297 * with the same name. E.g. for "en-math" use "en". */
4298 for (i = 0; i < ga.ga_len; ++i)
4299 {
4300 lp = LANGP_ENTRY(ga, i);
4301
4302 /* sound folding */
4303 if (lp->lp_slang->sl_sal.ga_len > 0)
4304 /* language does sound folding itself */
4305 lp->lp_sallang = lp->lp_slang;
4306 else
4307 /* find first similar language that does sound folding */
4308 for (j = 0; j < ga.ga_len; ++j)
4309 {
4310 lp2 = LANGP_ENTRY(ga, j);
4311 if (lp2->lp_slang->sl_sal.ga_len > 0
4312 && STRNCMP(lp->lp_slang->sl_name,
4313 lp2->lp_slang->sl_name, 2) == 0)
4314 {
4315 lp->lp_sallang = lp2->lp_slang;
4316 break;
4317 }
4318 }
4319
4320 /* REP items */
4321 if (lp->lp_slang->sl_rep.ga_len > 0)
4322 /* language has REP items itself */
4323 lp->lp_replang = lp->lp_slang;
4324 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004325 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004326 for (j = 0; j < ga.ga_len; ++j)
4327 {
4328 lp2 = LANGP_ENTRY(ga, j);
4329 if (lp2->lp_slang->sl_rep.ga_len > 0
4330 && STRNCMP(lp->lp_slang->sl_name,
4331 lp2->lp_slang->sl_name, 2) == 0)
4332 {
4333 lp->lp_replang = lp2->lp_slang;
4334 break;
4335 }
4336 }
4337 }
4338
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004339 return NULL;
4340}
4341
4342/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004343 * Clear the midword characters for buffer "buf".
4344 */
4345 static void
4346clear_midword(buf)
4347 buf_T *buf;
4348{
4349 vim_memset(buf->b_spell_ismw, 0, 256);
4350#ifdef FEAT_MBYTE
4351 vim_free(buf->b_spell_ismw_mb);
4352 buf->b_spell_ismw_mb = NULL;
4353#endif
4354}
4355
4356/*
4357 * Use the "sl_midword" field of language "lp" for buffer "buf".
4358 * They add up to any currently used midword characters.
4359 */
4360 static void
4361use_midword(lp, buf)
4362 slang_T *lp;
4363 buf_T *buf;
4364{
4365 char_u *p;
4366
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004367 if (lp->sl_midword == NULL) /* there aren't any */
4368 return;
4369
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004370 for (p = lp->sl_midword; *p != NUL; )
4371#ifdef FEAT_MBYTE
4372 if (has_mbyte)
4373 {
4374 int c, l, n;
4375 char_u *bp;
4376
4377 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004378 l = (*mb_ptr2len)(p);
4379 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004380 buf->b_spell_ismw[c] = TRUE;
4381 else if (buf->b_spell_ismw_mb == NULL)
4382 /* First multi-byte char in "b_spell_ismw_mb". */
4383 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4384 else
4385 {
4386 /* Append multi-byte chars to "b_spell_ismw_mb". */
4387 n = STRLEN(buf->b_spell_ismw_mb);
4388 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4389 if (bp != NULL)
4390 {
4391 vim_free(buf->b_spell_ismw_mb);
4392 buf->b_spell_ismw_mb = bp;
4393 vim_strncpy(bp + n, p, l);
4394 }
4395 }
4396 p += l;
4397 }
4398 else
4399#endif
4400 buf->b_spell_ismw[*p++] = TRUE;
4401}
4402
4403/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004404 * Find the region "region[2]" in "rp" (points to "sl_regions").
4405 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004406 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004407 */
4408 static int
4409find_region(rp, region)
4410 char_u *rp;
4411 char_u *region;
4412{
4413 int i;
4414
4415 for (i = 0; ; i += 2)
4416 {
4417 if (rp[i] == NUL)
4418 return REGION_ALL;
4419 if (rp[i] == region[0] && rp[i + 1] == region[1])
4420 break;
4421 }
4422 return i / 2;
4423}
4424
4425/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004427 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004428 * Word WF_ONECAP
4429 * W WORD WF_ALLCAP
4430 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004431 */
4432 static int
4433captype(word, end)
4434 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004436{
4437 char_u *p;
4438 int c;
4439 int firstcap;
4440 int allcap;
4441 int past_second = FALSE; /* past second word char */
4442
4443 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004444 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004446 return 0; /* only non-word characters, illegal word */
4447#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004448 if (has_mbyte)
4449 c = mb_ptr2char_adv(&p);
4450 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004451#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004452 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004453 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004454
4455 /*
4456 * Need to check all letters to find a word with mixed upper/lower.
4457 * But a word with an upper char only at start is a ONECAP.
4458 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004460 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004461 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004462 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004463 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 {
4465 /* UUl -> KEEPCAP */
4466 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004467 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004468 allcap = FALSE;
4469 }
4470 else if (!allcap)
4471 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004472 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004473 past_second = TRUE;
4474 }
4475
4476 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004477 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004478 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004479 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004480 return 0;
4481}
4482
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004483/*
4484 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4485 * capital. So that make_case_word() can turn WOrd into Word.
4486 * Add ALLCAP for "WOrD".
4487 */
4488 static int
4489badword_captype(word, end)
4490 char_u *word;
4491 char_u *end;
4492{
4493 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004494 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004495 int l, u;
4496 int first;
4497 char_u *p;
4498
4499 if (flags & WF_KEEPCAP)
4500 {
4501 /* Count the number of UPPER and lower case letters. */
4502 l = u = 0;
4503 first = FALSE;
4504 for (p = word; p < end; mb_ptr_adv(p))
4505 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004506 c = PTR2CHAR(p);
4507 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004508 {
4509 ++u;
4510 if (p == word)
4511 first = TRUE;
4512 }
4513 else
4514 ++l;
4515 }
4516
4517 /* If there are more UPPER than lower case letters suggest an
4518 * ALLCAP word. Otherwise, if the first letter is UPPER then
4519 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4520 * require three upper case letters. */
4521 if (u > l && u > 2)
4522 flags |= WF_ALLCAP;
4523 else if (first)
4524 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004525
4526 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4527 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004528 }
4529 return flags;
4530}
4531
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004532# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4533/*
4534 * Free all languages.
4535 */
4536 void
4537spell_free_all()
4538{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004539 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004540 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004541 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004542
4543 /* Go through all buffers and handle 'spelllang'. */
4544 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4545 ga_clear(&buf->b_langp);
4546
4547 while (first_lang != NULL)
4548 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004549 slang = first_lang;
4550 first_lang = slang->sl_next;
4551 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004552 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004553
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004554 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004555 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004556 /* Delete the internal wordlist and its .spl file */
4557 mch_remove(int_wordlist);
4558 int_wordlist_spl(fname);
4559 mch_remove(fname);
4560 vim_free(int_wordlist);
4561 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004562 }
4563
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004564 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004565
4566 vim_free(repl_to);
4567 repl_to = NULL;
4568 vim_free(repl_from);
4569 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004570}
4571# endif
4572
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573# if defined(FEAT_MBYTE) || defined(PROTO)
4574/*
4575 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004576 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 */
4578 void
4579spell_reload()
4580{
4581 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004582 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583
Bram Moolenaarea408852005-06-25 22:49:46 +00004584 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585 init_spell_chartab();
4586
4587 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004588 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589
4590 /* Go through all buffers and handle 'spelllang'. */
4591 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4592 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004593 /* Only load the wordlists when 'spelllang' is set and there is a
4594 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004596 {
4597 FOR_ALL_WINDOWS(wp)
4598 if (wp->w_buffer == buf && wp->w_p_spell)
4599 {
4600 (void)did_set_spelllang(buf);
4601# ifdef FEAT_WINDOWS
4602 break;
4603# endif
4604 }
4605 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 }
4607}
4608# endif
4609
Bram Moolenaarb765d632005-06-07 21:00:02 +00004610/*
4611 * Reload the spell file "fname" if it's loaded.
4612 */
4613 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004615 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004616 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004617{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004618 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004620
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004621 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004622 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004623 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004624 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004625 slang_clear(slang);
4626 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004627 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004628 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004629 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004631 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004632 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633
4634 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004635 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 if (added_word && !didit)
4637 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004638}
4639
4640
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641/*
4642 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004643 */
4644
Bram Moolenaar51485f02005-06-04 21:55:20 +00004645#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 and .dic file. */
4647/*
4648 * Main structure to store the contents of a ".aff" file.
4649 */
4650typedef struct afffile_S
4651{
4652 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004653 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004654 unsigned af_rare; /* RARE ID for rare word */
4655 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004656 unsigned af_bad; /* BAD ID for banned word */
4657 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004658 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004659 unsigned af_comproot; /* COMPOUNDROOT ID */
4660 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4661 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004662 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004663 int af_pfxpostpone; /* postpone prefixes without chop string and
4664 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004665 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4666 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004667 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004668} afffile_T;
4669
Bram Moolenaar6de68532005-08-24 22:08:48 +00004670#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004671#define AFT_LONG 1 /* flags are two characters */
4672#define AFT_CAPLONG 2 /* flags are one or two characters */
4673#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004674
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004675typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004676/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4677struct affentry_S
4678{
4679 affentry_T *ae_next; /* next affix with same name/number */
4680 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4681 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004682 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004683 char_u *ae_cond; /* condition (NULL for ".") */
4684 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004685};
4686
Bram Moolenaar6de68532005-08-24 22:08:48 +00004687#ifdef FEAT_MBYTE
4688# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4689#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004690# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004691#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004692
Bram Moolenaar51485f02005-06-04 21:55:20 +00004693/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4694typedef struct affheader_S
4695{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004696 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4697 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4698 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004699 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004700 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004701 affentry_T *ah_first; /* first affix entry */
4702} affheader_T;
4703
4704#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4705
Bram Moolenaar6de68532005-08-24 22:08:48 +00004706/* Flag used in compound items. */
4707typedef struct compitem_S
4708{
4709 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4710 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4711 int ci_newID; /* affix ID after renumbering. */
4712} compitem_T;
4713
4714#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4715
Bram Moolenaar51485f02005-06-04 21:55:20 +00004716/*
4717 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004718 * the need to keep track of each allocated thing, everything is freed all at
4719 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004720 */
4721#define SBLOCKSIZE 16000 /* size of sb_data */
4722typedef struct sblock_S sblock_T;
4723struct sblock_S
4724{
4725 sblock_T *sb_next; /* next block in list */
4726 int sb_used; /* nr of bytes already in use */
4727 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004728};
4729
4730/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004731 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004732 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004733typedef struct wordnode_S wordnode_T;
4734struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004735{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004736 union /* shared to save space */
4737 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004738 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004739 int index; /* index in written nodes (valid after first
4740 round) */
4741 } wn_u1;
4742 union /* shared to save space */
4743 {
4744 wordnode_T *next; /* next node with same hash key */
4745 wordnode_T *wnode; /* parent node that will write this node */
4746 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004747 wordnode_T *wn_child; /* child (next byte in word) */
4748 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4749 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004750 int wn_refs; /* Nr. of references to this node. Only
4751 relevant for first node in a list of
4752 siblings, in following siblings it is
4753 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004754 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004755
4756 /* Info for when "wn_byte" is NUL.
4757 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4758 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4759 * "wn_region" the LSW of the wordnr. */
4760 char_u wn_affixID; /* supported/required prefix ID or 0 */
4761 short_u wn_flags; /* WF_ flags */
4762 short wn_region; /* region mask */
4763
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004764#ifdef SPELL_PRINTTREE
4765 int wn_nr; /* sequence nr for printing */
4766#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004767};
4768
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004769#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4770
Bram Moolenaar51485f02005-06-04 21:55:20 +00004771#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004772
Bram Moolenaar51485f02005-06-04 21:55:20 +00004773/*
4774 * Info used while reading the spell files.
4775 */
4776typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004777{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004778 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004779 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004780
Bram Moolenaar51485f02005-06-04 21:55:20 +00004781 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004782 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004783
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004784 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004785
Bram Moolenaar4770d092006-01-12 23:22:24 +00004786 long si_sugtree; /* creating the soundfolding trie */
4787
Bram Moolenaar51485f02005-06-04 21:55:20 +00004788 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004789 long si_blocks_cnt; /* memory blocks allocated */
4790 long si_compress_cnt; /* words to add before lowering
4791 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004792 wordnode_T *si_first_free; /* List of nodes that have been freed during
4793 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004794 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004795#ifdef SPELL_PRINTTREE
4796 int si_wordnode_nr; /* sequence nr for nodes */
4797#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004798 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004799
Bram Moolenaar51485f02005-06-04 21:55:20 +00004800 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004801 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004802 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803 int si_region; /* region mask */
4804 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004805 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004806 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004807 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004808 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004809 int si_region_count; /* number of regions supported (1 when there
4810 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004811 char_u si_region_name[16]; /* region names; used only if
4812 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813
4814 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004815 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004817 char_u *si_sofofr; /* SOFOFROM text */
4818 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004819 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004820 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 int si_followup; /* soundsalike: ? */
4822 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004823 hashtab_T si_commonwords; /* hashtable for common words */
4824 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 int si_rem_accents; /* soundsalike: remove accents */
4826 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004827 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004828 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004829 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004830 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004831 int si_compoptions; /* COMP_ flags */
4832 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4833 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004834 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004835 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004836 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004837 garray_T si_prefcond; /* table with conditions for postponed
4838 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004839 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004840 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004841} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004843static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004844static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004845static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4846static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4847static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004848static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004849static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4850static void aff_check_number __ARGS((int spinval, int affval, char *name));
4851static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004852static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004853static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4854static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004855static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004856static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004857static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004858static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004859static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004860static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004861static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4862static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4863static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004865static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004866static 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 +00004867static 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 +00004868static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004869static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004870static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4871static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4872static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004873static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004874static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004875static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004876static void clear_node __ARGS((wordnode_T *node));
4877static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004878static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4879static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4880static int sug_maketable __ARGS((spellinfo_T *spin));
4881static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4882static int offset2bytes __ARGS((int nr, char_u *buf));
4883static int bytes2offset __ARGS((char_u **pp));
4884static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004886static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004887static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004888
Bram Moolenaar53805d12005-08-01 07:08:33 +00004889/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4890 * but it must be negative to indicate the prefix tree to tree_add_word().
4891 * Use a negative number with the lower 8 bits zero. */
4892#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004893
Bram Moolenaar5195e452005-08-19 20:32:47 +00004894/*
4895 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4896 */
4897static long compress_start = 30000; /* memory / SBLOCKSIZE */
4898static long compress_inc = 100; /* memory / SBLOCKSIZE */
4899static long compress_added = 500000; /* word count */
4900
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004901#ifdef SPELL_PRINTTREE
4902/*
4903 * For debugging the tree code: print the current tree in a (more or less)
4904 * readable format, so that we can see what happens when adding a word and/or
4905 * compressing the tree.
4906 * Based on code from Olaf Seibert.
4907 */
4908#define PRINTLINESIZE 1000
4909#define PRINTWIDTH 6
4910
4911#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4912 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4913
4914static char line1[PRINTLINESIZE];
4915static char line2[PRINTLINESIZE];
4916static char line3[PRINTLINESIZE];
4917
4918 static void
4919spell_clear_flags(wordnode_T *node)
4920{
4921 wordnode_T *np;
4922
4923 for (np = node; np != NULL; np = np->wn_sibling)
4924 {
4925 np->wn_u1.index = FALSE;
4926 spell_clear_flags(np->wn_child);
4927 }
4928}
4929
4930 static void
4931spell_print_node(wordnode_T *node, int depth)
4932{
4933 if (node->wn_u1.index)
4934 {
4935 /* Done this node before, print the reference. */
4936 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4937 PRINTSOME(line2, depth, " ", 0, 0);
4938 PRINTSOME(line3, depth, " ", 0, 0);
4939 msg(line1);
4940 msg(line2);
4941 msg(line3);
4942 }
4943 else
4944 {
4945 node->wn_u1.index = TRUE;
4946
4947 if (node->wn_byte != NUL)
4948 {
4949 if (node->wn_child != NULL)
4950 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4951 else
4952 /* Cannot happen? */
4953 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4954 }
4955 else
4956 PRINTSOME(line1, depth, " $ ", 0, 0);
4957
4958 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4959
4960 if (node->wn_sibling != NULL)
4961 PRINTSOME(line3, depth, " | ", 0, 0);
4962 else
4963 PRINTSOME(line3, depth, " ", 0, 0);
4964
4965 if (node->wn_byte == NUL)
4966 {
4967 msg(line1);
4968 msg(line2);
4969 msg(line3);
4970 }
4971
4972 /* do the children */
4973 if (node->wn_byte != NUL && node->wn_child != NULL)
4974 spell_print_node(node->wn_child, depth + 1);
4975
4976 /* do the siblings */
4977 if (node->wn_sibling != NULL)
4978 {
4979 /* get rid of all parent details except | */
4980 STRCPY(line1, line3);
4981 STRCPY(line2, line3);
4982 spell_print_node(node->wn_sibling, depth);
4983 }
4984 }
4985}
4986
4987 static void
4988spell_print_tree(wordnode_T *root)
4989{
4990 if (root != NULL)
4991 {
4992 /* Clear the "wn_u1.index" fields, used to remember what has been
4993 * done. */
4994 spell_clear_flags(root);
4995
4996 /* Recursively print the tree. */
4997 spell_print_node(root, 0);
4998 }
4999}
5000#endif /* SPELL_PRINTTREE */
5001
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005002/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005004 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005005 */
5006 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005007spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005008 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005009 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010{
5011 FILE *fd;
5012 afffile_T *aff;
5013 char_u rline[MAXLINELEN];
5014 char_u *line;
5015 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005016#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005017 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005018 int itemcnt;
5019 char_u *p;
5020 int lnum = 0;
5021 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005022 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005023 int aff_todo = 0;
5024 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005025 char_u *low = NULL;
5026 char_u *fol = NULL;
5027 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005028 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005029 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005030 int do_sal;
5031 int do_map;
5032 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005033 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005034 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005035 int compminlen = 0; /* COMPOUNDMIN value */
5036 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005037 int compoptions = 0; /* COMP_ flags */
5038 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005039 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005040 concatenated */
5041 char_u *midword = NULL; /* MIDWORD value */
5042 char_u *syllable = NULL; /* SYLLABLE value */
5043 char_u *sofofrom = NULL; /* SOFOFROM value */
5044 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005045
Bram Moolenaar51485f02005-06-04 21:55:20 +00005046 /*
5047 * Open the file.
5048 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005049 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005050 if (fd == NULL)
5051 {
5052 EMSG2(_(e_notopen), fname);
5053 return NULL;
5054 }
5055
Bram Moolenaar4770d092006-01-12 23:22:24 +00005056 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5057 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005059 /* Only do REP lines when not done in another .aff file already. */
5060 do_rep = spin->si_rep.ga_len == 0;
5061
Bram Moolenaar4770d092006-01-12 23:22:24 +00005062 /* Only do REPSAL lines when not done in another .aff file already. */
5063 do_repsal = spin->si_repsal.ga_len == 0;
5064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005065 /* Only do SAL lines when not done in another .aff file already. */
5066 do_sal = spin->si_sal.ga_len == 0;
5067
5068 /* Only do MAP lines when not done in another .aff file already. */
5069 do_map = spin->si_map.ga_len == 0;
5070
Bram Moolenaar51485f02005-06-04 21:55:20 +00005071 /*
5072 * Allocate and init the afffile_T structure.
5073 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005074 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005075 if (aff == NULL)
5076 return NULL;
5077 hash_init(&aff->af_pref);
5078 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005079 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005080
5081 /*
5082 * Read all the lines in the file one by one.
5083 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005084 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005085 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005086 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005087 ++lnum;
5088
5089 /* Skip comment lines. */
5090 if (*rline == '#')
5091 continue;
5092
5093 /* Convert from "SET" to 'encoding' when needed. */
5094 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005095#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005096 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005097 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005098 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005099 if (pc == NULL)
5100 {
5101 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5102 fname, lnum, rline);
5103 continue;
5104 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005105 line = pc;
5106 }
5107 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005108#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005109 {
5110 pc = NULL;
5111 line = rline;
5112 }
5113
5114 /* Split the line up in white separated items. Put a NUL after each
5115 * item. */
5116 itemcnt = 0;
5117 for (p = line; ; )
5118 {
5119 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5120 ++p;
5121 if (*p == NUL)
5122 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005123 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005124 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005125 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005126 /* A few items have arbitrary text argument, don't split them. */
5127 if (itemcnt == 2 && spell_info_item(items[0]))
5128 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5129 ++p;
5130 else
5131 while (*p > ' ') /* skip until white space or CR/NL */
5132 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005133 if (*p == NUL)
5134 break;
5135 *p++ = NUL;
5136 }
5137
5138 /* Handle non-empty lines. */
5139 if (itemcnt > 0)
5140 {
5141 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5142 && aff->af_enc == NULL)
5143 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005144#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005145 /* Setup for conversion from "ENC" to 'encoding'. */
5146 aff->af_enc = enc_canonize(items[1]);
5147 if (aff->af_enc != NULL && !spin->si_ascii
5148 && convert_setup(&spin->si_conv, aff->af_enc,
5149 p_enc) == FAIL)
5150 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5151 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005152 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005153#else
5154 smsg((char_u *)_("Conversion in %s not supported"), fname);
5155#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005156 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005157 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5158 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005159 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005160 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005161 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005162 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005163 aff->af_flagtype = AFT_NUM;
5164 else if (STRCMP(items[1], "caplong") == 0)
5165 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005166 else
5167 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5168 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005169 if (aff->af_rare != 0
5170 || aff->af_keepcase != 0
5171 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005172 || aff->af_needaffix != 0
5173 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005174 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005176 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005177 || aff->af_suff.ht_used > 0
5178 || aff->af_pref.ht_used > 0)
5179 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5180 fname, lnum, items[1]);
5181 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005182 else if (spell_info_item(items[0]))
5183 {
5184 p = (char_u *)getroom(spin,
5185 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5186 + STRLEN(items[0])
5187 + STRLEN(items[1]) + 3, FALSE);
5188 if (p != NULL)
5189 {
5190 if (spin->si_info != NULL)
5191 {
5192 STRCPY(p, spin->si_info);
5193 STRCAT(p, "\n");
5194 }
5195 STRCAT(p, items[0]);
5196 STRCAT(p, " ");
5197 STRCAT(p, items[1]);
5198 spin->si_info = p;
5199 }
5200 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005201 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5202 && midword == NULL)
5203 {
5204 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005209 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005210 /* TODO: remove "RAR" later */
5211 else if ((STRCMP(items[0], "RAR") == 0
5212 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5213 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005214 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005215 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005216 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005217 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005218 /* TODO: remove "KEP" later */
5219 else if ((STRCMP(items[0], "KEP") == 0
5220 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5221 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005222 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005223 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005224 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005225 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005226 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5227 && aff->af_bad == 0)
5228 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005229 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5230 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005231 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005232 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5233 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005234 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005235 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5236 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005237 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005238 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5239 && aff->af_nosuggest == 0)
5240 {
5241 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5242 fname, lnum);
5243 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005244 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5245 && aff->af_needcomp == 0)
5246 {
5247 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5248 fname, lnum);
5249 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005250 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5251 && aff->af_comproot == 0)
5252 {
5253 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5254 fname, lnum);
5255 }
5256 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5257 && itemcnt == 2 && aff->af_compforbid == 0)
5258 {
5259 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5260 fname, lnum);
5261 }
5262 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5263 && itemcnt == 2 && aff->af_comppermit == 0)
5264 {
5265 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5266 fname, lnum);
5267 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005268 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005269 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005270 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005271 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005272 * "Na" into "Na+", "1234" into "1234+". */
5273 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005274 if (p != NULL)
5275 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005276 STRCPY(p, items[1]);
5277 STRCAT(p, "+");
5278 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005279 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005280 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005281 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005282 {
5283 /* Concatenate this string to previously defined ones, using a
5284 * slash to separate them. */
5285 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005286 if (compflags != NULL)
5287 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005288 p = getroom(spin, l, FALSE);
5289 if (p != NULL)
5290 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005291 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005292 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005293 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005294 STRCAT(p, "/");
5295 }
5296 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005297 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005298 }
5299 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005300 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005301 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005302 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005303 compmax = atoi((char *)items[1]);
5304 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005305 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005306 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005307 }
5308 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005309 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005310 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005311 compminlen = atoi((char *)items[1]);
5312 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005313 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5314 fname, lnum, items[1]);
5315 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005316 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005317 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005318 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005319 compsylmax = atoi((char *)items[1]);
5320 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005321 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5322 fname, lnum, items[1]);
5323 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005324 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5325 {
5326 compoptions |= COMP_CHECKDUP;
5327 }
5328 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5329 {
5330 compoptions |= COMP_CHECKREP;
5331 }
5332 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5333 {
5334 compoptions |= COMP_CHECKCASE;
5335 }
5336 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5337 && itemcnt == 1)
5338 {
5339 compoptions |= COMP_CHECKTRIPLE;
5340 }
5341 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5342 && itemcnt == 2)
5343 {
5344 if (atoi((char *)items[1]) == 0)
5345 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5346 fname, lnum, items[1]);
5347 }
5348 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5349 && itemcnt == 3)
5350 {
5351 garray_T *gap = &spin->si_comppat;
5352 int i;
5353
5354 /* Only add the couple if it isn't already there. */
5355 for (i = 0; i < gap->ga_len - 1; i += 2)
5356 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5357 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5358 items[2]) == 0)
5359 break;
5360 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5361 {
5362 ((char_u **)(gap->ga_data))[gap->ga_len++]
5363 = getroom_save(spin, items[1]);
5364 ((char_u **)(gap->ga_data))[gap->ga_len++]
5365 = getroom_save(spin, items[2]);
5366 }
5367 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005368 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005369 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005370 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005371 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005372 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005373 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5374 {
5375 spin->si_nobreak = TRUE;
5376 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005377 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5378 {
5379 spin->si_nosplitsugs = TRUE;
5380 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005381 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5382 {
5383 spin->si_nosugfile = TRUE;
5384 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005385 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5386 {
5387 aff->af_pfxpostpone = TRUE;
5388 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005389 else if ((STRCMP(items[0], "PFX") == 0
5390 || STRCMP(items[0], "SFX") == 0)
5391 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005392 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005393 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005394 int lasti = 4;
5395 char_u key[AH_KEY_LEN];
5396
5397 if (*items[0] == 'P')
5398 tp = &aff->af_pref;
5399 else
5400 tp = &aff->af_suff;
5401
5402 /* Myspell allows the same affix name to be used multiple
5403 * times. The affix files that do this have an undocumented
5404 * "S" flag on all but the last block, thus we check for that
5405 * and store it in ah_follows. */
5406 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5407 hi = hash_find(tp, key);
5408 if (!HASHITEM_EMPTY(hi))
5409 {
5410 cur_aff = HI2AH(hi);
5411 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5412 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5413 fname, lnum, items[1]);
5414 if (!cur_aff->ah_follows)
5415 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5416 fname, lnum, items[1]);
5417 }
5418 else
5419 {
5420 /* New affix letter. */
5421 cur_aff = (affheader_T *)getroom(spin,
5422 sizeof(affheader_T), TRUE);
5423 if (cur_aff == NULL)
5424 break;
5425 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5426 fname, lnum);
5427 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5428 break;
5429 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005430 || cur_aff->ah_flag == aff->af_rare
5431 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005432 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005433 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005434 || cur_aff->ah_flag == aff->af_needcomp
5435 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005436 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 +00005437 fname, lnum, items[1]);
5438 STRCPY(cur_aff->ah_key, items[1]);
5439 hash_add(tp, cur_aff->ah_key);
5440
5441 cur_aff->ah_combine = (*items[2] == 'Y');
5442 }
5443
5444 /* Check for the "S" flag, which apparently means that another
5445 * block with the same affix name is following. */
5446 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5447 {
5448 ++lasti;
5449 cur_aff->ah_follows = TRUE;
5450 }
5451 else
5452 cur_aff->ah_follows = FALSE;
5453
Bram Moolenaar8db73182005-06-17 21:51:16 +00005454 /* Myspell allows extra text after the item, but that might
5455 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005456 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005457 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005458
Bram Moolenaar95529562005-08-25 21:21:38 +00005459 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005460 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5461 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005462
Bram Moolenaar95529562005-08-25 21:21:38 +00005463 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005464 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005465 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005466 {
5467 /* Use a new number in the .spl file later, to be able
5468 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005469 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005470 cur_aff->ah_newID = ++spin->si_newprefID;
5471
5472 /* We only really use ah_newID if the prefix is
5473 * postponed. We know that only after handling all
5474 * the items. */
5475 did_postpone_prefix = FALSE;
5476 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005477 else
5478 /* Did use the ID in a previous block. */
5479 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005480 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005481
Bram Moolenaar51485f02005-06-04 21:55:20 +00005482 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005483 }
5484 else if ((STRCMP(items[0], "PFX") == 0
5485 || STRCMP(items[0], "SFX") == 0)
5486 && aff_todo > 0
5487 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005488 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005489 {
5490 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005491 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005492 int lasti = 5;
5493
Bram Moolenaar8db73182005-06-17 21:51:16 +00005494 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005495 * mean mistakes go unnoticed. Require a comment-starter.
5496 * Hunspell uses a "-" item. */
5497 if (itemcnt > lasti && *items[lasti] != '#'
5498 && (STRCMP(items[lasti], "-") != 0
5499 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005500 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005501
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005502 /* New item for an affix letter. */
5503 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005504 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005505 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005506 if (aff_entry == NULL)
5507 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005508
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005509 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005510 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005511 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005513 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005514
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005515 /* Recognize flags on the affix: abcd/1234 */
5516 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5517 if (aff_entry->ae_flags != NULL)
5518 *aff_entry->ae_flags++ = NUL;
5519 }
5520
Bram Moolenaar51485f02005-06-04 21:55:20 +00005521 /* Don't use an affix entry with non-ASCII characters when
5522 * "spin->si_ascii" is TRUE. */
5523 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005524 || has_non_ascii(aff_entry->ae_add)))
5525 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005526 aff_entry->ae_next = cur_aff->ah_first;
5527 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005528
5529 if (STRCMP(items[4], ".") != 0)
5530 {
5531 char_u buf[MAXLINELEN];
5532
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005533 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005534 if (*items[0] == 'P')
5535 sprintf((char *)buf, "^%s", items[4]);
5536 else
5537 sprintf((char *)buf, "%s$", items[4]);
5538 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005539 RE_MAGIC + RE_STRING + RE_STRICT);
5540 if (aff_entry->ae_prog == NULL)
5541 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5542 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005543 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005544
5545 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005546 * for the condition. Use an existing one if possible.
5547 * Can't be done for an affix with flags. */
5548 if (*items[0] == 'P' && aff->af_pfxpostpone
5549 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005550 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005551 /* When the chop string is one lower-case letter and
5552 * the add string ends in the upper-case letter we set
5553 * the "upper" flag, clear "ae_chop" and remove the
5554 * letters from "ae_add". The condition must either
5555 * be empty or start with the same letter. */
5556 if (aff_entry->ae_chop != NULL
5557 && aff_entry->ae_add != NULL
5558#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005559 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005560 aff_entry->ae_chop)] == NUL
5561#else
5562 && aff_entry->ae_chop[1] == NUL
5563#endif
5564 )
5565 {
5566 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005567
Bram Moolenaar53805d12005-08-01 07:08:33 +00005568 c = PTR2CHAR(aff_entry->ae_chop);
5569 c_up = SPELL_TOUPPER(c);
5570 if (c_up != c
5571 && (aff_entry->ae_cond == NULL
5572 || PTR2CHAR(aff_entry->ae_cond) == c))
5573 {
5574 p = aff_entry->ae_add
5575 + STRLEN(aff_entry->ae_add);
5576 mb_ptr_back(aff_entry->ae_add, p);
5577 if (PTR2CHAR(p) == c_up)
5578 {
5579 upper = TRUE;
5580 aff_entry->ae_chop = NULL;
5581 *p = NUL;
5582
5583 /* The condition is matched with the
5584 * actual word, thus must check for the
5585 * upper-case letter. */
5586 if (aff_entry->ae_cond != NULL)
5587 {
5588 char_u buf[MAXLINELEN];
5589#ifdef FEAT_MBYTE
5590 if (has_mbyte)
5591 {
5592 onecap_copy(items[4], buf, TRUE);
5593 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005594 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005595 }
5596 else
5597#endif
5598 *aff_entry->ae_cond = c_up;
5599 if (aff_entry->ae_cond != NULL)
5600 {
5601 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005602 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005603 vim_free(aff_entry->ae_prog);
5604 aff_entry->ae_prog = vim_regcomp(
5605 buf, RE_MAGIC + RE_STRING);
5606 }
5607 }
5608 }
5609 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005610 }
5611
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005612 if (aff_entry->ae_chop == NULL
5613 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005614 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005615 int idx;
5616 char_u **pp;
5617 int n;
5618
Bram Moolenaar6de68532005-08-24 22:08:48 +00005619 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005620 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5621 --idx)
5622 {
5623 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5624 if (str_equal(p, aff_entry->ae_cond))
5625 break;
5626 }
5627 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5628 {
5629 /* Not found, add a new condition. */
5630 idx = spin->si_prefcond.ga_len++;
5631 pp = ((char_u **)spin->si_prefcond.ga_data)
5632 + idx;
5633 if (aff_entry->ae_cond == NULL)
5634 *pp = NULL;
5635 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005636 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005637 aff_entry->ae_cond);
5638 }
5639
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005640 if (aff_entry->ae_flags != NULL)
5641 smsg((char_u *)_("Affix flags ignored when PFXPOSTPONE used in %s line %d: %s"),
5642 fname, lnum, items[4]);
5643
Bram Moolenaar53805d12005-08-01 07:08:33 +00005644 /* Add the prefix to the prefix tree. */
5645 if (aff_entry->ae_add == NULL)
5646 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005647 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005648 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005649
Bram Moolenaar53805d12005-08-01 07:08:33 +00005650 /* PFX_FLAGS is a negative number, so that
5651 * tree_add_word() knows this is the prefix tree. */
5652 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005653 if (!cur_aff->ah_combine)
5654 n |= WFP_NC;
5655 if (upper)
5656 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005657 tree_add_word(spin, p, spin->si_prefroot, n,
5658 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005659 did_postpone_prefix = TRUE;
5660 }
5661
5662 /* Didn't actually use ah_newID, backup si_newprefID. */
5663 if (aff_todo == 0 && !did_postpone_prefix)
5664 {
5665 --spin->si_newprefID;
5666 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005667 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005668 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005669 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005670 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005671 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5672 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005673 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005674 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005675 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005676 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5677 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005678 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005679 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005680 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005681 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5682 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005683 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005684 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005685 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005686 else if ((STRCMP(items[0], "REP") == 0
5687 || STRCMP(items[0], "REPSAL") == 0)
5688 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005689 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005690 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005691 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005692 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005693 fname, lnum);
5694 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005695 else if ((STRCMP(items[0], "REP") == 0
5696 || STRCMP(items[0], "REPSAL") == 0)
5697 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005698 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005699 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005700 /* Myspell ignores extra arguments, we require it starts with
5701 * # to detect mistakes. */
5702 if (itemcnt > 3 && items[3][0] != '#')
5703 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005704 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005705 {
5706 /* Replace underscore with space (can't include a space
5707 * directly). */
5708 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5709 if (*p == '_')
5710 *p = ' ';
5711 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5712 if (*p == '_')
5713 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005714 add_fromto(spin, items[0][3] == 'S'
5715 ? &spin->si_repsal
5716 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005718 }
5719 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5720 {
5721 /* MAP item or count */
5722 if (!found_map)
5723 {
5724 /* First line contains the count. */
5725 found_map = TRUE;
5726 if (!isdigit(*items[1]))
5727 smsg((char_u *)_("Expected MAP count in %s line %d"),
5728 fname, lnum);
5729 }
5730 else if (do_map)
5731 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005732 int c;
5733
5734 /* Check that every character appears only once. */
5735 for (p = items[1]; *p != NUL; )
5736 {
5737#ifdef FEAT_MBYTE
5738 c = mb_ptr2char_adv(&p);
5739#else
5740 c = *p++;
5741#endif
5742 if ((spin->si_map.ga_len > 0
5743 && vim_strchr(spin->si_map.ga_data, c)
5744 != NULL)
5745 || vim_strchr(p, c) != NULL)
5746 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5747 fname, lnum);
5748 }
5749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005750 /* We simply concatenate all the MAP strings, separated by
5751 * slashes. */
5752 ga_concat(&spin->si_map, items[1]);
5753 ga_append(&spin->si_map, '/');
5754 }
5755 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005756 /* Accept "SAL from to" and "SAL from to # comment". */
5757 else if (STRCMP(items[0], "SAL") == 0
5758 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005759 {
5760 if (do_sal)
5761 {
5762 /* SAL item (sounds-a-like)
5763 * Either one of the known keys or a from-to pair. */
5764 if (STRCMP(items[1], "followup") == 0)
5765 spin->si_followup = sal_to_bool(items[2]);
5766 else if (STRCMP(items[1], "collapse_result") == 0)
5767 spin->si_collapse = sal_to_bool(items[2]);
5768 else if (STRCMP(items[1], "remove_accents") == 0)
5769 spin->si_rem_accents = sal_to_bool(items[2]);
5770 else
5771 /* when "to" is "_" it means empty */
5772 add_fromto(spin, &spin->si_sal, items[1],
5773 STRCMP(items[2], "_") == 0 ? (char_u *)""
5774 : items[2]);
5775 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005776 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005777 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005778 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005779 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005780 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005781 }
5782 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005783 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005784 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005785 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005786 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005787 else if (STRCMP(items[0], "COMMON") == 0)
5788 {
5789 int i;
5790
5791 for (i = 1; i < itemcnt; ++i)
5792 {
5793 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5794 items[i])))
5795 {
5796 p = vim_strsave(items[i]);
5797 if (p == NULL)
5798 break;
5799 hash_add(&spin->si_commonwords, p);
5800 }
5801 }
5802 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005803 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005804 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005805 fname, lnum, items[0]);
5806 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005807 }
5808
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005809 if (fol != NULL || low != NULL || upp != NULL)
5810 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005811 if (spin->si_clear_chartab)
5812 {
5813 /* Clear the char type tables, don't want to use any of the
5814 * currently used spell properties. */
5815 init_spell_chartab();
5816 spin->si_clear_chartab = FALSE;
5817 }
5818
Bram Moolenaar3982c542005-06-08 21:56:31 +00005819 /*
5820 * Don't write a word table for an ASCII file, so that we don't check
5821 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005822 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005823 * mb_get_class(), the list of chars in the file will be incomplete.
5824 */
5825 if (!spin->si_ascii
5826#ifdef FEAT_MBYTE
5827 && !enc_utf8
5828#endif
5829 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005830 {
5831 if (fol == NULL || low == NULL || upp == NULL)
5832 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5833 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005834 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005835 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005836
5837 vim_free(fol);
5838 vim_free(low);
5839 vim_free(upp);
5840 }
5841
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005842 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005843 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005844 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005845 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005846 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005847 }
5848
Bram Moolenaar6de68532005-08-24 22:08:48 +00005849 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005850 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005851 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5852 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005853 }
5854
Bram Moolenaar6de68532005-08-24 22:08:48 +00005855 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005856 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005857 if (syllable == NULL)
5858 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5859 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5860 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005861 }
5862
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005863 if (compoptions != 0)
5864 {
5865 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5866 spin->si_compoptions |= compoptions;
5867 }
5868
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 if (compflags != NULL)
5870 process_compflags(spin, aff, compflags);
5871
5872 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005873 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005874 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005875 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005876 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005877 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005878 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005879 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005880 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005881 }
5882
Bram Moolenaar6de68532005-08-24 22:08:48 +00005883 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005884 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005885 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5886 spin->si_syllable = syllable;
5887 }
5888
5889 if (sofofrom != NULL || sofoto != NULL)
5890 {
5891 if (sofofrom == NULL || sofoto == NULL)
5892 smsg((char_u *)_("Missing SOFO%s line in %s"),
5893 sofofrom == NULL ? "FROM" : "TO", fname);
5894 else if (spin->si_sal.ga_len > 0)
5895 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005896 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005897 {
5898 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5899 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5900 spin->si_sofofr = sofofrom;
5901 spin->si_sofoto = sofoto;
5902 }
5903 }
5904
5905 if (midword != NULL)
5906 {
5907 aff_check_string(spin->si_midword, midword, "MIDWORD");
5908 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005909 }
5910
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005911 vim_free(pc);
5912 fclose(fd);
5913 return aff;
5914}
5915
5916/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005917 * Return TRUE if "s" is the name of an info item in the affix file.
5918 */
5919 static int
5920spell_info_item(s)
5921 char_u *s;
5922{
5923 return STRCMP(s, "NAME") == 0
5924 || STRCMP(s, "HOME") == 0
5925 || STRCMP(s, "VERSION") == 0
5926 || STRCMP(s, "AUTHOR") == 0
5927 || STRCMP(s, "EMAIL") == 0
5928 || STRCMP(s, "COPYRIGHT") == 0;
5929}
5930
5931/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005932 * Turn an affix flag name into a number, according to the FLAG type.
5933 * returns zero for failure.
5934 */
5935 static unsigned
5936affitem2flag(flagtype, item, fname, lnum)
5937 int flagtype;
5938 char_u *item;
5939 char_u *fname;
5940 int lnum;
5941{
5942 unsigned res;
5943 char_u *p = item;
5944
5945 res = get_affitem(flagtype, &p);
5946 if (res == 0)
5947 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005948 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005949 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5950 fname, lnum, item);
5951 else
5952 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5953 fname, lnum, item);
5954 }
5955 if (*p != NUL)
5956 {
5957 smsg((char_u *)_(e_affname), fname, lnum, item);
5958 return 0;
5959 }
5960
5961 return res;
5962}
5963
5964/*
5965 * Get one affix name from "*pp" and advance the pointer.
5966 * Returns zero for an error, still advances the pointer then.
5967 */
5968 static unsigned
5969get_affitem(flagtype, pp)
5970 int flagtype;
5971 char_u **pp;
5972{
5973 int res;
5974
Bram Moolenaar95529562005-08-25 21:21:38 +00005975 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005976 {
5977 if (!VIM_ISDIGIT(**pp))
5978 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005979 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005980 return 0;
5981 }
5982 res = getdigits(pp);
5983 }
5984 else
5985 {
5986#ifdef FEAT_MBYTE
5987 res = mb_ptr2char_adv(pp);
5988#else
5989 res = *(*pp)++;
5990#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005991 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005992 && res >= 'A' && res <= 'Z'))
5993 {
5994 if (**pp == NUL)
5995 return 0;
5996#ifdef FEAT_MBYTE
5997 res = mb_ptr2char_adv(pp) + (res << 16);
5998#else
5999 res = *(*pp)++ + (res << 16);
6000#endif
6001 }
6002 }
6003 return res;
6004}
6005
6006/*
6007 * Process the "compflags" string used in an affix file and append it to
6008 * spin->si_compflags.
6009 * The processing involves changing the affix names to ID numbers, so that
6010 * they fit in one byte.
6011 */
6012 static void
6013process_compflags(spin, aff, compflags)
6014 spellinfo_T *spin;
6015 afffile_T *aff;
6016 char_u *compflags;
6017{
6018 char_u *p;
6019 char_u *prevp;
6020 unsigned flag;
6021 compitem_T *ci;
6022 int id;
6023 int len;
6024 char_u *tp;
6025 char_u key[AH_KEY_LEN];
6026 hashitem_T *hi;
6027
6028 /* Make room for the old and the new compflags, concatenated with a / in
6029 * between. Processing it makes it shorter, but we don't know by how
6030 * much, thus allocate the maximum. */
6031 len = STRLEN(compflags) + 1;
6032 if (spin->si_compflags != NULL)
6033 len += STRLEN(spin->si_compflags) + 1;
6034 p = getroom(spin, len, FALSE);
6035 if (p == NULL)
6036 return;
6037 if (spin->si_compflags != NULL)
6038 {
6039 STRCPY(p, spin->si_compflags);
6040 STRCAT(p, "/");
6041 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006042 spin->si_compflags = p;
6043 tp = p + STRLEN(p);
6044
6045 for (p = compflags; *p != NUL; )
6046 {
6047 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6048 /* Copy non-flag characters directly. */
6049 *tp++ = *p++;
6050 else
6051 {
6052 /* First get the flag number, also checks validity. */
6053 prevp = p;
6054 flag = get_affitem(aff->af_flagtype, &p);
6055 if (flag != 0)
6056 {
6057 /* Find the flag in the hashtable. If it was used before, use
6058 * the existing ID. Otherwise add a new entry. */
6059 vim_strncpy(key, prevp, p - prevp);
6060 hi = hash_find(&aff->af_comp, key);
6061 if (!HASHITEM_EMPTY(hi))
6062 id = HI2CI(hi)->ci_newID;
6063 else
6064 {
6065 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6066 if (ci == NULL)
6067 break;
6068 STRCPY(ci->ci_key, key);
6069 ci->ci_flag = flag;
6070 /* Avoid using a flag ID that has a special meaning in a
6071 * regexp (also inside []). */
6072 do
6073 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006074 check_renumber(spin);
6075 id = spin->si_newcompID--;
6076 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006077 ci->ci_newID = id;
6078 hash_add(&aff->af_comp, ci->ci_key);
6079 }
6080 *tp++ = id;
6081 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006082 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006083 ++p;
6084 }
6085 }
6086
6087 *tp = NUL;
6088}
6089
6090/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006091 * Check that the new IDs for postponed affixes and compounding don't overrun
6092 * each other. We have almost 255 available, but start at 0-127 to avoid
6093 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6094 * When that is used up an error message is given.
6095 */
6096 static void
6097check_renumber(spin)
6098 spellinfo_T *spin;
6099{
6100 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6101 {
6102 spin->si_newprefID = 127;
6103 spin->si_newcompID = 255;
6104 }
6105}
6106
6107/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006108 * Return TRUE if flag "flag" appears in affix list "afflist".
6109 */
6110 static int
6111flag_in_afflist(flagtype, afflist, flag)
6112 int flagtype;
6113 char_u *afflist;
6114 unsigned flag;
6115{
6116 char_u *p;
6117 unsigned n;
6118
6119 switch (flagtype)
6120 {
6121 case AFT_CHAR:
6122 return vim_strchr(afflist, flag) != NULL;
6123
Bram Moolenaar95529562005-08-25 21:21:38 +00006124 case AFT_CAPLONG:
6125 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006126 for (p = afflist; *p != NUL; )
6127 {
6128#ifdef FEAT_MBYTE
6129 n = mb_ptr2char_adv(&p);
6130#else
6131 n = *p++;
6132#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006133 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006134 && *p != NUL)
6135#ifdef FEAT_MBYTE
6136 n = mb_ptr2char_adv(&p) + (n << 16);
6137#else
6138 n = *p++ + (n << 16);
6139#endif
6140 if (n == flag)
6141 return TRUE;
6142 }
6143 break;
6144
Bram Moolenaar95529562005-08-25 21:21:38 +00006145 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006146 for (p = afflist; *p != NUL; )
6147 {
6148 n = getdigits(&p);
6149 if (n == flag)
6150 return TRUE;
6151 if (*p != NUL) /* skip over comma */
6152 ++p;
6153 }
6154 break;
6155 }
6156 return FALSE;
6157}
6158
6159/*
6160 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6161 */
6162 static void
6163aff_check_number(spinval, affval, name)
6164 int spinval;
6165 int affval;
6166 char *name;
6167{
6168 if (spinval != 0 && spinval != affval)
6169 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6170}
6171
6172/*
6173 * Give a warning when "spinval" and "affval" strings are set and not the same.
6174 */
6175 static void
6176aff_check_string(spinval, affval, name)
6177 char_u *spinval;
6178 char_u *affval;
6179 char *name;
6180{
6181 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6182 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6183}
6184
6185/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006186 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6187 * NULL as equal.
6188 */
6189 static int
6190str_equal(s1, s2)
6191 char_u *s1;
6192 char_u *s2;
6193{
6194 if (s1 == NULL || s2 == NULL)
6195 return s1 == s2;
6196 return STRCMP(s1, s2) == 0;
6197}
6198
6199/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006200 * Add a from-to item to "gap". Used for REP and SAL items.
6201 * They are stored case-folded.
6202 */
6203 static void
6204add_fromto(spin, gap, from, to)
6205 spellinfo_T *spin;
6206 garray_T *gap;
6207 char_u *from;
6208 char_u *to;
6209{
6210 fromto_T *ftp;
6211 char_u word[MAXWLEN];
6212
6213 if (ga_grow(gap, 1) == OK)
6214 {
6215 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
6216 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006217 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006218 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006219 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006220 ++gap->ga_len;
6221 }
6222}
6223
6224/*
6225 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6226 */
6227 static int
6228sal_to_bool(s)
6229 char_u *s;
6230{
6231 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6232}
6233
6234/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006235 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6236 * When "s" is NULL FALSE is returned.
6237 */
6238 static int
6239has_non_ascii(s)
6240 char_u *s;
6241{
6242 char_u *p;
6243
6244 if (s != NULL)
6245 for (p = s; *p != NUL; ++p)
6246 if (*p >= 128)
6247 return TRUE;
6248 return FALSE;
6249}
6250
6251/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006252 * Free the structure filled by spell_read_aff().
6253 */
6254 static void
6255spell_free_aff(aff)
6256 afffile_T *aff;
6257{
6258 hashtab_T *ht;
6259 hashitem_T *hi;
6260 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006261 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006262 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006263
6264 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006265
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006266 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006267 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6268 {
6269 todo = ht->ht_used;
6270 for (hi = ht->ht_array; todo > 0; ++hi)
6271 {
6272 if (!HASHITEM_EMPTY(hi))
6273 {
6274 --todo;
6275 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006276 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6277 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006278 }
6279 }
6280 if (ht == &aff->af_suff)
6281 break;
6282 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006283
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006284 hash_clear(&aff->af_pref);
6285 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006286 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006287}
6288
6289/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006290 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006291 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006292 */
6293 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006294spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006295 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006296 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006297 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006298{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006299 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006300 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006301 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006302 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006303 char_u store_afflist[MAXWLEN];
6304 int pfxlen;
6305 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006306 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006307 char_u *pc;
6308 char_u *w;
6309 int l;
6310 hash_T hash;
6311 hashitem_T *hi;
6312 FILE *fd;
6313 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006314 int non_ascii = 0;
6315 int retval = OK;
6316 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006317 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006318 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006319
Bram Moolenaar51485f02005-06-04 21:55:20 +00006320 /*
6321 * Open the file.
6322 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006323 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006324 if (fd == NULL)
6325 {
6326 EMSG2(_(e_notopen), fname);
6327 return FAIL;
6328 }
6329
Bram Moolenaar51485f02005-06-04 21:55:20 +00006330 /* The hashtable is only used to detect duplicated words. */
6331 hash_init(&ht);
6332
Bram Moolenaar4770d092006-01-12 23:22:24 +00006333 vim_snprintf((char *)IObuff, IOSIZE,
6334 _("Reading dictionary file %s ..."), fname);
6335 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006336
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006337 /* start with a message for the first line */
6338 spin->si_msg_count = 999999;
6339
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006340 /* Read and ignore the first line: word count. */
6341 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006342 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006343 EMSG2(_("E760: No word count in %s"), fname);
6344
6345 /*
6346 * Read all the lines in the file one by one.
6347 * The words are converted to 'encoding' here, before being added to
6348 * the hashtable.
6349 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006350 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006351 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006352 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006353 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006354 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006355 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006356
Bram Moolenaar51485f02005-06-04 21:55:20 +00006357 /* Remove CR, LF and white space from the end. White space halfway
6358 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006359 l = STRLEN(line);
6360 while (l > 0 && line[l - 1] <= ' ')
6361 --l;
6362 if (l == 0)
6363 continue; /* empty line */
6364 line[l] = NUL;
6365
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006366 /* Truncate the word at the "/", set "afflist" to what follows.
6367 * Replace "\/" by "/" and "\\" by "\". */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006368 afflist = NULL;
6369 for (p = line; *p != NUL; mb_ptr_adv(p))
6370 {
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006371 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6372 mch_memmove(p, p + 1, STRLEN(p));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006373 else if (*p == '/')
6374 {
6375 *p = NUL;
6376 afflist = p + 1;
6377 break;
6378 }
6379 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006380
6381 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6382 if (spin->si_ascii && has_non_ascii(line))
6383 {
6384 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006385 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006386 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00006387
Bram Moolenaarb765d632005-06-07 21:00:02 +00006388#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006389 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006390 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006391 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006392 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006393 if (pc == NULL)
6394 {
6395 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6396 fname, lnum, line);
6397 continue;
6398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006399 w = pc;
6400 }
6401 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006402#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006403 {
6404 pc = NULL;
6405 w = line;
6406 }
6407
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006408 /* This takes time, print a message every 10000 words. */
6409 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006410 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006411 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006412 vim_snprintf((char *)message, sizeof(message),
6413 _("line %6d, word %6d - %s"),
6414 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6415 msg_start();
6416 msg_puts_long_attr(message, 0);
6417 msg_clr_eos();
6418 msg_didout = FALSE;
6419 msg_col = 0;
6420 out_flush();
6421 }
6422
Bram Moolenaar51485f02005-06-04 21:55:20 +00006423 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006424 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006425 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006426 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006427 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006428 if (retval == FAIL)
6429 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006430
Bram Moolenaar51485f02005-06-04 21:55:20 +00006431 hash = hash_hash(dw);
6432 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006433 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006434 {
6435 if (p_verbose > 0)
6436 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006437 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006438 else if (duplicate == 0)
6439 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6440 fname, lnum, dw);
6441 ++duplicate;
6442 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006443 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006444 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006445
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006446 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006447 store_afflist[0] = NUL;
6448 pfxlen = 0;
6449 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006450 if (afflist != NULL)
6451 {
6452 /* Check for affix name that stands for keep-case word and stands
6453 * for rare word (if defined). */
Bram Moolenaar371baa92005-12-29 22:43:53 +00006454 if (affile->af_keepcase != 0 && flag_in_afflist(
6455 affile->af_flagtype, afflist, affile->af_keepcase))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006456 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar371baa92005-12-29 22:43:53 +00006457 if (affile->af_rare != 0 && flag_in_afflist(
6458 affile->af_flagtype, afflist, affile->af_rare))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006459 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006460 if (affile->af_bad != 0 && flag_in_afflist(
6461 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006462 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006463 if (affile->af_needaffix != 0 && flag_in_afflist(
6464 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006465 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006466 if (affile->af_needcomp != 0 && flag_in_afflist(
6467 affile->af_flagtype, afflist, affile->af_needcomp))
6468 flags |= WF_NEEDCOMP;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006469 if (affile->af_comproot != 0 && flag_in_afflist(
6470 affile->af_flagtype, afflist, affile->af_comproot))
6471 flags |= WF_COMPROOT;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006472 if (affile->af_nosuggest != 0 && flag_in_afflist(
6473 affile->af_flagtype, afflist, affile->af_nosuggest))
6474 flags |= WF_NOSUGGEST;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006475
6476 if (affile->af_pfxpostpone)
6477 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006478 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006479
Bram Moolenaar5195e452005-08-19 20:32:47 +00006480 if (spin->si_compflags != NULL)
6481 /* Need to store the list of compound flags with the word.
6482 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006483 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006484 }
6485
Bram Moolenaar51485f02005-06-04 21:55:20 +00006486 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006487 if (store_word(spin, dw, flags, spin->si_region,
6488 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006489 retval = FAIL;
6490
6491 if (afflist != NULL)
6492 {
6493 /* Find all matching suffixes and add the resulting words.
6494 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006495 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006496 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006497 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006498 retval = FAIL;
6499
6500 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006501 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006502 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006503 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006504 retval = FAIL;
6505 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006506 }
6507
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006508 if (duplicate > 0)
6509 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006510 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006511 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6512 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006513 hash_clear(&ht);
6514
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006515 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516 return retval;
6517}
6518
6519/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006520 * Get the list of prefix IDs from the affix list "afflist".
6521 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006522 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6523 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006524 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006525 static int
6526get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006527 afffile_T *affile;
6528 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006529 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006530{
6531 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006532 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006533 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006534 int id;
6535 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006536 hashitem_T *hi;
6537
Bram Moolenaar6de68532005-08-24 22:08:48 +00006538 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006539 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006540 prevp = p;
6541 if (get_affitem(affile->af_flagtype, &p) != 0)
6542 {
6543 /* A flag is a postponed prefix flag if it appears in "af_pref"
6544 * and it's ID is not zero. */
6545 vim_strncpy(key, prevp, p - prevp);
6546 hi = hash_find(&affile->af_pref, key);
6547 if (!HASHITEM_EMPTY(hi))
6548 {
6549 id = HI2AH(hi)->ah_newID;
6550 if (id != 0)
6551 store_afflist[cnt++] = id;
6552 }
6553 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006554 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006555 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006556 }
6557
Bram Moolenaar5195e452005-08-19 20:32:47 +00006558 store_afflist[cnt] = NUL;
6559 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006560}
6561
6562/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006563 * Get the list of compound IDs from the affix list "afflist" that are used
6564 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006565 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006566 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006567 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006568get_compflags(affile, afflist, store_afflist)
6569 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006570 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006571 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006572{
6573 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006574 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006575 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006576 char_u key[AH_KEY_LEN];
6577 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006578
Bram Moolenaar6de68532005-08-24 22:08:48 +00006579 for (p = afflist; *p != NUL; )
6580 {
6581 prevp = p;
6582 if (get_affitem(affile->af_flagtype, &p) != 0)
6583 {
6584 /* A flag is a compound flag if it appears in "af_comp". */
6585 vim_strncpy(key, prevp, p - prevp);
6586 hi = hash_find(&affile->af_comp, key);
6587 if (!HASHITEM_EMPTY(hi))
6588 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6589 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006590 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006591 ++p;
6592 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006593
Bram Moolenaar5195e452005-08-19 20:32:47 +00006594 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006595}
6596
6597/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006598 * Apply affixes to a word and store the resulting words.
6599 * "ht" is the hashtable with affentry_T that need to be applied, either
6600 * prefixes or suffixes.
6601 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6602 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006603 *
6604 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006605 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006606 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006607store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
6608 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006609 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006610 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006611 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006612 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006613 hashtab_T *ht;
6614 hashtab_T *xht;
6615 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006616 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006617 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006618 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6619 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006620{
6621 int todo;
6622 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 affheader_T *ah;
6624 affentry_T *ae;
6625 regmatch_T regmatch;
6626 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006627 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006628 int i;
6629 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006630 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006631 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006632 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006633 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006634
Bram Moolenaar51485f02005-06-04 21:55:20 +00006635 todo = ht->ht_used;
6636 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006637 {
6638 if (!HASHITEM_EMPTY(hi))
6639 {
6640 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006641 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006642
Bram Moolenaar51485f02005-06-04 21:55:20 +00006643 /* Check that the affix combines, if required, and that the word
6644 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006645 if ((!comb || ah->ah_combine) && flag_in_afflist(
6646 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006647 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006648 /* Loop over all affix entries with this name. */
6649 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006650 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006651 /* Check the condition. It's not logical to match case
6652 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006653 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006654 * Another requirement from Myspell is that the chop
6655 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006656 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006657 * prefixes with a chop string and/or flags. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006658 regmatch.regprog = ae->ae_prog;
6659 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006660 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006661 || ae->ae_chop != NULL
6662 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006663 && (ae->ae_chop == NULL
6664 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006665 && (ae->ae_prog == NULL
6666 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006667 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006668 /* Match. Remove the chop and add the affix. */
6669 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006670 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006671 /* prefix: chop/add at the start of the word */
6672 if (ae->ae_add == NULL)
6673 *newword = NUL;
6674 else
6675 STRCPY(newword, ae->ae_add);
6676 p = word;
6677 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006678 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006679 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006680#ifdef FEAT_MBYTE
6681 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006682 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006683 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006684 for ( ; i > 0; --i)
6685 mb_ptr_adv(p);
6686 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006687 else
6688#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006689 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006690 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006691 STRCAT(newword, p);
6692 }
6693 else
6694 {
6695 /* suffix: chop/add at the end of the word */
6696 STRCPY(newword, word);
6697 if (ae->ae_chop != NULL)
6698 {
6699 /* Remove chop string. */
6700 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006701 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006702 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006703 mb_ptr_back(newword, p);
6704 *p = NUL;
6705 }
6706 if (ae->ae_add != NULL)
6707 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006708 }
6709
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006710 /* Obey the "rare" flag of the affix. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006711 if (affile->af_rare != 0
6712 && ae->ae_flags != NULL
6713 && flag_in_afflist(
6714 affile->af_flagtype, ae->ae_flags,
6715 affile->af_rare))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006716 use_flags = flags | WF_RARE;
6717 else
6718 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006719
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006720 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006721 * use the compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006722 use_pfxlist = pfxlist;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006723 if (pfxlist != NULL
6724 && affile->af_compforbid != 0
6725 && ae->ae_flags != NULL
6726 && flag_in_afflist(
6727 affile->af_flagtype, ae->ae_flags,
6728 affile->af_compforbid))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006729 {
6730 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6731 use_pfxlist = pfx_pfxlist;
6732 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006733
6734 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006735 if (spin->si_prefroot != NULL
6736 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006737 {
6738 /* ... add a flag to indicate an affix was used. */
6739 use_flags |= WF_HAS_AFF;
6740
6741 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006742 * affixes is not allowed. But do use the
6743 * compound flags after them. */
6744 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6745 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006746 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006747
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006748 /* When compounding is supported and there is no
6749 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6750 * side where the affix is applied. */
6751 if (spin->si_compflags != NULL
6752 && (affile->af_comppermit == 0
6753 || ae->ae_flags == NULL
6754 || !flag_in_afflist(
6755 affile->af_flagtype, ae->ae_flags,
6756 affile->af_comppermit)))
6757 {
6758 if (xht != NULL)
6759 use_flags |= WF_NOCOMPAFT;
6760 else
6761 use_flags |= WF_NOCOMPBEF;
6762 }
6763
Bram Moolenaar51485f02005-06-04 21:55:20 +00006764 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006765 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006766 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006767 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006768
Bram Moolenaar51485f02005-06-04 21:55:20 +00006769 /* When added a suffix and combining is allowed also
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006770 * try adding prefixes additionally. RECURSIVE! */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006771 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006772 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006773 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006774 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006775 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006776 }
6777 }
6778 }
6779 }
6780 }
6781
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006782 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006783}
6784
6785/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006786 * Read a file with a list of words.
6787 */
6788 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006789spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006790 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006791 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006792{
6793 FILE *fd;
6794 long lnum = 0;
6795 char_u rline[MAXLINELEN];
6796 char_u *line;
6797 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006798 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006799 int l;
6800 int retval = OK;
6801 int did_word = FALSE;
6802 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006803 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006804 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006805
6806 /*
6807 * Open the file.
6808 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006809 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006810 if (fd == NULL)
6811 {
6812 EMSG2(_(e_notopen), fname);
6813 return FAIL;
6814 }
6815
Bram Moolenaar4770d092006-01-12 23:22:24 +00006816 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6817 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006818
6819 /*
6820 * Read all the lines in the file one by one.
6821 */
6822 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6823 {
6824 line_breakcheck();
6825 ++lnum;
6826
6827 /* Skip comment lines. */
6828 if (*rline == '#')
6829 continue;
6830
6831 /* Remove CR, LF and white space from the end. */
6832 l = STRLEN(rline);
6833 while (l > 0 && rline[l - 1] <= ' ')
6834 --l;
6835 if (l == 0)
6836 continue; /* empty or blank line */
6837 rline[l] = NUL;
6838
6839 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6840 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006841#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006842 if (spin->si_conv.vc_type != CONV_NONE)
6843 {
6844 pc = string_convert(&spin->si_conv, rline, NULL);
6845 if (pc == NULL)
6846 {
6847 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6848 fname, lnum, rline);
6849 continue;
6850 }
6851 line = pc;
6852 }
6853 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006854#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006855 {
6856 pc = NULL;
6857 line = rline;
6858 }
6859
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006860 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006861 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006862 ++line;
6863 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006864 {
6865 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006866 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6867 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006868 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006869 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6870 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006871 else
6872 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006873#ifdef FEAT_MBYTE
6874 char_u *enc;
6875
Bram Moolenaar51485f02005-06-04 21:55:20 +00006876 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006877 line += 10;
6878 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006879 if (enc != NULL && !spin->si_ascii
6880 && convert_setup(&spin->si_conv, enc,
6881 p_enc) == FAIL)
6882 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006883 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006884 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006885 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006886#else
6887 smsg((char_u *)_("Conversion in %s not supported"), fname);
6888#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006889 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006890 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006891 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006892
Bram Moolenaar3982c542005-06-08 21:56:31 +00006893 if (STRNCMP(line, "regions=", 8) == 0)
6894 {
6895 if (spin->si_region_count > 1)
6896 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6897 fname, lnum, line);
6898 else
6899 {
6900 line += 8;
6901 if (STRLEN(line) > 16)
6902 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6903 fname, lnum, line);
6904 else
6905 {
6906 spin->si_region_count = STRLEN(line) / 2;
6907 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006908
6909 /* Adjust the mask for a word valid in all regions. */
6910 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006911 }
6912 }
6913 continue;
6914 }
6915
Bram Moolenaar7887d882005-07-01 22:33:52 +00006916 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6917 fname, lnum, line - 1);
6918 continue;
6919 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006920
Bram Moolenaar7887d882005-07-01 22:33:52 +00006921 flags = 0;
6922 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006923
Bram Moolenaar7887d882005-07-01 22:33:52 +00006924 /* Check for flags and region after a slash. */
6925 p = vim_strchr(line, '/');
6926 if (p != NULL)
6927 {
6928 *p++ = NUL;
6929 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006930 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006931 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006932 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006933 else if (*p == '!') /* Bad, bad, wicked word. */
6934 flags |= WF_BANNED;
6935 else if (*p == '?') /* Rare word. */
6936 flags |= WF_RARE;
6937 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006938 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006939 if ((flags & WF_REGION) == 0) /* first one */
6940 regionmask = 0;
6941 flags |= WF_REGION;
6942
6943 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006944 if (l > spin->si_region_count)
6945 {
6946 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006947 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006948 break;
6949 }
6950 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006951 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006952 else
6953 {
6954 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6955 fname, lnum, p);
6956 break;
6957 }
6958 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006959 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006960 }
6961
6962 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6963 if (spin->si_ascii && has_non_ascii(line))
6964 {
6965 ++non_ascii;
6966 continue;
6967 }
6968
6969 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006970 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006971 {
6972 retval = FAIL;
6973 break;
6974 }
6975 did_word = TRUE;
6976 }
6977
6978 vim_free(pc);
6979 fclose(fd);
6980
Bram Moolenaar4770d092006-01-12 23:22:24 +00006981 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006982 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006983 vim_snprintf((char *)IObuff, IOSIZE,
6984 _("Ignored %d words with non-ASCII characters"), non_ascii);
6985 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006986 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006987
Bram Moolenaar51485f02005-06-04 21:55:20 +00006988 return retval;
6989}
6990
6991/*
6992 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006993 * This avoids calling free() for every little struct we use (and keeping
6994 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006995 * The memory is cleared to all zeros.
6996 * Returns NULL when out of memory.
6997 */
6998 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006999getroom(spin, len, align)
7000 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007001 size_t len; /* length needed */
7002 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007003{
7004 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007005 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007006
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007007 if (align && bl != NULL)
7008 /* Round size up for alignment. On some systems structures need to be
7009 * aligned to the size of a pointer (e.g., SPARC). */
7010 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7011 & ~(sizeof(char *) - 1);
7012
Bram Moolenaar51485f02005-06-04 21:55:20 +00007013 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7014 {
7015 /* Allocate a block of memory. This is not freed until much later. */
7016 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7017 if (bl == NULL)
7018 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007019 bl->sb_next = spin->si_blocks;
7020 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007021 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007022 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007023 }
7024
7025 p = bl->sb_data + bl->sb_used;
7026 bl->sb_used += len;
7027
7028 return p;
7029}
7030
7031/*
7032 * Make a copy of a string into memory allocated with getroom().
7033 */
7034 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007035getroom_save(spin, s)
7036 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007037 char_u *s;
7038{
7039 char_u *sc;
7040
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007041 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007042 if (sc != NULL)
7043 STRCPY(sc, s);
7044 return sc;
7045}
7046
7047
7048/*
7049 * Free the list of allocated sblock_T.
7050 */
7051 static void
7052free_blocks(bl)
7053 sblock_T *bl;
7054{
7055 sblock_T *next;
7056
7057 while (bl != NULL)
7058 {
7059 next = bl->sb_next;
7060 vim_free(bl);
7061 bl = next;
7062 }
7063}
7064
7065/*
7066 * Allocate the root of a word tree.
7067 */
7068 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007069wordtree_alloc(spin)
7070 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007071{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007072 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007073}
7074
7075/*
7076 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007077 * Always store it in the case-folded tree. For a keep-case word this is
7078 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7079 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007080 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007081 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7082 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007083 */
7084 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007085store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007086 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007087 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007088 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007089 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007090 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007091 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007092{
7093 int len = STRLEN(word);
7094 int ct = captype(word, word + len);
7095 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007096 int res = OK;
7097 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007100 for (p = pfxlist; res == OK; ++p)
7101 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007102 if (!need_affix || (p != NULL && *p != NUL))
7103 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007104 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007105 if (p == NULL || *p == NUL)
7106 break;
7107 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007108 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007109
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007110 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007111 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007112 for (p = pfxlist; res == OK; ++p)
7113 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007114 if (!need_affix || (p != NULL && *p != NUL))
7115 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007116 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007117 if (p == NULL || *p == NUL)
7118 break;
7119 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007120 ++spin->si_keepwcount;
7121 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007122 return res;
7123}
7124
7125/*
7126 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007127 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007128 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007129 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007130 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007131 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007132tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007133 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007134 char_u *word;
7135 wordnode_T *root;
7136 int flags;
7137 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007138 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007139{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007140 wordnode_T *node = root;
7141 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007142 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007143 wordnode_T **prev = NULL;
7144 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007145
Bram Moolenaar51485f02005-06-04 21:55:20 +00007146 /* Add each byte of the word to the tree, including the NUL at the end. */
7147 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007148 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007149 /* When there is more than one reference to this node we need to make
7150 * a copy, so that we can modify it. Copy the whole list of siblings
7151 * (we don't optimize for a partly shared list of siblings). */
7152 if (node != NULL && node->wn_refs > 1)
7153 {
7154 --node->wn_refs;
7155 copyprev = prev;
7156 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7157 {
7158 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007159 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007160 if (np == NULL)
7161 return FAIL;
7162 np->wn_child = copyp->wn_child;
7163 if (np->wn_child != NULL)
7164 ++np->wn_child->wn_refs; /* child gets extra ref */
7165 np->wn_byte = copyp->wn_byte;
7166 if (np->wn_byte == NUL)
7167 {
7168 np->wn_flags = copyp->wn_flags;
7169 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007170 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007171 }
7172
7173 /* Link the new node in the list, there will be one ref. */
7174 np->wn_refs = 1;
7175 *copyprev = np;
7176 copyprev = &np->wn_sibling;
7177
7178 /* Let "node" point to the head of the copied list. */
7179 if (copyp == node)
7180 node = np;
7181 }
7182 }
7183
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184 /* Look for the sibling that has the same character. They are sorted
7185 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007186 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007187 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007188 while (node != NULL
7189 && (node->wn_byte < word[i]
7190 || (node->wn_byte == NUL
7191 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007192 ? node->wn_affixID < (unsigned)affixID
7193 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007194 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007195 && (spin->si_sugtree
7196 ? (node->wn_region & 0xffff) < region
7197 : node->wn_affixID
7198 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007199 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007200 prev = &node->wn_sibling;
7201 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007202 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007203 if (node == NULL
7204 || node->wn_byte != word[i]
7205 || (word[i] == NUL
7206 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007207 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007208 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007209 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007210 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007211 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007212 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007213 if (np == NULL)
7214 return FAIL;
7215 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007216
7217 /* If "node" is NULL this is a new child or the end of the sibling
7218 * list: ref count is one. Otherwise use ref count of sibling and
7219 * make ref count of sibling one (matters when inserting in front
7220 * of the list of siblings). */
7221 if (node == NULL)
7222 np->wn_refs = 1;
7223 else
7224 {
7225 np->wn_refs = node->wn_refs;
7226 node->wn_refs = 1;
7227 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007228 *prev = np;
7229 np->wn_sibling = node;
7230 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007231 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007232
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007234 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007235 node->wn_flags = flags;
7236 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007237 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007238 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007239 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007240 prev = &node->wn_child;
7241 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007242 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007243#ifdef SPELL_PRINTTREE
7244 smsg("Added \"%s\"", word);
7245 spell_print_tree(root->wn_sibling);
7246#endif
7247
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007248 /* count nr of words added since last message */
7249 ++spin->si_msg_count;
7250
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007251 if (spin->si_compress_cnt > 1)
7252 {
7253 if (--spin->si_compress_cnt == 1)
7254 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007255 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007256 }
7257
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007258 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007259 * When we have allocated lots of memory we need to compress the word tree
7260 * to free up some room. But compression is slow, and we might actually
7261 * need that room, thus only compress in the following situations:
7262 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007263 * "compress_start" blocks.
7264 * 2. When compressed before and used "compress_inc" blocks before
7265 * adding "compress_added" words (si_compress_cnt > 1).
7266 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007267 * (si_compress_cnt == 1) and the number of free nodes drops below the
7268 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007269 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007270#ifndef SPELL_PRINTTREE
7271 if (spin->si_compress_cnt == 1
7272 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007273 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007274#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007275 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007276 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007277 * when the freed up room has been used and another "compress_inc"
7278 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007279 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007280 spin->si_blocks_cnt -= compress_inc;
7281 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007282
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007283 if (spin->si_verbose)
7284 {
7285 msg_start();
7286 msg_puts((char_u *)_(msg_compressing));
7287 msg_clr_eos();
7288 msg_didout = FALSE;
7289 msg_col = 0;
7290 out_flush();
7291 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007292
7293 /* Compress both trees. Either they both have many nodes, which makes
7294 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007295 * compression goes fast. But when filling the souldfold word tree
7296 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007297 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007298 if (affixID >= 0)
7299 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007300 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007301
7302 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007303}
7304
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007305/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007306 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7307 * Sets "sps_flags".
7308 */
7309 int
7310spell_check_msm()
7311{
7312 char_u *p = p_msm;
7313 long start = 0;
7314 long inc = 0;
7315 long added = 0;
7316
7317 if (!VIM_ISDIGIT(*p))
7318 return FAIL;
7319 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7320 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7321 if (*p != ',')
7322 return FAIL;
7323 ++p;
7324 if (!VIM_ISDIGIT(*p))
7325 return FAIL;
7326 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7327 if (*p != ',')
7328 return FAIL;
7329 ++p;
7330 if (!VIM_ISDIGIT(*p))
7331 return FAIL;
7332 added = getdigits(&p) * 1024;
7333 if (*p != NUL)
7334 return FAIL;
7335
7336 if (start == 0 || inc == 0 || added == 0 || inc > start)
7337 return FAIL;
7338
7339 compress_start = start;
7340 compress_inc = inc;
7341 compress_added = added;
7342 return OK;
7343}
7344
7345
7346/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007347 * Get a wordnode_T, either from the list of previously freed nodes or
7348 * allocate a new one.
7349 */
7350 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007351get_wordnode(spin)
7352 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007353{
7354 wordnode_T *n;
7355
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007356 if (spin->si_first_free == NULL)
7357 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007358 else
7359 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007360 n = spin->si_first_free;
7361 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007362 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007363 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007364 }
7365#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007366 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007367#endif
7368 return n;
7369}
7370
7371/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007372 * Decrement the reference count on a node (which is the head of a list of
7373 * siblings). If the reference count becomes zero free the node and its
7374 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007375 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007376 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007377 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007378deref_wordnode(spin, node)
7379 spellinfo_T *spin;
7380 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007381{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007382 wordnode_T *np;
7383 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007384
7385 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007386 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007387 for (np = node; np != NULL; np = np->wn_sibling)
7388 {
7389 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007390 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007391 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007392 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007393 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007394 ++cnt; /* length field */
7395 }
7396 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007397}
7398
7399/*
7400 * Free a wordnode_T for re-use later.
7401 * Only the "wn_child" field becomes invalid.
7402 */
7403 static void
7404free_wordnode(spin, n)
7405 spellinfo_T *spin;
7406 wordnode_T *n;
7407{
7408 n->wn_child = spin->si_first_free;
7409 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007410 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007411}
7412
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007413/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007414 * Compress a tree: find tails that are identical and can be shared.
7415 */
7416 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007417wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007418 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007419 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007420{
7421 hashtab_T ht;
7422 int n;
7423 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007424 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007425
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007426 /* Skip the root itself, it's not actually used. The first sibling is the
7427 * start of the tree. */
7428 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007429 {
7430 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007431 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007432
7433#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007434 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007435#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007436 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007437 if (tot > 1000000)
7438 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007439 else if (tot == 0)
7440 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007441 else
7442 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007443 vim_snprintf((char *)IObuff, IOSIZE,
7444 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7445 n, tot, tot - n, perc);
7446 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007447 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007448#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007449 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007450#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007451 hash_clear(&ht);
7452 }
7453}
7454
7455/*
7456 * Compress a node, its siblings and its children, depth first.
7457 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007458 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007459 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007460node_compress(spin, node, ht, tot)
7461 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007462 wordnode_T *node;
7463 hashtab_T *ht;
7464 int *tot; /* total count of nodes before compressing,
7465 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007466{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007467 wordnode_T *np;
7468 wordnode_T *tp;
7469 wordnode_T *child;
7470 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007471 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007472 int len = 0;
7473 unsigned nr, n;
7474 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007475
Bram Moolenaar51485f02005-06-04 21:55:20 +00007476 /*
7477 * Go through the list of siblings. Compress each child and then try
7478 * finding an identical child to replace it.
7479 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007480 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007481 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007482 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007483 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007484 ++len;
7485 if ((child = np->wn_child) != NULL)
7486 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007487 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007488 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007489
7490 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007491 hash = hash_hash(child->wn_u1.hashkey);
7492 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007493 if (!HASHITEM_EMPTY(hi))
7494 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007495 /* There are children we encountered before with a hash value
7496 * identical to the current child. Now check if there is one
7497 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007498 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007499 if (node_equal(child, tp))
7500 {
7501 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007502 * current one. This means the current child and all
7503 * its siblings is unlinked from the tree. */
7504 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007505 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007506 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007507 break;
7508 }
7509 if (tp == NULL)
7510 {
7511 /* No other child with this hash value equals the child of
7512 * the node, add it to the linked list after the first
7513 * item. */
7514 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007515 child->wn_u2.next = tp->wn_u2.next;
7516 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007517 }
7518 }
7519 else
7520 /* No other child has this hash value, add it to the
7521 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007522 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007523 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007524 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007525 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007526
7527 /*
7528 * Make a hash key for the node and its siblings, so that we can quickly
7529 * find a lookalike node. This must be done after compressing the sibling
7530 * list, otherwise the hash key would become invalid by the compression.
7531 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007532 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007533 nr = 0;
7534 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007535 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007536 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007537 /* end node: use wn_flags, wn_region and wn_affixID */
7538 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007539 else
7540 /* byte node: use the byte value and the child pointer */
7541 n = np->wn_byte + ((long_u)np->wn_child << 8);
7542 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007543 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007544
7545 /* Avoid NUL bytes, it terminates the hash key. */
7546 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007547 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007548 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007549 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007550 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007551 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007552 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007553 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7554 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007555
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007556 /* Check for CTRL-C pressed now and then. */
7557 fast_breakcheck();
7558
Bram Moolenaar51485f02005-06-04 21:55:20 +00007559 return compressed;
7560}
7561
7562/*
7563 * Return TRUE when two nodes have identical siblings and children.
7564 */
7565 static int
7566node_equal(n1, n2)
7567 wordnode_T *n1;
7568 wordnode_T *n2;
7569{
7570 wordnode_T *p1;
7571 wordnode_T *p2;
7572
7573 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7574 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7575 if (p1->wn_byte != p2->wn_byte
7576 || (p1->wn_byte == NUL
7577 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007578 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007579 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007580 : (p1->wn_child != p2->wn_child)))
7581 break;
7582
7583 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007584}
7585
7586/*
7587 * Write a number to file "fd", MSB first, in "len" bytes.
7588 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007589 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007590put_bytes(fd, nr, len)
7591 FILE *fd;
7592 long_u nr;
7593 int len;
7594{
7595 int i;
7596
7597 for (i = len - 1; i >= 0; --i)
7598 putc((int)(nr >> (i * 8)), fd);
7599}
7600
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007601#ifdef _MSC_VER
7602# if (_MSC_VER <= 1200)
7603/* This line is required for VC6 without the service pack. Also see the
7604 * matching #pragma below. */
7605/* # pragma optimize("", off) */
7606# endif
7607#endif
7608
Bram Moolenaar4770d092006-01-12 23:22:24 +00007609/*
7610 * Write spin->si_sugtime to file "fd".
7611 */
7612 static void
7613put_sugtime(spin, fd)
7614 spellinfo_T *spin;
7615 FILE *fd;
7616{
7617 int c;
7618 int i;
7619
7620 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7621 * can't use put_bytes() here. */
7622 for (i = 7; i >= 0; --i)
7623 if (i + 1 > sizeof(time_t))
7624 /* ">>" doesn't work well when shifting more bits than avail */
7625 putc(0, fd);
7626 else
7627 {
7628 c = (unsigned)spin->si_sugtime >> (i * 8);
7629 putc(c, fd);
7630 }
7631}
7632
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007633#ifdef _MSC_VER
7634# if (_MSC_VER <= 1200)
7635/* # pragma optimize("", on) */
7636# endif
7637#endif
7638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007639static int
7640#ifdef __BORLANDC__
7641_RTLENTRYF
7642#endif
7643rep_compare __ARGS((const void *s1, const void *s2));
7644
7645/*
7646 * Function given to qsort() to sort the REP items on "from" string.
7647 */
7648 static int
7649#ifdef __BORLANDC__
7650_RTLENTRYF
7651#endif
7652rep_compare(s1, s2)
7653 const void *s1;
7654 const void *s2;
7655{
7656 fromto_T *p1 = (fromto_T *)s1;
7657 fromto_T *p2 = (fromto_T *)s2;
7658
7659 return STRCMP(p1->ft_from, p2->ft_from);
7660}
7661
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007662/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007663 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007664 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007665 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007666 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007667write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007668 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007669 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007670{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007671 FILE *fd;
7672 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007673 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007674 wordnode_T *tree;
7675 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007676 int i;
7677 int l;
7678 garray_T *gap;
7679 fromto_T *ftp;
7680 char_u *p;
7681 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007682 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007683
Bram Moolenaarb765d632005-06-07 21:00:02 +00007684 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007685 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007686 {
7687 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007688 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007689 }
7690
Bram Moolenaar5195e452005-08-19 20:32:47 +00007691 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007692 /* <fileID> */
7693 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007694 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007695 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007696 retval = FAIL;
7697 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007698 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007699
Bram Moolenaar5195e452005-08-19 20:32:47 +00007700 /*
7701 * <SECTIONS>: <section> ... <sectionend>
7702 */
7703
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007704 /* SN_INFO: <infotext> */
7705 if (spin->si_info != NULL)
7706 {
7707 putc(SN_INFO, fd); /* <sectionID> */
7708 putc(0, fd); /* <sectionflags> */
7709
7710 i = STRLEN(spin->si_info);
7711 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7712 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7713 }
7714
Bram Moolenaar5195e452005-08-19 20:32:47 +00007715 /* SN_REGION: <regionname> ...
7716 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007717 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007718 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007719 putc(SN_REGION, fd); /* <sectionID> */
7720 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7721 l = spin->si_region_count * 2;
7722 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7723 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7724 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007725 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007726 }
7727 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007728 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007729
Bram Moolenaar5195e452005-08-19 20:32:47 +00007730 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7731 *
7732 * The table with character flags and the table for case folding.
7733 * This makes sure the same characters are recognized as word characters
7734 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007735 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007736 * 'encoding'.
7737 * Also skip this for an .add.spl file, the main spell file must contain
7738 * the table (avoids that it conflicts). File is shorter too.
7739 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007740 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007741 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007742 char_u folchars[128 * 8];
7743 int flags;
7744
Bram Moolenaard12a1322005-08-21 22:08:24 +00007745 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007746 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7747
7748 /* Form the <folchars> string first, we need to know its length. */
7749 l = 0;
7750 for (i = 128; i < 256; ++i)
7751 {
7752#ifdef FEAT_MBYTE
7753 if (has_mbyte)
7754 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7755 else
7756#endif
7757 folchars[l++] = spelltab.st_fold[i];
7758 }
7759 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7760
7761 fputc(128, fd); /* <charflagslen> */
7762 for (i = 128; i < 256; ++i)
7763 {
7764 flags = 0;
7765 if (spelltab.st_isw[i])
7766 flags |= CF_WORD;
7767 if (spelltab.st_isu[i])
7768 flags |= CF_UPPER;
7769 fputc(flags, fd); /* <charflags> */
7770 }
7771
7772 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7773 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007774 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007775
Bram Moolenaar5195e452005-08-19 20:32:47 +00007776 /* SN_MIDWORD: <midword> */
7777 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007778 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007779 putc(SN_MIDWORD, fd); /* <sectionID> */
7780 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7781
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007782 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007783 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007784 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7785 }
7786
Bram Moolenaar5195e452005-08-19 20:32:47 +00007787 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7788 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007789 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007790 putc(SN_PREFCOND, fd); /* <sectionID> */
7791 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7792
7793 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7794 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7795
7796 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007797 }
7798
Bram Moolenaar5195e452005-08-19 20:32:47 +00007799 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007800 * SN_SAL: <salflags> <salcount> <sal> ...
7801 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007802
Bram Moolenaar5195e452005-08-19 20:32:47 +00007803 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007804 * round 2: SN_SAL section (unless SN_SOFO is used)
7805 * round 3: SN_REPSAL section */
7806 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007807 {
7808 if (round == 1)
7809 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007810 else if (round == 2)
7811 {
7812 /* Don't write SN_SAL when using a SN_SOFO section */
7813 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7814 continue;
7815 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007816 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007817 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007818 gap = &spin->si_repsal;
7819
7820 /* Don't write the section if there are no items. */
7821 if (gap->ga_len == 0)
7822 continue;
7823
7824 /* Sort the REP/REPSAL items. */
7825 if (round != 2)
7826 qsort(gap->ga_data, (size_t)gap->ga_len,
7827 sizeof(fromto_T), rep_compare);
7828
7829 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
7830 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007831
Bram Moolenaar5195e452005-08-19 20:32:47 +00007832 /* This is for making suggestions, section is not required. */
7833 putc(0, fd); /* <sectionflags> */
7834
7835 /* Compute the length of what follows. */
7836 l = 2; /* count <repcount> or <salcount> */
7837 for (i = 0; i < gap->ga_len; ++i)
7838 {
7839 ftp = &((fromto_T *)gap->ga_data)[i];
7840 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7841 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7842 }
7843 if (round == 2)
7844 ++l; /* count <salflags> */
7845 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7846
7847 if (round == 2)
7848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007849 i = 0;
7850 if (spin->si_followup)
7851 i |= SAL_F0LLOWUP;
7852 if (spin->si_collapse)
7853 i |= SAL_COLLAPSE;
7854 if (spin->si_rem_accents)
7855 i |= SAL_REM_ACCENTS;
7856 putc(i, fd); /* <salflags> */
7857 }
7858
7859 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7860 for (i = 0; i < gap->ga_len; ++i)
7861 {
7862 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7863 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7864 ftp = &((fromto_T *)gap->ga_data)[i];
7865 for (rr = 1; rr <= 2; ++rr)
7866 {
7867 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7868 l = STRLEN(p);
7869 putc(l, fd);
7870 fwrite(p, l, (size_t)1, fd);
7871 }
7872 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007874 }
7875
Bram Moolenaar5195e452005-08-19 20:32:47 +00007876 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7877 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007878 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7879 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007880 putc(SN_SOFO, fd); /* <sectionID> */
7881 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007882
7883 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007884 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7885 /* <sectionlen> */
7886
7887 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7888 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007889
7890 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007891 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7892 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007893 }
7894
Bram Moolenaar4770d092006-01-12 23:22:24 +00007895 /* SN_WORDS: <word> ...
7896 * This is for making suggestions, section is not required. */
7897 if (spin->si_commonwords.ht_used > 0)
7898 {
7899 putc(SN_WORDS, fd); /* <sectionID> */
7900 putc(0, fd); /* <sectionflags> */
7901
7902 /* round 1: count the bytes
7903 * round 2: write the bytes */
7904 for (round = 1; round <= 2; ++round)
7905 {
7906 int todo;
7907 int len = 0;
7908 hashitem_T *hi;
7909
7910 todo = spin->si_commonwords.ht_used;
7911 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
7912 if (!HASHITEM_EMPTY(hi))
7913 {
7914 l = STRLEN(hi->hi_key) + 1;
7915 len += l;
7916 if (round == 2) /* <word> */
7917 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
7918 --todo;
7919 }
7920 if (round == 1)
7921 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
7922 }
7923 }
7924
Bram Moolenaar5195e452005-08-19 20:32:47 +00007925 /* SN_MAP: <mapstr>
7926 * This is for making suggestions, section is not required. */
7927 if (spin->si_map.ga_len > 0)
7928 {
7929 putc(SN_MAP, fd); /* <sectionID> */
7930 putc(0, fd); /* <sectionflags> */
7931 l = spin->si_map.ga_len;
7932 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7933 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7934 /* <mapstr> */
7935 }
7936
Bram Moolenaar4770d092006-01-12 23:22:24 +00007937 /* SN_SUGFILE: <timestamp>
7938 * This is used to notify that a .sug file may be available and at the
7939 * same time allows for checking that a .sug file that is found matches
7940 * with this .spl file. That's because the word numbers must be exactly
7941 * right. */
7942 if (!spin->si_nosugfile
7943 && (spin->si_sal.ga_len > 0
7944 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
7945 {
7946 putc(SN_SUGFILE, fd); /* <sectionID> */
7947 putc(0, fd); /* <sectionflags> */
7948 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
7949
7950 /* Set si_sugtime and write it to the file. */
7951 spin->si_sugtime = time(NULL);
7952 put_sugtime(spin, fd); /* <timestamp> */
7953 }
7954
Bram Moolenaare1438bb2006-03-01 22:01:55 +00007955 /* SN_NOSPLITSUGS: nothing
7956 * This is used to notify that no suggestions with word splits are to be
7957 * made. */
7958 if (spin->si_nosplitsugs)
7959 {
7960 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
7961 putc(0, fd); /* <sectionflags> */
7962 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7963 }
7964
Bram Moolenaar5195e452005-08-19 20:32:47 +00007965 /* SN_COMPOUND: compound info.
7966 * We don't mark it required, when not supported all compound words will
7967 * be bad words. */
7968 if (spin->si_compflags != NULL)
7969 {
7970 putc(SN_COMPOUND, fd); /* <sectionID> */
7971 putc(0, fd); /* <sectionflags> */
7972
7973 l = STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007974 for (i = 0; i < spin->si_comppat.ga_len; ++i)
7975 l += STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
7976 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
7977
Bram Moolenaar5195e452005-08-19 20:32:47 +00007978 putc(spin->si_compmax, fd); /* <compmax> */
7979 putc(spin->si_compminlen, fd); /* <compminlen> */
7980 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007981 putc(0, fd); /* for Vim 7.0b compatibility */
7982 putc(spin->si_compoptions, fd); /* <compoptions> */
7983 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
7984 /* <comppatcount> */
7985 for (i = 0; i < spin->si_comppat.ga_len; ++i)
7986 {
7987 p = ((char_u **)(spin->si_comppat.ga_data))[i];
7988 putc(STRLEN(p), fd); /* <comppatlen> */
7989 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
7990 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007991 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007992 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
7993 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007994 }
7995
Bram Moolenaar78622822005-08-23 21:00:13 +00007996 /* SN_NOBREAK: NOBREAK flag */
7997 if (spin->si_nobreak)
7998 {
7999 putc(SN_NOBREAK, fd); /* <sectionID> */
8000 putc(0, fd); /* <sectionflags> */
8001
8002 /* It's empty, the precense of the section flags the feature. */
8003 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8004 }
8005
Bram Moolenaar5195e452005-08-19 20:32:47 +00008006 /* SN_SYLLABLE: syllable info.
8007 * We don't mark it required, when not supported syllables will not be
8008 * counted. */
8009 if (spin->si_syllable != NULL)
8010 {
8011 putc(SN_SYLLABLE, fd); /* <sectionID> */
8012 putc(0, fd); /* <sectionflags> */
8013
8014 l = STRLEN(spin->si_syllable);
8015 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8016 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8017 }
8018
8019 /* end of <SECTIONS> */
8020 putc(SN_END, fd); /* <sectionend> */
8021
Bram Moolenaar50cde822005-06-05 21:54:54 +00008022
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008023 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008024 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008025 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008027 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008028 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008029 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008030 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008031 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008032 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008033 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008034 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008035
Bram Moolenaar0c405862005-06-22 22:26:26 +00008036 /* Clear the index and wnode fields in the tree. */
8037 clear_node(tree);
8038
Bram Moolenaar51485f02005-06-04 21:55:20 +00008039 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008040 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008041 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008042 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008043
Bram Moolenaar51485f02005-06-04 21:55:20 +00008044 /* number of nodes in 4 bytes */
8045 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008046 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008047
Bram Moolenaar51485f02005-06-04 21:55:20 +00008048 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008049 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008050 }
8051
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008052 /* Write another byte to check for errors. */
8053 if (putc(0, fd) == EOF)
8054 retval = FAIL;
8055
8056 if (fclose(fd) == EOF)
8057 retval = FAIL;
8058
8059 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008060}
8061
8062/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008063 * Clear the index and wnode fields of "node", it siblings and its
8064 * children. This is needed because they are a union with other items to save
8065 * space.
8066 */
8067 static void
8068clear_node(node)
8069 wordnode_T *node;
8070{
8071 wordnode_T *np;
8072
8073 if (node != NULL)
8074 for (np = node; np != NULL; np = np->wn_sibling)
8075 {
8076 np->wn_u1.index = 0;
8077 np->wn_u2.wnode = NULL;
8078
8079 if (np->wn_byte != NUL)
8080 clear_node(np->wn_child);
8081 }
8082}
8083
8084
8085/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008086 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008087 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008088 * This first writes the list of possible bytes (siblings). Then for each
8089 * byte recursively write the children.
8090 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008091 * NOTE: The code here must match the code in read_tree_node(), since
8092 * assumptions are made about the indexes (so that we don't have to write them
8093 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008094 *
8095 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008096 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008097 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00008098put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008099 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008100 wordnode_T *node;
8101 int index;
8102 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008103 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008104{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008105 int newindex = index;
8106 int siblingcount = 0;
8107 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008108 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008109
Bram Moolenaar51485f02005-06-04 21:55:20 +00008110 /* If "node" is zero the tree is empty. */
8111 if (node == NULL)
8112 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008113
Bram Moolenaar51485f02005-06-04 21:55:20 +00008114 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008115 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008116
8117 /* Count the number of siblings. */
8118 for (np = node; np != NULL; np = np->wn_sibling)
8119 ++siblingcount;
8120
8121 /* Write the sibling count. */
8122 if (fd != NULL)
8123 putc(siblingcount, fd); /* <siblingcount> */
8124
8125 /* Write each sibling byte and optionally extra info. */
8126 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008127 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008128 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008129 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008130 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008131 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008132 /* For a NUL byte (end of word) write the flags etc. */
8133 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008134 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008135 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008136 * associated condition nr (stored in wn_region). The
8137 * byte value is misused to store the "rare" and "not
8138 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008139 if (np->wn_flags == (short_u)PFX_FLAGS)
8140 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008141 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008142 {
8143 putc(BY_FLAGS, fd); /* <byte> */
8144 putc(np->wn_flags, fd); /* <pflags> */
8145 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008146 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008147 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008148 }
8149 else
8150 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008151 /* For word trees we write the flag/region items. */
8152 flags = np->wn_flags;
8153 if (regionmask != 0 && np->wn_region != regionmask)
8154 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008155 if (np->wn_affixID != 0)
8156 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008157 if (flags == 0)
8158 {
8159 /* word without flags or region */
8160 putc(BY_NOFLAGS, fd); /* <byte> */
8161 }
8162 else
8163 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008164 if (np->wn_flags >= 0x100)
8165 {
8166 putc(BY_FLAGS2, fd); /* <byte> */
8167 putc(flags, fd); /* <flags> */
8168 putc((unsigned)flags >> 8, fd); /* <flags2> */
8169 }
8170 else
8171 {
8172 putc(BY_FLAGS, fd); /* <byte> */
8173 putc(flags, fd); /* <flags> */
8174 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008175 if (flags & WF_REGION)
8176 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008177 if (flags & WF_AFX)
8178 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008179 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008180 }
8181 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008182 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008183 else
8184 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008185 if (np->wn_child->wn_u1.index != 0
8186 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008187 {
8188 /* The child is written elsewhere, write the reference. */
8189 if (fd != NULL)
8190 {
8191 putc(BY_INDEX, fd); /* <byte> */
8192 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008193 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008194 }
8195 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008196 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008197 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008198 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008199
Bram Moolenaar51485f02005-06-04 21:55:20 +00008200 if (fd != NULL)
8201 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8202 {
8203 EMSG(_(e_write));
8204 return 0;
8205 }
8206 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008207 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008208
8209 /* Space used in the array when reading: one for each sibling and one for
8210 * the count. */
8211 newindex += siblingcount + 1;
8212
8213 /* Recursively dump the children of each sibling. */
8214 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008215 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8216 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008217 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008218
8219 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008220}
8221
8222
8223/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008224 * ":mkspell [-ascii] outfile infile ..."
8225 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008226 */
8227 void
8228ex_mkspell(eap)
8229 exarg_T *eap;
8230{
8231 int fcount;
8232 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008233 char_u *arg = eap->arg;
8234 int ascii = FALSE;
8235
8236 if (STRNCMP(arg, "-ascii", 6) == 0)
8237 {
8238 ascii = TRUE;
8239 arg = skipwhite(arg + 6);
8240 }
8241
8242 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8243 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008246 FreeWild(fcount, fnames);
8247 }
8248}
8249
8250/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008251 * Create the .sug file.
8252 * Uses the soundfold info in "spin".
8253 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8254 */
8255 static void
8256spell_make_sugfile(spin, wfname)
8257 spellinfo_T *spin;
8258 char_u *wfname;
8259{
8260 char_u fname[MAXPATHL];
8261 int len;
8262 slang_T *slang;
8263 int free_slang = FALSE;
8264
8265 /*
8266 * Read back the .spl file that was written. This fills the required
8267 * info for soundfolding. This also uses less memory than the
8268 * pointer-linked version of the trie. And it avoids having two versions
8269 * of the code for the soundfolding stuff.
8270 * It might have been done already by spell_reload_one().
8271 */
8272 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8273 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8274 break;
8275 if (slang == NULL)
8276 {
8277 spell_message(spin, (char_u *)_("Reading back spell file..."));
8278 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8279 if (slang == NULL)
8280 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008281 free_slang = TRUE;
8282 }
8283
8284 /*
8285 * Clear the info in "spin" that is used.
8286 */
8287 spin->si_blocks = NULL;
8288 spin->si_blocks_cnt = 0;
8289 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8290 spin->si_free_count = 0;
8291 spin->si_first_free = NULL;
8292 spin->si_foldwcount = 0;
8293
8294 /*
8295 * Go through the trie of good words, soundfold each word and add it to
8296 * the soundfold trie.
8297 */
8298 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8299 if (sug_filltree(spin, slang) == FAIL)
8300 goto theend;
8301
8302 /*
8303 * Create the table which links each soundfold word with a list of the
8304 * good words it may come from. Creates buffer "spin->si_spellbuf".
8305 * This also removes the wordnr from the NUL byte entries to make
8306 * compression possible.
8307 */
8308 if (sug_maketable(spin) == FAIL)
8309 goto theend;
8310
8311 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8312 (long)spin->si_spellbuf->b_ml.ml_line_count);
8313
8314 /*
8315 * Compress the soundfold trie.
8316 */
8317 spell_message(spin, (char_u *)_(msg_compressing));
8318 wordtree_compress(spin, spin->si_foldroot);
8319
8320 /*
8321 * Write the .sug file.
8322 * Make the file name by changing ".spl" to ".sug".
8323 */
8324 STRCPY(fname, wfname);
8325 len = STRLEN(fname);
8326 fname[len - 2] = 'u';
8327 fname[len - 1] = 'g';
8328 sug_write(spin, fname);
8329
8330theend:
8331 if (free_slang)
8332 slang_free(slang);
8333 free_blocks(spin->si_blocks);
8334 close_spellbuf(spin->si_spellbuf);
8335}
8336
8337/*
8338 * Build the soundfold trie for language "slang".
8339 */
8340 static int
8341sug_filltree(spin, slang)
8342 spellinfo_T *spin;
8343 slang_T *slang;
8344{
8345 char_u *byts;
8346 idx_T *idxs;
8347 int depth;
8348 idx_T arridx[MAXWLEN];
8349 int curi[MAXWLEN];
8350 char_u tword[MAXWLEN];
8351 char_u tsalword[MAXWLEN];
8352 int c;
8353 idx_T n;
8354 unsigned words_done = 0;
8355 int wordcount[MAXWLEN];
8356
8357 /* We use si_foldroot for the souldfolded trie. */
8358 spin->si_foldroot = wordtree_alloc(spin);
8359 if (spin->si_foldroot == NULL)
8360 return FAIL;
8361
8362 /* let tree_add_word() know we're adding to the soundfolded tree */
8363 spin->si_sugtree = TRUE;
8364
8365 /*
8366 * Go through the whole case-folded tree, soundfold each word and put it
8367 * in the trie.
8368 */
8369 byts = slang->sl_fbyts;
8370 idxs = slang->sl_fidxs;
8371
8372 arridx[0] = 0;
8373 curi[0] = 1;
8374 wordcount[0] = 0;
8375
8376 depth = 0;
8377 while (depth >= 0 && !got_int)
8378 {
8379 if (curi[depth] > byts[arridx[depth]])
8380 {
8381 /* Done all bytes at this node, go up one level. */
8382 idxs[arridx[depth]] = wordcount[depth];
8383 if (depth > 0)
8384 wordcount[depth - 1] += wordcount[depth];
8385
8386 --depth;
8387 line_breakcheck();
8388 }
8389 else
8390 {
8391
8392 /* Do one more byte at this node. */
8393 n = arridx[depth] + curi[depth];
8394 ++curi[depth];
8395
8396 c = byts[n];
8397 if (c == 0)
8398 {
8399 /* Sound-fold the word. */
8400 tword[depth] = NUL;
8401 spell_soundfold(slang, tword, TRUE, tsalword);
8402
8403 /* We use the "flags" field for the MSB of the wordnr,
8404 * "region" for the LSB of the wordnr. */
8405 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8406 words_done >> 16, words_done & 0xffff,
8407 0) == FAIL)
8408 return FAIL;
8409
8410 ++words_done;
8411 ++wordcount[depth];
8412
8413 /* Reset the block count each time to avoid compression
8414 * kicking in. */
8415 spin->si_blocks_cnt = 0;
8416
8417 /* Skip over any other NUL bytes (same word with different
8418 * flags). */
8419 while (byts[n + 1] == 0)
8420 {
8421 ++n;
8422 ++curi[depth];
8423 }
8424 }
8425 else
8426 {
8427 /* Normal char, go one level deeper. */
8428 tword[depth++] = c;
8429 arridx[depth] = idxs[n];
8430 curi[depth] = 1;
8431 wordcount[depth] = 0;
8432 }
8433 }
8434 }
8435
8436 smsg((char_u *)_("Total number of words: %d"), words_done);
8437
8438 return OK;
8439}
8440
8441/*
8442 * Make the table that links each word in the soundfold trie to the words it
8443 * can be produced from.
8444 * This is not unlike lines in a file, thus use a memfile to be able to access
8445 * the table efficiently.
8446 * Returns FAIL when out of memory.
8447 */
8448 static int
8449sug_maketable(spin)
8450 spellinfo_T *spin;
8451{
8452 garray_T ga;
8453 int res = OK;
8454
8455 /* Allocate a buffer, open a memline for it and create the swap file
8456 * (uses a temp file, not a .swp file). */
8457 spin->si_spellbuf = open_spellbuf();
8458 if (spin->si_spellbuf == NULL)
8459 return FAIL;
8460
8461 /* Use a buffer to store the line info, avoids allocating many small
8462 * pieces of memory. */
8463 ga_init2(&ga, 1, 100);
8464
8465 /* recursively go through the tree */
8466 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8467 res = FAIL;
8468
8469 ga_clear(&ga);
8470 return res;
8471}
8472
8473/*
8474 * Fill the table for one node and its children.
8475 * Returns the wordnr at the start of the node.
8476 * Returns -1 when out of memory.
8477 */
8478 static int
8479sug_filltable(spin, node, startwordnr, gap)
8480 spellinfo_T *spin;
8481 wordnode_T *node;
8482 int startwordnr;
8483 garray_T *gap; /* place to store line of numbers */
8484{
8485 wordnode_T *p, *np;
8486 int wordnr = startwordnr;
8487 int nr;
8488 int prev_nr;
8489
8490 for (p = node; p != NULL; p = p->wn_sibling)
8491 {
8492 if (p->wn_byte == NUL)
8493 {
8494 gap->ga_len = 0;
8495 prev_nr = 0;
8496 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8497 {
8498 if (ga_grow(gap, 10) == FAIL)
8499 return -1;
8500
8501 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8502 /* Compute the offset from the previous nr and store the
8503 * offset in a way that it takes a minimum number of bytes.
8504 * It's a bit like utf-8, but without the need to mark
8505 * following bytes. */
8506 nr -= prev_nr;
8507 prev_nr += nr;
8508 gap->ga_len += offset2bytes(nr,
8509 (char_u *)gap->ga_data + gap->ga_len);
8510 }
8511
8512 /* add the NUL byte */
8513 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8514
8515 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8516 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8517 return -1;
8518 ++wordnr;
8519
8520 /* Remove extra NUL entries, we no longer need them. We don't
8521 * bother freeing the nodes, the won't be reused anyway. */
8522 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8523 p->wn_sibling = p->wn_sibling->wn_sibling;
8524
8525 /* Clear the flags on the remaining NUL node, so that compression
8526 * works a lot better. */
8527 p->wn_flags = 0;
8528 p->wn_region = 0;
8529 }
8530 else
8531 {
8532 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8533 if (wordnr == -1)
8534 return -1;
8535 }
8536 }
8537 return wordnr;
8538}
8539
8540/*
8541 * Convert an offset into a minimal number of bytes.
8542 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8543 * bytes.
8544 */
8545 static int
8546offset2bytes(nr, buf)
8547 int nr;
8548 char_u *buf;
8549{
8550 int rem;
8551 int b1, b2, b3, b4;
8552
8553 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8554 b1 = nr % 255 + 1;
8555 rem = nr / 255;
8556 b2 = rem % 255 + 1;
8557 rem = rem / 255;
8558 b3 = rem % 255 + 1;
8559 b4 = rem / 255 + 1;
8560
8561 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8562 {
8563 buf[0] = 0xe0 + b4;
8564 buf[1] = b3;
8565 buf[2] = b2;
8566 buf[3] = b1;
8567 return 4;
8568 }
8569 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8570 {
8571 buf[0] = 0xc0 + b3;
8572 buf[1] = b2;
8573 buf[2] = b1;
8574 return 3;
8575 }
8576 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8577 {
8578 buf[0] = 0x80 + b2;
8579 buf[1] = b1;
8580 return 2;
8581 }
8582 /* 1 byte */
8583 buf[0] = b1;
8584 return 1;
8585}
8586
8587/*
8588 * Opposite of offset2bytes().
8589 * "pp" points to the bytes and is advanced over it.
8590 * Returns the offset.
8591 */
8592 static int
8593bytes2offset(pp)
8594 char_u **pp;
8595{
8596 char_u *p = *pp;
8597 int nr;
8598 int c;
8599
8600 c = *p++;
8601 if ((c & 0x80) == 0x00) /* 1 byte */
8602 {
8603 nr = c - 1;
8604 }
8605 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8606 {
8607 nr = (c & 0x3f) - 1;
8608 nr = nr * 255 + (*p++ - 1);
8609 }
8610 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8611 {
8612 nr = (c & 0x1f) - 1;
8613 nr = nr * 255 + (*p++ - 1);
8614 nr = nr * 255 + (*p++ - 1);
8615 }
8616 else /* 4 bytes */
8617 {
8618 nr = (c & 0x0f) - 1;
8619 nr = nr * 255 + (*p++ - 1);
8620 nr = nr * 255 + (*p++ - 1);
8621 nr = nr * 255 + (*p++ - 1);
8622 }
8623
8624 *pp = p;
8625 return nr;
8626}
8627
8628/*
8629 * Write the .sug file in "fname".
8630 */
8631 static void
8632sug_write(spin, fname)
8633 spellinfo_T *spin;
8634 char_u *fname;
8635{
8636 FILE *fd;
8637 wordnode_T *tree;
8638 int nodecount;
8639 int wcount;
8640 char_u *line;
8641 linenr_T lnum;
8642 int len;
8643
8644 /* Create the file. Note that an existing file is silently overwritten! */
8645 fd = mch_fopen((char *)fname, "w");
8646 if (fd == NULL)
8647 {
8648 EMSG2(_(e_notopen), fname);
8649 return;
8650 }
8651
8652 vim_snprintf((char *)IObuff, IOSIZE,
8653 _("Writing suggestion file %s ..."), fname);
8654 spell_message(spin, IObuff);
8655
8656 /*
8657 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8658 */
8659 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8660 {
8661 EMSG(_(e_write));
8662 goto theend;
8663 }
8664 putc(VIMSUGVERSION, fd); /* <versionnr> */
8665
8666 /* Write si_sugtime to the file. */
8667 put_sugtime(spin, fd); /* <timestamp> */
8668
8669 /*
8670 * <SUGWORDTREE>
8671 */
8672 spin->si_memtot = 0;
8673 tree = spin->si_foldroot->wn_sibling;
8674
8675 /* Clear the index and wnode fields in the tree. */
8676 clear_node(tree);
8677
8678 /* Count the number of nodes. Needed to be able to allocate the
8679 * memory when reading the nodes. Also fills in index for shared
8680 * nodes. */
8681 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8682
8683 /* number of nodes in 4 bytes */
8684 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8685 spin->si_memtot += nodecount + nodecount * sizeof(int);
8686
8687 /* Write the nodes. */
8688 (void)put_node(fd, tree, 0, 0, FALSE);
8689
8690 /*
8691 * <SUGTABLE>: <sugwcount> <sugline> ...
8692 */
8693 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8694 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8695
8696 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8697 {
8698 /* <sugline>: <sugnr> ... NUL */
8699 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8700 len = STRLEN(line) + 1;
8701 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8702 {
8703 EMSG(_(e_write));
8704 goto theend;
8705 }
8706 spin->si_memtot += len;
8707 }
8708
8709 /* Write another byte to check for errors. */
8710 if (putc(0, fd) == EOF)
8711 EMSG(_(e_write));
8712
8713 vim_snprintf((char *)IObuff, IOSIZE,
8714 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8715 spell_message(spin, IObuff);
8716
8717theend:
8718 /* close the file */
8719 fclose(fd);
8720}
8721
8722/*
8723 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8724 * list and only contains text lines. Can use a swapfile to reduce memory
8725 * use.
8726 * Most other fields are invalid! Esp. watch out for string options being
8727 * NULL and there is no undo info.
8728 * Returns NULL when out of memory.
8729 */
8730 static buf_T *
8731open_spellbuf()
8732{
8733 buf_T *buf;
8734
8735 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8736 if (buf != NULL)
8737 {
8738 buf->b_spell = TRUE;
8739 buf->b_p_swf = TRUE; /* may create a swap file */
8740 ml_open(buf);
8741 ml_open_file(buf); /* create swap file now */
8742 }
8743 return buf;
8744}
8745
8746/*
8747 * Close the buffer used for spell info.
8748 */
8749 static void
8750close_spellbuf(buf)
8751 buf_T *buf;
8752{
8753 if (buf != NULL)
8754 {
8755 ml_close(buf, TRUE);
8756 vim_free(buf);
8757 }
8758}
8759
8760
8761/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008762 * Create a Vim spell file from one or more word lists.
8763 * "fnames[0]" is the output file name.
8764 * "fnames[fcount - 1]" is the last input file name.
8765 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8766 * and ".spl" is appended to make the output file name.
8767 */
8768 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008770 int fcount;
8771 char_u **fnames;
8772 int ascii; /* -ascii argument given */
8773 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008775{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008776 char_u fname[MAXPATHL];
8777 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008778 char_u **innames;
8779 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008780 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008781 int i;
8782 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008783 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008784 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008785 spellinfo_T spin;
8786
8787 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008789 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 spin.si_followup = TRUE;
8791 spin.si_rem_accents = TRUE;
8792 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008793 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8795 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008796 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008797 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008798 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008799 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008800
Bram Moolenaarb765d632005-06-07 21:00:02 +00008801 /* default: fnames[0] is output file, following are input files */
8802 innames = &fnames[1];
8803 incount = fcount - 1;
8804
8805 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008806 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008807 len = STRLEN(fnames[0]);
8808 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8809 {
8810 /* For ":mkspell path/en.latin1.add" output file is
8811 * "path/en.latin1.add.spl". */
8812 innames = &fnames[0];
8813 incount = 1;
8814 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8815 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008816 else if (fcount == 1)
8817 {
8818 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8819 innames = &fnames[0];
8820 incount = 1;
8821 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8822 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8823 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008824 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8825 {
8826 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008827 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008828 }
8829 else
8830 /* Name should be language, make the file name from it. */
8831 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8832 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8833
8834 /* Check for .ascii.spl. */
8835 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
8836 spin.si_ascii = TRUE;
8837
8838 /* Check for .add.spl. */
8839 if (strstr((char *)gettail(wfname), ".add.") != NULL)
8840 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00008841 }
8842
Bram Moolenaarb765d632005-06-07 21:00:02 +00008843 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008844 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008845 else if (vim_strchr(gettail(wfname), '_') != NULL)
8846 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008847 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008848 EMSG(_("E754: Only up to 8 regions supported"));
8849 else
8850 {
8851 /* Check for overwriting before doing things that may take a lot of
8852 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008853 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008854 {
8855 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008856 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008857 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008858 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008859 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008860 EMSG2(_(e_isadir2), wfname);
8861 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008862 }
8863
8864 /*
8865 * Init the aff and dic pointers.
8866 * Get the region names if there are more than 2 arguments.
8867 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008868 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008869 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008870 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008871
Bram Moolenaar3982c542005-06-08 21:56:31 +00008872 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008873 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008874 len = STRLEN(innames[i]);
8875 if (STRLEN(gettail(innames[i])) < 5
8876 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008877 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008878 EMSG2(_("E755: Invalid region in %s"), innames[i]);
8879 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008880 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008881 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
8882 spin.si_region_name[i * 2 + 1] =
8883 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008884 }
8885 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008886 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008887
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008888 spin.si_foldroot = wordtree_alloc(&spin);
8889 spin.si_keeproot = wordtree_alloc(&spin);
8890 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008891 if (spin.si_foldroot == NULL
8892 || spin.si_keeproot == NULL
8893 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008894 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008895 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008896 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008897 }
8898
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008899 /* When not producing a .add.spl file clear the character table when
8900 * we encounter one in the .aff file. This means we dump the current
8901 * one in the .spl file if the .aff file doesn't define one. That's
8902 * better than guessing the contents, the table will match a
8903 * previously loaded spell file. */
8904 if (!spin.si_add)
8905 spin.si_clear_chartab = TRUE;
8906
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008907 /*
8908 * Read all the .aff and .dic files.
8909 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00008910 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008911 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008912 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008913 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008914 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008915 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008916
Bram Moolenaarb765d632005-06-07 21:00:02 +00008917 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008918 if (mch_stat((char *)fname, &st) >= 0)
8919 {
8920 /* Read the .aff file. Will init "spin->si_conv" based on the
8921 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008922 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008923 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008924 error = TRUE;
8925 else
8926 {
8927 /* Read the .dic file and store the words in the trees. */
8928 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00008929 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008930 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008931 error = TRUE;
8932 }
8933 }
8934 else
8935 {
8936 /* No .aff file, try reading the file as a word list. Store
8937 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008938 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008939 error = TRUE;
8940 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008941
Bram Moolenaarb765d632005-06-07 21:00:02 +00008942#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008943 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008944 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008945#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008946 }
8947
Bram Moolenaar78622822005-08-23 21:00:13 +00008948 if (spin.si_compflags != NULL && spin.si_nobreak)
8949 MSG(_("Warning: both compounding and NOBREAK specified"));
8950
Bram Moolenaar4770d092006-01-12 23:22:24 +00008951 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008952 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008953 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008954 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008955 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008956 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008957 wordtree_compress(&spin, spin.si_foldroot);
8958 wordtree_compress(&spin, spin.si_keeproot);
8959 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008960 }
8961
Bram Moolenaar4770d092006-01-12 23:22:24 +00008962 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008963 {
8964 /*
8965 * Write the info in the spell file.
8966 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008967 vim_snprintf((char *)IObuff, IOSIZE,
8968 _("Writing spell file %s ..."), wfname);
8969 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00008970
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008971 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008972
Bram Moolenaar4770d092006-01-12 23:22:24 +00008973 spell_message(&spin, (char_u *)_("Done!"));
8974 vim_snprintf((char *)IObuff, IOSIZE,
8975 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
8976 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008977
Bram Moolenaar4770d092006-01-12 23:22:24 +00008978 /*
8979 * If the file is loaded need to reload it.
8980 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008981 if (!error)
8982 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008983 }
8984
8985 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008987 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 ga_clear(&spin.si_sal);
8989 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008990 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008991 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008992 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008993
8994 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008995 for (i = 0; i < incount; ++i)
8996 if (afile[i] != NULL)
8997 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008998
8999 /* Free all the bits and pieces at once. */
9000 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009001
9002 /*
9003 * If there is soundfolding info and no NOSUGFILE item create the
9004 * .sug file with the soundfolded word trie.
9005 */
9006 if (spin.si_sugtime != 0 && !error && !got_int)
9007 spell_make_sugfile(&spin, wfname);
9008
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009009 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009010}
9011
Bram Moolenaar4770d092006-01-12 23:22:24 +00009012/*
9013 * Display a message for spell file processing when 'verbose' is set or using
9014 * ":mkspell". "str" can be IObuff.
9015 */
9016 static void
9017spell_message(spin, str)
9018 spellinfo_T *spin;
9019 char_u *str;
9020{
9021 if (spin->si_verbose || p_verbose > 2)
9022 {
9023 if (!spin->si_verbose)
9024 verbose_enter();
9025 MSG(str);
9026 out_flush();
9027 if (!spin->si_verbose)
9028 verbose_leave();
9029 }
9030}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009031
9032/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009033 * ":[count]spellgood {word}"
9034 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009035 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009036 */
9037 void
9038ex_spell(eap)
9039 exarg_T *eap;
9040{
Bram Moolenaar7887d882005-07-01 22:33:52 +00009041 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009042 eap->forceit ? 0 : (int)eap->line2,
9043 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009044}
9045
9046/*
9047 * Add "word[len]" to 'spellfile' as a good or bad word.
9048 */
9049 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00009050spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009051 char_u *word;
9052 int len;
9053 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009054 int index; /* "zG" and "zW": zero, otherwise index in
9055 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009056 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009057{
9058 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009059 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009060 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009061 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009062 char_u fnamebuf[MAXPATHL];
9063 char_u line[MAXWLEN * 2];
9064 long fpos, fpos_next = 0;
9065 int i;
9066 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009067
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009068 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009069 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009070 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009071 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009072 int_wordlist = vim_tempname('s');
9073 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009074 return;
9075 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009076 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009077 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009078 else
9079 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009080 /* If 'spellfile' isn't set figure out a good default value. */
9081 if (*curbuf->b_p_spf == NUL)
9082 {
9083 init_spellfile();
9084 new_spf = TRUE;
9085 }
9086
9087 if (*curbuf->b_p_spf == NUL)
9088 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009089 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009090 return;
9091 }
9092
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009093 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9094 {
9095 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9096 if (i == index)
9097 break;
9098 if (*spf == NUL)
9099 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00009100 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009101 return;
9102 }
9103 }
9104
Bram Moolenaarb765d632005-06-07 21:00:02 +00009105 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009106 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009107 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9108 buf = NULL;
9109 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009110 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009111 EMSG(_(e_bufloaded));
9112 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009113 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009114
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009115 fname = fnamebuf;
9116 }
9117
Bram Moolenaard0131a82006-03-04 21:46:13 +00009118 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009119 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009120 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009121 * since its flags sort before the one with WF_BANNED. */
9122 fd = mch_fopen((char *)fname, "r");
9123 if (fd != NULL)
9124 {
9125 while (!vim_fgets(line, MAXWLEN * 2, fd))
9126 {
9127 fpos = fpos_next;
9128 fpos_next = ftell(fd);
9129 if (STRNCMP(word, line, len) == 0
9130 && (line[len] == '/' || line[len] < ' '))
9131 {
9132 /* Found duplicate word. Remove it by writing a '#' at
9133 * the start of the line. Mixing reading and writing
9134 * doesn't work for all systems, close the file first. */
9135 fclose(fd);
9136 fd = mch_fopen((char *)fname, "r+");
9137 if (fd == NULL)
9138 break;
9139 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009140 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009141 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009142 if (undo)
9143 smsg((char_u *)_("Word removed from %s"), NameBuff);
9144 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009145 fseek(fd, fpos_next, SEEK_SET);
9146 }
9147 }
9148 fclose(fd);
9149 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009150 }
9151
Bram Moolenaard0131a82006-03-04 21:46:13 +00009152 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009153 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009154 fd = mch_fopen((char *)fname, "a");
9155 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009156 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009157 /* We just initialized the 'spellfile' option and can't open the
9158 * file. We may need to create the "spell" directory first. We
9159 * already checked the runtime directory is writable in
9160 * init_spellfile(). */
9161 if (!dir_of_file_exists(fname))
9162 {
9163 /* The directory doesn't exist. Try creating it and opening
9164 * the file again. */
9165 vim_mkdir(NameBuff, 0755);
9166 fd = mch_fopen((char *)fname, "a");
9167 }
9168 }
9169
9170 if (fd == NULL)
9171 EMSG2(_(e_notopen), fname);
9172 else
9173 {
9174 if (bad)
9175 fprintf(fd, "%.*s/!\n", len, word);
9176 else
9177 fprintf(fd, "%.*s\n", len, word);
9178 fclose(fd);
9179
9180 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9181 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009182 }
9183 }
9184
Bram Moolenaard0131a82006-03-04 21:46:13 +00009185 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009186 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009187 /* Update the .add.spl file. */
9188 mkspell(1, &fname, FALSE, TRUE, TRUE);
9189
9190 /* If the .add file is edited somewhere, reload it. */
9191 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009192 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009193
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009194 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009195 }
9196}
9197
9198/*
9199 * Initialize 'spellfile' for the current buffer.
9200 */
9201 static void
9202init_spellfile()
9203{
9204 char_u buf[MAXPATHL];
9205 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009206 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009207 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009208 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009209 int aspath = FALSE;
9210 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009211
9212 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9213 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009214 /* Find the end of the language name. Exclude the region. If there
9215 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009216 for (lend = curbuf->b_p_spl; *lend != NUL
9217 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009218 if (vim_ispathsep(*lend))
9219 {
9220 aspath = TRUE;
9221 lstart = lend + 1;
9222 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009223
9224 /* Loop over all entries in 'runtimepath'. Use the first one where we
9225 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009226 rtp = p_rtp;
9227 while (*rtp != NUL)
9228 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009229 if (aspath)
9230 /* Use directory of an entry with path, e.g., for
9231 * "/dir/lg.utf-8.spl" use "/dir". */
9232 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9233 else
9234 /* Copy the path from 'runtimepath' to buf[]. */
9235 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009236 if (filewritable(buf) == 2)
9237 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009238 /* Use the first language name from 'spelllang' and the
9239 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009240 if (aspath)
9241 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9242 else
9243 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009244 /* Create the "spell" directory if it doesn't exist yet. */
9245 l = STRLEN(buf);
9246 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9247 if (!filewritable(buf) != 2)
9248 vim_mkdir(buf, 0755);
9249
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009250 l = STRLEN(buf);
9251 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009252 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009253 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009254 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009255 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9256 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9257 fname != NULL
9258 && strstr((char *)gettail(fname), ".ascii.") != NULL
9259 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009260 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9261 break;
9262 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009263 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009264 }
9265 }
9266}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009267
Bram Moolenaar51485f02005-06-04 21:55:20 +00009268
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009269/*
9270 * Init the chartab used for spelling for ASCII.
9271 * EBCDIC is not supported!
9272 */
9273 static void
9274clear_spell_chartab(sp)
9275 spelltab_T *sp;
9276{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009277 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009278
9279 /* Init everything to FALSE. */
9280 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9281 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9282 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009283 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009284 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009285 sp->st_upper[i] = i;
9286 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009287
9288 /* We include digits. A word shouldn't start with a digit, but handling
9289 * that is done separately. */
9290 for (i = '0'; i <= '9'; ++i)
9291 sp->st_isw[i] = TRUE;
9292 for (i = 'A'; i <= 'Z'; ++i)
9293 {
9294 sp->st_isw[i] = TRUE;
9295 sp->st_isu[i] = TRUE;
9296 sp->st_fold[i] = i + 0x20;
9297 }
9298 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009299 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009300 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009301 sp->st_upper[i] = i - 0x20;
9302 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009303}
9304
9305/*
9306 * Init the chartab used for spelling. Only depends on 'encoding'.
9307 * Called once while starting up and when 'encoding' changes.
9308 * The default is to use isalpha(), but the spell file should define the word
9309 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009310 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009311 */
9312 void
9313init_spell_chartab()
9314{
9315 int i;
9316
9317 did_set_spelltab = FALSE;
9318 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009319#ifdef FEAT_MBYTE
9320 if (enc_dbcs)
9321 {
9322 /* DBCS: assume double-wide characters are word characters. */
9323 for (i = 128; i <= 255; ++i)
9324 if (MB_BYTE2LEN(i) == 2)
9325 spelltab.st_isw[i] = TRUE;
9326 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009327 else if (enc_utf8)
9328 {
9329 for (i = 128; i < 256; ++i)
9330 {
9331 spelltab.st_isu[i] = utf_isupper(i);
9332 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9333 spelltab.st_fold[i] = utf_fold(i);
9334 spelltab.st_upper[i] = utf_toupper(i);
9335 }
9336 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009337 else
9338#endif
9339 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009340 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009341 for (i = 128; i < 256; ++i)
9342 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009343 if (MB_ISUPPER(i))
9344 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009345 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009346 spelltab.st_isu[i] = TRUE;
9347 spelltab.st_fold[i] = MB_TOLOWER(i);
9348 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009349 else if (MB_ISLOWER(i))
9350 {
9351 spelltab.st_isw[i] = TRUE;
9352 spelltab.st_upper[i] = MB_TOUPPER(i);
9353 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009354 }
9355 }
9356}
9357
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009358/*
9359 * Set the spell character tables from strings in the affix file.
9360 */
9361 static int
9362set_spell_chartab(fol, low, upp)
9363 char_u *fol;
9364 char_u *low;
9365 char_u *upp;
9366{
9367 /* We build the new tables here first, so that we can compare with the
9368 * previous one. */
9369 spelltab_T new_st;
9370 char_u *pf = fol, *pl = low, *pu = upp;
9371 int f, l, u;
9372
9373 clear_spell_chartab(&new_st);
9374
9375 while (*pf != NUL)
9376 {
9377 if (*pl == NUL || *pu == NUL)
9378 {
9379 EMSG(_(e_affform));
9380 return FAIL;
9381 }
9382#ifdef FEAT_MBYTE
9383 f = mb_ptr2char_adv(&pf);
9384 l = mb_ptr2char_adv(&pl);
9385 u = mb_ptr2char_adv(&pu);
9386#else
9387 f = *pf++;
9388 l = *pl++;
9389 u = *pu++;
9390#endif
9391 /* Every character that appears is a word character. */
9392 if (f < 256)
9393 new_st.st_isw[f] = TRUE;
9394 if (l < 256)
9395 new_st.st_isw[l] = TRUE;
9396 if (u < 256)
9397 new_st.st_isw[u] = TRUE;
9398
9399 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9400 * case-folding */
9401 if (l < 256 && l != f)
9402 {
9403 if (f >= 256)
9404 {
9405 EMSG(_(e_affrange));
9406 return FAIL;
9407 }
9408 new_st.st_fold[l] = f;
9409 }
9410
9411 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009412 * case-folding, it's upper case and the "UPP" is the upper case of
9413 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009414 if (u < 256 && u != f)
9415 {
9416 if (f >= 256)
9417 {
9418 EMSG(_(e_affrange));
9419 return FAIL;
9420 }
9421 new_st.st_fold[u] = f;
9422 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009423 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009424 }
9425 }
9426
9427 if (*pl != NUL || *pu != NUL)
9428 {
9429 EMSG(_(e_affform));
9430 return FAIL;
9431 }
9432
9433 return set_spell_finish(&new_st);
9434}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009435
9436/*
9437 * Set the spell character tables from strings in the .spl file.
9438 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009439 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009440set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009441 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009442 int cnt; /* length of "flags" */
9443 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009444{
9445 /* We build the new tables here first, so that we can compare with the
9446 * previous one. */
9447 spelltab_T new_st;
9448 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009449 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009450 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009451
9452 clear_spell_chartab(&new_st);
9453
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009454 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009455 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009456 if (i < cnt)
9457 {
9458 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9459 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9460 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009461
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009462 if (*p != NUL)
9463 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009464#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009465 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009466#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009467 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009468#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009469 new_st.st_fold[i + 128] = c;
9470 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9471 new_st.st_upper[c] = i + 128;
9472 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009473 }
9474
Bram Moolenaar5195e452005-08-19 20:32:47 +00009475 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009476}
9477
9478 static int
9479set_spell_finish(new_st)
9480 spelltab_T *new_st;
9481{
9482 int i;
9483
9484 if (did_set_spelltab)
9485 {
9486 /* check that it's the same table */
9487 for (i = 0; i < 256; ++i)
9488 {
9489 if (spelltab.st_isw[i] != new_st->st_isw[i]
9490 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009491 || spelltab.st_fold[i] != new_st->st_fold[i]
9492 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009493 {
9494 EMSG(_("E763: Word characters differ between spell files"));
9495 return FAIL;
9496 }
9497 }
9498 }
9499 else
9500 {
9501 /* copy the new spelltab into the one being used */
9502 spelltab = *new_st;
9503 did_set_spelltab = TRUE;
9504 }
9505
9506 return OK;
9507}
9508
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009509/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009510 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009511 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009512 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009513 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009514 */
9515 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009516spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009517 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009518 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009519{
Bram Moolenaarea408852005-06-25 22:49:46 +00009520#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009521 char_u *s;
9522 int l;
9523 int c;
9524
9525 if (has_mbyte)
9526 {
9527 l = MB_BYTE2LEN(*p);
9528 s = p;
9529 if (l == 1)
9530 {
9531 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009532 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009533 {
9534 s = p + 1; /* skip a mid-word character */
9535 l = MB_BYTE2LEN(*s);
9536 }
9537 }
9538 else
9539 {
9540 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009541 if (c < 256 ? buf->b_spell_ismw[c]
9542 : (buf->b_spell_ismw_mb != NULL
9543 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009544 {
9545 s = p + l;
9546 l = MB_BYTE2LEN(*s);
9547 }
9548 }
9549
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009550 c = mb_ptr2char(s);
9551 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009552 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009553 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009554 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009555#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009556
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009557 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9558}
9559
9560/*
9561 * Return TRUE if "p" points to a word character.
9562 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9563 */
9564 static int
9565spell_iswordp_nmw(p)
9566 char_u *p;
9567{
9568#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009569 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009570
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009571 if (has_mbyte)
9572 {
9573 c = mb_ptr2char(p);
9574 if (c > 255)
9575 return mb_get_class(p) >= 2;
9576 return spelltab.st_isw[c];
9577 }
9578#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009579 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009580}
9581
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009582#ifdef FEAT_MBYTE
9583/*
9584 * Return TRUE if "p" points to a word character.
9585 * Wide version of spell_iswordp().
9586 */
9587 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009588spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009589 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009590 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009591{
9592 int *s;
9593
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009594 if (*p < 256 ? buf->b_spell_ismw[*p]
9595 : (buf->b_spell_ismw_mb != NULL
9596 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009597 s = p + 1;
9598 else
9599 s = p;
9600
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009601 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009602 {
9603 if (enc_utf8)
9604 return utf_class(*s) >= 2;
9605 if (enc_dbcs)
9606 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9607 return 0;
9608 }
9609 return spelltab.st_isw[*s];
9610}
9611#endif
9612
Bram Moolenaarea408852005-06-25 22:49:46 +00009613/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009614 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009615 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009616 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009617 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009618write_spell_prefcond(fd, gap)
9619 FILE *fd;
9620 garray_T *gap;
9621{
9622 int i;
9623 char_u *p;
9624 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009625 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009626
Bram Moolenaar5195e452005-08-19 20:32:47 +00009627 if (fd != NULL)
9628 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9629
9630 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009631
9632 for (i = 0; i < gap->ga_len; ++i)
9633 {
9634 /* <prefcond> : <condlen> <condstr> */
9635 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009636 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009637 {
9638 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009639 if (fd != NULL)
9640 {
9641 fputc(len, fd);
9642 fwrite(p, (size_t)len, (size_t)1, fd);
9643 }
9644 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009645 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009646 else if (fd != NULL)
9647 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009648 }
9649
Bram Moolenaar5195e452005-08-19 20:32:47 +00009650 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009651}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009652
9653/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009654 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9655 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009656 * When using a multi-byte 'encoding' the length may change!
9657 * Returns FAIL when something wrong.
9658 */
9659 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009660spell_casefold(str, len, buf, buflen)
9661 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009662 int len;
9663 char_u *buf;
9664 int buflen;
9665{
9666 int i;
9667
9668 if (len >= buflen)
9669 {
9670 buf[0] = NUL;
9671 return FAIL; /* result will not fit */
9672 }
9673
9674#ifdef FEAT_MBYTE
9675 if (has_mbyte)
9676 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009677 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009678 char_u *p;
9679 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009680
9681 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009682 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009683 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009684 if (outi + MB_MAXBYTES > buflen)
9685 {
9686 buf[outi] = NUL;
9687 return FAIL;
9688 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009689 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009690 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009691 }
9692 buf[outi] = NUL;
9693 }
9694 else
9695#endif
9696 {
9697 /* Be quick for non-multibyte encodings. */
9698 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009699 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009700 buf[i] = NUL;
9701 }
9702
9703 return OK;
9704}
9705
Bram Moolenaar4770d092006-01-12 23:22:24 +00009706/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009707#define SPS_BEST 1
9708#define SPS_FAST 2
9709#define SPS_DOUBLE 4
9710
Bram Moolenaar4770d092006-01-12 23:22:24 +00009711static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9712static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009713
9714/*
9715 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009716 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009717 */
9718 int
9719spell_check_sps()
9720{
9721 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009722 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009723 char_u buf[MAXPATHL];
9724 int f;
9725
9726 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009727 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009728
9729 for (p = p_sps; *p != NUL; )
9730 {
9731 copy_option_part(&p, buf, MAXPATHL, ",");
9732
9733 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009734 if (VIM_ISDIGIT(*buf))
9735 {
9736 s = buf;
9737 sps_limit = getdigits(&s);
9738 if (*s != NUL && !VIM_ISDIGIT(*s))
9739 f = -1;
9740 }
9741 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009742 f = SPS_BEST;
9743 else if (STRCMP(buf, "fast") == 0)
9744 f = SPS_FAST;
9745 else if (STRCMP(buf, "double") == 0)
9746 f = SPS_DOUBLE;
9747 else if (STRNCMP(buf, "expr:", 5) != 0
9748 && STRNCMP(buf, "file:", 5) != 0)
9749 f = -1;
9750
9751 if (f == -1 || (sps_flags != 0 && f != 0))
9752 {
9753 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009754 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009755 return FAIL;
9756 }
9757 if (f != 0)
9758 sps_flags = f;
9759 }
9760
9761 if (sps_flags == 0)
9762 sps_flags = SPS_BEST;
9763
9764 return OK;
9765}
9766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767/*
9768 * "z?": Find badly spelled word under or after the cursor.
9769 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009770 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009771 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 */
9773 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009774spell_suggest(count)
9775 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776{
9777 char_u *line;
9778 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 char_u wcopy[MAXWLEN + 2];
9780 char_u *p;
9781 int i;
9782 int c;
9783 suginfo_T sug;
9784 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009785 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009786 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009787 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009788 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009789 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009791 if (no_spell_checking(curwin))
9792 return;
9793
9794#ifdef FEAT_VISUAL
9795 if (VIsual_active)
9796 {
9797 /* Use the Visually selected text as the bad word. But reject
9798 * a multi-line selection. */
9799 if (curwin->w_cursor.lnum != VIsual.lnum)
9800 {
9801 vim_beep();
9802 return;
9803 }
9804 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9805 if (badlen < 0)
9806 badlen = -badlen;
9807 else
9808 curwin->w_cursor.col = VIsual.col;
9809 ++badlen;
9810 end_visual_mode();
9811 }
9812 else
9813#endif
9814 /* Find the start of the badly spelled word. */
9815 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009816 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009817 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009818 /* No bad word or it starts after the cursor: use the word under the
9819 * cursor. */
9820 curwin->w_cursor = prev_cursor;
9821 line = ml_get_curline();
9822 p = line + curwin->w_cursor.col;
9823 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009824 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009825 mb_ptr_back(line, p);
9826 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009827 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009828 mb_ptr_adv(p);
9829
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009830 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009831 {
9832 beep_flush();
9833 return;
9834 }
9835 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009836 }
9837
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009838 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009840 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009841 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009842
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009843 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009844
Bram Moolenaar5195e452005-08-19 20:32:47 +00009845 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
9846 * 'spellsuggest', whatever is smaller. */
9847 if (sps_limit > (int)Rows - 2)
9848 limit = (int)Rows - 2;
9849 else
9850 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009851 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009852 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853
9854 if (sug.su_ga.ga_len == 0)
9855 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009856 else if (count > 0)
9857 {
9858 if (count > sug.su_ga.ga_len)
9859 smsg((char_u *)_("Sorry, only %ld suggestions"),
9860 (long)sug.su_ga.ga_len);
9861 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009862 else
9863 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009864 vim_free(repl_from);
9865 repl_from = NULL;
9866 vim_free(repl_to);
9867 repl_to = NULL;
9868
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009869#ifdef FEAT_RIGHTLEFT
9870 /* When 'rightleft' is set the list is drawn right-left. */
9871 cmdmsg_rl = curwin->w_p_rl;
9872 if (cmdmsg_rl)
9873 msg_col = Columns - 1;
9874#endif
9875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 /* List the suggestions. */
9877 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009878 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
9880 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009881#ifdef FEAT_RIGHTLEFT
9882 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
9883 {
9884 /* And now the rabbit from the high hat: Avoid showing the
9885 * untranslated message rightleft. */
9886 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
9887 sug.su_badlen, sug.su_badptr);
9888 }
9889#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 msg_puts(IObuff);
9891 msg_clr_eos();
9892 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00009893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 msg_scroll = TRUE;
9895 for (i = 0; i < sug.su_ga.ga_len; ++i)
9896 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009897 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898
9899 /* The suggested word may replace only part of the bad word, add
9900 * the not replaced part. */
9901 STRCPY(wcopy, stp->st_word);
9902 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009903 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 sug.su_badptr + stp->st_orglen,
9905 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009906 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
9907#ifdef FEAT_RIGHTLEFT
9908 if (cmdmsg_rl)
9909 rl_mirror(IObuff);
9910#endif
9911 msg_puts(IObuff);
9912
9913 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009914 msg_puts(IObuff);
9915
9916 /* The word may replace more than "su_badlen". */
9917 if (sug.su_badlen < stp->st_orglen)
9918 {
9919 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
9920 stp->st_orglen, sug.su_badptr);
9921 msg_puts(IObuff);
9922 }
9923
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009924 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009925 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009926 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009927 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009928 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009929 stp->st_salscore ? "s " : "",
9930 stp->st_score, stp->st_altscore);
9931 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009932 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00009933 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009934#ifdef FEAT_RIGHTLEFT
9935 if (cmdmsg_rl)
9936 /* Mirror the numbers, but keep the leading space. */
9937 rl_mirror(IObuff + 1);
9938#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00009939 msg_advance(30);
9940 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 msg_putchar('\n');
9943 }
9944
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009945#ifdef FEAT_RIGHTLEFT
9946 cmdmsg_rl = FALSE;
9947 msg_col = 0;
9948#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009950 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009951 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00009952 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00009953 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 }
9955
Bram Moolenaard12a1322005-08-21 22:08:24 +00009956 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
9957 {
9958 /* Save the from and to text for :spellrepall. */
9959 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00009960 if (sug.su_badlen > stp->st_orglen)
9961 {
9962 /* Replacing less than "su_badlen", append the remainder to
9963 * repl_to. */
9964 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
9965 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
9966 sug.su_badlen - stp->st_orglen,
9967 sug.su_badptr + stp->st_orglen);
9968 repl_to = vim_strsave(IObuff);
9969 }
9970 else
9971 {
9972 /* Replacing su_badlen or more, use the whole word. */
9973 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
9974 repl_to = vim_strsave(stp->st_word);
9975 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009976
9977 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009978 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009979 if (p != NULL)
9980 {
9981 c = sug.su_badptr - line;
9982 mch_memmove(p, line, c);
9983 STRCPY(p + c, stp->st_word);
9984 STRCAT(p, sug.su_badptr + stp->st_orglen);
9985 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9986 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009987
9988 /* For redo we use a change-word command. */
9989 ResetRedobuff();
9990 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00009991 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009992 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009993 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009994
9995 /* After this "p" may be invalid. */
9996 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009997 }
9998 }
9999 else
10000 curwin->w_cursor = prev_cursor;
10001
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010002 spell_find_cleanup(&sug);
10003}
10004
10005/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010006 * Check if the word at line "lnum" column "col" is required to start with a
10007 * capital. This uses 'spellcapcheck' of the current buffer.
10008 */
10009 static int
10010check_need_cap(lnum, col)
10011 linenr_T lnum;
10012 colnr_T col;
10013{
10014 int need_cap = FALSE;
10015 char_u *line;
10016 char_u *line_copy = NULL;
10017 char_u *p;
10018 colnr_T endcol;
10019 regmatch_T regmatch;
10020
10021 if (curbuf->b_cap_prog == NULL)
10022 return FALSE;
10023
10024 line = ml_get_curline();
10025 endcol = 0;
10026 if ((int)(skipwhite(line) - line) >= (int)col)
10027 {
10028 /* At start of line, check if previous line is empty or sentence
10029 * ends there. */
10030 if (lnum == 1)
10031 need_cap = TRUE;
10032 else
10033 {
10034 line = ml_get(lnum - 1);
10035 if (*skipwhite(line) == NUL)
10036 need_cap = TRUE;
10037 else
10038 {
10039 /* Append a space in place of the line break. */
10040 line_copy = concat_str(line, (char_u *)" ");
10041 line = line_copy;
10042 endcol = STRLEN(line);
10043 }
10044 }
10045 }
10046 else
10047 endcol = col;
10048
10049 if (endcol > 0)
10050 {
10051 /* Check if sentence ends before the bad word. */
10052 regmatch.regprog = curbuf->b_cap_prog;
10053 regmatch.rm_ic = FALSE;
10054 p = line + endcol;
10055 for (;;)
10056 {
10057 mb_ptr_back(line, p);
10058 if (p == line || spell_iswordp_nmw(p))
10059 break;
10060 if (vim_regexec(&regmatch, p, 0)
10061 && regmatch.endp[0] == line + endcol)
10062 {
10063 need_cap = TRUE;
10064 break;
10065 }
10066 }
10067 }
10068
10069 vim_free(line_copy);
10070
10071 return need_cap;
10072}
10073
10074
10075/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010076 * ":spellrepall"
10077 */
10078/*ARGSUSED*/
10079 void
10080ex_spellrepall(eap)
10081 exarg_T *eap;
10082{
10083 pos_T pos = curwin->w_cursor;
10084 char_u *frompat;
10085 int addlen;
10086 char_u *line;
10087 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010088 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010089 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010090
10091 if (repl_from == NULL || repl_to == NULL)
10092 {
10093 EMSG(_("E752: No previous spell replacement"));
10094 return;
10095 }
10096 addlen = STRLEN(repl_to) - STRLEN(repl_from);
10097
10098 frompat = alloc(STRLEN(repl_from) + 7);
10099 if (frompat == NULL)
10100 return;
10101 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10102 p_ws = FALSE;
10103
Bram Moolenaar5195e452005-08-19 20:32:47 +000010104 sub_nsubs = 0;
10105 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010106 curwin->w_cursor.lnum = 0;
10107 while (!got_int)
10108 {
10109 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10110 || u_save_cursor() == FAIL)
10111 break;
10112
10113 /* Only replace when the right word isn't there yet. This happens
10114 * when changing "etc" to "etc.". */
10115 line = ml_get_curline();
10116 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10117 repl_to, STRLEN(repl_to)) != 0)
10118 {
10119 p = alloc(STRLEN(line) + addlen + 1);
10120 if (p == NULL)
10121 break;
10122 mch_memmove(p, line, curwin->w_cursor.col);
10123 STRCPY(p + curwin->w_cursor.col, repl_to);
10124 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10125 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10126 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010127
10128 if (curwin->w_cursor.lnum != prev_lnum)
10129 {
10130 ++sub_nlines;
10131 prev_lnum = curwin->w_cursor.lnum;
10132 }
10133 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010134 }
10135 curwin->w_cursor.col += STRLEN(repl_to);
10136 }
10137
10138 p_ws = save_ws;
10139 curwin->w_cursor = pos;
10140 vim_free(frompat);
10141
Bram Moolenaar5195e452005-08-19 20:32:47 +000010142 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010143 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010144 else
10145 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010146}
10147
10148/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010149 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10150 * a list of allocated strings.
10151 */
10152 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010153spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010154 garray_T *gap;
10155 char_u *word;
10156 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010157 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010158 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010159{
10160 suginfo_T sug;
10161 int i;
10162 suggest_T *stp;
10163 char_u *wcopy;
10164
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010165 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010166
10167 /* Make room in "gap". */
10168 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010169 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010170 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010171 for (i = 0; i < sug.su_ga.ga_len; ++i)
10172 {
10173 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010174
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010175 /* The suggested word may replace only part of "word", add the not
10176 * replaced part. */
10177 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010178 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010179 if (wcopy == NULL)
10180 break;
10181 STRCPY(wcopy, stp->st_word);
10182 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10183 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10184 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010185 }
10186
10187 spell_find_cleanup(&sug);
10188}
10189
10190/*
10191 * Find spell suggestions for the word at the start of "badptr".
10192 * Return the suggestions in "su->su_ga".
10193 * The maximum number of suggestions is "maxcount".
10194 * Note: does use info for the current window.
10195 * This is based on the mechanisms of Aspell, but completely reimplemented.
10196 */
10197 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010198spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010199 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010200 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010201 suginfo_T *su;
10202 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010203 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010204 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010205 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010206{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010207 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010208 char_u buf[MAXPATHL];
10209 char_u *p;
10210 int do_combine = FALSE;
10211 char_u *sps_copy;
10212#ifdef FEAT_EVAL
10213 static int expr_busy = FALSE;
10214#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010215 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010216 int i;
10217 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010218
10219 /*
10220 * Set the info in "*su".
10221 */
10222 vim_memset(su, 0, sizeof(suginfo_T));
10223 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10224 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010225 if (*badptr == NUL)
10226 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010227 hash_init(&su->su_banned);
10228
10229 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010230 if (badlen != 0)
10231 su->su_badlen = badlen;
10232 else
10233 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010234 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010235 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010236
10237 if (su->su_badlen >= MAXWLEN)
10238 su->su_badlen = MAXWLEN - 1; /* just in case */
10239 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10240 (void)spell_casefold(su->su_badptr, su->su_badlen,
10241 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010242 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010243 su->su_badflags = badword_captype(su->su_badptr,
10244 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010245 if (need_cap)
10246 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010247
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010248 /* Find the default language for sound folding. We simply use the first
10249 * one in 'spelllang' that supports sound folding. That's good for when
10250 * using multiple files for one language, it's not that bad when mixing
10251 * languages (e.g., "pl,en"). */
10252 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10253 {
10254 lp = LANGP_ENTRY(curbuf->b_langp, i);
10255 if (lp->lp_sallang != NULL)
10256 {
10257 su->su_sallang = lp->lp_sallang;
10258 break;
10259 }
10260 }
10261
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010262 /* Soundfold the bad word with the default sound folding, so that we don't
10263 * have to do this many times. */
10264 if (su->su_sallang != NULL)
10265 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10266 su->su_sal_badword);
10267
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010268 /* If the word is not capitalised and spell_check() doesn't consider the
10269 * word to be bad then it might need to be capitalised. Add a suggestion
10270 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010271 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010272 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010273 {
10274 make_case_word(su->su_badword, buf, WF_ONECAP);
10275 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010276 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010277 }
10278
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010279 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010280 if (banbadword)
10281 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010282
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010283 /* Make a copy of 'spellsuggest', because the expression may change it. */
10284 sps_copy = vim_strsave(p_sps);
10285 if (sps_copy == NULL)
10286 return;
10287
10288 /* Loop over the items in 'spellsuggest'. */
10289 for (p = sps_copy; *p != NUL; )
10290 {
10291 copy_option_part(&p, buf, MAXPATHL, ",");
10292
10293 if (STRNCMP(buf, "expr:", 5) == 0)
10294 {
10295#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010296 /* Evaluate an expression. Skip this when called recursively,
10297 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010298 if (!expr_busy)
10299 {
10300 expr_busy = TRUE;
10301 spell_suggest_expr(su, buf + 5);
10302 expr_busy = FALSE;
10303 }
10304#endif
10305 }
10306 else if (STRNCMP(buf, "file:", 5) == 0)
10307 /* Use list of suggestions in a file. */
10308 spell_suggest_file(su, buf + 5);
10309 else
10310 {
10311 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010312 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010313 if (sps_flags & SPS_DOUBLE)
10314 do_combine = TRUE;
10315 }
10316 }
10317
10318 vim_free(sps_copy);
10319
10320 if (do_combine)
10321 /* Combine the two list of suggestions. This must be done last,
10322 * because sorting changes the order again. */
10323 score_combine(su);
10324}
10325
10326#ifdef FEAT_EVAL
10327/*
10328 * Find suggestions by evaluating expression "expr".
10329 */
10330 static void
10331spell_suggest_expr(su, expr)
10332 suginfo_T *su;
10333 char_u *expr;
10334{
10335 list_T *list;
10336 listitem_T *li;
10337 int score;
10338 char_u *p;
10339
10340 /* The work is split up in a few parts to avoid having to export
10341 * suginfo_T.
10342 * First evaluate the expression and get the resulting list. */
10343 list = eval_spell_expr(su->su_badword, expr);
10344 if (list != NULL)
10345 {
10346 /* Loop over the items in the list. */
10347 for (li = list->lv_first; li != NULL; li = li->li_next)
10348 if (li->li_tv.v_type == VAR_LIST)
10349 {
10350 /* Get the word and the score from the items. */
10351 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010352 if (score >= 0 && score <= su->su_maxscore)
10353 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10354 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010355 }
10356 list_unref(list);
10357 }
10358
Bram Moolenaar4770d092006-01-12 23:22:24 +000010359 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10360 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010361 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10362}
10363#endif
10364
10365/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010366 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010367 */
10368 static void
10369spell_suggest_file(su, fname)
10370 suginfo_T *su;
10371 char_u *fname;
10372{
10373 FILE *fd;
10374 char_u line[MAXWLEN * 2];
10375 char_u *p;
10376 int len;
10377 char_u cword[MAXWLEN];
10378
10379 /* Open the file. */
10380 fd = mch_fopen((char *)fname, "r");
10381 if (fd == NULL)
10382 {
10383 EMSG2(_(e_notopen), fname);
10384 return;
10385 }
10386
10387 /* Read it line by line. */
10388 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10389 {
10390 line_breakcheck();
10391
10392 p = vim_strchr(line, '/');
10393 if (p == NULL)
10394 continue; /* No Tab found, just skip the line. */
10395 *p++ = NUL;
10396 if (STRICMP(su->su_badword, line) == 0)
10397 {
10398 /* Match! Isolate the good word, until CR or NL. */
10399 for (len = 0; p[len] >= ' '; ++len)
10400 ;
10401 p[len] = NUL;
10402
10403 /* If the suggestion doesn't have specific case duplicate the case
10404 * of the bad word. */
10405 if (captype(p, NULL) == 0)
10406 {
10407 make_case_word(p, cword, su->su_badflags);
10408 p = cword;
10409 }
10410
10411 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010412 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010413 }
10414 }
10415
10416 fclose(fd);
10417
Bram Moolenaar4770d092006-01-12 23:22:24 +000010418 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10419 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010420 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10421}
10422
10423/*
10424 * Find suggestions for the internal method indicated by "sps_flags".
10425 */
10426 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010427spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010428 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010429 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010430{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010431 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010432 * Load the .sug file(s) that are available and not done yet.
10433 */
10434 suggest_load_files();
10435
10436 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010437 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010438 *
10439 * Set a maximum score to limit the combination of operations that is
10440 * tried.
10441 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010442 suggest_try_special(su);
10443
10444 /*
10445 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10446 * from the .aff file and inserting a space (split the word).
10447 */
10448 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010449
10450 /* For the resulting top-scorers compute the sound-a-like score. */
10451 if (sps_flags & SPS_DOUBLE)
10452 score_comp_sal(su);
10453
10454 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010455 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010456 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010457 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010458 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010459 if (sps_flags & SPS_BEST)
10460 /* Adjust the word score for the suggestions found so far for how
10461 * they sounds like. */
10462 rescore_suggestions(su);
10463
10464 /*
10465 * While going throught the soundfold tree "su_maxscore" is the score
10466 * for the soundfold word, limits the changes that are being tried,
10467 * and "su_sfmaxscore" the rescored score, which is set by
10468 * cleanup_suggestions().
10469 * First find words with a small edit distance, because this is much
10470 * faster and often already finds the top-N suggestions. If we didn't
10471 * find many suggestions try again with a higher edit distance.
10472 * "sl_sounddone" is used to avoid doing the same word twice.
10473 */
10474 suggest_try_soundalike_prep();
10475 su->su_maxscore = SCORE_SFMAX1;
10476 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010477 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010478 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10479 {
10480 /* We didn't find enough matches, try again, allowing more
10481 * changes to the soundfold word. */
10482 su->su_maxscore = SCORE_SFMAX2;
10483 suggest_try_soundalike(su);
10484 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10485 {
10486 /* Still didn't find enough matches, try again, allowing even
10487 * more changes to the soundfold word. */
10488 su->su_maxscore = SCORE_SFMAX3;
10489 suggest_try_soundalike(su);
10490 }
10491 }
10492 su->su_maxscore = su->su_sfmaxscore;
10493 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010494 }
10495
Bram Moolenaar4770d092006-01-12 23:22:24 +000010496 /* When CTRL-C was hit while searching do show the results. Only clear
10497 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010498 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010499 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010500 {
10501 (void)vgetc();
10502 got_int = FALSE;
10503 }
10504
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010505 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010506 {
10507 if (sps_flags & SPS_BEST)
10508 /* Adjust the word score for how it sounds like. */
10509 rescore_suggestions(su);
10510
Bram Moolenaar4770d092006-01-12 23:22:24 +000010511 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10512 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010513 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010514 }
10515}
10516
10517/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010518 * Load the .sug files for languages that have one and weren't loaded yet.
10519 */
10520 static void
10521suggest_load_files()
10522{
10523 langp_T *lp;
10524 int lpi;
10525 slang_T *slang;
10526 char_u *dotp;
10527 FILE *fd;
10528 char_u buf[MAXWLEN];
10529 int i;
10530 time_t timestamp;
10531 int wcount;
10532 int wordnr;
10533 garray_T ga;
10534 int c;
10535
10536 /* Do this for all languages that support sound folding. */
10537 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10538 {
10539 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10540 slang = lp->lp_slang;
10541 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10542 {
10543 /* Change ".spl" to ".sug" and open the file. When the file isn't
10544 * found silently skip it. Do set "sl_sugloaded" so that we
10545 * don't try again and again. */
10546 slang->sl_sugloaded = TRUE;
10547
10548 dotp = vim_strrchr(slang->sl_fname, '.');
10549 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10550 continue;
10551 STRCPY(dotp, ".sug");
10552 fd = fopen((char *)slang->sl_fname, "r");
10553 if (fd == NULL)
10554 goto nextone;
10555
10556 /*
10557 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10558 */
10559 for (i = 0; i < VIMSUGMAGICL; ++i)
10560 buf[i] = getc(fd); /* <fileID> */
10561 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10562 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010563 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010564 slang->sl_fname);
10565 goto nextone;
10566 }
10567 c = getc(fd); /* <versionnr> */
10568 if (c < VIMSUGVERSION)
10569 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010570 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010571 slang->sl_fname);
10572 goto nextone;
10573 }
10574 else if (c > VIMSUGVERSION)
10575 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010576 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010577 slang->sl_fname);
10578 goto nextone;
10579 }
10580
10581 /* Check the timestamp, it must be exactly the same as the one in
10582 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010583 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010584 if (timestamp != slang->sl_sugtime)
10585 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010586 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010587 slang->sl_fname);
10588 goto nextone;
10589 }
10590
10591 /*
10592 * <SUGWORDTREE>: <wordtree>
10593 * Read the trie with the soundfolded words.
10594 */
10595 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10596 FALSE, 0) != 0)
10597 {
10598someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010599 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010600 slang->sl_fname);
10601 slang_clear_sug(slang);
10602 goto nextone;
10603 }
10604
10605 /*
10606 * <SUGTABLE>: <sugwcount> <sugline> ...
10607 *
10608 * Read the table with word numbers. We use a file buffer for
10609 * this, because it's so much like a file with lines. Makes it
10610 * possible to swap the info and save on memory use.
10611 */
10612 slang->sl_sugbuf = open_spellbuf();
10613 if (slang->sl_sugbuf == NULL)
10614 goto someerror;
10615 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010616 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010617 if (wcount < 0)
10618 goto someerror;
10619
10620 /* Read all the wordnr lists into the buffer, one NUL terminated
10621 * list per line. */
10622 ga_init2(&ga, 1, 100);
10623 for (wordnr = 0; wordnr < wcount; ++wordnr)
10624 {
10625 ga.ga_len = 0;
10626 for (;;)
10627 {
10628 c = getc(fd); /* <sugline> */
10629 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10630 goto someerror;
10631 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10632 if (c == NUL)
10633 break;
10634 }
10635 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10636 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10637 goto someerror;
10638 }
10639 ga_clear(&ga);
10640
10641 /*
10642 * Need to put word counts in the word tries, so that we can find
10643 * a word by its number.
10644 */
10645 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10646 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10647
10648nextone:
10649 if (fd != NULL)
10650 fclose(fd);
10651 STRCPY(dotp, ".spl");
10652 }
10653 }
10654}
10655
10656
10657/*
10658 * Fill in the wordcount fields for a trie.
10659 * Returns the total number of words.
10660 */
10661 static void
10662tree_count_words(byts, idxs)
10663 char_u *byts;
10664 idx_T *idxs;
10665{
10666 int depth;
10667 idx_T arridx[MAXWLEN];
10668 int curi[MAXWLEN];
10669 int c;
10670 idx_T n;
10671 int wordcount[MAXWLEN];
10672
10673 arridx[0] = 0;
10674 curi[0] = 1;
10675 wordcount[0] = 0;
10676 depth = 0;
10677 while (depth >= 0 && !got_int)
10678 {
10679 if (curi[depth] > byts[arridx[depth]])
10680 {
10681 /* Done all bytes at this node, go up one level. */
10682 idxs[arridx[depth]] = wordcount[depth];
10683 if (depth > 0)
10684 wordcount[depth - 1] += wordcount[depth];
10685
10686 --depth;
10687 fast_breakcheck();
10688 }
10689 else
10690 {
10691 /* Do one more byte at this node. */
10692 n = arridx[depth] + curi[depth];
10693 ++curi[depth];
10694
10695 c = byts[n];
10696 if (c == 0)
10697 {
10698 /* End of word, count it. */
10699 ++wordcount[depth];
10700
10701 /* Skip over any other NUL bytes (same word with different
10702 * flags). */
10703 while (byts[n + 1] == 0)
10704 {
10705 ++n;
10706 ++curi[depth];
10707 }
10708 }
10709 else
10710 {
10711 /* Normal char, go one level deeper to count the words. */
10712 ++depth;
10713 arridx[depth] = idxs[n];
10714 curi[depth] = 1;
10715 wordcount[depth] = 0;
10716 }
10717 }
10718 }
10719}
10720
10721/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010722 * Free the info put in "*su" by spell_find_suggest().
10723 */
10724 static void
10725spell_find_cleanup(su)
10726 suginfo_T *su;
10727{
10728 int i;
10729
10730 /* Free the suggestions. */
10731 for (i = 0; i < su->su_ga.ga_len; ++i)
10732 vim_free(SUG(su->su_ga, i).st_word);
10733 ga_clear(&su->su_ga);
10734 for (i = 0; i < su->su_sga.ga_len; ++i)
10735 vim_free(SUG(su->su_sga, i).st_word);
10736 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010737
10738 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010739 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740}
10741
10742/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010743 * Make a copy of "word", with the first letter upper or lower cased, to
10744 * "wcopy[MAXWLEN]". "word" must not be empty.
10745 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 */
10747 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010748onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 char_u *wcopy;
10751 int upper; /* TRUE: first letter made upper case */
10752{
10753 char_u *p;
10754 int c;
10755 int l;
10756
10757 p = word;
10758#ifdef FEAT_MBYTE
10759 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010760 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 else
10762#endif
10763 c = *p++;
10764 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010765 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010767 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010768#ifdef FEAT_MBYTE
10769 if (has_mbyte)
10770 l = mb_char2bytes(c, wcopy);
10771 else
10772#endif
10773 {
10774 l = 1;
10775 wcopy[0] = c;
10776 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010777 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778}
10779
10780/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010781 * Make a copy of "word" with all the letters upper cased into
10782 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 */
10784 static void
10785allcap_copy(word, wcopy)
10786 char_u *word;
10787 char_u *wcopy;
10788{
10789 char_u *s;
10790 char_u *d;
10791 int c;
10792
10793 d = wcopy;
10794 for (s = word; *s != NUL; )
10795 {
10796#ifdef FEAT_MBYTE
10797 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010798 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010799 else
10800#endif
10801 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010802
10803#ifdef FEAT_MBYTE
10804 /* We only change ß to SS when we are certain latin1 is used. It
10805 * would cause weird errors in other 8-bit encodings. */
10806 if (enc_latin1like && c == 0xdf)
10807 {
10808 c = 'S';
10809 if (d - wcopy >= MAXWLEN - 1)
10810 break;
10811 *d++ = c;
10812 }
10813 else
10814#endif
10815 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816
10817#ifdef FEAT_MBYTE
10818 if (has_mbyte)
10819 {
10820 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10821 break;
10822 d += mb_char2bytes(c, d);
10823 }
10824 else
10825#endif
10826 {
10827 if (d - wcopy >= MAXWLEN - 1)
10828 break;
10829 *d++ = c;
10830 }
10831 }
10832 *d = NUL;
10833}
10834
10835/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010836 * Try finding suggestions by recognizing specific situations.
10837 */
10838 static void
10839suggest_try_special(su)
10840 suginfo_T *su;
10841{
10842 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010843 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010844 int c;
10845 char_u word[MAXWLEN];
10846
10847 /*
10848 * Recognize a word that is repeated: "the the".
10849 */
10850 p = skiptowhite(su->su_fbadword);
10851 len = p - su->su_fbadword;
10852 p = skipwhite(p);
10853 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10854 {
10855 /* Include badflags: if the badword is onecap or allcap
10856 * use that for the goodword too: "The the" -> "The". */
10857 c = su->su_fbadword[len];
10858 su->su_fbadword[len] = NUL;
10859 make_case_word(su->su_fbadword, word, su->su_badflags);
10860 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010861
10862 /* Give a soundalike score of 0, compute the score as if deleting one
10863 * character. */
10864 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010865 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010866 }
10867}
10868
10869/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 * Try finding suggestions by adding/removing/swapping letters.
10871 */
10872 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010873suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 suginfo_T *su;
10875{
10876 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010877 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010879 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010880 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881
10882 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000010883 * to find matches (esp. REP items). Append some more text, changing
10884 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010886 n = STRLEN(fword);
10887 p = su->su_badptr + su->su_badlen;
10888 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010890 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010892 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010893
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010894 /* If reloading a spell file fails it's still in the list but
10895 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010896 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010897 continue;
10898
Bram Moolenaar4770d092006-01-12 23:22:24 +000010899 /* Try it for this language. Will add possible suggestions. */
10900 suggest_trie_walk(su, lp, fword, FALSE);
10901 }
10902}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903
Bram Moolenaar4770d092006-01-12 23:22:24 +000010904/* Check the maximum score, if we go over it we won't try this change. */
10905#define TRY_DEEPER(su, stack, depth, add) \
10906 (stack[depth].ts_score + (add) < su->su_maxscore)
10907
10908/*
10909 * Try finding suggestions by adding/removing/swapping letters.
10910 *
10911 * This uses a state machine. At each node in the tree we try various
10912 * operations. When trying if an operation works "depth" is increased and the
10913 * stack[] is used to store info. This allows combinations, thus insert one
10914 * character, replace one and delete another. The number of changes is
10915 * limited by su->su_maxscore.
10916 *
10917 * After implementing this I noticed an article by Kemal Oflazer that
10918 * describes something similar: "Error-tolerant Finite State Recognition with
10919 * Applications to Morphological Analysis and Spelling Correction" (1996).
10920 * The implementation in the article is simplified and requires a stack of
10921 * unknown depth. The implementation here only needs a stack depth equal to
10922 * the length of the word.
10923 *
10924 * This is also used for the sound-folded word, "soundfold" is TRUE then.
10925 * The mechanism is the same, but we find a match with a sound-folded word
10926 * that comes from one or more original words. Each of these words may be
10927 * added, this is done by add_sound_suggest().
10928 * Don't use:
10929 * the prefix tree or the keep-case tree
10930 * "su->su_badlen"
10931 * anything to do with upper and lower case
10932 * anything to do with word or non-word characters ("spell_iswordp()")
10933 * banned words
10934 * word flags (rare, region, compounding)
10935 * word splitting for now
10936 * "similar_chars()"
10937 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
10938 */
10939 static void
10940suggest_trie_walk(su, lp, fword, soundfold)
10941 suginfo_T *su;
10942 langp_T *lp;
10943 char_u *fword;
10944 int soundfold;
10945{
10946 char_u tword[MAXWLEN]; /* good word collected so far */
10947 trystate_T stack[MAXWLEN];
10948 char_u preword[MAXWLEN * 3]; /* word found with proper case;
10949 * concatanation of prefix compound
10950 * words and split word. NUL terminated
10951 * when going deeper but not when coming
10952 * back. */
10953 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
10954 trystate_T *sp;
10955 int newscore;
10956 int score;
10957 char_u *byts, *fbyts, *pbyts;
10958 idx_T *idxs, *fidxs, *pidxs;
10959 int depth;
10960 int c, c2, c3;
10961 int n = 0;
10962 int flags;
10963 garray_T *gap;
10964 idx_T arridx;
10965 int len;
10966 char_u *p;
10967 fromto_T *ftp;
10968 int fl = 0, tl;
10969 int repextra = 0; /* extra bytes in fword[] from REP item */
10970 slang_T *slang = lp->lp_slang;
10971 int fword_ends;
10972 int goodword_ends;
10973#ifdef DEBUG_TRIEWALK
10974 /* Stores the name of the change made at each level. */
10975 char_u changename[MAXWLEN][80];
10976#endif
10977 int breakcheckcount = 1000;
10978 int compound_ok;
10979
10980 /*
10981 * Go through the whole case-fold tree, try changes at each node.
10982 * "tword[]" contains the word collected from nodes in the tree.
10983 * "fword[]" the word we are trying to match with (initially the bad
10984 * word).
10985 */
10986 depth = 0;
10987 sp = &stack[0];
10988 vim_memset(sp, 0, sizeof(trystate_T));
10989 sp->ts_curi = 1;
10990
10991 if (soundfold)
10992 {
10993 /* Going through the soundfold tree. */
10994 byts = fbyts = slang->sl_sbyts;
10995 idxs = fidxs = slang->sl_sidxs;
10996 pbyts = NULL;
10997 pidxs = NULL;
10998 sp->ts_prefixdepth = PFD_NOPREFIX;
10999 sp->ts_state = STATE_START;
11000 }
11001 else
11002 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011003 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011004 * When there are postponed prefixes we need to use these first. At
11005 * the end of the prefix we continue in the case-fold tree.
11006 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011007 fbyts = slang->sl_fbyts;
11008 fidxs = slang->sl_fidxs;
11009 pbyts = slang->sl_pbyts;
11010 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011011 if (pbyts != NULL)
11012 {
11013 byts = pbyts;
11014 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011015 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011016 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11017 }
11018 else
11019 {
11020 byts = fbyts;
11021 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011022 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011023 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011024 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011025 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011026
Bram Moolenaar4770d092006-01-12 23:22:24 +000011027 /*
11028 * Loop to find all suggestions. At each round we either:
11029 * - For the current state try one operation, advance "ts_curi",
11030 * increase "depth".
11031 * - When a state is done go to the next, set "ts_state".
11032 * - When all states are tried decrease "depth".
11033 */
11034 while (depth >= 0 && !got_int)
11035 {
11036 sp = &stack[depth];
11037 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011039 case STATE_START:
11040 case STATE_NOPREFIX:
11041 /*
11042 * Start of node: Deal with NUL bytes, which means
11043 * tword[] may end here.
11044 */
11045 arridx = sp->ts_arridx; /* current node in the tree */
11046 len = byts[arridx]; /* bytes in this node */
11047 arridx += sp->ts_curi; /* index of current byte */
11048
11049 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011051 /* Skip over the NUL bytes, we use them later. */
11052 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11053 ;
11054 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055
Bram Moolenaar4770d092006-01-12 23:22:24 +000011056 /* Always past NUL bytes now. */
11057 n = (int)sp->ts_state;
11058 sp->ts_state = STATE_ENDNUL;
11059 sp->ts_save_badflags = su->su_badflags;
11060
11061 /* At end of a prefix or at start of prefixtree: check for
11062 * following word. */
11063 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011064 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011065 /* Set su->su_badflags to the caps type at this position.
11066 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011067#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011068 if (has_mbyte)
11069 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11070 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011071#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011072 n = sp->ts_fidx;
11073 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11074 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011075 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011076#ifdef DEBUG_TRIEWALK
11077 sprintf(changename[depth], "prefix");
11078#endif
11079 go_deeper(stack, depth, 0);
11080 ++depth;
11081 sp = &stack[depth];
11082 sp->ts_prefixdepth = depth - 1;
11083 byts = fbyts;
11084 idxs = fidxs;
11085 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011086
Bram Moolenaar4770d092006-01-12 23:22:24 +000011087 /* Move the prefix to preword[] with the right case
11088 * and make find_keepcap_word() works. */
11089 tword[sp->ts_twordlen] = NUL;
11090 make_case_word(tword + sp->ts_splitoff,
11091 preword + sp->ts_prewordlen, flags);
11092 sp->ts_prewordlen = STRLEN(preword);
11093 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011094 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011095 break;
11096 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011097
Bram Moolenaar4770d092006-01-12 23:22:24 +000011098 if (sp->ts_curi > len || byts[arridx] != 0)
11099 {
11100 /* Past bytes in node and/or past NUL bytes. */
11101 sp->ts_state = STATE_ENDNUL;
11102 sp->ts_save_badflags = su->su_badflags;
11103 break;
11104 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105
Bram Moolenaar4770d092006-01-12 23:22:24 +000011106 /*
11107 * End of word in tree.
11108 */
11109 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110
Bram Moolenaar4770d092006-01-12 23:22:24 +000011111 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011112
11113 /* Skip words with the NOSUGGEST flag. */
11114 if (flags & WF_NOSUGGEST)
11115 break;
11116
Bram Moolenaar4770d092006-01-12 23:22:24 +000011117 fword_ends = (fword[sp->ts_fidx] == NUL
11118 || (soundfold
11119 ? vim_iswhite(fword[sp->ts_fidx])
11120 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11121 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122
Bram Moolenaar4770d092006-01-12 23:22:24 +000011123 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011124 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011125 {
11126 /* There was a prefix before the word. Check that the prefix
11127 * can be used with this word. */
11128 /* Count the length of the NULs in the prefix. If there are
11129 * none this must be the first try without a prefix. */
11130 n = stack[sp->ts_prefixdepth].ts_arridx;
11131 len = pbyts[n++];
11132 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11133 ;
11134 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011135 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011136 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011137 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011138 if (c == 0)
11139 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011140
Bram Moolenaar4770d092006-01-12 23:22:24 +000011141 /* Use the WF_RARE flag for a rare prefix. */
11142 if (c & WF_RAREPFX)
11143 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011144
Bram Moolenaar4770d092006-01-12 23:22:24 +000011145 /* Tricky: when checking for both prefix and compounding
11146 * we run into the prefix flag first.
11147 * Remember that it's OK, so that we accept the prefix
11148 * when arriving at a compound flag. */
11149 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011150 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011151 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011152
Bram Moolenaar4770d092006-01-12 23:22:24 +000011153 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11154 * appending another compound word below. */
11155 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011156 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011157 goodword_ends = FALSE;
11158 else
11159 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011160
Bram Moolenaar4770d092006-01-12 23:22:24 +000011161 p = NULL;
11162 compound_ok = TRUE;
11163 if (sp->ts_complen > sp->ts_compsplit)
11164 {
11165 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011166 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011167 /* There was a word before this word. When there was no
11168 * change in this word (it was correct) add the first word
11169 * as a suggestion. If this word was corrected too, we
11170 * need to check if a correct word follows. */
11171 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011172 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011173 && STRNCMP(fword + sp->ts_splitfidx,
11174 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011175 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011176 {
11177 preword[sp->ts_prewordlen] = NUL;
11178 newscore = score_wordcount_adj(slang, sp->ts_score,
11179 preword + sp->ts_prewordlen,
11180 sp->ts_prewordlen > 0);
11181 /* Add the suggestion if the score isn't too bad. */
11182 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011183 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011184 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011185 newscore, 0, FALSE,
11186 lp->lp_sallang, FALSE);
11187 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011188 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011189 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011190 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011191 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011192 /* There was a compound word before this word. If this
11193 * word does not support compounding then give up
11194 * (splitting is tried for the word without compound
11195 * flag). */
11196 if (((unsigned)flags >> 24) == 0
11197 || sp->ts_twordlen - sp->ts_splitoff
11198 < slang->sl_compminlen)
11199 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011200#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011201 /* For multi-byte chars check character length against
11202 * COMPOUNDMIN. */
11203 if (has_mbyte
11204 && slang->sl_compminlen > 0
11205 && mb_charlen(tword + sp->ts_splitoff)
11206 < slang->sl_compminlen)
11207 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011208#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011209
Bram Moolenaar4770d092006-01-12 23:22:24 +000011210 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11211 compflags[sp->ts_complen + 1] = NUL;
11212 vim_strncpy(preword + sp->ts_prewordlen,
11213 tword + sp->ts_splitoff,
11214 sp->ts_twordlen - sp->ts_splitoff);
11215 p = preword;
11216 while (*skiptowhite(p) != NUL)
11217 p = skipwhite(skiptowhite(p));
11218 if (fword_ends && !can_compound(slang, p,
11219 compflags + sp->ts_compsplit))
11220 /* Compound is not allowed. But it may still be
11221 * possible if we add another (short) word. */
11222 compound_ok = FALSE;
11223
11224 /* Get pointer to last char of previous word. */
11225 p = preword + sp->ts_prewordlen;
11226 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011227 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011228 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229
Bram Moolenaar4770d092006-01-12 23:22:24 +000011230 /*
11231 * Form the word with proper case in preword.
11232 * If there is a word from a previous split, append.
11233 * For the soundfold tree don't change the case, simply append.
11234 */
11235 if (soundfold)
11236 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11237 else if (flags & WF_KEEPCAP)
11238 /* Must find the word in the keep-case tree. */
11239 find_keepcap_word(slang, tword + sp->ts_splitoff,
11240 preword + sp->ts_prewordlen);
11241 else
11242 {
11243 /* Include badflags: If the badword is onecap or allcap
11244 * use that for the goodword too. But if the badword is
11245 * allcap and it's only one char long use onecap. */
11246 c = su->su_badflags;
11247 if ((c & WF_ALLCAP)
11248#ifdef FEAT_MBYTE
11249 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11250#else
11251 && su->su_badlen == 1
11252#endif
11253 )
11254 c = WF_ONECAP;
11255 c |= flags;
11256
11257 /* When appending a compound word after a word character don't
11258 * use Onecap. */
11259 if (p != NULL && spell_iswordp_nmw(p))
11260 c &= ~WF_ONECAP;
11261 make_case_word(tword + sp->ts_splitoff,
11262 preword + sp->ts_prewordlen, c);
11263 }
11264
11265 if (!soundfold)
11266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267 /* Don't use a banned word. It may appear again as a good
11268 * word, thus remember it. */
11269 if (flags & WF_BANNED)
11270 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011271 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 break;
11273 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011274 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011275 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11276 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011277 {
11278 if (slang->sl_compprog == NULL)
11279 break;
11280 /* the word so far was banned but we may try compounding */
11281 goodword_ends = FALSE;
11282 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011283 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 newscore = 0;
11286 if (!soundfold) /* soundfold words don't have flags */
11287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011289 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290 newscore += SCORE_REGION;
11291 if (flags & WF_RARE)
11292 newscore += SCORE_RARE;
11293
Bram Moolenaar0c405862005-06-22 22:26:26 +000011294 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011295 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298
Bram Moolenaar4770d092006-01-12 23:22:24 +000011299 /* TODO: how about splitting in the soundfold tree? */
11300 if (fword_ends
11301 && goodword_ends
11302 && sp->ts_fidx >= sp->ts_fidxtry
11303 && compound_ok)
11304 {
11305 /* The badword also ends: add suggestions. */
11306#ifdef DEBUG_TRIEWALK
11307 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011309 int j;
11310
11311 /* print the stack of changes that brought us here */
11312 smsg("------ %s -------", fword);
11313 for (j = 0; j < depth; ++j)
11314 smsg("%s", changename[j]);
11315 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011316#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011317 if (soundfold)
11318 {
11319 /* For soundfolded words we need to find the original
11320 * words, the edit distrance and then add them. */
11321 add_sound_suggest(su, preword, sp->ts_score, lp);
11322 }
11323 else
11324 {
11325 /* Give a penalty when changing non-word char to word
11326 * char, e.g., "thes," -> "these". */
11327 p = fword + sp->ts_fidx;
11328 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011329 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011330 {
11331 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011332 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011333 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011334 newscore += SCORE_NONWORD;
11335 }
11336
Bram Moolenaar4770d092006-01-12 23:22:24 +000011337 /* Give a bonus to words seen before. */
11338 score = score_wordcount_adj(slang,
11339 sp->ts_score + newscore,
11340 preword + sp->ts_prewordlen,
11341 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011342
Bram Moolenaar4770d092006-01-12 23:22:24 +000011343 /* Add the suggestion if the score isn't too bad. */
11344 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011345 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011346 add_suggestion(su, &su->su_ga, preword,
11347 sp->ts_fidx - repextra,
11348 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011349
11350 if (su->su_badflags & WF_MIXCAP)
11351 {
11352 /* We really don't know if the word should be
11353 * upper or lower case, add both. */
11354 c = captype(preword, NULL);
11355 if (c == 0 || c == WF_ALLCAP)
11356 {
11357 make_case_word(tword + sp->ts_splitoff,
11358 preword + sp->ts_prewordlen,
11359 c == 0 ? WF_ALLCAP : 0);
11360
11361 add_suggestion(su, &su->su_ga, preword,
11362 sp->ts_fidx - repextra,
11363 score + SCORE_ICASE, 0, FALSE,
11364 lp->lp_sallang, FALSE);
11365 }
11366 }
11367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011369 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011370
Bram Moolenaar4770d092006-01-12 23:22:24 +000011371 /*
11372 * Try word split and/or compounding.
11373 */
11374 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011375#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011376 /* Don't split halfway a character. */
11377 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011378#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 )
11380 {
11381 int try_compound;
11382 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011383
Bram Moolenaar4770d092006-01-12 23:22:24 +000011384 /* If past the end of the bad word don't try a split.
11385 * Otherwise try changing the next word. E.g., find
11386 * suggestions for "the the" where the second "the" is
11387 * different. It's done like a split.
11388 * TODO: word split for soundfold words */
11389 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11390 && !soundfold;
11391
11392 /* Get here in several situations:
11393 * 1. The word in the tree ends:
11394 * If the word allows compounding try that. Otherwise try
11395 * a split by inserting a space. For both check that a
11396 * valid words starts at fword[sp->ts_fidx].
11397 * For NOBREAK do like compounding to be able to check if
11398 * the next word is valid.
11399 * 2. The badword does end, but it was due to a change (e.g.,
11400 * a swap). No need to split, but do check that the
11401 * following word is valid.
11402 * 3. The badword and the word in the tree end. It may still
11403 * be possible to compound another (short) word.
11404 */
11405 try_compound = FALSE;
11406 if (!soundfold
11407 && slang->sl_compprog != NULL
11408 && ((unsigned)flags >> 24) != 0
11409 && sp->ts_twordlen - sp->ts_splitoff
11410 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011411#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011412 && (!has_mbyte
11413 || slang->sl_compminlen == 0
11414 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011415 >= slang->sl_compminlen)
11416#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011417 && (slang->sl_compsylmax < MAXWLEN
11418 || sp->ts_complen + 1 - sp->ts_compsplit
11419 < slang->sl_compmax)
11420 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11421 ? slang->sl_compstartflags
11422 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011423 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011424 {
11425 try_compound = TRUE;
11426 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11427 compflags[sp->ts_complen + 1] = NUL;
11428 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011429
Bram Moolenaar4770d092006-01-12 23:22:24 +000011430 /* For NOBREAK we never try splitting, it won't make any word
11431 * valid. */
11432 if (slang->sl_nobreak)
11433 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011434
Bram Moolenaar4770d092006-01-12 23:22:24 +000011435 /* If we could add a compound word, and it's also possible to
11436 * split at this point, do the split first and set
11437 * TSF_DIDSPLIT to avoid doing it again. */
11438 else if (!fword_ends
11439 && try_compound
11440 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11441 {
11442 try_compound = FALSE;
11443 sp->ts_flags |= TSF_DIDSPLIT;
11444 --sp->ts_curi; /* do the same NUL again */
11445 compflags[sp->ts_complen] = NUL;
11446 }
11447 else
11448 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011449
Bram Moolenaar4770d092006-01-12 23:22:24 +000011450 if (try_split || try_compound)
11451 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011452 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011453 {
11454 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011455 * words so far are valid for compounding. If there
11456 * is only one word it must not have the NEEDCOMPOUND
11457 * flag. */
11458 if (sp->ts_complen == sp->ts_compsplit
11459 && (flags & WF_NEEDCOMP))
11460 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011461 p = preword;
11462 while (*skiptowhite(p) != NUL)
11463 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011464 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011465 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011466 compflags + sp->ts_compsplit))
11467 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011468
11469 if (slang->sl_nosplitsugs)
11470 newscore += SCORE_SPLIT_NO;
11471 else
11472 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473
11474 /* Give a bonus to words seen before. */
11475 newscore = score_wordcount_adj(slang, newscore,
11476 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011477 }
11478
Bram Moolenaar4770d092006-01-12 23:22:24 +000011479 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011481 go_deeper(stack, depth, newscore);
11482#ifdef DEBUG_TRIEWALK
11483 if (!try_compound && !fword_ends)
11484 sprintf(changename[depth], "%.*s-%s: split",
11485 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11486 else
11487 sprintf(changename[depth], "%.*s-%s: compound",
11488 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11489#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011491 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011492 sp->ts_state = STATE_SPLITUNDO;
11493
11494 ++depth;
11495 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011497 /* Append a space to preword when splitting. */
11498 if (!try_compound && !fword_ends)
11499 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011500 sp->ts_prewordlen = STRLEN(preword);
11501 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011502 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011503
11504 /* If the badword has a non-word character at this
11505 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011506 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011507 * character when the word ends. But only when the
11508 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011509 if (((!try_compound && !spell_iswordp_nmw(fword
11510 + sp->ts_fidx))
11511 || fword_ends)
11512 && fword[sp->ts_fidx] != NUL
11513 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011514 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011515 int l;
11516
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011517#ifdef FEAT_MBYTE
11518 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011519 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011520 else
11521#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011522 l = 1;
11523 if (fword_ends)
11524 {
11525 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011526 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011527 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011528 sp->ts_prewordlen += l;
11529 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011530 }
11531 else
11532 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11533 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011534 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011535
Bram Moolenaard12a1322005-08-21 22:08:24 +000011536 /* When compounding include compound flag in
11537 * compflags[] (already set above). When splitting we
11538 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011539 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011540 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011541 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011542 sp->ts_compsplit = sp->ts_complen;
11543 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011544
Bram Moolenaar53805d12005-08-01 07:08:33 +000011545 /* set su->su_badflags to the caps type at this
11546 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011547#ifdef FEAT_MBYTE
11548 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011549 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011550 else
11551#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011552 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011553 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011554 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011557 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011558
11559 /* If there are postponed prefixes, try these too. */
11560 if (pbyts != NULL)
11561 {
11562 byts = pbyts;
11563 idxs = pidxs;
11564 sp->ts_prefixdepth = PFD_PREFIXTREE;
11565 sp->ts_state = STATE_NOPREFIX;
11566 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011567 }
11568 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011569 }
11570 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571
Bram Moolenaar4770d092006-01-12 23:22:24 +000011572 case STATE_SPLITUNDO:
11573 /* Undo the changes done for word split or compound word. */
11574 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575
Bram Moolenaar4770d092006-01-12 23:22:24 +000011576 /* Continue looking for NUL bytes. */
11577 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011578
Bram Moolenaar4770d092006-01-12 23:22:24 +000011579 /* In case we went into the prefix tree. */
11580 byts = fbyts;
11581 idxs = fidxs;
11582 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583
Bram Moolenaar4770d092006-01-12 23:22:24 +000011584 case STATE_ENDNUL:
11585 /* Past the NUL bytes in the node. */
11586 su->su_badflags = sp->ts_save_badflags;
11587 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011588#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011589 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011590#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 )
11592 {
11593 /* The badword ends, can't use STATE_PLAIN. */
11594 sp->ts_state = STATE_DEL;
11595 break;
11596 }
11597 sp->ts_state = STATE_PLAIN;
11598 /*FALLTHROUGH*/
11599
11600 case STATE_PLAIN:
11601 /*
11602 * Go over all possible bytes at this node, add each to tword[]
11603 * and use child node. "ts_curi" is the index.
11604 */
11605 arridx = sp->ts_arridx;
11606 if (sp->ts_curi > byts[arridx])
11607 {
11608 /* Done all bytes at this node, do next state. When still at
11609 * already changed bytes skip the other tricks. */
11610 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011611 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011613 sp->ts_state = STATE_FINAL;
11614 }
11615 else
11616 {
11617 arridx += sp->ts_curi++;
11618 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011619
Bram Moolenaar4770d092006-01-12 23:22:24 +000011620 /* Normal byte, go one level deeper. If it's not equal to the
11621 * byte in the bad word adjust the score. But don't even try
11622 * when the byte was already changed. And don't try when we
11623 * just deleted this byte, accepting it is always cheaper then
11624 * delete + substitute. */
11625 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011626#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011627 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011628#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011629 )
11630 newscore = 0;
11631 else
11632 newscore = SCORE_SUBST;
11633 if ((newscore == 0
11634 || (sp->ts_fidx >= sp->ts_fidxtry
11635 && ((sp->ts_flags & TSF_DIDDEL) == 0
11636 || c != fword[sp->ts_delidx])))
11637 && TRY_DEEPER(su, stack, depth, newscore))
11638 {
11639 go_deeper(stack, depth, newscore);
11640#ifdef DEBUG_TRIEWALK
11641 if (newscore > 0)
11642 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11643 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11644 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011645 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011646 sprintf(changename[depth], "%.*s-%s: accept %c",
11647 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11648 fword[sp->ts_fidx]);
11649#endif
11650 ++depth;
11651 sp = &stack[depth];
11652 ++sp->ts_fidx;
11653 tword[sp->ts_twordlen++] = c;
11654 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011655#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011656 if (newscore == SCORE_SUBST)
11657 sp->ts_isdiff = DIFF_YES;
11658 if (has_mbyte)
11659 {
11660 /* Multi-byte characters are a bit complicated to
11661 * handle: They differ when any of the bytes differ
11662 * and then their length may also differ. */
11663 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011664 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011665 /* First byte. */
11666 sp->ts_tcharidx = 0;
11667 sp->ts_tcharlen = MB_BYTE2LEN(c);
11668 sp->ts_fcharstart = sp->ts_fidx - 1;
11669 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011670 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011671 }
11672 else if (sp->ts_isdiff == DIFF_INSERT)
11673 /* When inserting trail bytes don't advance in the
11674 * bad word. */
11675 --sp->ts_fidx;
11676 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11677 {
11678 /* Last byte of character. */
11679 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011680 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011681 /* Correct ts_fidx for the byte length of the
11682 * character (we didn't check that before). */
11683 sp->ts_fidx = sp->ts_fcharstart
11684 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011685 fword[sp->ts_fcharstart]);
11686
Bram Moolenaar4770d092006-01-12 23:22:24 +000011687 /* For changing a composing character adjust
11688 * the score from SCORE_SUBST to
11689 * SCORE_SUBCOMP. */
11690 if (enc_utf8
11691 && utf_iscomposing(
11692 mb_ptr2char(tword
11693 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011694 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011695 && utf_iscomposing(
11696 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011697 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011698 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011699 SCORE_SUBST - SCORE_SUBCOMP;
11700
Bram Moolenaar4770d092006-01-12 23:22:24 +000011701 /* For a similar character adjust score from
11702 * SCORE_SUBST to SCORE_SIMILAR. */
11703 else if (!soundfold
11704 && slang->sl_has_map
11705 && similar_chars(slang,
11706 mb_ptr2char(tword
11707 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011708 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011709 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011710 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011711 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011712 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011713 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011714 else if (sp->ts_isdiff == DIFF_INSERT
11715 && sp->ts_twordlen > sp->ts_tcharlen)
11716 {
11717 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11718 c = mb_ptr2char(p);
11719 if (enc_utf8 && utf_iscomposing(c))
11720 {
11721 /* Inserting a composing char doesn't
11722 * count that much. */
11723 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11724 }
11725 else
11726 {
11727 /* If the previous character was the same,
11728 * thus doubling a character, give a bonus
11729 * to the score. Also for the soundfold
11730 * tree (might seem illogical but does
11731 * give better scores). */
11732 mb_ptr_back(tword, p);
11733 if (c == mb_ptr2char(p))
11734 sp->ts_score -= SCORE_INS
11735 - SCORE_INSDUP;
11736 }
11737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738
Bram Moolenaar4770d092006-01-12 23:22:24 +000011739 /* Starting a new char, reset the length. */
11740 sp->ts_tcharlen = 0;
11741 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011742 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011743 else
11744#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011745 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011746 /* If we found a similar char adjust the score.
11747 * We do this after calling go_deeper() because
11748 * it's slow. */
11749 if (newscore != 0
11750 && !soundfold
11751 && slang->sl_has_map
11752 && similar_chars(slang,
11753 c, fword[sp->ts_fidx - 1]))
11754 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011755 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011756 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011757 }
11758 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759
Bram Moolenaar4770d092006-01-12 23:22:24 +000011760 case STATE_DEL:
11761#ifdef FEAT_MBYTE
11762 /* When past the first byte of a multi-byte char don't try
11763 * delete/insert/swap a character. */
11764 if (has_mbyte && sp->ts_tcharlen > 0)
11765 {
11766 sp->ts_state = STATE_FINAL;
11767 break;
11768 }
11769#endif
11770 /*
11771 * Try skipping one character in the bad word (delete it).
11772 */
11773 sp->ts_state = STATE_INS_PREP;
11774 sp->ts_curi = 1;
11775 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11776 /* Deleting a vowel at the start of a word counts less, see
11777 * soundalike_score(). */
11778 newscore = 2 * SCORE_DEL / 3;
11779 else
11780 newscore = SCORE_DEL;
11781 if (fword[sp->ts_fidx] != NUL
11782 && TRY_DEEPER(su, stack, depth, newscore))
11783 {
11784 go_deeper(stack, depth, newscore);
11785#ifdef DEBUG_TRIEWALK
11786 sprintf(changename[depth], "%.*s-%s: delete %c",
11787 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11788 fword[sp->ts_fidx]);
11789#endif
11790 ++depth;
11791
11792 /* Remember what character we deleted, so that we can avoid
11793 * inserting it again. */
11794 stack[depth].ts_flags |= TSF_DIDDEL;
11795 stack[depth].ts_delidx = sp->ts_fidx;
11796
11797 /* Advance over the character in fword[]. Give a bonus to the
11798 * score if the same character is following "nn" -> "n". It's
11799 * a bit illogical for soundfold tree but it does give better
11800 * results. */
11801#ifdef FEAT_MBYTE
11802 if (has_mbyte)
11803 {
11804 c = mb_ptr2char(fword + sp->ts_fidx);
11805 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11806 if (enc_utf8 && utf_iscomposing(c))
11807 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11808 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11809 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11810 }
11811 else
11812#endif
11813 {
11814 ++stack[depth].ts_fidx;
11815 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11816 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11817 }
11818 break;
11819 }
11820 /*FALLTHROUGH*/
11821
11822 case STATE_INS_PREP:
11823 if (sp->ts_flags & TSF_DIDDEL)
11824 {
11825 /* If we just deleted a byte then inserting won't make sense,
11826 * a substitute is always cheaper. */
11827 sp->ts_state = STATE_SWAP;
11828 break;
11829 }
11830
11831 /* skip over NUL bytes */
11832 n = sp->ts_arridx;
11833 for (;;)
11834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011835 if (sp->ts_curi > byts[n])
11836 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011837 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011838 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011839 break;
11840 }
11841 if (byts[n + sp->ts_curi] != NUL)
11842 {
11843 /* Found a byte to insert. */
11844 sp->ts_state = STATE_INS;
11845 break;
11846 }
11847 ++sp->ts_curi;
11848 }
11849 break;
11850
11851 /*FALLTHROUGH*/
11852
11853 case STATE_INS:
11854 /* Insert one byte. Repeat this for each possible byte at this
11855 * node. */
11856 n = sp->ts_arridx;
11857 if (sp->ts_curi > byts[n])
11858 {
11859 /* Done all bytes at this node, go to next state. */
11860 sp->ts_state = STATE_SWAP;
11861 break;
11862 }
11863
11864 /* Do one more byte at this node, but:
11865 * - Skip NUL bytes.
11866 * - Skip the byte if it's equal to the byte in the word,
11867 * accepting that byte is always better.
11868 */
11869 n += sp->ts_curi++;
11870 c = byts[n];
11871 if (soundfold && sp->ts_twordlen == 0 && c == '*')
11872 /* Inserting a vowel at the start of a word counts less,
11873 * see soundalike_score(). */
11874 newscore = 2 * SCORE_INS / 3;
11875 else
11876 newscore = SCORE_INS;
11877 if (c != fword[sp->ts_fidx]
11878 && TRY_DEEPER(su, stack, depth, newscore))
11879 {
11880 go_deeper(stack, depth, newscore);
11881#ifdef DEBUG_TRIEWALK
11882 sprintf(changename[depth], "%.*s-%s: insert %c",
11883 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11884 c);
11885#endif
11886 ++depth;
11887 sp = &stack[depth];
11888 tword[sp->ts_twordlen++] = c;
11889 sp->ts_arridx = idxs[n];
11890#ifdef FEAT_MBYTE
11891 if (has_mbyte)
11892 {
11893 fl = MB_BYTE2LEN(c);
11894 if (fl > 1)
11895 {
11896 /* There are following bytes for the same character.
11897 * We must find all bytes before trying
11898 * delete/insert/swap/etc. */
11899 sp->ts_tcharlen = fl;
11900 sp->ts_tcharidx = 1;
11901 sp->ts_isdiff = DIFF_INSERT;
11902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 }
11904 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011905 fl = 1;
11906 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000011907#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011908 {
11909 /* If the previous character was the same, thus doubling a
11910 * character, give a bonus to the score. Also for
11911 * soundfold words (illogical but does give a better
11912 * score). */
11913 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000011914 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011915 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011917 }
11918 break;
11919
11920 case STATE_SWAP:
11921 /*
11922 * Swap two bytes in the bad word: "12" -> "21".
11923 * We change "fword" here, it's changed back afterwards at
11924 * STATE_UNSWAP.
11925 */
11926 p = fword + sp->ts_fidx;
11927 c = *p;
11928 if (c == NUL)
11929 {
11930 /* End of word, can't swap or replace. */
11931 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934
Bram Moolenaar4770d092006-01-12 23:22:24 +000011935 /* Don't swap if the first character is not a word character.
11936 * SWAP3 etc. also don't make sense then. */
11937 if (!soundfold && !spell_iswordp(p, curbuf))
11938 {
11939 sp->ts_state = STATE_REP_INI;
11940 break;
11941 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011942
Bram Moolenaar4770d092006-01-12 23:22:24 +000011943#ifdef FEAT_MBYTE
11944 if (has_mbyte)
11945 {
11946 n = mb_cptr2len(p);
11947 c = mb_ptr2char(p);
11948 if (!soundfold && !spell_iswordp(p + n, curbuf))
11949 c2 = c; /* don't swap non-word char */
11950 else
11951 c2 = mb_ptr2char(p + n);
11952 }
11953 else
11954#endif
11955 {
11956 if (!soundfold && !spell_iswordp(p + 1, curbuf))
11957 c2 = c; /* don't swap non-word char */
11958 else
11959 c2 = p[1];
11960 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011961
Bram Moolenaar4770d092006-01-12 23:22:24 +000011962 /* When characters are identical, swap won't do anything.
11963 * Also get here if the second char is not a word character. */
11964 if (c == c2)
11965 {
11966 sp->ts_state = STATE_SWAP3;
11967 break;
11968 }
11969 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
11970 {
11971 go_deeper(stack, depth, SCORE_SWAP);
11972#ifdef DEBUG_TRIEWALK
11973 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
11974 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11975 c, c2);
11976#endif
11977 sp->ts_state = STATE_UNSWAP;
11978 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011979#ifdef FEAT_MBYTE
11980 if (has_mbyte)
11981 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011982 fl = mb_char2len(c2);
11983 mch_memmove(p, p + n, fl);
11984 mb_char2bytes(c, p + fl);
11985 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011986 }
11987 else
11988#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011989 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011990 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011991 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011992 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011993 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011994 }
11995 else
11996 /* If this swap doesn't work then SWAP3 won't either. */
11997 sp->ts_state = STATE_REP_INI;
11998 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000011999
Bram Moolenaar4770d092006-01-12 23:22:24 +000012000 case STATE_UNSWAP:
12001 /* Undo the STATE_SWAP swap: "21" -> "12". */
12002 p = fword + sp->ts_fidx;
12003#ifdef FEAT_MBYTE
12004 if (has_mbyte)
12005 {
12006 n = MB_BYTE2LEN(*p);
12007 c = mb_ptr2char(p + n);
12008 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12009 mb_char2bytes(c, p);
12010 }
12011 else
12012#endif
12013 {
12014 c = *p;
12015 *p = p[1];
12016 p[1] = c;
12017 }
12018 /*FALLTHROUGH*/
12019
12020 case STATE_SWAP3:
12021 /* Swap two bytes, skipping one: "123" -> "321". We change
12022 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12023 p = fword + sp->ts_fidx;
12024#ifdef FEAT_MBYTE
12025 if (has_mbyte)
12026 {
12027 n = mb_cptr2len(p);
12028 c = mb_ptr2char(p);
12029 fl = mb_cptr2len(p + n);
12030 c2 = mb_ptr2char(p + n);
12031 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12032 c3 = c; /* don't swap non-word char */
12033 else
12034 c3 = mb_ptr2char(p + n + fl);
12035 }
12036 else
12037#endif
12038 {
12039 c = *p;
12040 c2 = p[1];
12041 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12042 c3 = c; /* don't swap non-word char */
12043 else
12044 c3 = p[2];
12045 }
12046
12047 /* When characters are identical: "121" then SWAP3 result is
12048 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12049 * same as SWAP on next char: "112". Thus skip all swapping.
12050 * Also skip when c3 is NUL.
12051 * Also get here when the third character is not a word character.
12052 * Second character may any char: "a.b" -> "b.a" */
12053 if (c == c3 || c3 == NUL)
12054 {
12055 sp->ts_state = STATE_REP_INI;
12056 break;
12057 }
12058 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12059 {
12060 go_deeper(stack, depth, SCORE_SWAP3);
12061#ifdef DEBUG_TRIEWALK
12062 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12063 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12064 c, c3);
12065#endif
12066 sp->ts_state = STATE_UNSWAP3;
12067 ++depth;
12068#ifdef FEAT_MBYTE
12069 if (has_mbyte)
12070 {
12071 tl = mb_char2len(c3);
12072 mch_memmove(p, p + n + fl, tl);
12073 mb_char2bytes(c2, p + tl);
12074 mb_char2bytes(c, p + fl + tl);
12075 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12076 }
12077 else
12078#endif
12079 {
12080 p[0] = p[2];
12081 p[2] = c;
12082 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12083 }
12084 }
12085 else
12086 sp->ts_state = STATE_REP_INI;
12087 break;
12088
12089 case STATE_UNSWAP3:
12090 /* Undo STATE_SWAP3: "321" -> "123" */
12091 p = fword + sp->ts_fidx;
12092#ifdef FEAT_MBYTE
12093 if (has_mbyte)
12094 {
12095 n = MB_BYTE2LEN(*p);
12096 c2 = mb_ptr2char(p + n);
12097 fl = MB_BYTE2LEN(p[n]);
12098 c = mb_ptr2char(p + n + fl);
12099 tl = MB_BYTE2LEN(p[n + fl]);
12100 mch_memmove(p + fl + tl, p, n);
12101 mb_char2bytes(c, p);
12102 mb_char2bytes(c2, p + tl);
12103 p = p + tl;
12104 }
12105 else
12106#endif
12107 {
12108 c = *p;
12109 *p = p[2];
12110 p[2] = c;
12111 ++p;
12112 }
12113
12114 if (!soundfold && !spell_iswordp(p, curbuf))
12115 {
12116 /* Middle char is not a word char, skip the rotate. First and
12117 * third char were already checked at swap and swap3. */
12118 sp->ts_state = STATE_REP_INI;
12119 break;
12120 }
12121
12122 /* Rotate three characters left: "123" -> "231". We change
12123 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12124 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12125 {
12126 go_deeper(stack, depth, SCORE_SWAP3);
12127#ifdef DEBUG_TRIEWALK
12128 p = fword + sp->ts_fidx;
12129 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12130 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12131 p[0], p[1], p[2]);
12132#endif
12133 sp->ts_state = STATE_UNROT3L;
12134 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012135 p = fword + sp->ts_fidx;
12136#ifdef FEAT_MBYTE
12137 if (has_mbyte)
12138 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012139 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012140 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012141 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012142 fl += mb_cptr2len(p + n + fl);
12143 mch_memmove(p, p + n, fl);
12144 mb_char2bytes(c, p + fl);
12145 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012146 }
12147 else
12148#endif
12149 {
12150 c = *p;
12151 *p = p[1];
12152 p[1] = p[2];
12153 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012154 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012155 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012156 }
12157 else
12158 sp->ts_state = STATE_REP_INI;
12159 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012160
Bram Moolenaar4770d092006-01-12 23:22:24 +000012161 case STATE_UNROT3L:
12162 /* Undo ROT3L: "231" -> "123" */
12163 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012164#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012165 if (has_mbyte)
12166 {
12167 n = MB_BYTE2LEN(*p);
12168 n += MB_BYTE2LEN(p[n]);
12169 c = mb_ptr2char(p + n);
12170 tl = MB_BYTE2LEN(p[n]);
12171 mch_memmove(p + tl, p, n);
12172 mb_char2bytes(c, p);
12173 }
12174 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012175#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012176 {
12177 c = p[2];
12178 p[2] = p[1];
12179 p[1] = *p;
12180 *p = c;
12181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012182
Bram Moolenaar4770d092006-01-12 23:22:24 +000012183 /* Rotate three bytes right: "123" -> "312". We change "fword"
12184 * here, it's changed back afterwards at STATE_UNROT3R. */
12185 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12186 {
12187 go_deeper(stack, depth, SCORE_SWAP3);
12188#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012190 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12191 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12192 p[0], p[1], p[2]);
12193#endif
12194 sp->ts_state = STATE_UNROT3R;
12195 ++depth;
12196 p = fword + sp->ts_fidx;
12197#ifdef FEAT_MBYTE
12198 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012199 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012200 n = mb_cptr2len(p);
12201 n += mb_cptr2len(p + n);
12202 c = mb_ptr2char(p + n);
12203 tl = mb_cptr2len(p + n);
12204 mch_memmove(p + tl, p, n);
12205 mb_char2bytes(c, p);
12206 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012207 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012208 else
12209#endif
12210 {
12211 c = p[2];
12212 p[2] = p[1];
12213 p[1] = *p;
12214 *p = c;
12215 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12216 }
12217 }
12218 else
12219 sp->ts_state = STATE_REP_INI;
12220 break;
12221
12222 case STATE_UNROT3R:
12223 /* Undo ROT3R: "312" -> "123" */
12224 p = fword + sp->ts_fidx;
12225#ifdef FEAT_MBYTE
12226 if (has_mbyte)
12227 {
12228 c = mb_ptr2char(p);
12229 tl = MB_BYTE2LEN(*p);
12230 n = MB_BYTE2LEN(p[tl]);
12231 n += MB_BYTE2LEN(p[tl + n]);
12232 mch_memmove(p, p + tl, n);
12233 mb_char2bytes(c, p + n);
12234 }
12235 else
12236#endif
12237 {
12238 c = *p;
12239 *p = p[1];
12240 p[1] = p[2];
12241 p[2] = c;
12242 }
12243 /*FALLTHROUGH*/
12244
12245 case STATE_REP_INI:
12246 /* Check if matching with REP items from the .aff file would work.
12247 * Quickly skip if:
12248 * - there are no REP items and we are not in the soundfold trie
12249 * - the score is going to be too high anyway
12250 * - already applied a REP item or swapped here */
12251 if ((lp->lp_replang == NULL && !soundfold)
12252 || sp->ts_score + SCORE_REP >= su->su_maxscore
12253 || sp->ts_fidx < sp->ts_fidxtry)
12254 {
12255 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012256 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012258
Bram Moolenaar4770d092006-01-12 23:22:24 +000012259 /* Use the first byte to quickly find the first entry that may
12260 * match. If the index is -1 there is none. */
12261 if (soundfold)
12262 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12263 else
12264 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265
Bram Moolenaar4770d092006-01-12 23:22:24 +000012266 if (sp->ts_curi < 0)
12267 {
12268 sp->ts_state = STATE_FINAL;
12269 break;
12270 }
12271
12272 sp->ts_state = STATE_REP;
12273 /*FALLTHROUGH*/
12274
12275 case STATE_REP:
12276 /* Try matching with REP items from the .aff file. For each match
12277 * replace the characters and check if the resulting word is
12278 * valid. */
12279 p = fword + sp->ts_fidx;
12280
12281 if (soundfold)
12282 gap = &slang->sl_repsal;
12283 else
12284 gap = &lp->lp_replang->sl_rep;
12285 while (sp->ts_curi < gap->ga_len)
12286 {
12287 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12288 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012289 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012290 /* past possible matching entries */
12291 sp->ts_curi = gap->ga_len;
12292 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012293 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012294 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12295 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12296 {
12297 go_deeper(stack, depth, SCORE_REP);
12298#ifdef DEBUG_TRIEWALK
12299 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12300 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12301 ftp->ft_from, ftp->ft_to);
12302#endif
12303 /* Need to undo this afterwards. */
12304 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012305
Bram Moolenaar4770d092006-01-12 23:22:24 +000012306 /* Change the "from" to the "to" string. */
12307 ++depth;
12308 fl = STRLEN(ftp->ft_from);
12309 tl = STRLEN(ftp->ft_to);
12310 if (fl != tl)
12311 {
12312 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12313 repextra += tl - fl;
12314 }
12315 mch_memmove(p, ftp->ft_to, tl);
12316 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12317#ifdef FEAT_MBYTE
12318 stack[depth].ts_tcharlen = 0;
12319#endif
12320 break;
12321 }
12322 }
12323
12324 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12325 /* No (more) matches. */
12326 sp->ts_state = STATE_FINAL;
12327
12328 break;
12329
12330 case STATE_REP_UNDO:
12331 /* Undo a REP replacement and continue with the next one. */
12332 if (soundfold)
12333 gap = &slang->sl_repsal;
12334 else
12335 gap = &lp->lp_replang->sl_rep;
12336 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12337 fl = STRLEN(ftp->ft_from);
12338 tl = STRLEN(ftp->ft_to);
12339 p = fword + sp->ts_fidx;
12340 if (fl != tl)
12341 {
12342 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12343 repextra -= tl - fl;
12344 }
12345 mch_memmove(p, ftp->ft_from, fl);
12346 sp->ts_state = STATE_REP;
12347 break;
12348
12349 default:
12350 /* Did all possible states at this level, go up one level. */
12351 --depth;
12352
12353 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12354 {
12355 /* Continue in or go back to the prefix tree. */
12356 byts = pbyts;
12357 idxs = pidxs;
12358 }
12359
12360 /* Don't check for CTRL-C too often, it takes time. */
12361 if (--breakcheckcount == 0)
12362 {
12363 ui_breakcheck();
12364 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012366 }
12367 }
12368}
12369
Bram Moolenaar4770d092006-01-12 23:22:24 +000012370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012371/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012372 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012373 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012374 static void
12375go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012376 trystate_T *stack;
12377 int depth;
12378 int score_add;
12379{
Bram Moolenaarea424162005-06-16 21:51:00 +000012380 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012381 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012382 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012384 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385}
12386
Bram Moolenaar53805d12005-08-01 07:08:33 +000012387#ifdef FEAT_MBYTE
12388/*
12389 * Case-folding may change the number of bytes: Count nr of chars in
12390 * fword[flen] and return the byte length of that many chars in "word".
12391 */
12392 static int
12393nofold_len(fword, flen, word)
12394 char_u *fword;
12395 int flen;
12396 char_u *word;
12397{
12398 char_u *p;
12399 int i = 0;
12400
12401 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12402 ++i;
12403 for (p = word; i > 0; mb_ptr_adv(p))
12404 --i;
12405 return (int)(p - word);
12406}
12407#endif
12408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012409/*
12410 * "fword" is a good word with case folded. Find the matching keep-case
12411 * words and put it in "kword".
12412 * Theoretically there could be several keep-case words that result in the
12413 * same case-folded word, but we only find one...
12414 */
12415 static void
12416find_keepcap_word(slang, fword, kword)
12417 slang_T *slang;
12418 char_u *fword;
12419 char_u *kword;
12420{
12421 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12422 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012423 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424
12425 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012426 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012427 int round[MAXWLEN];
12428 int fwordidx[MAXWLEN];
12429 int uwordidx[MAXWLEN];
12430 int kwordlen[MAXWLEN];
12431
12432 int flen, ulen;
12433 int l;
12434 int len;
12435 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012436 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012437 char_u *p;
12438 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012439 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012440
12441 if (byts == NULL)
12442 {
12443 /* array is empty: "cannot happen" */
12444 *kword = NUL;
12445 return;
12446 }
12447
12448 /* Make an all-cap version of "fword". */
12449 allcap_copy(fword, uword);
12450
12451 /*
12452 * Each character needs to be tried both case-folded and upper-case.
12453 * All this gets very complicated if we keep in mind that changing case
12454 * may change the byte length of a multi-byte character...
12455 */
12456 depth = 0;
12457 arridx[0] = 0;
12458 round[0] = 0;
12459 fwordidx[0] = 0;
12460 uwordidx[0] = 0;
12461 kwordlen[0] = 0;
12462 while (depth >= 0)
12463 {
12464 if (fword[fwordidx[depth]] == NUL)
12465 {
12466 /* We are at the end of "fword". If the tree allows a word to end
12467 * here we have found a match. */
12468 if (byts[arridx[depth] + 1] == 0)
12469 {
12470 kword[kwordlen[depth]] = NUL;
12471 return;
12472 }
12473
12474 /* kword is getting too long, continue one level up */
12475 --depth;
12476 }
12477 else if (++round[depth] > 2)
12478 {
12479 /* tried both fold-case and upper-case character, continue one
12480 * level up */
12481 --depth;
12482 }
12483 else
12484 {
12485 /*
12486 * round[depth] == 1: Try using the folded-case character.
12487 * round[depth] == 2: Try using the upper-case character.
12488 */
12489#ifdef FEAT_MBYTE
12490 if (has_mbyte)
12491 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012492 flen = mb_cptr2len(fword + fwordidx[depth]);
12493 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 }
12495 else
12496#endif
12497 ulen = flen = 1;
12498 if (round[depth] == 1)
12499 {
12500 p = fword + fwordidx[depth];
12501 l = flen;
12502 }
12503 else
12504 {
12505 p = uword + uwordidx[depth];
12506 l = ulen;
12507 }
12508
12509 for (tryidx = arridx[depth]; l > 0; --l)
12510 {
12511 /* Perform a binary search in the list of accepted bytes. */
12512 len = byts[tryidx++];
12513 c = *p++;
12514 lo = tryidx;
12515 hi = tryidx + len - 1;
12516 while (lo < hi)
12517 {
12518 m = (lo + hi) / 2;
12519 if (byts[m] > c)
12520 hi = m - 1;
12521 else if (byts[m] < c)
12522 lo = m + 1;
12523 else
12524 {
12525 lo = hi = m;
12526 break;
12527 }
12528 }
12529
12530 /* Stop if there is no matching byte. */
12531 if (hi < lo || byts[lo] != c)
12532 break;
12533
12534 /* Continue at the child (if there is one). */
12535 tryidx = idxs[lo];
12536 }
12537
12538 if (l == 0)
12539 {
12540 /*
12541 * Found the matching char. Copy it to "kword" and go a
12542 * level deeper.
12543 */
12544 if (round[depth] == 1)
12545 {
12546 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12547 flen);
12548 kwordlen[depth + 1] = kwordlen[depth] + flen;
12549 }
12550 else
12551 {
12552 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12553 ulen);
12554 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12555 }
12556 fwordidx[depth + 1] = fwordidx[depth] + flen;
12557 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12558
12559 ++depth;
12560 arridx[depth] = tryidx;
12561 round[depth] = 0;
12562 }
12563 }
12564 }
12565
12566 /* Didn't find it: "cannot happen". */
12567 *kword = NUL;
12568}
12569
12570/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012571 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12572 * su->su_sga.
12573 */
12574 static void
12575score_comp_sal(su)
12576 suginfo_T *su;
12577{
12578 langp_T *lp;
12579 char_u badsound[MAXWLEN];
12580 int i;
12581 suggest_T *stp;
12582 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012583 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012584 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012585
12586 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12587 return;
12588
12589 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012590 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012591 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012592 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012593 if (lp->lp_slang->sl_sal.ga_len > 0)
12594 {
12595 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012596 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012597
12598 for (i = 0; i < su->su_ga.ga_len; ++i)
12599 {
12600 stp = &SUG(su->su_ga, i);
12601
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012602 /* Case-fold the suggested word, sound-fold it and compute the
12603 * sound-a-like score. */
12604 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012605 if (score < SCORE_MAXMAX)
12606 {
12607 /* Add the suggestion. */
12608 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12609 sstp->st_word = vim_strsave(stp->st_word);
12610 if (sstp->st_word != NULL)
12611 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012612 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012613 sstp->st_score = score;
12614 sstp->st_altscore = 0;
12615 sstp->st_orglen = stp->st_orglen;
12616 ++su->su_sga.ga_len;
12617 }
12618 }
12619 }
12620 break;
12621 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012622 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012623}
12624
12625/*
12626 * Combine the list of suggestions in su->su_ga and su->su_sga.
12627 * They are intwined.
12628 */
12629 static void
12630score_combine(su)
12631 suginfo_T *su;
12632{
12633 int i;
12634 int j;
12635 garray_T ga;
12636 garray_T *gap;
12637 langp_T *lp;
12638 suggest_T *stp;
12639 char_u *p;
12640 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012641 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012642 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012643 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012644
12645 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012646 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012647 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012648 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012649 if (lp->lp_slang->sl_sal.ga_len > 0)
12650 {
12651 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012652 slang = lp->lp_slang;
12653 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012654
12655 for (i = 0; i < su->su_ga.ga_len; ++i)
12656 {
12657 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012658 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012659 if (stp->st_altscore == SCORE_MAXMAX)
12660 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12661 else
12662 stp->st_score = (stp->st_score * 3
12663 + stp->st_altscore) / 4;
12664 stp->st_salscore = FALSE;
12665 }
12666 break;
12667 }
12668 }
12669
Bram Moolenaar4770d092006-01-12 23:22:24 +000012670 if (slang == NULL) /* just in case */
12671 return;
12672
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012673 /* Add the alternate score to su_sga. */
12674 for (i = 0; i < su->su_sga.ga_len; ++i)
12675 {
12676 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012677 stp->st_altscore = spell_edit_score(slang,
12678 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012679 if (stp->st_score == SCORE_MAXMAX)
12680 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12681 else
12682 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12683 stp->st_salscore = TRUE;
12684 }
12685
Bram Moolenaar4770d092006-01-12 23:22:24 +000012686 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12687 * for both lists. */
12688 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012689 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012690 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012691 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12692
12693 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12694 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12695 return;
12696
12697 stp = &SUG(ga, 0);
12698 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12699 {
12700 /* round 1: get a suggestion from su_ga
12701 * round 2: get a suggestion from su_sga */
12702 for (round = 1; round <= 2; ++round)
12703 {
12704 gap = round == 1 ? &su->su_ga : &su->su_sga;
12705 if (i < gap->ga_len)
12706 {
12707 /* Don't add a word if it's already there. */
12708 p = SUG(*gap, i).st_word;
12709 for (j = 0; j < ga.ga_len; ++j)
12710 if (STRCMP(stp[j].st_word, p) == 0)
12711 break;
12712 if (j == ga.ga_len)
12713 stp[ga.ga_len++] = SUG(*gap, i);
12714 else
12715 vim_free(p);
12716 }
12717 }
12718 }
12719
12720 ga_clear(&su->su_ga);
12721 ga_clear(&su->su_sga);
12722
12723 /* Truncate the list to the number of suggestions that will be displayed. */
12724 if (ga.ga_len > su->su_maxcount)
12725 {
12726 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12727 vim_free(stp[i].st_word);
12728 ga.ga_len = su->su_maxcount;
12729 }
12730
12731 su->su_ga = ga;
12732}
12733
12734/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012735 * For the goodword in "stp" compute the soundalike score compared to the
12736 * badword.
12737 */
12738 static int
12739stp_sal_score(stp, su, slang, badsound)
12740 suggest_T *stp;
12741 suginfo_T *su;
12742 slang_T *slang;
12743 char_u *badsound; /* sound-folded badword */
12744{
12745 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012746 char_u *pbad;
12747 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012748 char_u badsound2[MAXWLEN];
12749 char_u fword[MAXWLEN];
12750 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012751 char_u goodword[MAXWLEN];
12752 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012753
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012754 lendiff = (int)(su->su_badlen - stp->st_orglen);
12755 if (lendiff >= 0)
12756 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012757 else
12758 {
12759 /* soundfold the bad word with more characters following */
12760 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12761
12762 /* When joining two words the sound often changes a lot. E.g., "t he"
12763 * sounds like "t h" while "the" sounds like "@". Avoid that by
12764 * removing the space. Don't do it when the good word also contains a
12765 * space. */
12766 if (vim_iswhite(su->su_badptr[su->su_badlen])
12767 && *skiptowhite(stp->st_word) == NUL)
12768 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12769 mch_memmove(p, p + 1, STRLEN(p));
12770
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012771 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012772 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012773 }
12774
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012775 if (lendiff > 0)
12776 {
12777 /* Add part of the bad word to the good word, so that we soundfold
12778 * what replaces the bad word. */
12779 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012780 vim_strncpy(goodword + stp->st_wordlen,
12781 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012782 pgood = goodword;
12783 }
12784 else
12785 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012786
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012787 /* Sound-fold the word and compute the score for the difference. */
12788 spell_soundfold(slang, pgood, FALSE, goodsound);
12789
12790 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012791}
12792
Bram Moolenaar4770d092006-01-12 23:22:24 +000012793/* structure used to store soundfolded words that add_sound_suggest() has
12794 * handled already. */
12795typedef struct
12796{
12797 short sft_score; /* lowest score used */
12798 char_u sft_word[1]; /* soundfolded word, actually longer */
12799} sftword_T;
12800
12801static sftword_T dumsft;
12802#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12803#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12804
12805/*
12806 * Prepare for calling suggest_try_soundalike().
12807 */
12808 static void
12809suggest_try_soundalike_prep()
12810{
12811 langp_T *lp;
12812 int lpi;
12813 slang_T *slang;
12814
12815 /* Do this for all languages that support sound folding and for which a
12816 * .sug file has been loaded. */
12817 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12818 {
12819 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12820 slang = lp->lp_slang;
12821 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12822 /* prepare the hashtable used by add_sound_suggest() */
12823 hash_init(&slang->sl_sounddone);
12824 }
12825}
12826
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012827/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012829 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 */
12831 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012832suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012833 suginfo_T *su;
12834{
12835 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012837 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012838 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839
Bram Moolenaar4770d092006-01-12 23:22:24 +000012840 /* Do this for all languages that support sound folding and for which a
12841 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012842 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012844 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12845 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012846 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012847 {
12848 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012849 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012850
Bram Moolenaar4770d092006-01-12 23:22:24 +000012851 /* try all kinds of inserts/deletes/swaps/etc. */
12852 /* TODO: also soundfold the next words, so that we can try joining
12853 * and splitting */
12854 suggest_trie_walk(su, lp, salword, TRUE);
12855 }
12856 }
12857}
12858
12859/*
12860 * Finish up after calling suggest_try_soundalike().
12861 */
12862 static void
12863suggest_try_soundalike_finish()
12864{
12865 langp_T *lp;
12866 int lpi;
12867 slang_T *slang;
12868 int todo;
12869 hashitem_T *hi;
12870
12871 /* Do this for all languages that support sound folding and for which a
12872 * .sug file has been loaded. */
12873 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12874 {
12875 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12876 slang = lp->lp_slang;
12877 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12878 {
12879 /* Free the info about handled words. */
12880 todo = slang->sl_sounddone.ht_used;
12881 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
12882 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012883 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012884 vim_free(HI2SFT(hi));
12885 --todo;
12886 }
12887 hash_clear(&slang->sl_sounddone);
12888 }
12889 }
12890}
12891
12892/*
12893 * A match with a soundfolded word is found. Add the good word(s) that
12894 * produce this soundfolded word.
12895 */
12896 static void
12897add_sound_suggest(su, goodword, score, lp)
12898 suginfo_T *su;
12899 char_u *goodword;
12900 int score; /* soundfold score */
12901 langp_T *lp;
12902{
12903 slang_T *slang = lp->lp_slang; /* language for sound folding */
12904 int sfwordnr;
12905 char_u *nrline;
12906 int orgnr;
12907 char_u theword[MAXWLEN];
12908 int i;
12909 int wlen;
12910 char_u *byts;
12911 idx_T *idxs;
12912 int n;
12913 int wordcount;
12914 int wc;
12915 int goodscore;
12916 hash_T hash;
12917 hashitem_T *hi;
12918 sftword_T *sft;
12919 int bc, gc;
12920 int limit;
12921
12922 /*
12923 * It's very well possible that the same soundfold word is found several
12924 * times with different scores. Since the following is quite slow only do
12925 * the words that have a better score than before. Use a hashtable to
12926 * remember the words that have been done.
12927 */
12928 hash = hash_hash(goodword);
12929 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
12930 if (HASHITEM_EMPTY(hi))
12931 {
12932 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
12933 if (sft != NULL)
12934 {
12935 sft->sft_score = score;
12936 STRCPY(sft->sft_word, goodword);
12937 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
12938 }
12939 }
12940 else
12941 {
12942 sft = HI2SFT(hi);
12943 if (score >= sft->sft_score)
12944 return;
12945 sft->sft_score = score;
12946 }
12947
12948 /*
12949 * Find the word nr in the soundfold tree.
12950 */
12951 sfwordnr = soundfold_find(slang, goodword);
12952 if (sfwordnr < 0)
12953 {
12954 EMSG2(_(e_intern2), "add_sound_suggest()");
12955 return;
12956 }
12957
12958 /*
12959 * go over the list of good words that produce this soundfold word
12960 */
12961 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
12962 orgnr = 0;
12963 while (*nrline != NUL)
12964 {
12965 /* The wordnr was stored in a minimal nr of bytes as an offset to the
12966 * previous wordnr. */
12967 orgnr += bytes2offset(&nrline);
12968
12969 byts = slang->sl_fbyts;
12970 idxs = slang->sl_fidxs;
12971
12972 /* Lookup the word "orgnr" one of the two tries. */
12973 n = 0;
12974 wlen = 0;
12975 wordcount = 0;
12976 for (;;)
12977 {
12978 i = 1;
12979 if (wordcount == orgnr && byts[n + 1] == NUL)
12980 break; /* found end of word */
12981
12982 if (byts[n + 1] == NUL)
12983 ++wordcount;
12984
12985 /* skip over the NUL bytes */
12986 for ( ; byts[n + i] == NUL; ++i)
12987 if (i > byts[n]) /* safety check */
12988 {
12989 STRCPY(theword + wlen, "BAD");
12990 goto badword;
12991 }
12992
12993 /* One of the siblings must have the word. */
12994 for ( ; i < byts[n]; ++i)
12995 {
12996 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
12997 if (wordcount + wc > orgnr)
12998 break;
12999 wordcount += wc;
13000 }
13001
13002 theword[wlen++] = byts[n + i];
13003 n = idxs[n + i];
13004 }
13005badword:
13006 theword[wlen] = NUL;
13007
13008 /* Go over the possible flags and regions. */
13009 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13010 {
13011 char_u cword[MAXWLEN];
13012 char_u *p;
13013 int flags = (int)idxs[n + i];
13014
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013015 /* Skip words with the NOSUGGEST flag */
13016 if (flags & WF_NOSUGGEST)
13017 continue;
13018
Bram Moolenaar4770d092006-01-12 23:22:24 +000013019 if (flags & WF_KEEPCAP)
13020 {
13021 /* Must find the word in the keep-case tree. */
13022 find_keepcap_word(slang, theword, cword);
13023 p = cword;
13024 }
13025 else
13026 {
13027 flags |= su->su_badflags;
13028 if ((flags & WF_CAPMASK) != 0)
13029 {
13030 /* Need to fix case according to "flags". */
13031 make_case_word(theword, cword, flags);
13032 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013033 }
13034 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013035 p = theword;
13036 }
13037
13038 /* Add the suggestion. */
13039 if (sps_flags & SPS_DOUBLE)
13040 {
13041 /* Add the suggestion if the score isn't too bad. */
13042 if (score <= su->su_maxscore)
13043 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13044 score, 0, FALSE, slang, FALSE);
13045 }
13046 else
13047 {
13048 /* Add a penalty for words in another region. */
13049 if ((flags & WF_REGION)
13050 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13051 goodscore = SCORE_REGION;
13052 else
13053 goodscore = 0;
13054
13055 /* Add a small penalty for changing the first letter from
13056 * lower to upper case. Helps for "tath" -> "Kath", which is
13057 * less common thatn "tath" -> "path". Don't do it when the
13058 * letter is the same, that has already been counted. */
13059 gc = PTR2CHAR(p);
13060 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013062 bc = PTR2CHAR(su->su_badword);
13063 if (!SPELL_ISUPPER(bc)
13064 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13065 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 }
13067
Bram Moolenaar4770d092006-01-12 23:22:24 +000013068 /* Compute the score for the good word. This only does letter
13069 * insert/delete/swap/replace. REP items are not considered,
13070 * which may make the score a bit higher.
13071 * Use a limit for the score to make it work faster. Use
13072 * MAXSCORE(), because RESCORE() will change the score.
13073 * If the limit is very high then the iterative method is
13074 * inefficient, using an array is quicker. */
13075 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13076 if (limit > SCORE_LIMITMAX)
13077 goodscore += spell_edit_score(slang, su->su_badword, p);
13078 else
13079 goodscore += spell_edit_score_limit(slang, su->su_badword,
13080 p, limit);
13081
13082 /* When going over the limit don't bother to do the rest. */
13083 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013085 /* Give a bonus to words seen before. */
13086 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087
Bram Moolenaar4770d092006-01-12 23:22:24 +000013088 /* Add the suggestion if the score isn't too bad. */
13089 goodscore = RESCORE(goodscore, score);
13090 if (goodscore <= su->su_sfmaxscore)
13091 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13092 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 }
13095 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013096 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 }
13098}
13099
13100/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013101 * Find word "word" in fold-case tree for "slang" and return the word number.
13102 */
13103 static int
13104soundfold_find(slang, word)
13105 slang_T *slang;
13106 char_u *word;
13107{
13108 idx_T arridx = 0;
13109 int len;
13110 int wlen = 0;
13111 int c;
13112 char_u *ptr = word;
13113 char_u *byts;
13114 idx_T *idxs;
13115 int wordnr = 0;
13116
13117 byts = slang->sl_sbyts;
13118 idxs = slang->sl_sidxs;
13119
13120 for (;;)
13121 {
13122 /* First byte is the number of possible bytes. */
13123 len = byts[arridx++];
13124
13125 /* If the first possible byte is a zero the word could end here.
13126 * If the word ends we found the word. If not skip the NUL bytes. */
13127 c = ptr[wlen];
13128 if (byts[arridx] == NUL)
13129 {
13130 if (c == NUL)
13131 break;
13132
13133 /* Skip over the zeros, there can be several. */
13134 while (len > 0 && byts[arridx] == NUL)
13135 {
13136 ++arridx;
13137 --len;
13138 }
13139 if (len == 0)
13140 return -1; /* no children, word should have ended here */
13141 ++wordnr;
13142 }
13143
13144 /* If the word ends we didn't find it. */
13145 if (c == NUL)
13146 return -1;
13147
13148 /* Perform a binary search in the list of accepted bytes. */
13149 if (c == TAB) /* <Tab> is handled like <Space> */
13150 c = ' ';
13151 while (byts[arridx] < c)
13152 {
13153 /* The word count is in the first idxs[] entry of the child. */
13154 wordnr += idxs[idxs[arridx]];
13155 ++arridx;
13156 if (--len == 0) /* end of the bytes, didn't find it */
13157 return -1;
13158 }
13159 if (byts[arridx] != c) /* didn't find the byte */
13160 return -1;
13161
13162 /* Continue at the child (if there is one). */
13163 arridx = idxs[arridx];
13164 ++wlen;
13165
13166 /* One space in the good word may stand for several spaces in the
13167 * checked word. */
13168 if (c == ' ')
13169 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13170 ++wlen;
13171 }
13172
13173 return wordnr;
13174}
13175
13176/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013177 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 */
13179 static void
13180make_case_word(fword, cword, flags)
13181 char_u *fword;
13182 char_u *cword;
13183 int flags;
13184{
13185 if (flags & WF_ALLCAP)
13186 /* Make it all upper-case */
13187 allcap_copy(fword, cword);
13188 else if (flags & WF_ONECAP)
13189 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013190 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013191 else
13192 /* Use goodword as-is. */
13193 STRCPY(cword, fword);
13194}
13195
Bram Moolenaarea424162005-06-16 21:51:00 +000013196/*
13197 * Use map string "map" for languages "lp".
13198 */
13199 static void
13200set_map_str(lp, map)
13201 slang_T *lp;
13202 char_u *map;
13203{
13204 char_u *p;
13205 int headc = 0;
13206 int c;
13207 int i;
13208
13209 if (*map == NUL)
13210 {
13211 lp->sl_has_map = FALSE;
13212 return;
13213 }
13214 lp->sl_has_map = TRUE;
13215
Bram Moolenaar4770d092006-01-12 23:22:24 +000013216 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013217 for (i = 0; i < 256; ++i)
13218 lp->sl_map_array[i] = 0;
13219#ifdef FEAT_MBYTE
13220 hash_init(&lp->sl_map_hash);
13221#endif
13222
13223 /*
13224 * The similar characters are stored separated with slashes:
13225 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13226 * before the same slash. For characters above 255 sl_map_hash is used.
13227 */
13228 for (p = map; *p != NUL; )
13229 {
13230#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013231 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013232#else
13233 c = *p++;
13234#endif
13235 if (c == '/')
13236 headc = 0;
13237 else
13238 {
13239 if (headc == 0)
13240 headc = c;
13241
13242#ifdef FEAT_MBYTE
13243 /* Characters above 255 don't fit in sl_map_array[], put them in
13244 * the hash table. Each entry is the char, a NUL the headchar and
13245 * a NUL. */
13246 if (c >= 256)
13247 {
13248 int cl = mb_char2len(c);
13249 int headcl = mb_char2len(headc);
13250 char_u *b;
13251 hash_T hash;
13252 hashitem_T *hi;
13253
13254 b = alloc((unsigned)(cl + headcl + 2));
13255 if (b == NULL)
13256 return;
13257 mb_char2bytes(c, b);
13258 b[cl] = NUL;
13259 mb_char2bytes(headc, b + cl + 1);
13260 b[cl + 1 + headcl] = NUL;
13261 hash = hash_hash(b);
13262 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13263 if (HASHITEM_EMPTY(hi))
13264 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13265 else
13266 {
13267 /* This should have been checked when generating the .spl
13268 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013269 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013270 vim_free(b);
13271 }
13272 }
13273 else
13274#endif
13275 lp->sl_map_array[c] = headc;
13276 }
13277 }
13278}
13279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280/*
13281 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13282 * lines in the .aff file.
13283 */
13284 static int
13285similar_chars(slang, c1, c2)
13286 slang_T *slang;
13287 int c1;
13288 int c2;
13289{
Bram Moolenaarea424162005-06-16 21:51:00 +000013290 int m1, m2;
13291#ifdef FEAT_MBYTE
13292 char_u buf[MB_MAXBYTES];
13293 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294
Bram Moolenaarea424162005-06-16 21:51:00 +000013295 if (c1 >= 256)
13296 {
13297 buf[mb_char2bytes(c1, buf)] = 0;
13298 hi = hash_find(&slang->sl_map_hash, buf);
13299 if (HASHITEM_EMPTY(hi))
13300 m1 = 0;
13301 else
13302 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13303 }
13304 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013305#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013306 m1 = slang->sl_map_array[c1];
13307 if (m1 == 0)
13308 return FALSE;
13309
13310
13311#ifdef FEAT_MBYTE
13312 if (c2 >= 256)
13313 {
13314 buf[mb_char2bytes(c2, buf)] = 0;
13315 hi = hash_find(&slang->sl_map_hash, buf);
13316 if (HASHITEM_EMPTY(hi))
13317 m2 = 0;
13318 else
13319 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13320 }
13321 else
13322#endif
13323 m2 = slang->sl_map_array[c2];
13324
13325 return m1 == m2;
13326}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327
13328/*
13329 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013330 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013331 */
13332 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013333add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13334 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013335 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013336 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013337 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013338 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013339 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013340 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013341 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013342 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013343 int maxsf; /* su_maxscore applies to soundfold score,
13344 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013345{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013346 int goodlen; /* len of goodword changed */
13347 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013349 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013350 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013351 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013353 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13354 * "thee the" is added next to changing the first "the" the "thee". */
13355 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013356 pbad = su->su_badptr + badlenarg;
13357 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013358 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013359 goodlen = pgood - goodword;
13360 badlen = pbad - su->su_badptr;
13361 if (goodlen <= 0 || badlen <= 0)
13362 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013363 mb_ptr_back(goodword, pgood);
13364 mb_ptr_back(su->su_badptr, pbad);
13365#ifdef FEAT_MBYTE
13366 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013367 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013368 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13369 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013370 }
13371 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013372#endif
13373 if (*pgood != *pbad)
13374 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013375 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013376
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013377 if (badlen == 0 && goodlen == 0)
13378 /* goodword doesn't change anything; may happen for "the the" changing
13379 * the first "the" to itself. */
13380 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013381
Bram Moolenaar4770d092006-01-12 23:22:24 +000013382 /* Check if the word is already there. Also check the length that is
13383 * being replaced "thes," -> "these" is a different suggestion from
13384 * "thes" -> "these". */
13385 stp = &SUG(*gap, 0);
13386 for (i = gap->ga_len; --i >= 0; ++stp)
13387 if (stp->st_wordlen == goodlen
13388 && stp->st_orglen == badlen
13389 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13390 {
13391 /*
13392 * Found it. Remember the word with the lowest score.
13393 */
13394 if (stp->st_slang == NULL)
13395 stp->st_slang = slang;
13396
13397 new_sug.st_score = score;
13398 new_sug.st_altscore = altscore;
13399 new_sug.st_had_bonus = had_bonus;
13400
13401 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013403 /* Only one of the two had the soundalike score computed.
13404 * Need to do that for the other one now, otherwise the
13405 * scores can't be compared. This happens because
13406 * suggest_try_change() doesn't compute the soundalike
13407 * word to keep it fast, while some special methods set
13408 * the soundalike score to zero. */
13409 if (had_bonus)
13410 rescore_one(su, stp);
13411 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013412 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013413 new_sug.st_word = stp->st_word;
13414 new_sug.st_wordlen = stp->st_wordlen;
13415 new_sug.st_slang = stp->st_slang;
13416 new_sug.st_orglen = badlen;
13417 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013419 }
13420
Bram Moolenaar4770d092006-01-12 23:22:24 +000013421 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013423 stp->st_score = new_sug.st_score;
13424 stp->st_altscore = new_sug.st_altscore;
13425 stp->st_had_bonus = new_sug.st_had_bonus;
13426 }
13427 break;
13428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013429
Bram Moolenaar4770d092006-01-12 23:22:24 +000013430 if (i < 0 && ga_grow(gap, 1) == OK)
13431 {
13432 /* Add a suggestion. */
13433 stp = &SUG(*gap, gap->ga_len);
13434 stp->st_word = vim_strnsave(goodword, goodlen);
13435 if (stp->st_word != NULL)
13436 {
13437 stp->st_wordlen = goodlen;
13438 stp->st_score = score;
13439 stp->st_altscore = altscore;
13440 stp->st_had_bonus = had_bonus;
13441 stp->st_orglen = badlen;
13442 stp->st_slang = slang;
13443 ++gap->ga_len;
13444
13445 /* If we have too many suggestions now, sort the list and keep
13446 * the best suggestions. */
13447 if (gap->ga_len > SUG_MAX_COUNT(su))
13448 {
13449 if (maxsf)
13450 su->su_sfmaxscore = cleanup_suggestions(gap,
13451 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13452 else
13453 {
13454 i = su->su_maxscore;
13455 su->su_maxscore = cleanup_suggestions(gap,
13456 su->su_maxscore, SUG_CLEAN_COUNT(su));
13457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458 }
13459 }
13460 }
13461}
13462
13463/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013464 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13465 * for split words, such as "the the". Remove these from the list here.
13466 */
13467 static void
13468check_suggestions(su, gap)
13469 suginfo_T *su;
13470 garray_T *gap; /* either su_ga or su_sga */
13471{
13472 suggest_T *stp;
13473 int i;
13474 char_u longword[MAXWLEN + 1];
13475 int len;
13476 hlf_T attr;
13477
13478 stp = &SUG(*gap, 0);
13479 for (i = gap->ga_len - 1; i >= 0; --i)
13480 {
13481 /* Need to append what follows to check for "the the". */
13482 STRCPY(longword, stp[i].st_word);
13483 len = stp[i].st_wordlen;
13484 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13485 MAXWLEN - len);
13486 attr = HLF_COUNT;
13487 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13488 if (attr != HLF_COUNT)
13489 {
13490 /* Remove this entry. */
13491 vim_free(stp[i].st_word);
13492 --gap->ga_len;
13493 if (i < gap->ga_len)
13494 mch_memmove(stp + i, stp + i + 1,
13495 sizeof(suggest_T) * (gap->ga_len - i));
13496 }
13497 }
13498}
13499
13500
13501/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 * Add a word to be banned.
13503 */
13504 static void
13505add_banned(su, word)
13506 suginfo_T *su;
13507 char_u *word;
13508{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013509 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 hash_T hash;
13511 hashitem_T *hi;
13512
Bram Moolenaar4770d092006-01-12 23:22:24 +000013513 hash = hash_hash(word);
13514 hi = hash_lookup(&su->su_banned, word, hash);
13515 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013516 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013517 s = vim_strsave(word);
13518 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 hash_add_item(&su->su_banned, hi, s, hash);
13520 }
13521}
13522
13523/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013524 * Recompute the score for all suggestions if sound-folding is possible. This
13525 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013526 */
13527 static void
13528rescore_suggestions(su)
13529 suginfo_T *su;
13530{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013531 int i;
13532
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013533 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013534 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013535 rescore_one(su, &SUG(su->su_ga, i));
13536}
13537
13538/*
13539 * Recompute the score for one suggestion if sound-folding is possible.
13540 */
13541 static void
13542rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013543 suginfo_T *su;
13544 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013545{
13546 slang_T *slang = stp->st_slang;
13547 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013548 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013549
13550 /* Only rescore suggestions that have no sal score yet and do have a
13551 * language. */
13552 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13553 {
13554 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013555 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013556 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013557 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013558 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013559 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013560 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013561
13562 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013563 if (stp->st_altscore == SCORE_MAXMAX)
13564 stp->st_altscore = SCORE_BIG;
13565 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13566 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013567 }
13568}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570static int
13571#ifdef __BORLANDC__
13572_RTLENTRYF
13573#endif
13574sug_compare __ARGS((const void *s1, const void *s2));
13575
13576/*
13577 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013578 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 */
13580 static int
13581#ifdef __BORLANDC__
13582_RTLENTRYF
13583#endif
13584sug_compare(s1, s2)
13585 const void *s1;
13586 const void *s2;
13587{
13588 suggest_T *p1 = (suggest_T *)s1;
13589 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013590 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013592 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013593 {
13594 n = p1->st_altscore - p2->st_altscore;
13595 if (n == 0)
13596 n = STRICMP(p1->st_word, p2->st_word);
13597 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013598 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013599}
13600
13601/*
13602 * Cleanup the suggestions:
13603 * - Sort on score.
13604 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013605 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013607 static int
13608cleanup_suggestions(gap, maxscore, keep)
13609 garray_T *gap;
13610 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013611 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013613 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 int i;
13615
13616 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013617 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618
13619 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013620 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013622 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013624 gap->ga_len = keep;
13625 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013627 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628}
13629
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013630#if defined(FEAT_EVAL) || defined(PROTO)
13631/*
13632 * Soundfold a string, for soundfold().
13633 * Result is in allocated memory, NULL for an error.
13634 */
13635 char_u *
13636eval_soundfold(word)
13637 char_u *word;
13638{
13639 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013640 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013641 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013642
13643 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13644 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013645 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013646 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013647 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013648 if (lp->lp_slang->sl_sal.ga_len > 0)
13649 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013650 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013651 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013652 return vim_strsave(sound);
13653 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013654 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013655
13656 /* No language with sound folding, return word as-is. */
13657 return vim_strsave(word);
13658}
13659#endif
13660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661/*
13662 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013663 *
13664 * There are many ways to turn a word into a sound-a-like representation. The
13665 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13666 * swedish name matching - survey and test of different algorithms" by Klas
13667 * Erikson.
13668 *
13669 * We support two methods:
13670 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13671 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 */
13673 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013674spell_soundfold(slang, inword, folded, res)
13675 slang_T *slang;
13676 char_u *inword;
13677 int folded; /* "inword" is already case-folded */
13678 char_u *res;
13679{
13680 char_u fword[MAXWLEN];
13681 char_u *word;
13682
13683 if (slang->sl_sofo)
13684 /* SOFOFROM and SOFOTO used */
13685 spell_soundfold_sofo(slang, inword, res);
13686 else
13687 {
13688 /* SAL items used. Requires the word to be case-folded. */
13689 if (folded)
13690 word = inword;
13691 else
13692 {
13693 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13694 word = fword;
13695 }
13696
13697#ifdef FEAT_MBYTE
13698 if (has_mbyte)
13699 spell_soundfold_wsal(slang, word, res);
13700 else
13701#endif
13702 spell_soundfold_sal(slang, word, res);
13703 }
13704}
13705
13706/*
13707 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13708 * SOFOTO lines.
13709 */
13710 static void
13711spell_soundfold_sofo(slang, inword, res)
13712 slang_T *slang;
13713 char_u *inword;
13714 char_u *res;
13715{
13716 char_u *s;
13717 int ri = 0;
13718 int c;
13719
13720#ifdef FEAT_MBYTE
13721 if (has_mbyte)
13722 {
13723 int prevc = 0;
13724 int *ip;
13725
13726 /* The sl_sal_first[] table contains the translation for chars up to
13727 * 255, sl_sal the rest. */
13728 for (s = inword; *s != NUL; )
13729 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013730 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013731 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13732 c = ' ';
13733 else if (c < 256)
13734 c = slang->sl_sal_first[c];
13735 else
13736 {
13737 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13738 if (ip == NULL) /* empty list, can't match */
13739 c = NUL;
13740 else
13741 for (;;) /* find "c" in the list */
13742 {
13743 if (*ip == 0) /* not found */
13744 {
13745 c = NUL;
13746 break;
13747 }
13748 if (*ip == c) /* match! */
13749 {
13750 c = ip[1];
13751 break;
13752 }
13753 ip += 2;
13754 }
13755 }
13756
13757 if (c != NUL && c != prevc)
13758 {
13759 ri += mb_char2bytes(c, res + ri);
13760 if (ri + MB_MAXBYTES > MAXWLEN)
13761 break;
13762 prevc = c;
13763 }
13764 }
13765 }
13766 else
13767#endif
13768 {
13769 /* The sl_sal_first[] table contains the translation. */
13770 for (s = inword; (c = *s) != NUL; ++s)
13771 {
13772 if (vim_iswhite(c))
13773 c = ' ';
13774 else
13775 c = slang->sl_sal_first[c];
13776 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13777 res[ri++] = c;
13778 }
13779 }
13780
13781 res[ri] = NUL;
13782}
13783
13784 static void
13785spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 slang_T *slang;
13787 char_u *inword;
13788 char_u *res;
13789{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013790 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013791 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013792 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013794 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013796 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 int n, k = 0;
13798 int z0;
13799 int k0;
13800 int n0;
13801 int c;
13802 int pri;
13803 int p0 = -333;
13804 int c0;
13805
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013806 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013807 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013808 if (slang->sl_rem_accents)
13809 {
13810 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013811 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013812 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013813 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013814 {
13815 *t++ = ' ';
13816 s = skipwhite(s);
13817 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013818 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013820 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 *t++ = *s;
13822 ++s;
13823 }
13824 }
13825 *t = NUL;
13826 }
13827 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013828 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013830 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831
13832 /*
13833 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013834 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013836 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 while ((c = word[i]) != NUL)
13838 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013839 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 n = slang->sl_sal_first[c];
13841 z0 = 0;
13842
13843 if (n >= 0)
13844 {
13845 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013846 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013848 /* Quickly skip entries that don't match the word. Most
13849 * entries are less then three chars, optimize for that. */
13850 k = smp[n].sm_leadlen;
13851 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013852 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013853 if (word[i + 1] != s[1])
13854 continue;
13855 if (k > 2)
13856 {
13857 for (j = 2; j < k; ++j)
13858 if (word[i + j] != s[j])
13859 break;
13860 if (j < k)
13861 continue;
13862 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 }
13864
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013865 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013866 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013867 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013868 while (*pf != NUL && *pf != word[i + k])
13869 ++pf;
13870 if (*pf == NUL)
13871 continue;
13872 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013874 s = smp[n].sm_rules;
13875 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876
13877 p0 = *s;
13878 k0 = k;
13879 while (*s == '-' && k > 1)
13880 {
13881 k--;
13882 s++;
13883 }
13884 if (*s == '<')
13885 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013886 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 {
13888 /* determine priority */
13889 pri = *s - '0';
13890 s++;
13891 }
13892 if (*s == '^' && *(s + 1) == '^')
13893 s++;
13894
13895 if (*s == NUL
13896 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013897 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013898 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013900 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013901 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013902 && spell_iswordp(word + i - 1, curbuf)
13903 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 {
13905 /* search for followup rules, if: */
13906 /* followup and k > 1 and NO '-' in searchstring */
13907 c0 = word[i + k - 1];
13908 n0 = slang->sl_sal_first[c0];
13909
13910 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013911 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 {
13913 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013914 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013916 /* Quickly skip entries that don't match the word.
13917 * */
13918 k0 = smp[n0].sm_leadlen;
13919 if (k0 > 1)
13920 {
13921 if (word[i + k] != s[1])
13922 continue;
13923 if (k0 > 2)
13924 {
13925 pf = word + i + k + 1;
13926 for (j = 2; j < k0; ++j)
13927 if (*pf++ != s[j])
13928 break;
13929 if (j < k0)
13930 continue;
13931 }
13932 }
13933 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013934
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013935 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013936 {
13937 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013938 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013939 while (*pf != NUL && *pf != word[i + k0])
13940 ++pf;
13941 if (*pf == NUL)
13942 continue;
13943 ++k0;
13944 }
13945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013947 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 while (*s == '-')
13949 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013950 /* "k0" gets NOT reduced because
13951 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 s++;
13953 }
13954 if (*s == '<')
13955 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013956 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013957 {
13958 p0 = *s - '0';
13959 s++;
13960 }
13961
13962 if (*s == NUL
13963 /* *s == '^' cuts */
13964 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013965 && !spell_iswordp(word + i + k0,
13966 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013967 {
13968 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013969 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013971
13972 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 /* rule fits; stop search */
13976 break;
13977 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 }
13979
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013980 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 }
13983
13984 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013985 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013986 if (s == NULL)
13987 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013988 pf = smp[n].sm_rules;
13989 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 if (p0 == 1 && z == 0)
13991 {
13992 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013993 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
13994 || res[reslen - 1] == *s))
13995 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013996 z0 = 1;
13997 z = 1;
13998 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013999 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000 {
14001 word[i + k0] = *s;
14002 k0++;
14003 s++;
14004 }
14005 if (k > k0)
14006 mch_memmove(word + i + k0, word + i + k,
14007 STRLEN(word + i + k) + 1);
14008
14009 /* new "actual letter" */
14010 c = word[i];
14011 }
14012 else
14013 {
14014 /* no '<' rule used */
14015 i += k - 1;
14016 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014017 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014019 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014020 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 s++;
14022 }
14023 /* new "actual letter" */
14024 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014025 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 {
14027 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014028 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 mch_memmove(word, word + i + 1,
14030 STRLEN(word + i + 1) + 1);
14031 i = 0;
14032 z0 = 1;
14033 }
14034 }
14035 break;
14036 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 }
14038 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014039 else if (vim_iswhite(c))
14040 {
14041 c = ' ';
14042 k = 1;
14043 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044
14045 if (z0 == 0)
14046 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014047 if (k && !p0 && reslen < MAXWLEN && c != NUL
14048 && (!slang->sl_collapse || reslen == 0
14049 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014050 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014051 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052
14053 i++;
14054 z = 0;
14055 k = 0;
14056 }
14057 }
14058
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014059 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060}
14061
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014062#ifdef FEAT_MBYTE
14063/*
14064 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14065 * Multi-byte version of spell_soundfold().
14066 */
14067 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014068spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014069 slang_T *slang;
14070 char_u *inword;
14071 char_u *res;
14072{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014073 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014074 int word[MAXWLEN];
14075 int wres[MAXWLEN];
14076 int l;
14077 char_u *s;
14078 int *ws;
14079 char_u *t;
14080 int *pf;
14081 int i, j, z;
14082 int reslen;
14083 int n, k = 0;
14084 int z0;
14085 int k0;
14086 int n0;
14087 int c;
14088 int pri;
14089 int p0 = -333;
14090 int c0;
14091 int did_white = FALSE;
14092
14093 /*
14094 * Convert the multi-byte string to a wide-character string.
14095 * Remove accents, if wanted. We actually remove all non-word characters.
14096 * But keep white space.
14097 */
14098 n = 0;
14099 for (s = inword; *s != NUL; )
14100 {
14101 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014102 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014103 if (slang->sl_rem_accents)
14104 {
14105 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14106 {
14107 if (did_white)
14108 continue;
14109 c = ' ';
14110 did_white = TRUE;
14111 }
14112 else
14113 {
14114 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014115 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014116 continue;
14117 }
14118 }
14119 word[n++] = c;
14120 }
14121 word[n] = NUL;
14122
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014123 /*
14124 * This comes from Aspell phonet.cpp.
14125 * Converted from C++ to C. Added support for multi-byte chars.
14126 * Changed to keep spaces.
14127 */
14128 i = reslen = z = 0;
14129 while ((c = word[i]) != NUL)
14130 {
14131 /* Start with the first rule that has the character in the word. */
14132 n = slang->sl_sal_first[c & 0xff];
14133 z0 = 0;
14134
14135 if (n >= 0)
14136 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014137 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014138 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14139 {
14140 /* Quickly skip entries that don't match the word. Most
14141 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014142 if (c != ws[0])
14143 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014144 k = smp[n].sm_leadlen;
14145 if (k > 1)
14146 {
14147 if (word[i + 1] != ws[1])
14148 continue;
14149 if (k > 2)
14150 {
14151 for (j = 2; j < k; ++j)
14152 if (word[i + j] != ws[j])
14153 break;
14154 if (j < k)
14155 continue;
14156 }
14157 }
14158
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014159 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014160 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014161 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014162 while (*pf != NUL && *pf != word[i + k])
14163 ++pf;
14164 if (*pf == NUL)
14165 continue;
14166 ++k;
14167 }
14168 s = smp[n].sm_rules;
14169 pri = 5; /* default priority */
14170
14171 p0 = *s;
14172 k0 = k;
14173 while (*s == '-' && k > 1)
14174 {
14175 k--;
14176 s++;
14177 }
14178 if (*s == '<')
14179 s++;
14180 if (VIM_ISDIGIT(*s))
14181 {
14182 /* determine priority */
14183 pri = *s - '0';
14184 s++;
14185 }
14186 if (*s == '^' && *(s + 1) == '^')
14187 s++;
14188
14189 if (*s == NUL
14190 || (*s == '^'
14191 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014192 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014193 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014194 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014195 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014196 && spell_iswordp_w(word + i - 1, curbuf)
14197 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014198 {
14199 /* search for followup rules, if: */
14200 /* followup and k > 1 and NO '-' in searchstring */
14201 c0 = word[i + k - 1];
14202 n0 = slang->sl_sal_first[c0 & 0xff];
14203
14204 if (slang->sl_followup && k > 1 && n0 >= 0
14205 && p0 != '-' && word[i + k] != NUL)
14206 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014207 /* Test follow-up rule for "word[i + k]"; loop over
14208 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014209 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14210 == (c0 & 0xff); ++n0)
14211 {
14212 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014213 */
14214 if (c0 != ws[0])
14215 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014216 k0 = smp[n0].sm_leadlen;
14217 if (k0 > 1)
14218 {
14219 if (word[i + k] != ws[1])
14220 continue;
14221 if (k0 > 2)
14222 {
14223 pf = word + i + k + 1;
14224 for (j = 2; j < k0; ++j)
14225 if (*pf++ != ws[j])
14226 break;
14227 if (j < k0)
14228 continue;
14229 }
14230 }
14231 k0 += k - 1;
14232
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014233 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014234 {
14235 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014236 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014237 while (*pf != NUL && *pf != word[i + k0])
14238 ++pf;
14239 if (*pf == NUL)
14240 continue;
14241 ++k0;
14242 }
14243
14244 p0 = 5;
14245 s = smp[n0].sm_rules;
14246 while (*s == '-')
14247 {
14248 /* "k0" gets NOT reduced because
14249 * "if (k0 == k)" */
14250 s++;
14251 }
14252 if (*s == '<')
14253 s++;
14254 if (VIM_ISDIGIT(*s))
14255 {
14256 p0 = *s - '0';
14257 s++;
14258 }
14259
14260 if (*s == NUL
14261 /* *s == '^' cuts */
14262 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014263 && !spell_iswordp_w(word + i + k0,
14264 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014265 {
14266 if (k0 == k)
14267 /* this is just a piece of the string */
14268 continue;
14269
14270 if (p0 < pri)
14271 /* priority too low */
14272 continue;
14273 /* rule fits; stop search */
14274 break;
14275 }
14276 }
14277
14278 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14279 == (c0 & 0xff))
14280 continue;
14281 }
14282
14283 /* replace string */
14284 ws = smp[n].sm_to_w;
14285 s = smp[n].sm_rules;
14286 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14287 if (p0 == 1 && z == 0)
14288 {
14289 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014290 if (reslen > 0 && ws != NULL && *ws != NUL
14291 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014292 || wres[reslen - 1] == *ws))
14293 reslen--;
14294 z0 = 1;
14295 z = 1;
14296 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014297 if (ws != NULL)
14298 while (*ws != NUL && word[i + k0] != NUL)
14299 {
14300 word[i + k0] = *ws;
14301 k0++;
14302 ws++;
14303 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014304 if (k > k0)
14305 mch_memmove(word + i + k0, word + i + k,
14306 sizeof(int) * (STRLEN(word + i + k) + 1));
14307
14308 /* new "actual letter" */
14309 c = word[i];
14310 }
14311 else
14312 {
14313 /* no '<' rule used */
14314 i += k - 1;
14315 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014316 if (ws != NULL)
14317 while (*ws != NUL && ws[1] != NUL
14318 && reslen < MAXWLEN)
14319 {
14320 if (reslen == 0 || wres[reslen - 1] != *ws)
14321 wres[reslen++] = *ws;
14322 ws++;
14323 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014324 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014325 if (ws == NULL)
14326 c = NUL;
14327 else
14328 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014329 if (strstr((char *)s, "^^") != NULL)
14330 {
14331 if (c != NUL)
14332 wres[reslen++] = c;
14333 mch_memmove(word, word + i + 1,
14334 sizeof(int) * (STRLEN(word + i + 1) + 1));
14335 i = 0;
14336 z0 = 1;
14337 }
14338 }
14339 break;
14340 }
14341 }
14342 }
14343 else if (vim_iswhite(c))
14344 {
14345 c = ' ';
14346 k = 1;
14347 }
14348
14349 if (z0 == 0)
14350 {
14351 if (k && !p0 && reslen < MAXWLEN && c != NUL
14352 && (!slang->sl_collapse || reslen == 0
14353 || wres[reslen - 1] != c))
14354 /* condense only double letters */
14355 wres[reslen++] = c;
14356
14357 i++;
14358 z = 0;
14359 k = 0;
14360 }
14361 }
14362
14363 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14364 l = 0;
14365 for (n = 0; n < reslen; ++n)
14366 {
14367 l += mb_char2bytes(wres[n], res + l);
14368 if (l + MB_MAXBYTES > MAXWLEN)
14369 break;
14370 }
14371 res[l] = NUL;
14372}
14373#endif
14374
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014375/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376 * Compute a score for two sound-a-like words.
14377 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14378 * Instead of a generic loop we write out the code. That keeps it fast by
14379 * avoiding checks that will not be possible.
14380 */
14381 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014382soundalike_score(goodstart, badstart)
14383 char_u *goodstart; /* sound-folded good word */
14384 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014385{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014386 char_u *goodsound = goodstart;
14387 char_u *badsound = badstart;
14388 int goodlen;
14389 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014390 int n;
14391 char_u *pl, *ps;
14392 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014393 int score = 0;
14394
14395 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14396 * counted so much, vowels halfway the word aren't counted at all. */
14397 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14398 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014399 if (badsound[1] == goodsound[1]
14400 || (badsound[1] != NUL
14401 && goodsound[1] != NUL
14402 && badsound[2] == goodsound[2]))
14403 {
14404 /* handle like a substitute */
14405 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014406 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014407 {
14408 score = 2 * SCORE_DEL / 3;
14409 if (*badsound == '*')
14410 ++badsound;
14411 else
14412 ++goodsound;
14413 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014414 }
14415
14416 goodlen = STRLEN(goodsound);
14417 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014418
14419 /* Return quickly if the lenghts are too different to be fixed by two
14420 * changes. */
14421 n = goodlen - badlen;
14422 if (n < -2 || n > 2)
14423 return SCORE_MAXMAX;
14424
14425 if (n > 0)
14426 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014427 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014428 ps = badsound;
14429 }
14430 else
14431 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014432 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014433 ps = goodsound;
14434 }
14435
14436 /* Skip over the identical part. */
14437 while (*pl == *ps && *pl != NUL)
14438 {
14439 ++pl;
14440 ++ps;
14441 }
14442
14443 switch (n)
14444 {
14445 case -2:
14446 case 2:
14447 /*
14448 * Must delete two characters from "pl".
14449 */
14450 ++pl; /* first delete */
14451 while (*pl == *ps)
14452 {
14453 ++pl;
14454 ++ps;
14455 }
14456 /* strings must be equal after second delete */
14457 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014458 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014459
14460 /* Failed to compare. */
14461 break;
14462
14463 case -1:
14464 case 1:
14465 /*
14466 * Minimal one delete from "pl" required.
14467 */
14468
14469 /* 1: delete */
14470 pl2 = pl + 1;
14471 ps2 = ps;
14472 while (*pl2 == *ps2)
14473 {
14474 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014475 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014476 ++pl2;
14477 ++ps2;
14478 }
14479
14480 /* 2: delete then swap, then rest must be equal */
14481 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14482 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014483 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014484
14485 /* 3: delete then substitute, then the rest must be equal */
14486 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014487 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014488
14489 /* 4: first swap then delete */
14490 if (pl[0] == ps[1] && pl[1] == ps[0])
14491 {
14492 pl2 = pl + 2; /* swap, skip two chars */
14493 ps2 = ps + 2;
14494 while (*pl2 == *ps2)
14495 {
14496 ++pl2;
14497 ++ps2;
14498 }
14499 /* delete a char and then strings must be equal */
14500 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014501 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014502 }
14503
14504 /* 5: first substitute then delete */
14505 pl2 = pl + 1; /* substitute, skip one char */
14506 ps2 = ps + 1;
14507 while (*pl2 == *ps2)
14508 {
14509 ++pl2;
14510 ++ps2;
14511 }
14512 /* delete a char and then strings must be equal */
14513 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014514 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014515
14516 /* Failed to compare. */
14517 break;
14518
14519 case 0:
14520 /*
14521 * Lenghts are equal, thus changes must result in same length: An
14522 * insert is only possible in combination with a delete.
14523 * 1: check if for identical strings
14524 */
14525 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014526 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014527
14528 /* 2: swap */
14529 if (pl[0] == ps[1] && pl[1] == ps[0])
14530 {
14531 pl2 = pl + 2; /* swap, skip two chars */
14532 ps2 = ps + 2;
14533 while (*pl2 == *ps2)
14534 {
14535 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014536 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014537 ++pl2;
14538 ++ps2;
14539 }
14540 /* 3: swap and swap again */
14541 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14542 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014543 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014544
14545 /* 4: swap and substitute */
14546 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014547 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014548 }
14549
14550 /* 5: substitute */
14551 pl2 = pl + 1;
14552 ps2 = ps + 1;
14553 while (*pl2 == *ps2)
14554 {
14555 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014556 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014557 ++pl2;
14558 ++ps2;
14559 }
14560
14561 /* 6: substitute and swap */
14562 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14563 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014564 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014565
14566 /* 7: substitute and substitute */
14567 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014568 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014569
14570 /* 8: insert then delete */
14571 pl2 = pl;
14572 ps2 = ps + 1;
14573 while (*pl2 == *ps2)
14574 {
14575 ++pl2;
14576 ++ps2;
14577 }
14578 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014579 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014580
14581 /* 9: delete then insert */
14582 pl2 = pl + 1;
14583 ps2 = ps;
14584 while (*pl2 == *ps2)
14585 {
14586 ++pl2;
14587 ++ps2;
14588 }
14589 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014590 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014591
14592 /* Failed to compare. */
14593 break;
14594 }
14595
14596 return SCORE_MAXMAX;
14597}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599/*
14600 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014601 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014602 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014603 * The algorithm is described by Du and Chang, 1992.
14604 * The implementation of the algorithm comes from Aspell editdist.cpp,
14605 * edit_distance(). It has been converted from C++ to C and modified to
14606 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607 */
14608 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014609spell_edit_score(slang, badword, goodword)
14610 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 char_u *badword;
14612 char_u *goodword;
14613{
14614 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014615 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 int j, i;
14617 int t;
14618 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014619 int pbc, pgc;
14620#ifdef FEAT_MBYTE
14621 char_u *p;
14622 int wbadword[MAXWLEN];
14623 int wgoodword[MAXWLEN];
14624
14625 if (has_mbyte)
14626 {
14627 /* Get the characters from the multi-byte strings and put them in an
14628 * int array for easy access. */
14629 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014630 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014631 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014632 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014633 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014634 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014635 }
14636 else
14637#endif
14638 {
14639 badlen = STRLEN(badword) + 1;
14640 goodlen = STRLEN(goodword) + 1;
14641 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642
14643 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14644#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014645 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14646 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014647 if (cnt == NULL)
14648 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649
14650 CNT(0, 0) = 0;
14651 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014652 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653
14654 for (i = 1; i <= badlen; ++i)
14655 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014656 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 for (j = 1; j <= goodlen; ++j)
14658 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014659#ifdef FEAT_MBYTE
14660 if (has_mbyte)
14661 {
14662 bc = wbadword[i - 1];
14663 gc = wgoodword[j - 1];
14664 }
14665 else
14666#endif
14667 {
14668 bc = badword[i - 1];
14669 gc = goodword[j - 1];
14670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 if (bc == gc)
14672 CNT(i, j) = CNT(i - 1, j - 1);
14673 else
14674 {
14675 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014676 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14678 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014679 {
14680 /* For a similar character use SCORE_SIMILAR. */
14681 if (slang != NULL
14682 && slang->sl_has_map
14683 && similar_chars(slang, gc, bc))
14684 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14685 else
14686 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014689 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014691#ifdef FEAT_MBYTE
14692 if (has_mbyte)
14693 {
14694 pbc = wbadword[i - 2];
14695 pgc = wgoodword[j - 2];
14696 }
14697 else
14698#endif
14699 {
14700 pbc = badword[i - 2];
14701 pgc = goodword[j - 2];
14702 }
14703 if (bc == pgc && pbc == gc)
14704 {
14705 t = SCORE_SWAP + CNT(i - 2, j - 2);
14706 if (t < CNT(i, j))
14707 CNT(i, j) = t;
14708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014709 }
14710 t = SCORE_DEL + CNT(i - 1, j);
14711 if (t < CNT(i, j))
14712 CNT(i, j) = t;
14713 t = SCORE_INS + CNT(i, j - 1);
14714 if (t < CNT(i, j))
14715 CNT(i, j) = t;
14716 }
14717 }
14718 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014719
14720 i = CNT(badlen - 1, goodlen - 1);
14721 vim_free(cnt);
14722 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014723}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014724
Bram Moolenaar4770d092006-01-12 23:22:24 +000014725typedef struct
14726{
14727 int badi;
14728 int goodi;
14729 int score;
14730} limitscore_T;
14731
14732/*
14733 * Like spell_edit_score(), but with a limit on the score to make it faster.
14734 * May return SCORE_MAXMAX when the score is higher than "limit".
14735 *
14736 * This uses a stack for the edits still to be tried.
14737 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14738 * for multi-byte characters.
14739 */
14740 static int
14741spell_edit_score_limit(slang, badword, goodword, limit)
14742 slang_T *slang;
14743 char_u *badword;
14744 char_u *goodword;
14745 int limit;
14746{
14747 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14748 int stackidx;
14749 int bi, gi;
14750 int bi2, gi2;
14751 int bc, gc;
14752 int score;
14753 int score_off;
14754 int minscore;
14755 int round;
14756
14757#ifdef FEAT_MBYTE
14758 /* Multi-byte characters require a bit more work, use a different function
14759 * to avoid testing "has_mbyte" quite often. */
14760 if (has_mbyte)
14761 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14762#endif
14763
14764 /*
14765 * The idea is to go from start to end over the words. So long as
14766 * characters are equal just continue, this always gives the lowest score.
14767 * When there is a difference try several alternatives. Each alternative
14768 * increases "score" for the edit distance. Some of the alternatives are
14769 * pushed unto a stack and tried later, some are tried right away. At the
14770 * end of the word the score for one alternative is known. The lowest
14771 * possible score is stored in "minscore".
14772 */
14773 stackidx = 0;
14774 bi = 0;
14775 gi = 0;
14776 score = 0;
14777 minscore = limit + 1;
14778
14779 for (;;)
14780 {
14781 /* Skip over an equal part, score remains the same. */
14782 for (;;)
14783 {
14784 bc = badword[bi];
14785 gc = goodword[gi];
14786 if (bc != gc) /* stop at a char that's different */
14787 break;
14788 if (bc == NUL) /* both words end */
14789 {
14790 if (score < minscore)
14791 minscore = score;
14792 goto pop; /* do next alternative */
14793 }
14794 ++bi;
14795 ++gi;
14796 }
14797
14798 if (gc == NUL) /* goodword ends, delete badword chars */
14799 {
14800 do
14801 {
14802 if ((score += SCORE_DEL) >= minscore)
14803 goto pop; /* do next alternative */
14804 } while (badword[++bi] != NUL);
14805 minscore = score;
14806 }
14807 else if (bc == NUL) /* badword ends, insert badword chars */
14808 {
14809 do
14810 {
14811 if ((score += SCORE_INS) >= minscore)
14812 goto pop; /* do next alternative */
14813 } while (goodword[++gi] != NUL);
14814 minscore = score;
14815 }
14816 else /* both words continue */
14817 {
14818 /* If not close to the limit, perform a change. Only try changes
14819 * that may lead to a lower score than "minscore".
14820 * round 0: try deleting a char from badword
14821 * round 1: try inserting a char in badword */
14822 for (round = 0; round <= 1; ++round)
14823 {
14824 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14825 if (score_off < minscore)
14826 {
14827 if (score_off + SCORE_EDIT_MIN >= minscore)
14828 {
14829 /* Near the limit, rest of the words must match. We
14830 * can check that right now, no need to push an item
14831 * onto the stack. */
14832 bi2 = bi + 1 - round;
14833 gi2 = gi + round;
14834 while (goodword[gi2] == badword[bi2])
14835 {
14836 if (goodword[gi2] == NUL)
14837 {
14838 minscore = score_off;
14839 break;
14840 }
14841 ++bi2;
14842 ++gi2;
14843 }
14844 }
14845 else
14846 {
14847 /* try deleting/inserting a character later */
14848 stack[stackidx].badi = bi + 1 - round;
14849 stack[stackidx].goodi = gi + round;
14850 stack[stackidx].score = score_off;
14851 ++stackidx;
14852 }
14853 }
14854 }
14855
14856 if (score + SCORE_SWAP < minscore)
14857 {
14858 /* If swapping two characters makes a match then the
14859 * substitution is more expensive, thus there is no need to
14860 * try both. */
14861 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14862 {
14863 /* Swap two characters, that is: skip them. */
14864 gi += 2;
14865 bi += 2;
14866 score += SCORE_SWAP;
14867 continue;
14868 }
14869 }
14870
14871 /* Substitute one character for another which is the same
14872 * thing as deleting a character from both goodword and badword.
14873 * Use a better score when there is only a case difference. */
14874 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14875 score += SCORE_ICASE;
14876 else
14877 {
14878 /* For a similar character use SCORE_SIMILAR. */
14879 if (slang != NULL
14880 && slang->sl_has_map
14881 && similar_chars(slang, gc, bc))
14882 score += SCORE_SIMILAR;
14883 else
14884 score += SCORE_SUBST;
14885 }
14886
14887 if (score < minscore)
14888 {
14889 /* Do the substitution. */
14890 ++gi;
14891 ++bi;
14892 continue;
14893 }
14894 }
14895pop:
14896 /*
14897 * Get here to try the next alternative, pop it from the stack.
14898 */
14899 if (stackidx == 0) /* stack is empty, finished */
14900 break;
14901
14902 /* pop an item from the stack */
14903 --stackidx;
14904 gi = stack[stackidx].goodi;
14905 bi = stack[stackidx].badi;
14906 score = stack[stackidx].score;
14907 }
14908
14909 /* When the score goes over "limit" it may actually be much higher.
14910 * Return a very large number to avoid going below the limit when giving a
14911 * bonus. */
14912 if (minscore > limit)
14913 return SCORE_MAXMAX;
14914 return minscore;
14915}
14916
14917#ifdef FEAT_MBYTE
14918/*
14919 * Multi-byte version of spell_edit_score_limit().
14920 * Keep it in sync with the above!
14921 */
14922 static int
14923spell_edit_score_limit_w(slang, badword, goodword, limit)
14924 slang_T *slang;
14925 char_u *badword;
14926 char_u *goodword;
14927 int limit;
14928{
14929 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14930 int stackidx;
14931 int bi, gi;
14932 int bi2, gi2;
14933 int bc, gc;
14934 int score;
14935 int score_off;
14936 int minscore;
14937 int round;
14938 char_u *p;
14939 int wbadword[MAXWLEN];
14940 int wgoodword[MAXWLEN];
14941
14942 /* Get the characters from the multi-byte strings and put them in an
14943 * int array for easy access. */
14944 bi = 0;
14945 for (p = badword; *p != NUL; )
14946 wbadword[bi++] = mb_cptr2char_adv(&p);
14947 wbadword[bi++] = 0;
14948 gi = 0;
14949 for (p = goodword; *p != NUL; )
14950 wgoodword[gi++] = mb_cptr2char_adv(&p);
14951 wgoodword[gi++] = 0;
14952
14953 /*
14954 * The idea is to go from start to end over the words. So long as
14955 * characters are equal just continue, this always gives the lowest score.
14956 * When there is a difference try several alternatives. Each alternative
14957 * increases "score" for the edit distance. Some of the alternatives are
14958 * pushed unto a stack and tried later, some are tried right away. At the
14959 * end of the word the score for one alternative is known. The lowest
14960 * possible score is stored in "minscore".
14961 */
14962 stackidx = 0;
14963 bi = 0;
14964 gi = 0;
14965 score = 0;
14966 minscore = limit + 1;
14967
14968 for (;;)
14969 {
14970 /* Skip over an equal part, score remains the same. */
14971 for (;;)
14972 {
14973 bc = wbadword[bi];
14974 gc = wgoodword[gi];
14975
14976 if (bc != gc) /* stop at a char that's different */
14977 break;
14978 if (bc == NUL) /* both words end */
14979 {
14980 if (score < minscore)
14981 minscore = score;
14982 goto pop; /* do next alternative */
14983 }
14984 ++bi;
14985 ++gi;
14986 }
14987
14988 if (gc == NUL) /* goodword ends, delete badword chars */
14989 {
14990 do
14991 {
14992 if ((score += SCORE_DEL) >= minscore)
14993 goto pop; /* do next alternative */
14994 } while (wbadword[++bi] != NUL);
14995 minscore = score;
14996 }
14997 else if (bc == NUL) /* badword ends, insert badword chars */
14998 {
14999 do
15000 {
15001 if ((score += SCORE_INS) >= minscore)
15002 goto pop; /* do next alternative */
15003 } while (wgoodword[++gi] != NUL);
15004 minscore = score;
15005 }
15006 else /* both words continue */
15007 {
15008 /* If not close to the limit, perform a change. Only try changes
15009 * that may lead to a lower score than "minscore".
15010 * round 0: try deleting a char from badword
15011 * round 1: try inserting a char in badword */
15012 for (round = 0; round <= 1; ++round)
15013 {
15014 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15015 if (score_off < minscore)
15016 {
15017 if (score_off + SCORE_EDIT_MIN >= minscore)
15018 {
15019 /* Near the limit, rest of the words must match. We
15020 * can check that right now, no need to push an item
15021 * onto the stack. */
15022 bi2 = bi + 1 - round;
15023 gi2 = gi + round;
15024 while (wgoodword[gi2] == wbadword[bi2])
15025 {
15026 if (wgoodword[gi2] == NUL)
15027 {
15028 minscore = score_off;
15029 break;
15030 }
15031 ++bi2;
15032 ++gi2;
15033 }
15034 }
15035 else
15036 {
15037 /* try deleting a character from badword later */
15038 stack[stackidx].badi = bi + 1 - round;
15039 stack[stackidx].goodi = gi + round;
15040 stack[stackidx].score = score_off;
15041 ++stackidx;
15042 }
15043 }
15044 }
15045
15046 if (score + SCORE_SWAP < minscore)
15047 {
15048 /* If swapping two characters makes a match then the
15049 * substitution is more expensive, thus there is no need to
15050 * try both. */
15051 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15052 {
15053 /* Swap two characters, that is: skip them. */
15054 gi += 2;
15055 bi += 2;
15056 score += SCORE_SWAP;
15057 continue;
15058 }
15059 }
15060
15061 /* Substitute one character for another which is the same
15062 * thing as deleting a character from both goodword and badword.
15063 * Use a better score when there is only a case difference. */
15064 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15065 score += SCORE_ICASE;
15066 else
15067 {
15068 /* For a similar character use SCORE_SIMILAR. */
15069 if (slang != NULL
15070 && slang->sl_has_map
15071 && similar_chars(slang, gc, bc))
15072 score += SCORE_SIMILAR;
15073 else
15074 score += SCORE_SUBST;
15075 }
15076
15077 if (score < minscore)
15078 {
15079 /* Do the substitution. */
15080 ++gi;
15081 ++bi;
15082 continue;
15083 }
15084 }
15085pop:
15086 /*
15087 * Get here to try the next alternative, pop it from the stack.
15088 */
15089 if (stackidx == 0) /* stack is empty, finished */
15090 break;
15091
15092 /* pop an item from the stack */
15093 --stackidx;
15094 gi = stack[stackidx].goodi;
15095 bi = stack[stackidx].badi;
15096 score = stack[stackidx].score;
15097 }
15098
15099 /* When the score goes over "limit" it may actually be much higher.
15100 * Return a very large number to avoid going below the limit when giving a
15101 * bonus. */
15102 if (minscore > limit)
15103 return SCORE_MAXMAX;
15104 return minscore;
15105}
15106#endif
15107
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015108/*
15109 * ":spellinfo"
15110 */
15111/*ARGSUSED*/
15112 void
15113ex_spellinfo(eap)
15114 exarg_T *eap;
15115{
15116 int lpi;
15117 langp_T *lp;
15118 char_u *p;
15119
15120 if (no_spell_checking(curwin))
15121 return;
15122
15123 msg_start();
15124 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15125 {
15126 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15127 msg_puts((char_u *)"file: ");
15128 msg_puts(lp->lp_slang->sl_fname);
15129 msg_putchar('\n');
15130 p = lp->lp_slang->sl_info;
15131 if (p != NULL)
15132 {
15133 msg_puts(p);
15134 msg_putchar('\n');
15135 }
15136 }
15137 msg_end();
15138}
15139
Bram Moolenaar4770d092006-01-12 23:22:24 +000015140#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15141#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015142#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015143#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15144#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015145
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015146/*
15147 * ":spelldump"
15148 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015149 void
15150ex_spelldump(eap)
15151 exarg_T *eap;
15152{
15153 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015154
15155 if (no_spell_checking(curwin))
15156 return;
15157
15158 /* Create a new empty buffer by splitting the window. */
15159 do_cmdline_cmd((char_u *)"new");
15160 if (!bufempty() || !buf_valid(buf))
15161 return;
15162
15163 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15164
15165 /* Delete the empty line that we started with. */
15166 if (curbuf->b_ml.ml_line_count > 1)
15167 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15168
15169 redraw_later(NOT_VALID);
15170}
15171
15172/*
15173 * Go through all possible words and:
15174 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15175 * "ic" and "dir" are not used.
15176 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15177 */
15178 void
15179spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15180 buf_T *buf; /* buffer with spell checking */
15181 char_u *pat; /* leading part of the word */
15182 int ic; /* ignore case */
15183 int *dir; /* direction for adding matches */
15184 int dumpflags_arg; /* DUMPFLAG_* */
15185{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015186 langp_T *lp;
15187 slang_T *slang;
15188 idx_T arridx[MAXWLEN];
15189 int curi[MAXWLEN];
15190 char_u word[MAXWLEN];
15191 int c;
15192 char_u *byts;
15193 idx_T *idxs;
15194 linenr_T lnum = 0;
15195 int round;
15196 int depth;
15197 int n;
15198 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015199 char_u *region_names = NULL; /* region names being used */
15200 int do_region = TRUE; /* dump region names and numbers */
15201 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015202 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015203 int dumpflags = dumpflags_arg;
15204 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015205
Bram Moolenaard0131a82006-03-04 21:46:13 +000015206 /* When ignoring case or when the pattern starts with capital pass this on
15207 * to dump_word(). */
15208 if (pat != NULL)
15209 {
15210 if (ic)
15211 dumpflags |= DUMPFLAG_ICASE;
15212 else
15213 {
15214 n = captype(pat, NULL);
15215 if (n == WF_ONECAP)
15216 dumpflags |= DUMPFLAG_ONECAP;
15217 else if (n == WF_ALLCAP
15218#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015219 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015220#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015221 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015222#endif
15223 )
15224 dumpflags |= DUMPFLAG_ALLCAP;
15225 }
15226 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015227
Bram Moolenaar7887d882005-07-01 22:33:52 +000015228 /* Find out if we can support regions: All languages must support the same
15229 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015230 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015231 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015232 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015233 p = lp->lp_slang->sl_regions;
15234 if (p[0] != 0)
15235 {
15236 if (region_names == NULL) /* first language with regions */
15237 region_names = p;
15238 else if (STRCMP(region_names, p) != 0)
15239 {
15240 do_region = FALSE; /* region names are different */
15241 break;
15242 }
15243 }
15244 }
15245
15246 if (do_region && region_names != NULL)
15247 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015248 if (pat == NULL)
15249 {
15250 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15251 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15252 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015253 }
15254 else
15255 do_region = FALSE;
15256
15257 /*
15258 * Loop over all files loaded for the entries in 'spelllang'.
15259 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015260 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015261 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015262 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015263 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015264 if (slang->sl_fbyts == NULL) /* reloading failed */
15265 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015266
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015267 if (pat == NULL)
15268 {
15269 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15270 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15271 }
15272
15273 /* When matching with a pattern and there are no prefixes only use
15274 * parts of the tree that match "pat". */
15275 if (pat != NULL && slang->sl_pbyts == NULL)
15276 patlen = STRLEN(pat);
15277 else
15278 patlen = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015279
15280 /* round 1: case-folded tree
15281 * round 2: keep-case tree */
15282 for (round = 1; round <= 2; ++round)
15283 {
15284 if (round == 1)
15285 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015286 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015287 byts = slang->sl_fbyts;
15288 idxs = slang->sl_fidxs;
15289 }
15290 else
15291 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015292 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015293 byts = slang->sl_kbyts;
15294 idxs = slang->sl_kidxs;
15295 }
15296 if (byts == NULL)
15297 continue; /* array is empty */
15298
15299 depth = 0;
15300 arridx[0] = 0;
15301 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015302 while (depth >= 0 && !got_int
15303 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015304 {
15305 if (curi[depth] > byts[arridx[depth]])
15306 {
15307 /* Done all bytes at this node, go up one level. */
15308 --depth;
15309 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015310 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015311 }
15312 else
15313 {
15314 /* Do one more byte at this node. */
15315 n = arridx[depth] + curi[depth];
15316 ++curi[depth];
15317 c = byts[n];
15318 if (c == 0)
15319 {
15320 /* End of word, deal with the word.
15321 * Don't use keep-case words in the fold-case tree,
15322 * they will appear in the keep-case tree.
15323 * Only use the word when the region matches. */
15324 flags = (int)idxs[n];
15325 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015326 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015327 && (do_region
15328 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015329 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015330 & lp->lp_region) != 0))
15331 {
15332 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015333 if (!do_region)
15334 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015335
15336 /* Dump the basic word if there is no prefix or
15337 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015338 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015339 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015340 {
15341 dump_word(slang, word, pat, dir,
15342 dumpflags, flags, lnum);
15343 if (pat == NULL)
15344 ++lnum;
15345 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015346
15347 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015348 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015349 lnum = dump_prefixes(slang, word, pat, dir,
15350 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015351 }
15352 }
15353 else
15354 {
15355 /* Normal char, go one level deeper. */
15356 word[depth++] = c;
15357 arridx[depth] = idxs[n];
15358 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015359
15360 /* Check if this characters matches with the pattern.
15361 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015362 * Always ignore case here, dump_word() will check
15363 * proper case later. This isn't exactly right when
15364 * length changes for multi-byte characters with
15365 * ignore case... */
15366 if (depth <= patlen
15367 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015368 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015369 }
15370 }
15371 }
15372 }
15373 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015374}
15375
15376/*
15377 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015378 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015379 */
15380 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015381dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015382 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015383 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015384 char_u *pat;
15385 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015386 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015387 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015388 linenr_T lnum;
15389{
15390 int keepcap = FALSE;
15391 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015392 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015393 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015394 char_u badword[MAXWLEN + 10];
15395 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015396 int flags = wordflags;
15397
15398 if (dumpflags & DUMPFLAG_ONECAP)
15399 flags |= WF_ONECAP;
15400 if (dumpflags & DUMPFLAG_ALLCAP)
15401 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015402
Bram Moolenaar4770d092006-01-12 23:22:24 +000015403 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015404 {
15405 /* Need to fix case according to "flags". */
15406 make_case_word(word, cword, flags);
15407 p = cword;
15408 }
15409 else
15410 {
15411 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015412 if ((dumpflags & DUMPFLAG_KEEPCASE)
15413 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015414 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015415 keepcap = TRUE;
15416 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015417 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015418
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015419 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015420 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015421 /* Add flags and regions after a slash. */
15422 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015423 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015424 STRCPY(badword, p);
15425 STRCAT(badword, "/");
15426 if (keepcap)
15427 STRCAT(badword, "=");
15428 if (flags & WF_BANNED)
15429 STRCAT(badword, "!");
15430 else if (flags & WF_RARE)
15431 STRCAT(badword, "?");
15432 if (flags & WF_REGION)
15433 for (i = 0; i < 7; ++i)
15434 if (flags & (0x10000 << i))
15435 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15436 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015437 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015438
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015439 if (dumpflags & DUMPFLAG_COUNT)
15440 {
15441 hashitem_T *hi;
15442
15443 /* Include the word count for ":spelldump!". */
15444 hi = hash_find(&slang->sl_wordcount, tw);
15445 if (!HASHITEM_EMPTY(hi))
15446 {
15447 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15448 tw, HI2WC(hi)->wc_count);
15449 p = IObuff;
15450 }
15451 }
15452
15453 ml_append(lnum, p, (colnr_T)0, FALSE);
15454 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015455 else if (((dumpflags & DUMPFLAG_ICASE)
15456 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15457 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015458 && ins_compl_add_infercase(p, (int)STRLEN(p),
15459 dumpflags & DUMPFLAG_ICASE,
15460 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015461 /* if dir was BACKWARD then honor it just once */
15462 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015463}
15464
15465/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015466 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15467 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015468 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015469 * Return the updated line number.
15470 */
15471 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015472dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015473 slang_T *slang;
15474 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015475 char_u *pat;
15476 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015477 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015478 int flags; /* flags with prefix ID */
15479 linenr_T startlnum;
15480{
15481 idx_T arridx[MAXWLEN];
15482 int curi[MAXWLEN];
15483 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015484 char_u word_up[MAXWLEN];
15485 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015486 int c;
15487 char_u *byts;
15488 idx_T *idxs;
15489 linenr_T lnum = startlnum;
15490 int depth;
15491 int n;
15492 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015493 int i;
15494
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015495 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015496 * upper-case letter in word_up[]. */
15497 c = PTR2CHAR(word);
15498 if (SPELL_TOUPPER(c) != c)
15499 {
15500 onecap_copy(word, word_up, TRUE);
15501 has_word_up = TRUE;
15502 }
15503
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015504 byts = slang->sl_pbyts;
15505 idxs = slang->sl_pidxs;
15506 if (byts != NULL) /* array not is empty */
15507 {
15508 /*
15509 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015510 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015511 */
15512 depth = 0;
15513 arridx[0] = 0;
15514 curi[0] = 1;
15515 while (depth >= 0 && !got_int)
15516 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015517 n = arridx[depth];
15518 len = byts[n];
15519 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015520 {
15521 /* Done all bytes at this node, go up one level. */
15522 --depth;
15523 line_breakcheck();
15524 }
15525 else
15526 {
15527 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015528 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015529 ++curi[depth];
15530 c = byts[n];
15531 if (c == 0)
15532 {
15533 /* End of prefix, find out how many IDs there are. */
15534 for (i = 1; i < len; ++i)
15535 if (byts[n + i] != 0)
15536 break;
15537 curi[depth] += i - 1;
15538
Bram Moolenaar53805d12005-08-01 07:08:33 +000015539 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15540 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015541 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015542 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015543 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015544 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015545 : flags, lnum);
15546 if (lnum != 0)
15547 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015549
15550 /* Check for prefix that matches the word when the
15551 * first letter is upper-case, but only if the prefix has
15552 * a condition. */
15553 if (has_word_up)
15554 {
15555 c = valid_word_prefix(i, n, flags, word_up, slang,
15556 TRUE);
15557 if (c != 0)
15558 {
15559 vim_strncpy(prefix + depth, word_up,
15560 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015561 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015562 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015563 : flags, lnum);
15564 if (lnum != 0)
15565 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015566 }
15567 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015568 }
15569 else
15570 {
15571 /* Normal char, go one level deeper. */
15572 prefix[depth++] = c;
15573 arridx[depth] = idxs[n];
15574 curi[depth] = 1;
15575 }
15576 }
15577 }
15578 }
15579
15580 return lnum;
15581}
15582
Bram Moolenaar95529562005-08-25 21:21:38 +000015583/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015584 * Move "p" to the end of word "start".
15585 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015586 */
15587 char_u *
15588spell_to_word_end(start, buf)
15589 char_u *start;
15590 buf_T *buf;
15591{
15592 char_u *p = start;
15593
15594 while (*p != NUL && spell_iswordp(p, buf))
15595 mb_ptr_adv(p);
15596 return p;
15597}
15598
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015599#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015600/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015601 * For Insert mode completion CTRL-X s:
15602 * Find start of the word in front of column "startcol".
15603 * We don't check if it is badly spelled, with completion we can only change
15604 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015605 * Returns the column number of the word.
15606 */
15607 int
15608spell_word_start(startcol)
15609 int startcol;
15610{
15611 char_u *line;
15612 char_u *p;
15613 int col = 0;
15614
Bram Moolenaar95529562005-08-25 21:21:38 +000015615 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015616 return startcol;
15617
15618 /* Find a word character before "startcol". */
15619 line = ml_get_curline();
15620 for (p = line + startcol; p > line; )
15621 {
15622 mb_ptr_back(line, p);
15623 if (spell_iswordp_nmw(p))
15624 break;
15625 }
15626
15627 /* Go back to start of the word. */
15628 while (p > line)
15629 {
15630 col = p - line;
15631 mb_ptr_back(line, p);
15632 if (!spell_iswordp(p, curbuf))
15633 break;
15634 col = 0;
15635 }
15636
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015637 return col;
15638}
15639
15640/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015641 * Need to check for 'spellcapcheck' now, the word is removed before
15642 * expand_spelling() is called. Therefore the ugly global variable.
15643 */
15644static int spell_expand_need_cap;
15645
15646 void
15647spell_expand_check_cap(col)
15648 colnr_T col;
15649{
15650 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15651}
15652
15653/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015654 * Get list of spelling suggestions.
15655 * Used for Insert mode completion CTRL-X ?.
15656 * Returns the number of matches. The matches are in "matchp[]", array of
15657 * allocated strings.
15658 */
15659/*ARGSUSED*/
15660 int
15661expand_spelling(lnum, col, pat, matchp)
15662 linenr_T lnum;
15663 int col;
15664 char_u *pat;
15665 char_u ***matchp;
15666{
15667 garray_T ga;
15668
Bram Moolenaar4770d092006-01-12 23:22:24 +000015669 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015670 *matchp = ga.ga_data;
15671 return ga.ga_len;
15672}
15673#endif
15674
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015675#endif /* FEAT_SPELL */