blob: 39b5b9426e434be49e8ea3f09e62a631df4030ae [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
80 * compute the the maximum word score that can be used.
81 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaare19defe2005-03-21 08:23:33 +0000308#endif
309
310#include "vim.h"
311
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000312#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000313
314#ifdef HAVE_FCNTL_H
315# include <fcntl.h>
316#endif
317
Bram Moolenaar4770d092006-01-12 23:22:24 +0000318#ifndef UNIX /* it's in os_unix.h for Unix */
319# include <time.h> /* for time_t */
320#endif
321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000322#define MAXWLEN 250 /* Assume max. word len is this many bytes.
323 Some places assume a word length fits in a
324 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000325
Bram Moolenaare52325c2005-08-22 22:54:29 +0000326/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000327 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000328#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000329typedef int idx_T;
330#else
331typedef long idx_T;
332#endif
333
334/* Flags used for a word. Only the lowest byte can be used, the region byte
335 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336#define WF_REGION 0x01 /* region byte follows */
337#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
338#define WF_ALLCAP 0x04 /* word must be all capitals */
339#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000340#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000341#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000342#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000345/* for <flags2>, shifted up one byte to be used in wn_flags */
346#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000347#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000348#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000349#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000350#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
351#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000352
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000353/* only used for su_badflags */
354#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
355
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000356#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000357
Bram Moolenaar53805d12005-08-01 07:08:33 +0000358/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000359#define WFP_RARE 0x01 /* rare prefix */
360#define WFP_NC 0x02 /* prefix is not combining */
361#define WFP_UP 0x04 /* to-upper prefix */
362#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
363#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000364
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000365/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
366 * byte) and prefcondnr (two bytes). */
367#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
368#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
369#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
370#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
371 * COMPOUNDPERMITFLAG */
372#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
373 * COMPOUNDFORBIDFLAG */
374
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000375
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000376/* flags for <compoptions> */
377#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
378#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
379#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
380#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
381
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000382/* Special byte values for <byte>. Some are only used in the tree for
383 * postponed prefixes, some only in the other trees. This is a bit messy... */
384#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000385 * postponed prefix: no <pflags> */
386#define BY_INDEX 1 /* child is shared, index follows */
387#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
388 * postponed prefix: <pflags> follows */
389#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
390 * follow; never used in prefix tree */
391#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000392
Bram Moolenaar4770d092006-01-12 23:22:24 +0000393/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
394 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 * One replacement: from "ft_from" to "ft_to". */
396typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398 char_u *ft_from;
399 char_u *ft_to;
400} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000401
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000402/* Info from "SAL" entries in ".aff" file used in sl_sal.
403 * The info is split for quick processing by spell_soundfold().
404 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
405typedef struct salitem_S
406{
407 char_u *sm_lead; /* leading letters */
408 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000409 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000410 char_u *sm_rules; /* rules like ^, $, priority */
411 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000412#ifdef FEAT_MBYTE
413 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000414 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000415 int *sm_to_w; /* wide character copy of "sm_to" */
416#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000417} salitem_T;
418
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000419#ifdef FEAT_MBYTE
420typedef int salfirst_T;
421#else
422typedef short salfirst_T;
423#endif
424
Bram Moolenaar5195e452005-08-19 20:32:47 +0000425/* Values for SP_*ERROR are negative, positive values are used by
426 * read_cnt_string(). */
427#define SP_TRUNCERROR -1 /* spell file truncated error */
428#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000429#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000430
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000432 * Structure used to store words and other info for one language, loaded from
433 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000434 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
435 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
436 *
437 * The "byts" array stores the possible bytes in each tree node, preceded by
438 * the number of possible bytes, sorted on byte value:
439 * <len> <byte1> <byte2> ...
440 * The "idxs" array stores the index of the child node corresponding to the
441 * byte in "byts".
442 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000443 * the flags, region mask and affixID for the word. There may be several
444 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000445 */
446typedef struct slang_S slang_T;
447struct slang_S
448{
449 slang_T *sl_next; /* next language */
450 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000451 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000452 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000453
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000455 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000458 char_u *sl_pbyts; /* prefix tree word bytes */
459 idx_T *sl_pidxs; /* prefix tree word indexes */
460
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000461 char_u *sl_info; /* infotext string or NULL */
462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000463 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000465 char_u *sl_midword; /* MIDWORD string or NULL */
466
Bram Moolenaar4770d092006-01-12 23:22:24 +0000467 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
468
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000469 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000470 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000472 int sl_compoptions; /* COMP_* flags */
473 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000475 * (NULL when no compounding) */
476 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000477 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000478 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
480 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000481
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000482 int sl_prefixcnt; /* number of items in "sl_prefprog" */
483 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485 garray_T sl_rep; /* list of fromto_T entries from REP lines */
486 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
487 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000489 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490 there is none */
491 int sl_followup; /* SAL followup */
492 int sl_collapse; /* SAL collapse_result */
493 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000494 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
495 * "sl_sal_first" maps chars, when has_mbyte
496 * "sl_sal" is a list of wide char lists. */
497 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
498 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000499 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000500
501 /* Info from the .sug file. Loaded on demand. */
502 time_t sl_sugtime; /* timestamp for .sug file */
503 char_u *sl_sbyts; /* soundfolded word bytes */
504 idx_T *sl_sidxs; /* soundfolded word indexes */
505 buf_T *sl_sugbuf; /* buffer with word number table */
506 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
507 load */
508
Bram Moolenaarea424162005-06-16 21:51:00 +0000509 int sl_has_map; /* TRUE if there is a MAP line */
510#ifdef FEAT_MBYTE
511 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
512 int sl_map_array[256]; /* MAP for first 256 chars */
513#else
514 char_u sl_map_array[256]; /* MAP for first 256 chars */
515#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516 hashtab_T sl_sounddone; /* table with soundfolded words that have
517 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000518};
519
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000520/* First language that is loaded, start of the linked list of loaded
521 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000522static slang_T *first_lang = NULL;
523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000524/* Flags used in .spl file for soundsalike flags. */
525#define SAL_F0LLOWUP 1
526#define SAL_COLLAPSE 2
527#define SAL_REM_ACCENTS 4
528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529/*
530 * Structure used in "b_langp", filled from 'spelllang'.
531 */
532typedef struct langp_S
533{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000534 slang_T *lp_slang; /* info for this language */
535 slang_T *lp_sallang; /* language used for sound folding or NULL */
536 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 int lp_region; /* bitmask for region or REGION_ALL */
538} langp_T;
539
540#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
541
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542#define REGION_ALL 0xff /* word valid in all regions */
543
Bram Moolenaar5195e452005-08-19 20:32:47 +0000544#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
545#define VIMSPELLMAGICL 8
546#define VIMSPELLVERSION 50
547
Bram Moolenaar4770d092006-01-12 23:22:24 +0000548#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
549#define VIMSUGMAGICL 6
550#define VIMSUGVERSION 1
551
Bram Moolenaar5195e452005-08-19 20:32:47 +0000552/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
553#define SN_REGION 0 /* <regionname> section */
554#define SN_CHARFLAGS 1 /* charflags section */
555#define SN_MIDWORD 2 /* <midword> section */
556#define SN_PREFCOND 3 /* <prefcond> section */
557#define SN_REP 4 /* REP items section */
558#define SN_SAL 5 /* SAL items section */
559#define SN_SOFO 6 /* soundfolding section */
560#define SN_MAP 7 /* MAP items section */
561#define SN_COMPOUND 8 /* compound words section */
562#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000563#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000564#define SN_SUGFILE 11 /* timestamp for .sug file */
565#define SN_REPSAL 12 /* REPSAL items section */
566#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000567#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000568#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000569#define SN_END 255 /* end of sections */
570
571#define SNF_REQUIRED 1 /* <sectionflags>: required section */
572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573/* Result values. Lower number is accepted over higher one. */
574#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000575#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576#define SP_RARE 1
577#define SP_LOCAL 2
578#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000579
Bram Moolenaar7887d882005-07-01 22:33:52 +0000580/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000581static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000582
Bram Moolenaar4770d092006-01-12 23:22:24 +0000583typedef struct wordcount_S
584{
585 short_u wc_count; /* nr of times word was seen */
586 char_u wc_word[1]; /* word, actually longer */
587} wordcount_T;
588
589static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000590#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000591#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
592#define MAXWORDCOUNT 0xffff
593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000594/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000595 * Information used when looking for suggestions.
596 */
597typedef struct suginfo_S
598{
599 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000602 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 char_u *su_badptr; /* start of bad word in line */
605 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000606 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
608 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000609 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000611 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000612} suginfo_T;
613
614/* One word suggestion. Used in "si_ga". */
615typedef struct suggest_S
616{
617 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000618 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619 int st_orglen; /* length of replaced text */
620 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621 int st_altscore; /* used when st_score compares equal */
622 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000624 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625} suggest_T;
626
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628
Bram Moolenaar4770d092006-01-12 23:22:24 +0000629/* TRUE if a word appears in the list of banned words. */
630#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
631
632/* Number of suggestions kept when cleaning up. we need to keep more than
633 * what is displayed, because when rescore_suggestions() is called the score
634 * may change and wrong suggestions may be removed later. */
635#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000636
637/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
638 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000639#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640
641/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000642#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000643#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000645#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000647#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000649#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650#define SCORE_SUBST 93 /* substitute a character */
651#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000652#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000653#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000654#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000658#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000659#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000661#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
663 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000664
Bram Moolenaar4770d092006-01-12 23:22:24 +0000665#define SCORE_COMMON1 30 /* subtracted for words seen before */
666#define SCORE_COMMON2 40 /* subtracted for words often seen */
667#define SCORE_COMMON3 50 /* subtracted for words very often seen */
668#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
669#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
670
671/* When trying changed soundfold words it becomes slow when trying more than
672 * two changes. With less then two changes it's slightly faster but we miss a
673 * few good suggestions. In rare cases we need to try three of four changes.
674 */
675#define SCORE_SFMAX1 200 /* maximum score for first try */
676#define SCORE_SFMAX2 300 /* maximum score for second try */
677#define SCORE_SFMAX3 400 /* maximum score for third try */
678
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000679#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000680#define SCORE_MAXMAX 999999 /* accept any score */
681#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
682
683/* for spell_edit_score_limit() we need to know the minimum value of
684 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
685#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686
687/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000688 * Structure to store info for word matching.
689 */
690typedef struct matchinf_S
691{
692 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000693
694 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000696 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000698 char_u *mi_cend; /* char after what was used for
699 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
701 /* case-folded text */
702 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000704
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000705 /* for when checking word after a prefix */
706 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000707 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 int mi_prefcnt; /* number of entries at mi_prefarridx */
709 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000710#ifdef FEAT_MBYTE
711 int mi_cprefixlen; /* byte length of prefix in original
712 case */
713#else
714# define mi_cprefixlen mi_prefixlen /* it's the same value */
715#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000716
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000717 /* for when checking a compound word */
718 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000719 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
720 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000721 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000722
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000723 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000724 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000725 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000726 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000727
728 /* for NOBREAK */
729 int mi_result2; /* "mi_resul" without following word */
730 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731} matchinf_T;
732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000733/*
734 * The tables used for recognizing word characters according to spelling.
735 * These are only used for the first 256 characters of 'encoding'.
736 */
737typedef struct spelltab_S
738{
739 char_u st_isw[256]; /* flags: is word char */
740 char_u st_isu[256]; /* flags: is uppercase char */
741 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000742 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000743} spelltab_T;
744
745static spelltab_T spelltab;
746static int did_set_spelltab;
747
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000748#define CF_WORD 0x01
749#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000750
751static void clear_spell_chartab __ARGS((spelltab_T *sp));
752static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000753static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
754static int spell_iswordp_nmw __ARGS((char_u *p));
755#ifdef FEAT_MBYTE
756static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
757#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000758static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000759
760/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000762 */
763typedef enum
764{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 STATE_START = 0, /* At start of node check for NUL bytes (goodword
766 * ends); if badword ends there is a match, otherwise
767 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000768 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
771 STATE_PLAIN, /* Use each byte of the node. */
772 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000773 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_INS, /* Insert a byte in the bad word. */
775 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNSWAP, /* Undo swap two characters. */
777 STATE_SWAP3, /* Swap two characters over three. */
778 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000779 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000781 STATE_REP_INI, /* Prepare for using REP items. */
782 STATE_REP, /* Use matching REP items from the .aff file. */
783 STATE_REP_UNDO, /* Undo a REP item replacement. */
784 STATE_FINAL /* End of this node. */
785} state_T;
786
787/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000788 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789 */
790typedef struct trystate_S
791{
Bram Moolenaarea424162005-06-16 21:51:00 +0000792 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000795 short ts_curi; /* index in list of child nodes */
796 char_u ts_fidx; /* index in fword[], case-folded bad word */
797 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
798 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000799 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000800 * PFD_PREFIXTREE or PFD_NOPREFIX */
801 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000802#ifdef FEAT_MBYTE
803 char_u ts_tcharlen; /* number of bytes in tword character */
804 char_u ts_tcharidx; /* current byte index in tword character */
805 char_u ts_isdiff; /* DIFF_ values */
806 char_u ts_fcharstart; /* index in fword where badword char started */
807#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000808 char_u ts_prewordlen; /* length of word in "preword[]" */
809 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000810 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000811 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000812 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000813 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000814 char_u ts_delidx; /* index in fword for char that was deleted,
815 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816} trystate_T;
817
Bram Moolenaarea424162005-06-16 21:51:00 +0000818/* values for ts_isdiff */
819#define DIFF_NONE 0 /* no different byte (yet) */
820#define DIFF_YES 1 /* different byte found */
821#define DIFF_INSERT 2 /* inserting character */
822
Bram Moolenaard12a1322005-08-21 22:08:24 +0000823/* values for ts_flags */
824#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
825#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000826#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000828/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000829#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000830#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000831#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000834#define FIND_FOLDWORD 0 /* find word case-folded */
835#define FIND_KEEPWORD 1 /* find keep-case word */
836#define FIND_PREFIX 2 /* find word after prefix */
837#define FIND_COMPOUND 3 /* find case-folded compound word */
838#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000839
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840static slang_T *slang_alloc __ARGS((char_u *lang));
841static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000842static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000843static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000845static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000846static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000847static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000849static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000850static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000851static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000852static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000853static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000854static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000855static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000856static int get2c __ARGS((FILE *fd));
857static int get3c __ARGS((FILE *fd));
858static int get4c __ARGS((FILE *fd));
859static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000860static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000861static char_u *read_string __ARGS((FILE *fd, int cnt));
862static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
863static int read_charflags_section __ARGS((FILE *fd));
864static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000865static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
868static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
869static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
871static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000872static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000873static int init_syl_tab __ARGS((slang_T *slang));
874static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000875static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
876static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000877#ifdef FEAT_MBYTE
878static int *mb_str2wide __ARGS((char_u *s));
879#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000880static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
881static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000882static void clear_midword __ARGS((buf_T *buf));
883static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000884static int find_region __ARGS((char_u *rp, char_u *region));
885static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000886static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000887static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000891static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000892static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000893#ifdef FEAT_EVAL
894static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
895#endif
896static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000897static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
898static void suggest_load_files __ARGS((void));
899static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000903static void suggest_try_special __ARGS((suginfo_T *su));
904static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000905static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
906static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000907#ifdef FEAT_MBYTE
908static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000911static void score_comp_sal __ARGS((suginfo_T *su));
912static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000913static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000914static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000915static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_finish __ARGS((void));
917static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
918static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000919static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000920static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
923static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000924static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000925static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000926static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000927static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
929static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
930static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000931#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000932static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000934static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000935static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
936static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
937#ifdef FEAT_MBYTE
938static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000940static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
941static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000942static buf_T *open_spellbuf __ARGS((void));
943static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000944
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000945/*
946 * Use our own character-case definitions, because the current locale may
947 * differ from what the .spl file uses.
948 * These must not be called with negative number!
949 */
950#ifndef FEAT_MBYTE
951/* Non-multi-byte implementation. */
952# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
953# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
954# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
955#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000956# if defined(HAVE_WCHAR_H)
957# include <wchar.h> /* for towupper() and towlower() */
958# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000959/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
960 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
961 * the "w" library function for characters above 255 if available. */
962# ifdef HAVE_TOWLOWER
963# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
964 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
965# else
966# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
967 : (c) < 256 ? spelltab.st_fold[c] : (c))
968# endif
969
970# ifdef HAVE_TOWUPPER
971# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
972 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
973# else
974# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
975 : (c) < 256 ? spelltab.st_upper[c] : (c))
976# endif
977
978# ifdef HAVE_ISWUPPER
979# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
980 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
981# else
982# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000983 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000984# endif
985#endif
986
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000987
988static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000989static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000990static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000991static char *e_affname = N_("Affix name too long in %s line %d: %s");
992static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
993static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000994static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000996/* Remember what "z?" replaced. */
997static char_u *repl_from = NULL;
998static char_u *repl_to = NULL;
999
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001000/*
1001 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001002 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001003 * "*attrp" is set to the highlight index for a badly spelled word. For a
1004 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001007 * "capcol" is used to check for a Capitalised word after the end of a
1008 * sentence. If it's zero then perform the check. Return the column where to
1009 * check next, or -1 when no sentence end was found. If it's NULL then don't
1010 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001011 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 * Returns the length of the word in bytes, also when it's OK, so that the
1013 * caller can skip over the word.
1014 */
1015 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001016spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001019 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001020 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001021 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022{
1023 matchinf_T mi; /* Most things are put in "mi" so that it can
1024 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001025 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001026 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001027 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001028 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001029 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001030
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001031 /* A word never starts at a space or a control character. Return quickly
1032 * then, skipping over the character. */
1033 if (*ptr <= ' ')
1034 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001035
1036 /* Return here when loading language files failed. */
1037 if (wp->w_buffer->b_langp.ga_len == 0)
1038 return 1;
1039
Bram Moolenaar5195e452005-08-19 20:32:47 +00001040 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001042 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001043 * 0X99FF. But always do check spelling to find "3GPP" and "11
1044 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001045 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001046 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001047 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1048 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 else
1050 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001051 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001053
Bram Moolenaar0c405862005-06-22 22:26:26 +00001054 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001056 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001057 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001058 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001059 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001061 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001062 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001063
1064 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1065 {
1066 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001067 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001069 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 if (capcol != NULL)
1073 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* We always use the characters up to the next non-word character,
1076 * also for bad words. */
1077 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001078
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001079 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001080 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001081
Bram Moolenaar5195e452005-08-19 20:32:47 +00001082 /* case-fold the word with one non-word character, so that we can check
1083 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001084 if (*mi.mi_fend != NUL)
1085 mb_ptr_adv(mi.mi_fend);
1086
1087 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1088 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001089 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090
1091 /* The word is bad unless we recognize it. */
1092 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001093 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /*
1096 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001097 * We check them all, because a word may be matched longer in another
1098 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001099 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001100 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001102 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1103
1104 /* If reloading fails the language is still in the list but everything
1105 * has been cleared. */
1106 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1107 continue;
1108
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001109 /* Check for a matching word in case-folded words. */
1110 find_word(&mi, FIND_FOLDWORD);
1111
1112 /* Check for a matching word in keep-case words. */
1113 find_word(&mi, FIND_KEEPWORD);
1114
1115 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001116 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001117
1118 /* For a NOBREAK language, may want to use a word without a following
1119 * word as a backup. */
1120 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1121 && mi.mi_result2 != SP_BAD)
1122 {
1123 mi.mi_result = mi.mi_result2;
1124 mi.mi_end = mi.mi_end2;
1125 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001126
1127 /* Count the word in the first language where it's found to be OK. */
1128 if (count_word && mi.mi_result == SP_OK)
1129 {
1130 count_common_word(mi.mi_lp->lp_slang, ptr,
1131 (int)(mi.mi_end - ptr), 1);
1132 count_word = FALSE;
1133 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001134 }
1135
1136 if (mi.mi_result != SP_OK)
1137 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001138 /* If we found a number skip over it. Allows for "42nd". Do flag
1139 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001140 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001141 {
1142 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1143 return nrlen;
1144 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001145
1146 /* When we are at a non-word character there is no error, just
1147 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001148 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001149 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001150 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1151 {
1152 regmatch_T regmatch;
1153
1154 /* Check for end of sentence. */
1155 regmatch.regprog = wp->w_buffer->b_cap_prog;
1156 regmatch.rm_ic = FALSE;
1157 if (vim_regexec(&regmatch, ptr, 0))
1158 *capcol = (int)(regmatch.endp[0] - ptr);
1159 }
1160
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001161#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001163 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001165 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001166 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001167 else if (mi.mi_end == ptr)
1168 /* Always include at least one character. Required for when there
1169 * is a mixup in "midword". */
1170 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 else if (mi.mi_result == SP_BAD
1172 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1173 {
1174 char_u *p, *fp;
1175 int save_result = mi.mi_result;
1176
1177 /* First language in 'spelllang' is NOBREAK. Find first position
1178 * at which any word would be valid. */
1179 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001180 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001182 p = mi.mi_word;
1183 fp = mi.mi_fword;
1184 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001186 mb_ptr_adv(p);
1187 mb_ptr_adv(fp);
1188 if (p >= mi.mi_end)
1189 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001190 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001191 find_word(&mi, FIND_COMPOUND);
1192 if (mi.mi_result != SP_BAD)
1193 {
1194 mi.mi_end = p;
1195 break;
1196 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001198 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001199 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001201
1202 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001204 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001205 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001206 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001207 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001208 }
1209
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1211 {
1212 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001213 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001214 return wrongcaplen;
1215 }
1216
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217 return (int)(mi.mi_end - ptr);
1218}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001219
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220/*
1221 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001222 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1223 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1224 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1225 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 *
1227 * For a match mip->mi_result is updated.
1228 */
1229 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001234 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001236 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 int endidxcnt = 0;
1238 int len;
1239 int wlen = 0;
1240 int flen;
1241 int c;
1242 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001243 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244#ifdef FEAT_MBYTE
1245 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001246#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001247 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001248 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 slang_T *slang = mip->mi_lp->lp_slang;
1250 unsigned flags;
1251 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001252 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001253 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001254 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001255 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 /* Check for word with matching case in keep-case tree. */
1260 ptr = mip->mi_word;
1261 flen = 9999; /* no case folding, always enough bytes */
1262 byts = slang->sl_kbyts;
1263 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001264
1265 if (mode == FIND_KEEPCOMPOUND)
1266 /* Skip over the previously found word(s). */
1267 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001268 }
1269 else
1270 {
1271 /* Check for case-folded in case-folded tree. */
1272 ptr = mip->mi_fword;
1273 flen = mip->mi_fwordlen; /* available case-folded bytes */
1274 byts = slang->sl_fbyts;
1275 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001276
1277 if (mode == FIND_PREFIX)
1278 {
1279 /* Skip over the prefix. */
1280 wlen = mip->mi_prefixlen;
1281 flen -= mip->mi_prefixlen;
1282 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001283 else if (mode == FIND_COMPOUND)
1284 {
1285 /* Skip over the previously found word(s). */
1286 wlen = mip->mi_compoff;
1287 flen -= mip->mi_compoff;
1288 }
1289
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290 }
1291
Bram Moolenaar51485f02005-06-04 21:55:20 +00001292 if (byts == NULL)
1293 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294
Bram Moolenaar51485f02005-06-04 21:55:20 +00001295 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001296 * Repeat advancing in the tree until:
1297 * - there is a byte that doesn't match,
1298 * - we reach the end of the tree,
1299 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001300 */
1301 for (;;)
1302 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001303 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001304 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001305
1306 len = byts[arridx++];
1307
1308 /* If the first possible byte is a zero the word could end here.
1309 * Remember this index, we first check for the longest word. */
1310 if (byts[arridx] == 0)
1311 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001312 if (endidxcnt == MAXWLEN)
1313 {
1314 /* Must be a corrupted spell file. */
1315 EMSG(_(e_format));
1316 return;
1317 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 endlen[endidxcnt] = wlen;
1319 endidx[endidxcnt++] = arridx++;
1320 --len;
1321
1322 /* Skip over the zeros, there can be several flag/region
1323 * combinations. */
1324 while (len > 0 && byts[arridx] == 0)
1325 {
1326 ++arridx;
1327 --len;
1328 }
1329 if (len == 0)
1330 break; /* no children, word must end here */
1331 }
1332
1333 /* Stop looking at end of the line. */
1334 if (ptr[wlen] == NUL)
1335 break;
1336
1337 /* Perform a binary search in the list of accepted bytes. */
1338 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001339 if (c == TAB) /* <Tab> is handled like <Space> */
1340 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001341 lo = arridx;
1342 hi = arridx + len - 1;
1343 while (lo < hi)
1344 {
1345 m = (lo + hi) / 2;
1346 if (byts[m] > c)
1347 hi = m - 1;
1348 else if (byts[m] < c)
1349 lo = m + 1;
1350 else
1351 {
1352 lo = hi = m;
1353 break;
1354 }
1355 }
1356
1357 /* Stop if there is no matching byte. */
1358 if (hi < lo || byts[lo] != c)
1359 break;
1360
1361 /* Continue at the child (if there is one). */
1362 arridx = idxs[lo];
1363 ++wlen;
1364 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001365
1366 /* One space in the good word may stand for several spaces in the
1367 * checked word. */
1368 if (c == ' ')
1369 {
1370 for (;;)
1371 {
1372 if (flen <= 0 && *mip->mi_fend != NUL)
1373 flen = fold_more(mip);
1374 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1375 break;
1376 ++wlen;
1377 --flen;
1378 }
1379 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380 }
1381
1382 /*
1383 * Verify that one of the possible endings is valid. Try the longest
1384 * first.
1385 */
1386 while (endidxcnt > 0)
1387 {
1388 --endidxcnt;
1389 arridx = endidx[endidxcnt];
1390 wlen = endlen[endidxcnt];
1391
1392#ifdef FEAT_MBYTE
1393 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1394 continue; /* not at first byte of character */
1395#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001396 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001399 continue; /* next char is a word character */
1400 word_ends = FALSE;
1401 }
1402 else
1403 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001404 /* The prefix flag is before compound flags. Once a valid prefix flag
1405 * has been found we try compound flags. */
1406 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407
1408#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 {
1411 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 * when folding case. This can be slow, take a shortcut when the
1413 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001415 if (STRNCMP(ptr, p, wlen) != 0)
1416 {
1417 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1418 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001419 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001421 }
1422#endif
1423
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 /* Check flags and region. For FIND_PREFIX check the condition and
1425 * prefix ID.
1426 * Repeat this if there are more flags/region alternatives until there
1427 * is a match. */
1428 res = SP_BAD;
1429 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1430 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 {
1432 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001433
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001434 /* For the fold-case tree check that the case of the checked word
1435 * matches with what the word in the tree requires.
1436 * For keep-case tree the case is always right. For prefixes we
1437 * don't bother to check. */
1438 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 if (mip->mi_cend != mip->mi_word + wlen)
1441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 /* mi_capflags was set for a different word length, need
1443 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 }
1447
Bram Moolenaar0c405862005-06-22 22:26:26 +00001448 if (mip->mi_capflags == WF_KEEPCAP
1449 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001450 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001451 }
1452
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001453 /* When mode is FIND_PREFIX the word must support the prefix:
1454 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001455 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001456 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001458 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001459 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001460 mip->mi_word + mip->mi_cprefixlen, slang,
1461 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001463 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001464
1465 /* Use the WF_RARE flag for a rare prefix. */
1466 if (c & WF_RAREPFX)
1467 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001468 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469 }
1470
Bram Moolenaar78622822005-08-23 21:00:13 +00001471 if (slang->sl_nobreak)
1472 {
1473 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1474 && (flags & WF_BANNED) == 0)
1475 {
1476 /* NOBREAK: found a valid following word. That's all we
1477 * need to know, so return. */
1478 mip->mi_result = SP_OK;
1479 break;
1480 }
1481 }
1482
1483 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1484 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001485 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001486 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001487 * COMPOUNDMIN reject it quickly.
1488 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 * that's too short... Myspell compatibility requires this
1490 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001491 if (((unsigned)flags >> 24) == 0
1492 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001494#ifdef FEAT_MBYTE
1495 /* For multi-byte chars check character length against
1496 * COMPOUNDMIN. */
1497 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001498 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001499 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1500 wlen - mip->mi_compoff) < slang->sl_compminlen)
1501 continue;
1502#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001503
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001504 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001505 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001506 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1507 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001508 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001509 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001510
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001511 /* Don't allow compounding on a side where an affix was added,
1512 * unless COMPOUNDPERMITFLAG was used. */
1513 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1514 continue;
1515 if (!word_ends && (flags & WF_NOCOMPAFT))
1516 continue;
1517
Bram Moolenaard12a1322005-08-21 22:08:24 +00001518 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001519 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001520 ? slang->sl_compstartflags
1521 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001522 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001523 continue;
1524
Bram Moolenaare52325c2005-08-22 22:54:29 +00001525 if (mode == FIND_COMPOUND)
1526 {
1527 int capflags;
1528
1529 /* Need to check the caps type of the appended compound
1530 * word. */
1531#ifdef FEAT_MBYTE
1532 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1533 mip->mi_compoff) != 0)
1534 {
1535 /* case folding may have changed the length */
1536 p = mip->mi_word;
1537 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1538 mb_ptr_adv(p);
1539 }
1540 else
1541#endif
1542 p = mip->mi_word + mip->mi_compoff;
1543 capflags = captype(p, mip->mi_word + wlen);
1544 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1545 && (flags & WF_FIXCAP) != 0))
1546 continue;
1547
1548 if (capflags != WF_ALLCAP)
1549 {
1550 /* When the character before the word is a word
1551 * character we do not accept a Onecap word. We do
1552 * accept a no-caps word, even when the dictionary
1553 * word specifies ONECAP. */
1554 mb_ptr_back(mip->mi_word, p);
1555 if (spell_iswordp_nmw(p)
1556 ? capflags == WF_ONECAP
1557 : (flags & WF_ONECAP) != 0
1558 && capflags != WF_ONECAP)
1559 continue;
1560 }
1561 }
1562
Bram Moolenaar5195e452005-08-19 20:32:47 +00001563 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001564 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001565 * the number of syllables must not be too large. */
1566 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1567 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1568 if (word_ends)
1569 {
1570 char_u fword[MAXWLEN];
1571
1572 if (slang->sl_compsylmax < MAXWLEN)
1573 {
1574 /* "fword" is only needed for checking syllables. */
1575 if (ptr == mip->mi_word)
1576 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1577 else
1578 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1579 }
1580 if (!can_compound(slang, fword, mip->mi_compflags))
1581 continue;
1582 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001583 }
1584
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001585 /* Check NEEDCOMPOUND: can't use word without compounding. */
1586 else if (flags & WF_NEEDCOMP)
1587 continue;
1588
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 nobreak_result = SP_OK;
1590
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001591 if (!word_ends)
1592 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001593 int save_result = mip->mi_result;
1594 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001595 langp_T *save_lp = mip->mi_lp;
1596 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001597
1598 /* Check that a valid word follows. If there is one and we
1599 * are compounding, it will set "mi_result", thus we are
1600 * always finished here. For NOBREAK we only check that a
1601 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001602 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 if (slang->sl_nobreak)
1604 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001605
1606 /* Find following word in case-folded tree. */
1607 mip->mi_compoff = endlen[endidxcnt];
1608#ifdef FEAT_MBYTE
1609 if (has_mbyte && mode == FIND_KEEPWORD)
1610 {
1611 /* Compute byte length in case-folded word from "wlen":
1612 * byte length in keep-case word. Length may change when
1613 * folding case. This can be slow, take a shortcut when
1614 * the case-folded word is equal to the keep-case word. */
1615 p = mip->mi_fword;
1616 if (STRNCMP(ptr, p, wlen) != 0)
1617 {
1618 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1619 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001620 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001621 }
1622 }
1623#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001624 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001625 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001626 if (flags & WF_COMPROOT)
1627 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001628
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001629 /* For NOBREAK we need to try all NOBREAK languages, at least
1630 * to find the ".add" file(s). */
1631 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001632 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001633 if (slang->sl_nobreak)
1634 {
1635 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1636 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1637 || !mip->mi_lp->lp_slang->sl_nobreak)
1638 continue;
1639 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001640
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001641 find_word(mip, FIND_COMPOUND);
1642
1643 /* When NOBREAK any word that matches is OK. Otherwise we
1644 * need to find the longest match, thus try with keep-case
1645 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001646 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1647 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001648 /* Find following word in keep-case tree. */
1649 mip->mi_compoff = wlen;
1650 find_word(mip, FIND_KEEPCOMPOUND);
1651
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001652#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1653 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1654 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1656 {
1657 /* Check for following word with prefix. */
1658 mip->mi_compoff = c;
1659 find_prefix(mip, FIND_COMPOUND);
1660 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001661#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663
1664 if (!slang->sl_nobreak)
1665 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001666 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001667 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001668 if (flags & WF_COMPROOT)
1669 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001670 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001671
Bram Moolenaar78622822005-08-23 21:00:13 +00001672 if (slang->sl_nobreak)
1673 {
1674 nobreak_result = mip->mi_result;
1675 mip->mi_result = save_result;
1676 mip->mi_end = save_end;
1677 }
1678 else
1679 {
1680 if (mip->mi_result == SP_OK)
1681 break;
1682 continue;
1683 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001684 }
1685
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001686 if (flags & WF_BANNED)
1687 res = SP_BANNED;
1688 else if (flags & WF_REGION)
1689 {
1690 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001691 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001692 res = SP_OK;
1693 else
1694 res = SP_LOCAL;
1695 }
1696 else if (flags & WF_RARE)
1697 res = SP_RARE;
1698 else
1699 res = SP_OK;
1700
Bram Moolenaar78622822005-08-23 21:00:13 +00001701 /* Always use the longest match and the best result. For NOBREAK
1702 * we separately keep the longest match without a following good
1703 * word as a fall-back. */
1704 if (nobreak_result == SP_BAD)
1705 {
1706 if (mip->mi_result2 > res)
1707 {
1708 mip->mi_result2 = res;
1709 mip->mi_end2 = mip->mi_word + wlen;
1710 }
1711 else if (mip->mi_result2 == res
1712 && mip->mi_end2 < mip->mi_word + wlen)
1713 mip->mi_end2 = mip->mi_word + wlen;
1714 }
1715 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716 {
1717 mip->mi_result = res;
1718 mip->mi_end = mip->mi_word + wlen;
1719 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001720 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001721 mip->mi_end = mip->mi_word + wlen;
1722
Bram Moolenaar78622822005-08-23 21:00:13 +00001723 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 }
1726
Bram Moolenaar78622822005-08-23 21:00:13 +00001727 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001728 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001729 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001730}
1731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001733 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1734 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001735 */
1736 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001737can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001738 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001739 char_u *word;
1740 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001741{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001742 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001743#ifdef FEAT_MBYTE
1744 char_u uflags[MAXWLEN * 2];
1745 int i;
1746#endif
1747 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001748
1749 if (slang->sl_compprog == NULL)
1750 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001751#ifdef FEAT_MBYTE
1752 if (enc_utf8)
1753 {
1754 /* Need to convert the single byte flags to utf8 characters. */
1755 p = uflags;
1756 for (i = 0; flags[i] != NUL; ++i)
1757 p += mb_char2bytes(flags[i], p);
1758 *p = NUL;
1759 p = uflags;
1760 }
1761 else
1762#endif
1763 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001764 regmatch.regprog = slang->sl_compprog;
1765 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001766 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001767 return FALSE;
1768
Bram Moolenaare52325c2005-08-22 22:54:29 +00001769 /* Count the number of syllables. This may be slow, do it last. If there
1770 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001771 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001772 if (slang->sl_compsylmax < MAXWLEN
1773 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001774 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001775 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001776}
1777
1778/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001779 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1780 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001781 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001782 */
1783 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001784valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001785 int totprefcnt; /* nr of prefix IDs */
1786 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001787 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001788 char_u *word;
1789 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001790 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791{
1792 int prefcnt;
1793 int pidx;
1794 regprog_T *rp;
1795 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001796 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001797
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001798 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1800 {
1801 pidx = slang->sl_pidxs[arridx + prefcnt];
1802
1803 /* Check the prefix ID. */
1804 if (prefid != (pidx & 0xff))
1805 continue;
1806
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001807 /* Check if the prefix doesn't combine and the word already has a
1808 * suffix. */
1809 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1810 continue;
1811
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001812 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001813 * stored in the two bytes above the prefix ID byte. */
1814 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001815 if (rp != NULL)
1816 {
1817 regmatch.regprog = rp;
1818 regmatch.rm_ic = FALSE;
1819 if (!vim_regexec(&regmatch, word, 0))
1820 continue;
1821 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001822 else if (cond_req)
1823 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001824
Bram Moolenaar53805d12005-08-01 07:08:33 +00001825 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001826 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001827 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001828 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001829}
1830
1831/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001832 * Check if the word at "mip->mi_word" has a matching prefix.
1833 * If it does, then check the following word.
1834 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001835 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1836 * prefix in a compound word.
1837 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838 * For a match mip->mi_result is updated.
1839 */
1840 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001841find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001842 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001843 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001844{
1845 idx_T arridx = 0;
1846 int len;
1847 int wlen = 0;
1848 int flen;
1849 int c;
1850 char_u *ptr;
1851 idx_T lo, hi, m;
1852 slang_T *slang = mip->mi_lp->lp_slang;
1853 char_u *byts;
1854 idx_T *idxs;
1855
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001856 byts = slang->sl_pbyts;
1857 if (byts == NULL)
1858 return; /* array is empty */
1859
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001860 /* We use the case-folded word here, since prefixes are always
1861 * case-folded. */
1862 ptr = mip->mi_fword;
1863 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001864 if (mode == FIND_COMPOUND)
1865 {
1866 /* Skip over the previously found word(s). */
1867 ptr += mip->mi_compoff;
1868 flen -= mip->mi_compoff;
1869 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001870 idxs = slang->sl_pidxs;
1871
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001872 /*
1873 * Repeat advancing in the tree until:
1874 * - there is a byte that doesn't match,
1875 * - we reach the end of the tree,
1876 * - or we reach the end of the line.
1877 */
1878 for (;;)
1879 {
1880 if (flen == 0 && *mip->mi_fend != NUL)
1881 flen = fold_more(mip);
1882
1883 len = byts[arridx++];
1884
1885 /* If the first possible byte is a zero the prefix could end here.
1886 * Check if the following word matches and supports the prefix. */
1887 if (byts[arridx] == 0)
1888 {
1889 /* There can be several prefixes with different conditions. We
1890 * try them all, since we don't know which one will give the
1891 * longest match. The word is the same each time, pass the list
1892 * of possible prefixes to find_word(). */
1893 mip->mi_prefarridx = arridx;
1894 mip->mi_prefcnt = len;
1895 while (len > 0 && byts[arridx] == 0)
1896 {
1897 ++arridx;
1898 --len;
1899 }
1900 mip->mi_prefcnt -= len;
1901
1902 /* Find the word that comes after the prefix. */
1903 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001904 if (mode == FIND_COMPOUND)
1905 /* Skip over the previously found word(s). */
1906 mip->mi_prefixlen += mip->mi_compoff;
1907
Bram Moolenaar53805d12005-08-01 07:08:33 +00001908#ifdef FEAT_MBYTE
1909 if (has_mbyte)
1910 {
1911 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001912 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1913 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001914 }
1915 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001916 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001917#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001918 find_word(mip, FIND_PREFIX);
1919
1920
1921 if (len == 0)
1922 break; /* no children, word must end here */
1923 }
1924
1925 /* Stop looking at end of the line. */
1926 if (ptr[wlen] == NUL)
1927 break;
1928
1929 /* Perform a binary search in the list of accepted bytes. */
1930 c = ptr[wlen];
1931 lo = arridx;
1932 hi = arridx + len - 1;
1933 while (lo < hi)
1934 {
1935 m = (lo + hi) / 2;
1936 if (byts[m] > c)
1937 hi = m - 1;
1938 else if (byts[m] < c)
1939 lo = m + 1;
1940 else
1941 {
1942 lo = hi = m;
1943 break;
1944 }
1945 }
1946
1947 /* Stop if there is no matching byte. */
1948 if (hi < lo || byts[lo] != c)
1949 break;
1950
1951 /* Continue at the child (if there is one). */
1952 arridx = idxs[lo];
1953 ++wlen;
1954 --flen;
1955 }
1956}
1957
1958/*
1959 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001960 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001961 * Return the length of the folded chars in bytes.
1962 */
1963 static int
1964fold_more(mip)
1965 matchinf_T *mip;
1966{
1967 int flen;
1968 char_u *p;
1969
1970 p = mip->mi_fend;
1971 do
1972 {
1973 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001974 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001976 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 if (*mip->mi_fend != NUL)
1978 mb_ptr_adv(mip->mi_fend);
1979
1980 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1981 mip->mi_fword + mip->mi_fwordlen,
1982 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001984 mip->mi_fwordlen += flen;
1985 return flen;
1986}
1987
1988/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 * Check case flags for a word. Return TRUE if the word has the requested
1990 * case.
1991 */
1992 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001993spell_valid_case(wordflags, treeflags)
1994 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 int treeflags; /* flags for the word in the spell tree */
1996{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001997 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001999 && ((treeflags & WF_ONECAP) == 0
2000 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001}
2002
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002003/*
2004 * Return TRUE if spell checking is not enabled.
2005 */
2006 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002007no_spell_checking(wp)
2008 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002009{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002010 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2011 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002012 {
2013 EMSG(_("E756: Spell checking is not enabled"));
2014 return TRUE;
2015 }
2016 return FALSE;
2017}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019/*
2020 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002021 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2022 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002023 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2024 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002025 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002026 */
2027 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002028spell_move_to(wp, dir, allwords, curline, attrp)
2029 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002030 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002031 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002033 hlf_T *attrp; /* return: attributes of bad word or NULL
2034 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002036 linenr_T lnum;
2037 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002038 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 char_u *line;
2040 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002041 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002042 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002043 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002044# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002045 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002046# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002047 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002048 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002049 char_u *buf = NULL;
2050 int buflen = 0;
2051 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002052 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002053 int found_one = FALSE;
2054 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaar95529562005-08-25 21:21:38 +00002056 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002057 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002058
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002059 /*
2060 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002061 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002062 *
2063 * When searching backwards, we continue in the line to find the last
2064 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002065 *
2066 * We concatenate the start of the next line, so that wrapped words work
2067 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2068 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002069 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002070 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002071 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072
2073 while (!got_int)
2074 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002075 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002078 if (buflen < len + MAXWLEN + 2)
2079 {
2080 vim_free(buf);
2081 buflen = len + MAXWLEN + 2;
2082 buf = alloc(buflen);
2083 if (buf == NULL)
2084 break;
2085 }
2086
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002087 /* In first line check first word for Capital. */
2088 if (lnum == 1)
2089 capcol = 0;
2090
2091 /* For checking first word with a capital skip white space. */
2092 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 capcol = (int)(skipwhite(line) - line);
2094 else if (curline && wp == curwin)
2095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002097 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002098 if (check_need_cap(lnum, col))
2099 capcol = col;
2100
2101 /* Need to get the line again, may have looked at the previous
2102 * one. */
2103 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2104 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002105
Bram Moolenaar0c405862005-06-22 22:26:26 +00002106 /* Copy the line into "buf" and append the start of the next line if
2107 * possible. */
2108 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002109 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002110 spell_cat_line(buf + STRLEN(buf),
2111 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002112
2113 p = buf + skip;
2114 endp = buf + len;
2115 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002117 /* When searching backward don't search after the cursor. Unless
2118 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002119 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002120 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002121 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002122 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002123 break;
2124
2125 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002126 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002127 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002128
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002129 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002132 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002133 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002134 /* When searching forward only accept a bad word after
2135 * the cursor. */
2136 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002137 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002138 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002139 && (wrapped
2140 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002141 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002142 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002143 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002144# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002145 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002147 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002148 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002149 FALSE, &can_spell);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002150 if (!can_spell)
2151 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 }
2153 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002154#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002155 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 if (can_spell)
2158 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002159 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002160 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002161 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002162#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002163 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002164#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165 if (dir == FORWARD)
2166 {
2167 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002168 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002169 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002170 if (attrp != NULL)
2171 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002172 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002173 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002174 else if (curline)
2175 /* Insert mode completion: put cursor after
2176 * the bad word. */
2177 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002178 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002179 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002181 else
2182 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 }
2185
Bram Moolenaar51485f02005-06-04 21:55:20 +00002186 /* advance to character after the word */
2187 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002188 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002189 }
2190
Bram Moolenaar5195e452005-08-19 20:32:47 +00002191 if (dir == BACKWARD && found_pos.lnum != 0)
2192 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002193 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002194 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002195 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002197 }
2198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002199 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002200 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002202 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002203 if (dir == BACKWARD)
2204 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002205 /* If we are back at the starting line and searched it again there
2206 * is no match, give up. */
2207 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002208 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002209
2210 if (lnum > 1)
2211 --lnum;
2212 else if (!p_ws)
2213 break; /* at first line and 'nowrapscan' */
2214 else
2215 {
2216 /* Wrap around to the end of the buffer. May search the
2217 * starting line again and accept the last match. */
2218 lnum = wp->w_buffer->b_ml.ml_line_count;
2219 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 if (!shortmess(SHM_SEARCH))
2221 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002222 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002223 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002224 }
2225 else
2226 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002227 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2228 ++lnum;
2229 else if (!p_ws)
2230 break; /* at first line and 'nowrapscan' */
2231 else
2232 {
2233 /* Wrap around to the start of the buffer. May search the
2234 * starting line again and accept the first match. */
2235 lnum = 1;
2236 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 if (!shortmess(SHM_SEARCH))
2238 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002239 }
2240
2241 /* If we are back at the starting line and there is no match then
2242 * give up. */
2243 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002244 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245
2246 /* Skip the characters at the start of the next line that were
2247 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002248 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002249 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002250 else
2251 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002253 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002254 --capcol;
2255
2256 /* But after empty line check first word in next line */
2257 if (*skipwhite(line) == NUL)
2258 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002260
2261 line_breakcheck();
2262 }
2263
Bram Moolenaar0c405862005-06-22 22:26:26 +00002264 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002265 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002266}
2267
2268/*
2269 * For spell checking: concatenate the start of the following line "line" into
2270 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2271 */
2272 void
2273spell_cat_line(buf, line, maxlen)
2274 char_u *buf;
2275 char_u *line;
2276 int maxlen;
2277{
2278 char_u *p;
2279 int n;
2280
2281 p = skipwhite(line);
2282 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2283 p = skipwhite(p + 1);
2284
2285 if (*p != NUL)
2286 {
2287 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002288 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002289 n = (int)(p - line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002290 if (n >= maxlen)
2291 n = maxlen - 1;
2292 vim_memset(buf + 1, ' ', n);
2293 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002294}
2295
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002296/*
2297 * Structure used for the cookie argument of do_in_runtimepath().
2298 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002299typedef struct spelload_S
2300{
2301 char_u sl_lang[MAXWLEN + 1]; /* language name */
2302 slang_T *sl_slang; /* resulting slang_T struct */
2303 int sl_nobreak; /* NOBREAK language found */
2304} spelload_T;
2305
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002306/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002307 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002308 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002309 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002310 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002311spell_load_lang(lang)
2312 char_u *lang;
2313{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002314 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002316 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002317#ifdef FEAT_AUTOCMD
2318 int round;
2319#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320
Bram Moolenaarb765d632005-06-07 21:00:02 +00002321 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002322 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002323 STRCPY(sl.sl_lang, lang);
2324 sl.sl_slang = NULL;
2325 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002326
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002327#ifdef FEAT_AUTOCMD
2328 /* We may retry when no spell file is found for the language, an
2329 * autocommand may load it then. */
2330 for (round = 1; round <= 2; ++round)
2331#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002332 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002333 /*
2334 * Find the first spell file for "lang" in 'runtimepath' and load it.
2335 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002336 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002337 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002338 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002339
2340 if (r == FAIL && *sl.sl_lang != NUL)
2341 {
2342 /* Try loading the ASCII version. */
2343 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2344 "spell/%s.ascii.spl", lang);
2345 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2346
2347#ifdef FEAT_AUTOCMD
2348 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2349 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2350 curbuf->b_fname, FALSE, curbuf))
2351 continue;
2352 break;
2353#endif
2354 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355#ifdef FEAT_AUTOCMD
2356 break;
2357#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002358 }
2359
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002360 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002361 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002362 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2363 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002364 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002365 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002366 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002367 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002368 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002369 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002370 }
2371}
2372
2373/*
2374 * Return the encoding used for spell checking: Use 'encoding', except that we
2375 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2376 */
2377 static char_u *
2378spell_enc()
2379{
2380
2381#ifdef FEAT_MBYTE
2382 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2383 return p_enc;
2384#endif
2385 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002386}
2387
2388/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002389 * Get the name of the .spl file for the internal wordlist into
2390 * "fname[MAXPATHL]".
2391 */
2392 static void
2393int_wordlist_spl(fname)
2394 char_u *fname;
2395{
2396 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2397 int_wordlist, spell_enc());
2398}
2399
2400/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002401 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002402 * Caller must fill "sl_next".
2403 */
2404 static slang_T *
2405slang_alloc(lang)
2406 char_u *lang;
2407{
2408 slang_T *lp;
2409
Bram Moolenaar51485f02005-06-04 21:55:20 +00002410 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002411 if (lp != NULL)
2412 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002413 if (lang != NULL)
2414 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002416 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002417 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002418 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002419 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002420 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002421
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002422 return lp;
2423}
2424
2425/*
2426 * Free the contents of an slang_T and the structure itself.
2427 */
2428 static void
2429slang_free(lp)
2430 slang_T *lp;
2431{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002432 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002433 vim_free(lp->sl_fname);
2434 slang_clear(lp);
2435 vim_free(lp);
2436}
2437
2438/*
2439 * Clear an slang_T so that the file can be reloaded.
2440 */
2441 static void
2442slang_clear(lp)
2443 slang_T *lp;
2444{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002445 garray_T *gap;
2446 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002447 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002448 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002449 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450
Bram Moolenaar51485f02005-06-04 21:55:20 +00002451 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002452 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002453 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002454 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002455 vim_free(lp->sl_pbyts);
2456 lp->sl_pbyts = NULL;
2457
Bram Moolenaar51485f02005-06-04 21:55:20 +00002458 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002459 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002460 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002461 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002462 vim_free(lp->sl_pidxs);
2463 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464
Bram Moolenaar4770d092006-01-12 23:22:24 +00002465 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002467 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2468 while (gap->ga_len > 0)
2469 {
2470 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2471 vim_free(ftp->ft_from);
2472 vim_free(ftp->ft_to);
2473 }
2474 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002476
2477 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002478 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002479 {
2480 /* "ga_len" is set to 1 without adding an item for latin1 */
2481 if (gap->ga_data != NULL)
2482 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2483 for (i = 0; i < gap->ga_len; ++i)
2484 vim_free(((int **)gap->ga_data)[i]);
2485 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002486 else
2487 /* SAL items: free salitem_T items */
2488 while (gap->ga_len > 0)
2489 {
2490 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2491 vim_free(smp->sm_lead);
2492 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2493 vim_free(smp->sm_to);
2494#ifdef FEAT_MBYTE
2495 vim_free(smp->sm_lead_w);
2496 vim_free(smp->sm_oneof_w);
2497 vim_free(smp->sm_to_w);
2498#endif
2499 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002500 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002502 for (i = 0; i < lp->sl_prefixcnt; ++i)
2503 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002504 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002505 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002506 lp->sl_prefprog = NULL;
2507
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002508 vim_free(lp->sl_info);
2509 lp->sl_info = NULL;
2510
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002511 vim_free(lp->sl_midword);
2512 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002513
Bram Moolenaar5195e452005-08-19 20:32:47 +00002514 vim_free(lp->sl_compprog);
2515 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002516 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002517 lp->sl_compprog = NULL;
2518 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002519 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002520
2521 vim_free(lp->sl_syllable);
2522 lp->sl_syllable = NULL;
2523 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002524
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002525 ga_clear_strings(&lp->sl_comppat);
2526
Bram Moolenaar4770d092006-01-12 23:22:24 +00002527 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2528 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002529
Bram Moolenaar4770d092006-01-12 23:22:24 +00002530#ifdef FEAT_MBYTE
2531 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002532#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002533
Bram Moolenaar4770d092006-01-12 23:22:24 +00002534 /* Clear info from .sug file. */
2535 slang_clear_sug(lp);
2536
Bram Moolenaar5195e452005-08-19 20:32:47 +00002537 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002538 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002539 lp->sl_compsylmax = MAXWLEN;
2540 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541}
2542
2543/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002544 * Clear the info from the .sug file in "lp".
2545 */
2546 static void
2547slang_clear_sug(lp)
2548 slang_T *lp;
2549{
2550 vim_free(lp->sl_sbyts);
2551 lp->sl_sbyts = NULL;
2552 vim_free(lp->sl_sidxs);
2553 lp->sl_sidxs = NULL;
2554 close_spellbuf(lp->sl_sugbuf);
2555 lp->sl_sugbuf = NULL;
2556 lp->sl_sugloaded = FALSE;
2557 lp->sl_sugtime = 0;
2558}
2559
2560/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002561 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002562 * Invoked through do_in_runtimepath().
2563 */
2564 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002565spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002566 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002567 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002568{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002569 spelload_T *slp = (spelload_T *)cookie;
2570 slang_T *slang;
2571
2572 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2573 if (slang != NULL)
2574 {
2575 /* When a previously loaded file has NOBREAK also use it for the
2576 * ".add" files. */
2577 if (slp->sl_nobreak && slang->sl_add)
2578 slang->sl_nobreak = TRUE;
2579 else if (slang->sl_nobreak)
2580 slp->sl_nobreak = TRUE;
2581
2582 slp->sl_slang = slang;
2583 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002584}
2585
2586/*
2587 * Load one spell file and store the info into a slang_T.
2588 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002589 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002590 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2591 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2592 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2593 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002594 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002595 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2596 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002597 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002598 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002599 static slang_T *
2600spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002601 char_u *fname;
2602 char_u *lang;
2603 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002604 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002605{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002606 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002607 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002608 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002610 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002612 char_u *save_sourcing_name = sourcing_name;
2613 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002614 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002615 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002616 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002617
Bram Moolenaarb765d632005-06-07 21:00:02 +00002618 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002619 if (fd == NULL)
2620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 if (!silent)
2622 EMSG2(_(e_notopen), fname);
2623 else if (p_verbose > 2)
2624 {
2625 verbose_enter();
2626 smsg((char_u *)e_notopen, fname);
2627 verbose_leave();
2628 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002629 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002630 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002631 if (p_verbose > 2)
2632 {
2633 verbose_enter();
2634 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2635 verbose_leave();
2636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002637
Bram Moolenaarb765d632005-06-07 21:00:02 +00002638 if (old_lp == NULL)
2639 {
2640 lp = slang_alloc(lang);
2641 if (lp == NULL)
2642 goto endFAIL;
2643
2644 /* Remember the file name, used to reload the file when it's updated. */
2645 lp->sl_fname = vim_strsave(fname);
2646 if (lp->sl_fname == NULL)
2647 goto endFAIL;
2648
2649 /* Check for .add.spl. */
2650 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2651 }
2652 else
2653 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002654
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002655 /* Set sourcing_name, so that error messages mention the file name. */
2656 sourcing_name = fname;
2657 sourcing_lnum = 0;
2658
Bram Moolenaar4770d092006-01-12 23:22:24 +00002659 /*
2660 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002661 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002662 for (i = 0; i < VIMSPELLMAGICL; ++i)
2663 buf[i] = getc(fd); /* <fileID> */
2664 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2665 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002666 EMSG(_("E757: This does not look like a spell file"));
2667 goto endFAIL;
2668 }
2669 c = getc(fd); /* <versionnr> */
2670 if (c < VIMSPELLVERSION)
2671 {
2672 EMSG(_("E771: Old spell file, needs to be updated"));
2673 goto endFAIL;
2674 }
2675 else if (c > VIMSPELLVERSION)
2676 {
2677 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002678 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002679 }
2680
Bram Moolenaar5195e452005-08-19 20:32:47 +00002681
2682 /*
2683 * <SECTIONS>: <section> ... <sectionend>
2684 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2685 */
2686 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002687 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002688 n = getc(fd); /* <sectionID> or <sectionend> */
2689 if (n == SN_END)
2690 break;
2691 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002692 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693 if (len < 0)
2694 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002695
Bram Moolenaar5195e452005-08-19 20:32:47 +00002696 res = 0;
2697 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002698 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 case SN_INFO:
2700 lp->sl_info = read_string(fd, len); /* <infotext> */
2701 if (lp->sl_info == NULL)
2702 goto endFAIL;
2703 break;
2704
Bram Moolenaar5195e452005-08-19 20:32:47 +00002705 case SN_REGION:
2706 res = read_region_section(fd, lp, len);
2707 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002708
Bram Moolenaar5195e452005-08-19 20:32:47 +00002709 case SN_CHARFLAGS:
2710 res = read_charflags_section(fd);
2711 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002712
Bram Moolenaar5195e452005-08-19 20:32:47 +00002713 case SN_MIDWORD:
2714 lp->sl_midword = read_string(fd, len); /* <midword> */
2715 if (lp->sl_midword == NULL)
2716 goto endFAIL;
2717 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002718
Bram Moolenaar5195e452005-08-19 20:32:47 +00002719 case SN_PREFCOND:
2720 res = read_prefcond_section(fd, lp);
2721 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002722
Bram Moolenaar5195e452005-08-19 20:32:47 +00002723 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002724 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2725 break;
2726
2727 case SN_REPSAL:
2728 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002729 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002730
Bram Moolenaar5195e452005-08-19 20:32:47 +00002731 case SN_SAL:
2732 res = read_sal_section(fd, lp);
2733 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002734
Bram Moolenaar5195e452005-08-19 20:32:47 +00002735 case SN_SOFO:
2736 res = read_sofo_section(fd, lp);
2737 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002738
Bram Moolenaar5195e452005-08-19 20:32:47 +00002739 case SN_MAP:
2740 p = read_string(fd, len); /* <mapstr> */
2741 if (p == NULL)
2742 goto endFAIL;
2743 set_map_str(lp, p);
2744 vim_free(p);
2745 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002746
Bram Moolenaar4770d092006-01-12 23:22:24 +00002747 case SN_WORDS:
2748 res = read_words_section(fd, lp, len);
2749 break;
2750
2751 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002752 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002753 break;
2754
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002755 case SN_NOSPLITSUGS:
2756 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2757 break;
2758
Bram Moolenaar5195e452005-08-19 20:32:47 +00002759 case SN_COMPOUND:
2760 res = read_compound(fd, lp, len);
2761 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002762
Bram Moolenaar78622822005-08-23 21:00:13 +00002763 case SN_NOBREAK:
2764 lp->sl_nobreak = TRUE;
2765 break;
2766
Bram Moolenaar5195e452005-08-19 20:32:47 +00002767 case SN_SYLLABLE:
2768 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2769 if (lp->sl_syllable == NULL)
2770 goto endFAIL;
2771 if (init_syl_tab(lp) == FAIL)
2772 goto endFAIL;
2773 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002774
Bram Moolenaar5195e452005-08-19 20:32:47 +00002775 default:
2776 /* Unsupported section. When it's required give an error
2777 * message. When it's not required skip the contents. */
2778 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002779 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002780 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002781 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002782 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002783 while (--len >= 0)
2784 if (getc(fd) < 0)
2785 goto truncerr;
2786 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002787 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002788someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002789 if (res == SP_FORMERROR)
2790 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002791 EMSG(_(e_format));
2792 goto endFAIL;
2793 }
2794 if (res == SP_TRUNCERROR)
2795 {
2796truncerr:
2797 EMSG(_(e_spell_trunc));
2798 goto endFAIL;
2799 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002800 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002801 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002802 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803
Bram Moolenaar4770d092006-01-12 23:22:24 +00002804 /* <LWORDTREE> */
2805 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2806 if (res != 0)
2807 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002808
Bram Moolenaar4770d092006-01-12 23:22:24 +00002809 /* <KWORDTREE> */
2810 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2811 if (res != 0)
2812 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002813
Bram Moolenaar4770d092006-01-12 23:22:24 +00002814 /* <PREFIXTREE> */
2815 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2816 lp->sl_prefixcnt);
2817 if (res != 0)
2818 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002819
Bram Moolenaarb765d632005-06-07 21:00:02 +00002820 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002821 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002822 {
2823 lp->sl_next = first_lang;
2824 first_lang = lp;
2825 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002826
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002827 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002828
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002829endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002830 if (lang != NULL)
2831 /* truncating the name signals the error to spell_load_lang() */
2832 *lang = NUL;
2833 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002834 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002835 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002836
2837endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002838 if (fd != NULL)
2839 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002840 sourcing_name = save_sourcing_name;
2841 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842
2843 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002844}
2845
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002846/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002847 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2848 */
2849 static int
2850get2c(fd)
2851 FILE *fd;
2852{
2853 long n;
2854
2855 n = getc(fd);
2856 n = (n << 8) + getc(fd);
2857 return n;
2858}
2859
2860/*
2861 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2862 */
2863 static int
2864get3c(fd)
2865 FILE *fd;
2866{
2867 long n;
2868
2869 n = getc(fd);
2870 n = (n << 8) + getc(fd);
2871 n = (n << 8) + getc(fd);
2872 return n;
2873}
2874
2875/*
2876 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2877 */
2878 static int
2879get4c(fd)
2880 FILE *fd;
2881{
2882 long n;
2883
2884 n = getc(fd);
2885 n = (n << 8) + getc(fd);
2886 n = (n << 8) + getc(fd);
2887 n = (n << 8) + getc(fd);
2888 return n;
2889}
2890
2891/*
2892 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2893 */
2894 static time_t
2895get8c(fd)
2896 FILE *fd;
2897{
2898 time_t n = 0;
2899 int i;
2900
2901 for (i = 0; i < 8; ++i)
2902 n = (n << 8) + getc(fd);
2903 return n;
2904}
2905
2906/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002907 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002908 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002909 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002910 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2911 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002912 */
2913 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002914read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002915 FILE *fd;
2916 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002917 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002918{
2919 int cnt = 0;
2920 int i;
2921 char_u *str;
2922
2923 /* read the length bytes, MSB first */
2924 for (i = 0; i < cnt_bytes; ++i)
2925 cnt = (cnt << 8) + getc(fd);
2926 if (cnt < 0)
2927 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002928 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002929 return NULL;
2930 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002931 *cntp = cnt;
2932 if (cnt == 0)
2933 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002934
Bram Moolenaar5195e452005-08-19 20:32:47 +00002935 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002936 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002937 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002938 return str;
2939}
2940
Bram Moolenaar7887d882005-07-01 22:33:52 +00002941/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002942 * Read a string of length "cnt" from "fd" into allocated memory.
2943 * Returns NULL when out of memory.
2944 */
2945 static char_u *
2946read_string(fd, cnt)
2947 FILE *fd;
2948 int cnt;
2949{
2950 char_u *str;
2951 int i;
2952
2953 /* allocate memory */
2954 str = alloc((unsigned)cnt + 1);
2955 if (str != NULL)
2956 {
2957 /* Read the string. Doesn't check for truncated file. */
2958 for (i = 0; i < cnt; ++i)
2959 str[i] = getc(fd);
2960 str[i] = NUL;
2961 }
2962 return str;
2963}
2964
2965/*
2966 * Read SN_REGION: <regionname> ...
2967 * Return SP_*ERROR flags.
2968 */
2969 static int
2970read_region_section(fd, lp, len)
2971 FILE *fd;
2972 slang_T *lp;
2973 int len;
2974{
2975 int i;
2976
2977 if (len > 16)
2978 return SP_FORMERROR;
2979 for (i = 0; i < len; ++i)
2980 lp->sl_regions[i] = getc(fd); /* <regionname> */
2981 lp->sl_regions[len] = NUL;
2982 return 0;
2983}
2984
2985/*
2986 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2987 * <folcharslen> <folchars>
2988 * Return SP_*ERROR flags.
2989 */
2990 static int
2991read_charflags_section(fd)
2992 FILE *fd;
2993{
2994 char_u *flags;
2995 char_u *fol;
2996 int flagslen, follen;
2997
2998 /* <charflagslen> <charflags> */
2999 flags = read_cnt_string(fd, 1, &flagslen);
3000 if (flagslen < 0)
3001 return flagslen;
3002
3003 /* <folcharslen> <folchars> */
3004 fol = read_cnt_string(fd, 2, &follen);
3005 if (follen < 0)
3006 {
3007 vim_free(flags);
3008 return follen;
3009 }
3010
3011 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3012 if (flags != NULL && fol != NULL)
3013 set_spell_charflags(flags, flagslen, fol);
3014
3015 vim_free(flags);
3016 vim_free(fol);
3017
3018 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3019 if ((flags == NULL) != (fol == NULL))
3020 return SP_FORMERROR;
3021 return 0;
3022}
3023
3024/*
3025 * Read SN_PREFCOND section.
3026 * Return SP_*ERROR flags.
3027 */
3028 static int
3029read_prefcond_section(fd, lp)
3030 FILE *fd;
3031 slang_T *lp;
3032{
3033 int cnt;
3034 int i;
3035 int n;
3036 char_u *p;
3037 char_u buf[MAXWLEN + 1];
3038
3039 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003040 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003041 if (cnt <= 0)
3042 return SP_FORMERROR;
3043
3044 lp->sl_prefprog = (regprog_T **)alloc_clear(
3045 (unsigned)sizeof(regprog_T *) * cnt);
3046 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003047 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003048 lp->sl_prefixcnt = cnt;
3049
3050 for (i = 0; i < cnt; ++i)
3051 {
3052 /* <prefcond> : <condlen> <condstr> */
3053 n = getc(fd); /* <condlen> */
3054 if (n < 0 || n >= MAXWLEN)
3055 return SP_FORMERROR;
3056
3057 /* When <condlen> is zero we have an empty condition. Otherwise
3058 * compile the regexp program used to check for the condition. */
3059 if (n > 0)
3060 {
3061 buf[0] = '^'; /* always match at one position only */
3062 p = buf + 1;
3063 while (n-- > 0)
3064 *p++ = getc(fd); /* <condstr> */
3065 *p = NUL;
3066 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3067 }
3068 }
3069 return 0;
3070}
3071
3072/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003073 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003074 * Return SP_*ERROR flags.
3075 */
3076 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003077read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003078 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003079 garray_T *gap;
3080 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003081{
3082 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003083 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003084 int i;
3085
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003086 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003087 if (cnt < 0)
3088 return SP_TRUNCERROR;
3089
Bram Moolenaar5195e452005-08-19 20:32:47 +00003090 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003091 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003092
3093 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3094 for (; gap->ga_len < cnt; ++gap->ga_len)
3095 {
3096 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3097 ftp->ft_from = read_cnt_string(fd, 1, &i);
3098 if (i < 0)
3099 return i;
3100 if (i == 0)
3101 return SP_FORMERROR;
3102 ftp->ft_to = read_cnt_string(fd, 1, &i);
3103 if (i <= 0)
3104 {
3105 vim_free(ftp->ft_from);
3106 if (i < 0)
3107 return i;
3108 return SP_FORMERROR;
3109 }
3110 }
3111
3112 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003113 for (i = 0; i < 256; ++i)
3114 first[i] = -1;
3115 for (i = 0; i < gap->ga_len; ++i)
3116 {
3117 ftp = &((fromto_T *)gap->ga_data)[i];
3118 if (first[*ftp->ft_from] == -1)
3119 first[*ftp->ft_from] = i;
3120 }
3121 return 0;
3122}
3123
3124/*
3125 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3126 * Return SP_*ERROR flags.
3127 */
3128 static int
3129read_sal_section(fd, slang)
3130 FILE *fd;
3131 slang_T *slang;
3132{
3133 int i;
3134 int cnt;
3135 garray_T *gap;
3136 salitem_T *smp;
3137 int ccnt;
3138 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003139 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003140
3141 slang->sl_sofo = FALSE;
3142
3143 i = getc(fd); /* <salflags> */
3144 if (i & SAL_F0LLOWUP)
3145 slang->sl_followup = TRUE;
3146 if (i & SAL_COLLAPSE)
3147 slang->sl_collapse = TRUE;
3148 if (i & SAL_REM_ACCENTS)
3149 slang->sl_rem_accents = TRUE;
3150
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003151 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003152 if (cnt < 0)
3153 return SP_TRUNCERROR;
3154
3155 gap = &slang->sl_sal;
3156 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003157 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003158 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159
3160 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3161 for (; gap->ga_len < cnt; ++gap->ga_len)
3162 {
3163 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3164 ccnt = getc(fd); /* <salfromlen> */
3165 if (ccnt < 0)
3166 return SP_TRUNCERROR;
3167 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003168 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003169 smp->sm_lead = p;
3170
3171 /* Read up to the first special char into sm_lead. */
3172 for (i = 0; i < ccnt; ++i)
3173 {
3174 c = getc(fd); /* <salfrom> */
3175 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3176 break;
3177 *p++ = c;
3178 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003179 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003180 *p++ = NUL;
3181
3182 /* Put (abc) chars in sm_oneof, if any. */
3183 if (c == '(')
3184 {
3185 smp->sm_oneof = p;
3186 for (++i; i < ccnt; ++i)
3187 {
3188 c = getc(fd); /* <salfrom> */
3189 if (c == ')')
3190 break;
3191 *p++ = c;
3192 }
3193 *p++ = NUL;
3194 if (++i < ccnt)
3195 c = getc(fd);
3196 }
3197 else
3198 smp->sm_oneof = NULL;
3199
3200 /* Any following chars go in sm_rules. */
3201 smp->sm_rules = p;
3202 if (i < ccnt)
3203 /* store the char we got while checking for end of sm_lead */
3204 *p++ = c;
3205 for (++i; i < ccnt; ++i)
3206 *p++ = getc(fd); /* <salfrom> */
3207 *p++ = NUL;
3208
3209 /* <saltolen> <salto> */
3210 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3211 if (ccnt < 0)
3212 {
3213 vim_free(smp->sm_lead);
3214 return ccnt;
3215 }
3216
3217#ifdef FEAT_MBYTE
3218 if (has_mbyte)
3219 {
3220 /* convert the multi-byte strings to wide char strings */
3221 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3222 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3223 if (smp->sm_oneof == NULL)
3224 smp->sm_oneof_w = NULL;
3225 else
3226 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3227 if (smp->sm_to == NULL)
3228 smp->sm_to_w = NULL;
3229 else
3230 smp->sm_to_w = mb_str2wide(smp->sm_to);
3231 if (smp->sm_lead_w == NULL
3232 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3233 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3234 {
3235 vim_free(smp->sm_lead);
3236 vim_free(smp->sm_to);
3237 vim_free(smp->sm_lead_w);
3238 vim_free(smp->sm_oneof_w);
3239 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003240 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003241 }
3242 }
3243#endif
3244 }
3245
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003246 if (gap->ga_len > 0)
3247 {
3248 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3249 * that we need to check the index every time. */
3250 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3251 if ((p = alloc(1)) == NULL)
3252 return SP_OTHERERROR;
3253 p[0] = NUL;
3254 smp->sm_lead = p;
3255 smp->sm_leadlen = 0;
3256 smp->sm_oneof = NULL;
3257 smp->sm_rules = p;
3258 smp->sm_to = NULL;
3259#ifdef FEAT_MBYTE
3260 if (has_mbyte)
3261 {
3262 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3263 smp->sm_leadlen = 0;
3264 smp->sm_oneof_w = NULL;
3265 smp->sm_to_w = NULL;
3266 }
3267#endif
3268 ++gap->ga_len;
3269 }
3270
Bram Moolenaar5195e452005-08-19 20:32:47 +00003271 /* Fill the first-index table. */
3272 set_sal_first(slang);
3273
3274 return 0;
3275}
3276
3277/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003278 * Read SN_WORDS: <word> ...
3279 * Return SP_*ERROR flags.
3280 */
3281 static int
3282read_words_section(fd, lp, len)
3283 FILE *fd;
3284 slang_T *lp;
3285 int len;
3286{
3287 int done = 0;
3288 int i;
3289 char_u word[MAXWLEN];
3290
3291 while (done < len)
3292 {
3293 /* Read one word at a time. */
3294 for (i = 0; ; ++i)
3295 {
3296 word[i] = getc(fd);
3297 if (word[i] == NUL)
3298 break;
3299 if (i == MAXWLEN - 1)
3300 return SP_FORMERROR;
3301 }
3302
3303 /* Init the count to 10. */
3304 count_common_word(lp, word, -1, 10);
3305 done += i + 1;
3306 }
3307 return 0;
3308}
3309
3310/*
3311 * Add a word to the hashtable of common words.
3312 * If it's already there then the counter is increased.
3313 */
3314 static void
3315count_common_word(lp, word, len, count)
3316 slang_T *lp;
3317 char_u *word;
3318 int len; /* word length, -1 for upto NUL */
3319 int count; /* 1 to count once, 10 to init */
3320{
3321 hash_T hash;
3322 hashitem_T *hi;
3323 wordcount_T *wc;
3324 char_u buf[MAXWLEN];
3325 char_u *p;
3326
3327 if (len == -1)
3328 p = word;
3329 else
3330 {
3331 vim_strncpy(buf, word, len);
3332 p = buf;
3333 }
3334
3335 hash = hash_hash(p);
3336 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3337 if (HASHITEM_EMPTY(hi))
3338 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003339 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003340 if (wc == NULL)
3341 return;
3342 STRCPY(wc->wc_word, p);
3343 wc->wc_count = count;
3344 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3345 }
3346 else
3347 {
3348 wc = HI2WC(hi);
3349 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3350 wc->wc_count = MAXWORDCOUNT;
3351 }
3352}
3353
3354/*
3355 * Adjust the score of common words.
3356 */
3357 static int
3358score_wordcount_adj(slang, score, word, split)
3359 slang_T *slang;
3360 int score;
3361 char_u *word;
3362 int split; /* word was split, less bonus */
3363{
3364 hashitem_T *hi;
3365 wordcount_T *wc;
3366 int bonus;
3367 int newscore;
3368
3369 hi = hash_find(&slang->sl_wordcount, word);
3370 if (!HASHITEM_EMPTY(hi))
3371 {
3372 wc = HI2WC(hi);
3373 if (wc->wc_count < SCORE_THRES2)
3374 bonus = SCORE_COMMON1;
3375 else if (wc->wc_count < SCORE_THRES3)
3376 bonus = SCORE_COMMON2;
3377 else
3378 bonus = SCORE_COMMON3;
3379 if (split)
3380 newscore = score - bonus / 2;
3381 else
3382 newscore = score - bonus;
3383 if (newscore < 0)
3384 return 0;
3385 return newscore;
3386 }
3387 return score;
3388}
3389
3390/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003391 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3392 * Return SP_*ERROR flags.
3393 */
3394 static int
3395read_sofo_section(fd, slang)
3396 FILE *fd;
3397 slang_T *slang;
3398{
3399 int cnt;
3400 char_u *from, *to;
3401 int res;
3402
3403 slang->sl_sofo = TRUE;
3404
3405 /* <sofofromlen> <sofofrom> */
3406 from = read_cnt_string(fd, 2, &cnt);
3407 if (cnt < 0)
3408 return cnt;
3409
3410 /* <sofotolen> <sofoto> */
3411 to = read_cnt_string(fd, 2, &cnt);
3412 if (cnt < 0)
3413 {
3414 vim_free(from);
3415 return cnt;
3416 }
3417
3418 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3419 if (from != NULL && to != NULL)
3420 res = set_sofo(slang, from, to);
3421 else if (from != NULL || to != NULL)
3422 res = SP_FORMERROR; /* only one of two strings is an error */
3423 else
3424 res = 0;
3425
3426 vim_free(from);
3427 vim_free(to);
3428 return res;
3429}
3430
3431/*
3432 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003433 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003434 * Returns SP_*ERROR flags.
3435 */
3436 static int
3437read_compound(fd, slang, len)
3438 FILE *fd;
3439 slang_T *slang;
3440 int len;
3441{
3442 int todo = len;
3443 int c;
3444 int atstart;
3445 char_u *pat;
3446 char_u *pp;
3447 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003448 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003449 int cnt;
3450 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003451
3452 if (todo < 2)
3453 return SP_FORMERROR; /* need at least two bytes */
3454
3455 --todo;
3456 c = getc(fd); /* <compmax> */
3457 if (c < 2)
3458 c = MAXWLEN;
3459 slang->sl_compmax = c;
3460
3461 --todo;
3462 c = getc(fd); /* <compminlen> */
3463 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003464 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003465 slang->sl_compminlen = c;
3466
3467 --todo;
3468 c = getc(fd); /* <compsylmax> */
3469 if (c < 1)
3470 c = MAXWLEN;
3471 slang->sl_compsylmax = c;
3472
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003473 c = getc(fd); /* <compoptions> */
3474 if (c != 0)
3475 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3476 else
3477 {
3478 --todo;
3479 c = getc(fd); /* only use the lower byte for now */
3480 --todo;
3481 slang->sl_compoptions = c;
3482
3483 gap = &slang->sl_comppat;
3484 c = get2c(fd); /* <comppatcount> */
3485 todo -= 2;
3486 ga_init2(gap, sizeof(char_u *), c);
3487 if (ga_grow(gap, c) == OK)
3488 while (--c >= 0)
3489 {
3490 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3491 read_cnt_string(fd, 1, &cnt);
3492 /* <comppatlen> <comppattext> */
3493 if (cnt < 0)
3494 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003495 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003496 }
3497 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003498 if (todo < 0)
3499 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003500
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003502 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003503 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3504 * Conversion to utf-8 may double the size. */
3505 c = todo * 2 + 7;
3506#ifdef FEAT_MBYTE
3507 if (enc_utf8)
3508 c += todo * 2;
3509#endif
3510 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003511 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003512 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003513
Bram Moolenaard12a1322005-08-21 22:08:24 +00003514 /* We also need a list of all flags that can appear at the start and one
3515 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003516 cp = alloc(todo + 1);
3517 if (cp == NULL)
3518 {
3519 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003520 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003521 }
3522 slang->sl_compstartflags = cp;
3523 *cp = NUL;
3524
Bram Moolenaard12a1322005-08-21 22:08:24 +00003525 ap = alloc(todo + 1);
3526 if (ap == NULL)
3527 {
3528 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003529 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003530 }
3531 slang->sl_compallflags = ap;
3532 *ap = NUL;
3533
Bram Moolenaar5195e452005-08-19 20:32:47 +00003534 pp = pat;
3535 *pp++ = '^';
3536 *pp++ = '\\';
3537 *pp++ = '(';
3538
3539 atstart = 1;
3540 while (todo-- > 0)
3541 {
3542 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003543
3544 /* Add all flags to "sl_compallflags". */
3545 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003546 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003547 {
3548 *ap++ = c;
3549 *ap = NUL;
3550 }
3551
Bram Moolenaar5195e452005-08-19 20:32:47 +00003552 if (atstart != 0)
3553 {
3554 /* At start of item: copy flags to "sl_compstartflags". For a
3555 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3556 if (c == '[')
3557 atstart = 2;
3558 else if (c == ']')
3559 atstart = 0;
3560 else
3561 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003562 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003563 {
3564 *cp++ = c;
3565 *cp = NUL;
3566 }
3567 if (atstart == 1)
3568 atstart = 0;
3569 }
3570 }
3571 if (c == '/') /* slash separates two items */
3572 {
3573 *pp++ = '\\';
3574 *pp++ = '|';
3575 atstart = 1;
3576 }
3577 else /* normal char, "[abc]" and '*' are copied as-is */
3578 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003579 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003580 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003581#ifdef FEAT_MBYTE
3582 if (enc_utf8)
3583 pp += mb_char2bytes(c, pp);
3584 else
3585#endif
3586 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003587 }
3588 }
3589
3590 *pp++ = '\\';
3591 *pp++ = ')';
3592 *pp++ = '$';
3593 *pp = NUL;
3594
3595 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3596 vim_free(pat);
3597 if (slang->sl_compprog == NULL)
3598 return SP_FORMERROR;
3599
3600 return 0;
3601}
3602
Bram Moolenaar6de68532005-08-24 22:08:48 +00003603/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003604 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003605 * Like strchr() but independent of locale.
3606 */
3607 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003608byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003609 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003610 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003611{
3612 char_u *p;
3613
3614 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003615 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003616 return TRUE;
3617 return FALSE;
3618}
3619
Bram Moolenaar5195e452005-08-19 20:32:47 +00003620#define SY_MAXLEN 30
3621typedef struct syl_item_S
3622{
3623 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3624 int sy_len;
3625} syl_item_T;
3626
3627/*
3628 * Truncate "slang->sl_syllable" at the first slash and put the following items
3629 * in "slang->sl_syl_items".
3630 */
3631 static int
3632init_syl_tab(slang)
3633 slang_T *slang;
3634{
3635 char_u *p;
3636 char_u *s;
3637 int l;
3638 syl_item_T *syl;
3639
3640 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3641 p = vim_strchr(slang->sl_syllable, '/');
3642 while (p != NULL)
3643 {
3644 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003645 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003646 break;
3647 s = p;
3648 p = vim_strchr(p, '/');
3649 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003650 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003651 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003652 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003653 if (l >= SY_MAXLEN)
3654 return SP_FORMERROR;
3655 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003656 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003657 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3658 + slang->sl_syl_items.ga_len++;
3659 vim_strncpy(syl->sy_chars, s, l);
3660 syl->sy_len = l;
3661 }
3662 return OK;
3663}
3664
3665/*
3666 * Count the number of syllables in "word".
3667 * When "word" contains spaces the syllables after the last space are counted.
3668 * Returns zero if syllables are not defines.
3669 */
3670 static int
3671count_syllables(slang, word)
3672 slang_T *slang;
3673 char_u *word;
3674{
3675 int cnt = 0;
3676 int skip = FALSE;
3677 char_u *p;
3678 int len;
3679 int i;
3680 syl_item_T *syl;
3681 int c;
3682
3683 if (slang->sl_syllable == NULL)
3684 return 0;
3685
3686 for (p = word; *p != NUL; p += len)
3687 {
3688 /* When running into a space reset counter. */
3689 if (*p == ' ')
3690 {
3691 len = 1;
3692 cnt = 0;
3693 continue;
3694 }
3695
3696 /* Find longest match of syllable items. */
3697 len = 0;
3698 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3699 {
3700 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3701 if (syl->sy_len > len
3702 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3703 len = syl->sy_len;
3704 }
3705 if (len != 0) /* found a match, count syllable */
3706 {
3707 ++cnt;
3708 skip = FALSE;
3709 }
3710 else
3711 {
3712 /* No recognized syllable item, at least a syllable char then? */
3713#ifdef FEAT_MBYTE
3714 c = mb_ptr2char(p);
3715 len = (*mb_ptr2len)(p);
3716#else
3717 c = *p;
3718 len = 1;
3719#endif
3720 if (vim_strchr(slang->sl_syllable, c) == NULL)
3721 skip = FALSE; /* No, search for next syllable */
3722 else if (!skip)
3723 {
3724 ++cnt; /* Yes, count it */
3725 skip = TRUE; /* don't count following syllable chars */
3726 }
3727 }
3728 }
3729 return cnt;
3730}
3731
3732/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003733 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003734 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003735 */
3736 static int
3737set_sofo(lp, from, to)
3738 slang_T *lp;
3739 char_u *from;
3740 char_u *to;
3741{
3742 int i;
3743
3744#ifdef FEAT_MBYTE
3745 garray_T *gap;
3746 char_u *s;
3747 char_u *p;
3748 int c;
3749 int *inp;
3750
3751 if (has_mbyte)
3752 {
3753 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3754 * characters. The index is the low byte of the character.
3755 * The list contains from-to pairs with a terminating NUL.
3756 * sl_sal_first[] is used for latin1 "from" characters. */
3757 gap = &lp->sl_sal;
3758 ga_init2(gap, sizeof(int *), 1);
3759 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003760 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003761 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3762 gap->ga_len = 256;
3763
3764 /* First count the number of items for each list. Temporarily use
3765 * sl_sal_first[] for this. */
3766 for (p = from, s = to; *p != NUL && *s != NUL; )
3767 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003768 c = mb_cptr2char_adv(&p);
3769 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003770 if (c >= 256)
3771 ++lp->sl_sal_first[c & 0xff];
3772 }
3773 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003774 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003775
3776 /* Allocate the lists. */
3777 for (i = 0; i < 256; ++i)
3778 if (lp->sl_sal_first[i] > 0)
3779 {
3780 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3781 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003782 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003783 ((int **)gap->ga_data)[i] = (int *)p;
3784 *(int *)p = 0;
3785 }
3786
3787 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3788 * list. */
3789 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3790 for (p = from, s = to; *p != NUL && *s != NUL; )
3791 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003792 c = mb_cptr2char_adv(&p);
3793 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003794 if (c >= 256)
3795 {
3796 /* Append the from-to chars at the end of the list with
3797 * the low byte. */
3798 inp = ((int **)gap->ga_data)[c & 0xff];
3799 while (*inp != 0)
3800 ++inp;
3801 *inp++ = c; /* from char */
3802 *inp++ = i; /* to char */
3803 *inp++ = NUL; /* NUL at the end */
3804 }
3805 else
3806 /* mapping byte to char is done in sl_sal_first[] */
3807 lp->sl_sal_first[c] = i;
3808 }
3809 }
3810 else
3811#endif
3812 {
3813 /* mapping bytes to bytes is done in sl_sal_first[] */
3814 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003815 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003816
3817 for (i = 0; to[i] != NUL; ++i)
3818 lp->sl_sal_first[from[i]] = to[i];
3819 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3820 }
3821
Bram Moolenaar5195e452005-08-19 20:32:47 +00003822 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003823}
3824
3825/*
3826 * Fill the first-index table for "lp".
3827 */
3828 static void
3829set_sal_first(lp)
3830 slang_T *lp;
3831{
3832 salfirst_T *sfirst;
3833 int i;
3834 salitem_T *smp;
3835 int c;
3836 garray_T *gap = &lp->sl_sal;
3837
3838 sfirst = lp->sl_sal_first;
3839 for (i = 0; i < 256; ++i)
3840 sfirst[i] = -1;
3841 smp = (salitem_T *)gap->ga_data;
3842 for (i = 0; i < gap->ga_len; ++i)
3843 {
3844#ifdef FEAT_MBYTE
3845 if (has_mbyte)
3846 /* Use the lowest byte of the first character. For latin1 it's
3847 * the character, for other encodings it should differ for most
3848 * characters. */
3849 c = *smp[i].sm_lead_w & 0xff;
3850 else
3851#endif
3852 c = *smp[i].sm_lead;
3853 if (sfirst[c] == -1)
3854 {
3855 sfirst[c] = i;
3856#ifdef FEAT_MBYTE
3857 if (has_mbyte)
3858 {
3859 int n;
3860
3861 /* Make sure all entries with this byte are following each
3862 * other. Move the ones that are in the wrong position. Do
3863 * keep the same ordering! */
3864 while (i + 1 < gap->ga_len
3865 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3866 /* Skip over entry with same index byte. */
3867 ++i;
3868
3869 for (n = 1; i + n < gap->ga_len; ++n)
3870 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3871 {
3872 salitem_T tsal;
3873
3874 /* Move entry with same index byte after the entries
3875 * we already found. */
3876 ++i;
3877 --n;
3878 tsal = smp[i + n];
3879 mch_memmove(smp + i + 1, smp + i,
3880 sizeof(salitem_T) * n);
3881 smp[i] = tsal;
3882 }
3883 }
3884#endif
3885 }
3886 }
3887}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003888
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003889#ifdef FEAT_MBYTE
3890/*
3891 * Turn a multi-byte string into a wide character string.
3892 * Return it in allocated memory (NULL for out-of-memory)
3893 */
3894 static int *
3895mb_str2wide(s)
3896 char_u *s;
3897{
3898 int *res;
3899 char_u *p;
3900 int i = 0;
3901
3902 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3903 if (res != NULL)
3904 {
3905 for (p = s; *p != NUL; )
3906 res[i++] = mb_ptr2char_adv(&p);
3907 res[i] = NUL;
3908 }
3909 return res;
3910}
3911#endif
3912
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003914 * Read a tree from the .spl or .sug file.
3915 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3916 * This is skipped when the tree has zero length.
3917 * Returns zero when OK, SP_ value for an error.
3918 */
3919 static int
3920spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3921 FILE *fd;
3922 char_u **bytsp;
3923 idx_T **idxsp;
3924 int prefixtree; /* TRUE for the prefix tree */
3925 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3926{
3927 int len;
3928 int idx;
3929 char_u *bp;
3930 idx_T *ip;
3931
3932 /* The tree size was computed when writing the file, so that we can
3933 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003934 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003935 if (len < 0)
3936 return SP_TRUNCERROR;
3937 if (len > 0)
3938 {
3939 /* Allocate the byte array. */
3940 bp = lalloc((long_u)len, TRUE);
3941 if (bp == NULL)
3942 return SP_OTHERERROR;
3943 *bytsp = bp;
3944
3945 /* Allocate the index array. */
3946 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3947 if (ip == NULL)
3948 return SP_OTHERERROR;
3949 *idxsp = ip;
3950
3951 /* Recursively read the tree and store it in the array. */
3952 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3953 if (idx < 0)
3954 return idx;
3955 }
3956 return 0;
3957}
3958
3959/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003960 * Read one row of siblings from the spell file and store it in the byte array
3961 * "byts" and index array "idxs". Recursively read the children.
3962 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003963 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003964 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003965 * Returns the index (>= 0) following the siblings.
3966 * Returns SP_TRUNCERROR if the file is shorter than expected.
3967 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003968 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003969 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003970read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003971 FILE *fd;
3972 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003973 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003974 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003975 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003976 int prefixtree; /* TRUE for reading PREFIXTREE */
3977 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003978{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003979 int len;
3980 int i;
3981 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003982 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003984 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003985#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003986
Bram Moolenaar51485f02005-06-04 21:55:20 +00003987 len = getc(fd); /* <siblingcount> */
3988 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003989 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003990
3991 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003992 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003993 byts[idx++] = len;
3994
3995 /* Read the byte values, flag/region bytes and shared indexes. */
3996 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003997 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003998 c = getc(fd); /* <byte> */
3999 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004000 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004001 if (c <= BY_SPECIAL)
4002 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004003 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004004 {
4005 /* No flags, all regions. */
4006 idxs[idx] = 0;
4007 c = 0;
4008 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004009 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004010 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004011 if (prefixtree)
4012 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004013 /* Read the optional pflags byte, the prefix ID and the
4014 * condition nr. In idxs[] store the prefix ID in the low
4015 * byte, the condition index shifted up 8 bits, the flags
4016 * shifted up 24 bits. */
4017 if (c == BY_FLAGS)
4018 c = getc(fd) << 24; /* <pflags> */
4019 else
4020 c = 0;
4021
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004022 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004023
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004024 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004025 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004026 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004027 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004028 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004029 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004030 {
4031 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004032 * idxs[] the flags go in the low two bytes, region above
4033 * that and prefix ID above the region. */
4034 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004036 if (c2 == BY_FLAGS2)
4037 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004038 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004039 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004040 if (c & WF_AFX)
4041 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004042 }
4043
Bram Moolenaar51485f02005-06-04 21:55:20 +00004044 idxs[idx] = c;
4045 c = 0;
4046 }
4047 else /* c == BY_INDEX */
4048 {
4049 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004050 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004051 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004052 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004053 idxs[idx] = n + SHARED_MASK;
4054 c = getc(fd); /* <xbyte> */
4055 }
4056 }
4057 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058 }
4059
Bram Moolenaar51485f02005-06-04 21:55:20 +00004060 /* Recursively read the children for non-shared siblings.
4061 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4062 * remove SHARED_MASK) */
4063 for (i = 1; i <= len; ++i)
4064 if (byts[startidx + i] != 0)
4065 {
4066 if (idxs[startidx + i] & SHARED_MASK)
4067 idxs[startidx + i] &= ~SHARED_MASK;
4068 else
4069 {
4070 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004071 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004072 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 if (idx < 0)
4074 break;
4075 }
4076 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077
Bram Moolenaar51485f02005-06-04 21:55:20 +00004078 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079}
4080
4081/*
4082 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004083 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084 */
4085 char_u *
4086did_set_spelllang(buf)
4087 buf_T *buf;
4088{
4089 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004090 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004091 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004092 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004093 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004094 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004095 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004097 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004099 int len;
4100 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004101 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004102 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004103 char_u *use_region = NULL;
4104 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004105 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004106 int i, j;
4107 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004108
4109 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004110 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004111
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004112 /* loop over comma separated language names. */
4113 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004114 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004115 /* Get one language name. */
4116 copy_option_part(&splp, lang, MAXWLEN, ",");
4117
Bram Moolenaar5482f332005-04-17 20:18:43 +00004118 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004119 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004120
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004121 /* If the name ends in ".spl" use it as the name of the spell file.
4122 * If there is a region name let "region" point to it and remove it
4123 * from the name. */
4124 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4125 {
4126 filename = TRUE;
4127
Bram Moolenaarb6356332005-07-18 21:40:44 +00004128 /* Locate a region and remove it from the file name. */
4129 p = vim_strchr(gettail(lang), '_');
4130 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4131 && !ASCII_ISALPHA(p[3]))
4132 {
4133 vim_strncpy(region_cp, p + 1, 2);
4134 mch_memmove(p, p + 3, len - (p - lang) - 2);
4135 len -= 3;
4136 region = region_cp;
4137 }
4138 else
4139 dont_use_region = TRUE;
4140
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004141 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004142 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4143 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004144 break;
4145 }
4146 else
4147 {
4148 filename = FALSE;
4149 if (len > 3 && lang[len - 3] == '_')
4150 {
4151 region = lang + len - 2;
4152 len -= 3;
4153 lang[len] = NUL;
4154 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004155 else
4156 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004157
4158 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004159 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4160 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004161 break;
4162 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004163
Bram Moolenaarb6356332005-07-18 21:40:44 +00004164 if (region != NULL)
4165 {
4166 /* If the region differs from what was used before then don't
4167 * use it for 'spellfile'. */
4168 if (use_region != NULL && STRCMP(region, use_region) != 0)
4169 dont_use_region = TRUE;
4170 use_region = region;
4171 }
4172
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004173 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004174 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004175 {
4176 if (filename)
4177 (void)spell_load_file(lang, lang, NULL, FALSE);
4178 else
4179 spell_load_lang(lang);
4180 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004181
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004182 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004183 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004184 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004185 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4186 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4187 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004188 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004189 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004190 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004191 {
4192 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004193 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004194 if (c == REGION_ALL)
4195 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004196 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004197 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004198 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004199 /* This addition file is for other regions. */
4200 region_mask = 0;
4201 }
4202 else
4203 /* This is probably an error. Give a warning and
4204 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004205 smsg((char_u *)
4206 _("Warning: region %s not supported"),
4207 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004208 }
4209 else
4210 region_mask = 1 << c;
4211 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004212
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004213 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004214 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004215 if (ga_grow(&ga, 1) == FAIL)
4216 {
4217 ga_clear(&ga);
4218 return e_outofmem;
4219 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004220 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004221 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4222 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004223 use_midword(slang, buf);
4224 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004225 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004226 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004228 }
4229
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004230 /* round 0: load int_wordlist, if possible.
4231 * round 1: load first name in 'spellfile'.
4232 * round 2: load second name in 'spellfile.
4233 * etc. */
4234 spf = curbuf->b_p_spf;
4235 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004237 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004238 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004239 /* Internal wordlist, if there is one. */
4240 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004241 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004242 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004243 }
4244 else
4245 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004246 /* One entry in 'spellfile'. */
4247 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4248 STRCAT(spf_name, ".spl");
4249
4250 /* If it was already found above then skip it. */
4251 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004252 {
4253 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4254 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004255 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004256 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004257 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004258 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004259 }
4260
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004261 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4263 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004265 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004267 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004268 * region name, the region is ignored otherwise. for int_wordlist
4269 * use an arbitrary name. */
4270 if (round == 0)
4271 STRCPY(lang, "internal wordlist");
4272 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004273 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004274 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004275 p = vim_strchr(lang, '.');
4276 if (p != NULL)
4277 *p = NUL; /* truncate at ".encoding.add" */
4278 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004279 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004280
4281 /* If one of the languages has NOBREAK we assume the addition
4282 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004283 if (slang != NULL && nobreak)
4284 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004286 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004288 region_mask = REGION_ALL;
4289 if (use_region != NULL && !dont_use_region)
4290 {
4291 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004292 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004293 if (c != REGION_ALL)
4294 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004296 /* This spell file is for other regions. */
4297 region_mask = 0;
4298 }
4299
4300 if (region_mask != 0)
4301 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004302 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4303 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4304 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004305 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4306 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 }
4310 }
4311
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004312 /* Everything is fine, store the new b_langp value. */
4313 ga_clear(&buf->b_langp);
4314 buf->b_langp = ga;
4315
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316 /* For each language figure out what language to use for sound folding and
4317 * REP items. If the language doesn't support it itself use another one
4318 * with the same name. E.g. for "en-math" use "en". */
4319 for (i = 0; i < ga.ga_len; ++i)
4320 {
4321 lp = LANGP_ENTRY(ga, i);
4322
4323 /* sound folding */
4324 if (lp->lp_slang->sl_sal.ga_len > 0)
4325 /* language does sound folding itself */
4326 lp->lp_sallang = lp->lp_slang;
4327 else
4328 /* find first similar language that does sound folding */
4329 for (j = 0; j < ga.ga_len; ++j)
4330 {
4331 lp2 = LANGP_ENTRY(ga, j);
4332 if (lp2->lp_slang->sl_sal.ga_len > 0
4333 && STRNCMP(lp->lp_slang->sl_name,
4334 lp2->lp_slang->sl_name, 2) == 0)
4335 {
4336 lp->lp_sallang = lp2->lp_slang;
4337 break;
4338 }
4339 }
4340
4341 /* REP items */
4342 if (lp->lp_slang->sl_rep.ga_len > 0)
4343 /* language has REP items itself */
4344 lp->lp_replang = lp->lp_slang;
4345 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004346 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347 for (j = 0; j < ga.ga_len; ++j)
4348 {
4349 lp2 = LANGP_ENTRY(ga, j);
4350 if (lp2->lp_slang->sl_rep.ga_len > 0
4351 && STRNCMP(lp->lp_slang->sl_name,
4352 lp2->lp_slang->sl_name, 2) == 0)
4353 {
4354 lp->lp_replang = lp2->lp_slang;
4355 break;
4356 }
4357 }
4358 }
4359
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004360 return NULL;
4361}
4362
4363/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004364 * Clear the midword characters for buffer "buf".
4365 */
4366 static void
4367clear_midword(buf)
4368 buf_T *buf;
4369{
4370 vim_memset(buf->b_spell_ismw, 0, 256);
4371#ifdef FEAT_MBYTE
4372 vim_free(buf->b_spell_ismw_mb);
4373 buf->b_spell_ismw_mb = NULL;
4374#endif
4375}
4376
4377/*
4378 * Use the "sl_midword" field of language "lp" for buffer "buf".
4379 * They add up to any currently used midword characters.
4380 */
4381 static void
4382use_midword(lp, buf)
4383 slang_T *lp;
4384 buf_T *buf;
4385{
4386 char_u *p;
4387
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004388 if (lp->sl_midword == NULL) /* there aren't any */
4389 return;
4390
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004391 for (p = lp->sl_midword; *p != NUL; )
4392#ifdef FEAT_MBYTE
4393 if (has_mbyte)
4394 {
4395 int c, l, n;
4396 char_u *bp;
4397
4398 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004399 l = (*mb_ptr2len)(p);
4400 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004401 buf->b_spell_ismw[c] = TRUE;
4402 else if (buf->b_spell_ismw_mb == NULL)
4403 /* First multi-byte char in "b_spell_ismw_mb". */
4404 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4405 else
4406 {
4407 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004408 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004409 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4410 if (bp != NULL)
4411 {
4412 vim_free(buf->b_spell_ismw_mb);
4413 buf->b_spell_ismw_mb = bp;
4414 vim_strncpy(bp + n, p, l);
4415 }
4416 }
4417 p += l;
4418 }
4419 else
4420#endif
4421 buf->b_spell_ismw[*p++] = TRUE;
4422}
4423
4424/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004425 * Find the region "region[2]" in "rp" (points to "sl_regions").
4426 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004427 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004428 */
4429 static int
4430find_region(rp, region)
4431 char_u *rp;
4432 char_u *region;
4433{
4434 int i;
4435
4436 for (i = 0; ; i += 2)
4437 {
4438 if (rp[i] == NUL)
4439 return REGION_ALL;
4440 if (rp[i] == region[0] && rp[i + 1] == region[1])
4441 break;
4442 }
4443 return i / 2;
4444}
4445
4446/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004447 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004448 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004449 * Word WF_ONECAP
4450 * W WORD WF_ALLCAP
4451 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004452 */
4453 static int
4454captype(word, end)
4455 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004456 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004457{
4458 char_u *p;
4459 int c;
4460 int firstcap;
4461 int allcap;
4462 int past_second = FALSE; /* past second word char */
4463
4464 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004465 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004466 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004467 return 0; /* only non-word characters, illegal word */
4468#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004469 if (has_mbyte)
4470 c = mb_ptr2char_adv(&p);
4471 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004473 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004474 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475
4476 /*
4477 * Need to check all letters to find a word with mixed upper/lower.
4478 * But a word with an upper char only at start is a ONECAP.
4479 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004481 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004482 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004483 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004484 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004485 {
4486 /* UUl -> KEEPCAP */
4487 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004488 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004489 allcap = FALSE;
4490 }
4491 else if (!allcap)
4492 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004493 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004494 past_second = TRUE;
4495 }
4496
4497 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004498 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004499 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004500 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004501 return 0;
4502}
4503
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004504/*
4505 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4506 * capital. So that make_case_word() can turn WOrd into Word.
4507 * Add ALLCAP for "WOrD".
4508 */
4509 static int
4510badword_captype(word, end)
4511 char_u *word;
4512 char_u *end;
4513{
4514 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004515 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004516 int l, u;
4517 int first;
4518 char_u *p;
4519
4520 if (flags & WF_KEEPCAP)
4521 {
4522 /* Count the number of UPPER and lower case letters. */
4523 l = u = 0;
4524 first = FALSE;
4525 for (p = word; p < end; mb_ptr_adv(p))
4526 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004527 c = PTR2CHAR(p);
4528 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004529 {
4530 ++u;
4531 if (p == word)
4532 first = TRUE;
4533 }
4534 else
4535 ++l;
4536 }
4537
4538 /* If there are more UPPER than lower case letters suggest an
4539 * ALLCAP word. Otherwise, if the first letter is UPPER then
4540 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4541 * require three upper case letters. */
4542 if (u > l && u > 2)
4543 flags |= WF_ALLCAP;
4544 else if (first)
4545 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004546
4547 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4548 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004549 }
4550 return flags;
4551}
4552
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004553# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4554/*
4555 * Free all languages.
4556 */
4557 void
4558spell_free_all()
4559{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004560 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004561 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004562 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004563
4564 /* Go through all buffers and handle 'spelllang'. */
4565 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4566 ga_clear(&buf->b_langp);
4567
4568 while (first_lang != NULL)
4569 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004570 slang = first_lang;
4571 first_lang = slang->sl_next;
4572 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004573 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004574
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004575 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004576 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004577 /* Delete the internal wordlist and its .spl file */
4578 mch_remove(int_wordlist);
4579 int_wordlist_spl(fname);
4580 mch_remove(fname);
4581 vim_free(int_wordlist);
4582 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004583 }
4584
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004585 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004586
4587 vim_free(repl_to);
4588 repl_to = NULL;
4589 vim_free(repl_from);
4590 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004591}
4592# endif
4593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594# if defined(FEAT_MBYTE) || defined(PROTO)
4595/*
4596 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004597 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004598 */
4599 void
4600spell_reload()
4601{
4602 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004603 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004604
Bram Moolenaarea408852005-06-25 22:49:46 +00004605 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 init_spell_chartab();
4607
4608 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004609 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004610
4611 /* Go through all buffers and handle 'spelllang'. */
4612 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4613 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004614 /* Only load the wordlists when 'spelllang' is set and there is a
4615 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004616 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004617 {
4618 FOR_ALL_WINDOWS(wp)
4619 if (wp->w_buffer == buf && wp->w_p_spell)
4620 {
4621 (void)did_set_spelllang(buf);
4622# ifdef FEAT_WINDOWS
4623 break;
4624# endif
4625 }
4626 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004627 }
4628}
4629# endif
4630
Bram Moolenaarb765d632005-06-07 21:00:02 +00004631/*
4632 * Reload the spell file "fname" if it's loaded.
4633 */
4634 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004636 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004638{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004639 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004640 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004642 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004643 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004644 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004645 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004646 slang_clear(slang);
4647 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004648 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004649 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004650 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004652 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004654
4655 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004656 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004657 if (added_word && !didit)
4658 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004659}
4660
4661
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004662/*
4663 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004664 */
4665
Bram Moolenaar51485f02005-06-04 21:55:20 +00004666#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004667 and .dic file. */
4668/*
4669 * Main structure to store the contents of a ".aff" file.
4670 */
4671typedef struct afffile_S
4672{
4673 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004674 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004675 unsigned af_rare; /* RARE ID for rare word */
4676 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004677 unsigned af_bad; /* BAD ID for banned word */
4678 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004679 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004680 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004681 unsigned af_comproot; /* COMPOUNDROOT ID */
4682 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4683 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004684 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004685 int af_pfxpostpone; /* postpone prefixes without chop string and
4686 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004687 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4688 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004689 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004690} afffile_T;
4691
Bram Moolenaar6de68532005-08-24 22:08:48 +00004692#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004693#define AFT_LONG 1 /* flags are two characters */
4694#define AFT_CAPLONG 2 /* flags are one or two characters */
4695#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004696
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004697typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004698/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4699struct affentry_S
4700{
4701 affentry_T *ae_next; /* next affix with same name/number */
4702 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4703 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004704 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004705 char_u *ae_cond; /* condition (NULL for ".") */
4706 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004707 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4708 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004709};
4710
Bram Moolenaar6de68532005-08-24 22:08:48 +00004711#ifdef FEAT_MBYTE
4712# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4713#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004714# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004715#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004716
Bram Moolenaar51485f02005-06-04 21:55:20 +00004717/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4718typedef struct affheader_S
4719{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004720 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4721 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4722 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004723 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004724 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004725 affentry_T *ah_first; /* first affix entry */
4726} affheader_T;
4727
4728#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4729
Bram Moolenaar6de68532005-08-24 22:08:48 +00004730/* Flag used in compound items. */
4731typedef struct compitem_S
4732{
4733 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4734 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4735 int ci_newID; /* affix ID after renumbering. */
4736} compitem_T;
4737
4738#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4739
Bram Moolenaar51485f02005-06-04 21:55:20 +00004740/*
4741 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004742 * the need to keep track of each allocated thing, everything is freed all at
4743 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004744 */
4745#define SBLOCKSIZE 16000 /* size of sb_data */
4746typedef struct sblock_S sblock_T;
4747struct sblock_S
4748{
4749 sblock_T *sb_next; /* next block in list */
4750 int sb_used; /* nr of bytes already in use */
4751 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004752};
4753
4754/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004755 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004756 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004757typedef struct wordnode_S wordnode_T;
4758struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004759{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004760 union /* shared to save space */
4761 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004762 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004763 int index; /* index in written nodes (valid after first
4764 round) */
4765 } wn_u1;
4766 union /* shared to save space */
4767 {
4768 wordnode_T *next; /* next node with same hash key */
4769 wordnode_T *wnode; /* parent node that will write this node */
4770 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004771 wordnode_T *wn_child; /* child (next byte in word) */
4772 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4773 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004774 int wn_refs; /* Nr. of references to this node. Only
4775 relevant for first node in a list of
4776 siblings, in following siblings it is
4777 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004778 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004779
4780 /* Info for when "wn_byte" is NUL.
4781 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4782 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4783 * "wn_region" the LSW of the wordnr. */
4784 char_u wn_affixID; /* supported/required prefix ID or 0 */
4785 short_u wn_flags; /* WF_ flags */
4786 short wn_region; /* region mask */
4787
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004788#ifdef SPELL_PRINTTREE
4789 int wn_nr; /* sequence nr for printing */
4790#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004791};
4792
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004793#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4794
Bram Moolenaar51485f02005-06-04 21:55:20 +00004795#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004796
Bram Moolenaar51485f02005-06-04 21:55:20 +00004797/*
4798 * Info used while reading the spell files.
4799 */
4800typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004801{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004802 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004803 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004804
Bram Moolenaar51485f02005-06-04 21:55:20 +00004805 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004806 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004807
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004808 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004809
Bram Moolenaar4770d092006-01-12 23:22:24 +00004810 long si_sugtree; /* creating the soundfolding trie */
4811
Bram Moolenaar51485f02005-06-04 21:55:20 +00004812 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004813 long si_blocks_cnt; /* memory blocks allocated */
4814 long si_compress_cnt; /* words to add before lowering
4815 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004816 wordnode_T *si_first_free; /* List of nodes that have been freed during
4817 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004818 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004819#ifdef SPELL_PRINTTREE
4820 int si_wordnode_nr; /* sequence nr for nodes */
4821#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004822 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004823
Bram Moolenaar51485f02005-06-04 21:55:20 +00004824 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004825 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004826 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004827 int si_region; /* region mask */
4828 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004829 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004830 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004831 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004832 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004833 int si_region_count; /* number of regions supported (1 when there
4834 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004835 char_u si_region_name[16]; /* region names; used only if
4836 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004837
4838 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004839 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004840 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004841 char_u *si_sofofr; /* SOFOFROM text */
4842 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004843 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004844 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 int si_followup; /* soundsalike: ? */
4846 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004847 hashtab_T si_commonwords; /* hashtable for common words */
4848 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004849 int si_rem_accents; /* soundsalike: remove accents */
4850 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004851 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004852 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004853 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004854 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004855 int si_compoptions; /* COMP_ flags */
4856 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4857 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004858 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004859 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004860 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004861 garray_T si_prefcond; /* table with conditions for postponed
4862 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004863 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004864 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004865} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004866
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004867static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004868static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004869static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004870static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4871static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4872static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004873static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004874static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4875static void aff_check_number __ARGS((int spinval, int affval, char *name));
4876static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004877static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4879static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004880static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004881static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004882static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004883static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004884static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004885static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004886static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004887static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4888static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4889static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004890static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004891static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004892static 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 +00004893static 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 +00004894static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004895static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004896static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4897static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4898static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004899static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004900static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004901static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004902static void clear_node __ARGS((wordnode_T *node));
4903static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004904static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4905static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4906static int sug_maketable __ARGS((spellinfo_T *spin));
4907static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4908static int offset2bytes __ARGS((int nr, char_u *buf));
4909static int bytes2offset __ARGS((char_u **pp));
4910static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004911static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004912static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004913static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004914
Bram Moolenaar53805d12005-08-01 07:08:33 +00004915/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4916 * but it must be negative to indicate the prefix tree to tree_add_word().
4917 * Use a negative number with the lower 8 bits zero. */
4918#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004919
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004920/* flags for "condit" argument of store_aff_word() */
4921#define CONDIT_COMB 1 /* affix must combine */
4922#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4923#define CONDIT_SUF 4 /* add a suffix for matching flags */
4924#define CONDIT_AFF 8 /* word already has an affix */
4925
Bram Moolenaar5195e452005-08-19 20:32:47 +00004926/*
4927 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4928 */
4929static long compress_start = 30000; /* memory / SBLOCKSIZE */
4930static long compress_inc = 100; /* memory / SBLOCKSIZE */
4931static long compress_added = 500000; /* word count */
4932
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004933#ifdef SPELL_PRINTTREE
4934/*
4935 * For debugging the tree code: print the current tree in a (more or less)
4936 * readable format, so that we can see what happens when adding a word and/or
4937 * compressing the tree.
4938 * Based on code from Olaf Seibert.
4939 */
4940#define PRINTLINESIZE 1000
4941#define PRINTWIDTH 6
4942
4943#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4944 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4945
4946static char line1[PRINTLINESIZE];
4947static char line2[PRINTLINESIZE];
4948static char line3[PRINTLINESIZE];
4949
4950 static void
4951spell_clear_flags(wordnode_T *node)
4952{
4953 wordnode_T *np;
4954
4955 for (np = node; np != NULL; np = np->wn_sibling)
4956 {
4957 np->wn_u1.index = FALSE;
4958 spell_clear_flags(np->wn_child);
4959 }
4960}
4961
4962 static void
4963spell_print_node(wordnode_T *node, int depth)
4964{
4965 if (node->wn_u1.index)
4966 {
4967 /* Done this node before, print the reference. */
4968 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4969 PRINTSOME(line2, depth, " ", 0, 0);
4970 PRINTSOME(line3, depth, " ", 0, 0);
4971 msg(line1);
4972 msg(line2);
4973 msg(line3);
4974 }
4975 else
4976 {
4977 node->wn_u1.index = TRUE;
4978
4979 if (node->wn_byte != NUL)
4980 {
4981 if (node->wn_child != NULL)
4982 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4983 else
4984 /* Cannot happen? */
4985 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4986 }
4987 else
4988 PRINTSOME(line1, depth, " $ ", 0, 0);
4989
4990 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4991
4992 if (node->wn_sibling != NULL)
4993 PRINTSOME(line3, depth, " | ", 0, 0);
4994 else
4995 PRINTSOME(line3, depth, " ", 0, 0);
4996
4997 if (node->wn_byte == NUL)
4998 {
4999 msg(line1);
5000 msg(line2);
5001 msg(line3);
5002 }
5003
5004 /* do the children */
5005 if (node->wn_byte != NUL && node->wn_child != NULL)
5006 spell_print_node(node->wn_child, depth + 1);
5007
5008 /* do the siblings */
5009 if (node->wn_sibling != NULL)
5010 {
5011 /* get rid of all parent details except | */
5012 STRCPY(line1, line3);
5013 STRCPY(line2, line3);
5014 spell_print_node(node->wn_sibling, depth);
5015 }
5016 }
5017}
5018
5019 static void
5020spell_print_tree(wordnode_T *root)
5021{
5022 if (root != NULL)
5023 {
5024 /* Clear the "wn_u1.index" fields, used to remember what has been
5025 * done. */
5026 spell_clear_flags(root);
5027
5028 /* Recursively print the tree. */
5029 spell_print_node(root, 0);
5030 }
5031}
5032#endif /* SPELL_PRINTTREE */
5033
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005034/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005035 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005036 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005037 */
5038 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005039spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005040 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005041 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005042{
5043 FILE *fd;
5044 afffile_T *aff;
5045 char_u rline[MAXLINELEN];
5046 char_u *line;
5047 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005048#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005049 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005050 int itemcnt;
5051 char_u *p;
5052 int lnum = 0;
5053 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005054 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005055 int aff_todo = 0;
5056 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005057 char_u *low = NULL;
5058 char_u *fol = NULL;
5059 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005060 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005061 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005062 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005063 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005064 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005065 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005066 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005067 int compminlen = 0; /* COMPOUNDMIN value */
5068 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005069 int compoptions = 0; /* COMP_ flags */
5070 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005071 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005072 concatenated */
5073 char_u *midword = NULL; /* MIDWORD value */
5074 char_u *syllable = NULL; /* SYLLABLE value */
5075 char_u *sofofrom = NULL; /* SOFOFROM value */
5076 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005077
Bram Moolenaar51485f02005-06-04 21:55:20 +00005078 /*
5079 * Open the file.
5080 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005081 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005082 if (fd == NULL)
5083 {
5084 EMSG2(_(e_notopen), fname);
5085 return NULL;
5086 }
5087
Bram Moolenaar4770d092006-01-12 23:22:24 +00005088 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5089 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005091 /* Only do REP lines when not done in another .aff file already. */
5092 do_rep = spin->si_rep.ga_len == 0;
5093
Bram Moolenaar4770d092006-01-12 23:22:24 +00005094 /* Only do REPSAL lines when not done in another .aff file already. */
5095 do_repsal = spin->si_repsal.ga_len == 0;
5096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097 /* Only do SAL lines when not done in another .aff file already. */
5098 do_sal = spin->si_sal.ga_len == 0;
5099
5100 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005101 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005102
Bram Moolenaar51485f02005-06-04 21:55:20 +00005103 /*
5104 * Allocate and init the afffile_T structure.
5105 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005106 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005107 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005108 {
5109 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005110 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005111 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005112 hash_init(&aff->af_pref);
5113 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005114 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005115
5116 /*
5117 * Read all the lines in the file one by one.
5118 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005119 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005120 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005121 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005122 ++lnum;
5123
5124 /* Skip comment lines. */
5125 if (*rline == '#')
5126 continue;
5127
5128 /* Convert from "SET" to 'encoding' when needed. */
5129 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005130#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005131 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005132 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005133 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005134 if (pc == NULL)
5135 {
5136 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5137 fname, lnum, rline);
5138 continue;
5139 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005140 line = pc;
5141 }
5142 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005143#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005144 {
5145 pc = NULL;
5146 line = rline;
5147 }
5148
5149 /* Split the line up in white separated items. Put a NUL after each
5150 * item. */
5151 itemcnt = 0;
5152 for (p = line; ; )
5153 {
5154 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5155 ++p;
5156 if (*p == NUL)
5157 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005158 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005159 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005160 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005161 /* A few items have arbitrary text argument, don't split them. */
5162 if (itemcnt == 2 && spell_info_item(items[0]))
5163 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5164 ++p;
5165 else
5166 while (*p > ' ') /* skip until white space or CR/NL */
5167 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005168 if (*p == NUL)
5169 break;
5170 *p++ = NUL;
5171 }
5172
5173 /* Handle non-empty lines. */
5174 if (itemcnt > 0)
5175 {
5176 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5177 && aff->af_enc == NULL)
5178 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005179#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005180 /* Setup for conversion from "ENC" to 'encoding'. */
5181 aff->af_enc = enc_canonize(items[1]);
5182 if (aff->af_enc != NULL && !spin->si_ascii
5183 && convert_setup(&spin->si_conv, aff->af_enc,
5184 p_enc) == FAIL)
5185 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5186 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005187 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005188#else
5189 smsg((char_u *)_("Conversion in %s not supported"), fname);
5190#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005191 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005192 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5193 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005194 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005195 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005196 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005197 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005198 aff->af_flagtype = AFT_NUM;
5199 else if (STRCMP(items[1], "caplong") == 0)
5200 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005201 else
5202 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5203 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005204 if (aff->af_rare != 0
5205 || aff->af_keepcase != 0
5206 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005207 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005208 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005209 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005210 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005211 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005212 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005213 || aff->af_suff.ht_used > 0
5214 || aff->af_pref.ht_used > 0)
5215 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5216 fname, lnum, items[1]);
5217 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005218 else if (spell_info_item(items[0]))
5219 {
5220 p = (char_u *)getroom(spin,
5221 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5222 + STRLEN(items[0])
5223 + STRLEN(items[1]) + 3, FALSE);
5224 if (p != NULL)
5225 {
5226 if (spin->si_info != NULL)
5227 {
5228 STRCPY(p, spin->si_info);
5229 STRCAT(p, "\n");
5230 }
5231 STRCAT(p, items[0]);
5232 STRCAT(p, " ");
5233 STRCAT(p, items[1]);
5234 spin->si_info = p;
5235 }
5236 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005237 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5238 && midword == NULL)
5239 {
5240 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005245 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005246 /* TODO: remove "RAR" later */
5247 else if ((STRCMP(items[0], "RAR") == 0
5248 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5249 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005250 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005251 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005252 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005253 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005254 /* TODO: remove "KEP" later */
5255 else if ((STRCMP(items[0], "KEP") == 0
5256 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5257 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005258 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005259 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005260 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005261 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005262 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5263 && aff->af_bad == 0)
5264 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005265 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5266 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005267 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005268 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5269 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005270 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005271 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5272 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005273 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005274 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5275 && aff->af_circumfix == 0)
5276 {
5277 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5278 fname, lnum);
5279 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005280 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5281 && aff->af_nosuggest == 0)
5282 {
5283 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5284 fname, lnum);
5285 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005286 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5287 && aff->af_needcomp == 0)
5288 {
5289 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5290 fname, lnum);
5291 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005292 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5293 && aff->af_comproot == 0)
5294 {
5295 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5296 fname, lnum);
5297 }
5298 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5299 && itemcnt == 2 && aff->af_compforbid == 0)
5300 {
5301 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5302 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005303 if (aff->af_pref.ht_used > 0)
5304 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5305 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005306 }
5307 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5308 && itemcnt == 2 && aff->af_comppermit == 0)
5309 {
5310 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5311 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005312 if (aff->af_pref.ht_used > 0)
5313 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5314 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005315 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005316 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005317 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005318 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005319 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005320 * "Na" into "Na+", "1234" into "1234+". */
5321 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005322 if (p != NULL)
5323 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005324 STRCPY(p, items[1]);
5325 STRCAT(p, "+");
5326 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005327 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005328 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005329 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005330 {
5331 /* Concatenate this string to previously defined ones, using a
5332 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005333 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005334 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005335 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005336 p = getroom(spin, l, FALSE);
5337 if (p != NULL)
5338 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005339 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005340 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005341 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005342 STRCAT(p, "/");
5343 }
5344 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005345 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005346 }
5347 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005348 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005349 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005350 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005351 compmax = atoi((char *)items[1]);
5352 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005353 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005354 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005355 }
5356 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005357 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005358 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005359 compminlen = atoi((char *)items[1]);
5360 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005361 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5362 fname, lnum, items[1]);
5363 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005364 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005365 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005366 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005367 compsylmax = atoi((char *)items[1]);
5368 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005369 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5370 fname, lnum, items[1]);
5371 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005372 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5373 {
5374 compoptions |= COMP_CHECKDUP;
5375 }
5376 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5377 {
5378 compoptions |= COMP_CHECKREP;
5379 }
5380 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5381 {
5382 compoptions |= COMP_CHECKCASE;
5383 }
5384 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5385 && itemcnt == 1)
5386 {
5387 compoptions |= COMP_CHECKTRIPLE;
5388 }
5389 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5390 && itemcnt == 2)
5391 {
5392 if (atoi((char *)items[1]) == 0)
5393 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5394 fname, lnum, items[1]);
5395 }
5396 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5397 && itemcnt == 3)
5398 {
5399 garray_T *gap = &spin->si_comppat;
5400 int i;
5401
5402 /* Only add the couple if it isn't already there. */
5403 for (i = 0; i < gap->ga_len - 1; i += 2)
5404 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5405 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5406 items[2]) == 0)
5407 break;
5408 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5409 {
5410 ((char_u **)(gap->ga_data))[gap->ga_len++]
5411 = getroom_save(spin, items[1]);
5412 ((char_u **)(gap->ga_data))[gap->ga_len++]
5413 = getroom_save(spin, items[2]);
5414 }
5415 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005416 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005417 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005418 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005419 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005420 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005421 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5422 {
5423 spin->si_nobreak = TRUE;
5424 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005425 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5426 {
5427 spin->si_nosplitsugs = TRUE;
5428 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005429 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5430 {
5431 spin->si_nosugfile = TRUE;
5432 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005433 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5434 {
5435 aff->af_pfxpostpone = TRUE;
5436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005437 else if ((STRCMP(items[0], "PFX") == 0
5438 || STRCMP(items[0], "SFX") == 0)
5439 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005440 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005441 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005442 int lasti = 4;
5443 char_u key[AH_KEY_LEN];
5444
5445 if (*items[0] == 'P')
5446 tp = &aff->af_pref;
5447 else
5448 tp = &aff->af_suff;
5449
5450 /* Myspell allows the same affix name to be used multiple
5451 * times. The affix files that do this have an undocumented
5452 * "S" flag on all but the last block, thus we check for that
5453 * and store it in ah_follows. */
5454 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5455 hi = hash_find(tp, key);
5456 if (!HASHITEM_EMPTY(hi))
5457 {
5458 cur_aff = HI2AH(hi);
5459 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5460 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5461 fname, lnum, items[1]);
5462 if (!cur_aff->ah_follows)
5463 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5464 fname, lnum, items[1]);
5465 }
5466 else
5467 {
5468 /* New affix letter. */
5469 cur_aff = (affheader_T *)getroom(spin,
5470 sizeof(affheader_T), TRUE);
5471 if (cur_aff == NULL)
5472 break;
5473 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5474 fname, lnum);
5475 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5476 break;
5477 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005478 || cur_aff->ah_flag == aff->af_rare
5479 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005480 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005481 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005482 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005483 || cur_aff->ah_flag == aff->af_needcomp
5484 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005485 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 +00005486 fname, lnum, items[1]);
5487 STRCPY(cur_aff->ah_key, items[1]);
5488 hash_add(tp, cur_aff->ah_key);
5489
5490 cur_aff->ah_combine = (*items[2] == 'Y');
5491 }
5492
5493 /* Check for the "S" flag, which apparently means that another
5494 * block with the same affix name is following. */
5495 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5496 {
5497 ++lasti;
5498 cur_aff->ah_follows = TRUE;
5499 }
5500 else
5501 cur_aff->ah_follows = FALSE;
5502
Bram Moolenaar8db73182005-06-17 21:51:16 +00005503 /* Myspell allows extra text after the item, but that might
5504 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005505 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005506 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005507
Bram Moolenaar95529562005-08-25 21:21:38 +00005508 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005509 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5510 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005511
Bram Moolenaar95529562005-08-25 21:21:38 +00005512 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005513 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005514 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005515 {
5516 /* Use a new number in the .spl file later, to be able
5517 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005518 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005519 cur_aff->ah_newID = ++spin->si_newprefID;
5520
5521 /* We only really use ah_newID if the prefix is
5522 * postponed. We know that only after handling all
5523 * the items. */
5524 did_postpone_prefix = FALSE;
5525 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005526 else
5527 /* Did use the ID in a previous block. */
5528 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005529 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005530
Bram Moolenaar51485f02005-06-04 21:55:20 +00005531 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005532 }
5533 else if ((STRCMP(items[0], "PFX") == 0
5534 || STRCMP(items[0], "SFX") == 0)
5535 && aff_todo > 0
5536 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005537 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005538 {
5539 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005540 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005541 int lasti = 5;
5542
Bram Moolenaar8db73182005-06-17 21:51:16 +00005543 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005544 * mean mistakes go unnoticed. Require a comment-starter.
5545 * Hunspell uses a "-" item. */
5546 if (itemcnt > lasti && *items[lasti] != '#'
5547 && (STRCMP(items[lasti], "-") != 0
5548 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005549 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005550
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005551 /* New item for an affix letter. */
5552 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005553 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005554 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005555 if (aff_entry == NULL)
5556 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005557
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005558 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005559 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005560 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005561 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005562 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005563
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005564 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005565 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5566 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005567 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005568 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005569 aff_process_flags(aff, aff_entry);
5570 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005571 }
5572
Bram Moolenaar51485f02005-06-04 21:55:20 +00005573 /* Don't use an affix entry with non-ASCII characters when
5574 * "spin->si_ascii" is TRUE. */
5575 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005576 || has_non_ascii(aff_entry->ae_add)))
5577 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005578 aff_entry->ae_next = cur_aff->ah_first;
5579 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005580
5581 if (STRCMP(items[4], ".") != 0)
5582 {
5583 char_u buf[MAXLINELEN];
5584
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005585 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005586 if (*items[0] == 'P')
5587 sprintf((char *)buf, "^%s", items[4]);
5588 else
5589 sprintf((char *)buf, "%s$", items[4]);
5590 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005591 RE_MAGIC + RE_STRING + RE_STRICT);
5592 if (aff_entry->ae_prog == NULL)
5593 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5594 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005595 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005596
5597 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005598 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005599 * Can't be done for an affix with flags, ignoring
5600 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005601 if (*items[0] == 'P' && aff->af_pfxpostpone
5602 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005603 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005604 /* When the chop string is one lower-case letter and
5605 * the add string ends in the upper-case letter we set
5606 * the "upper" flag, clear "ae_chop" and remove the
5607 * letters from "ae_add". The condition must either
5608 * be empty or start with the same letter. */
5609 if (aff_entry->ae_chop != NULL
5610 && aff_entry->ae_add != NULL
5611#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005612 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005613 aff_entry->ae_chop)] == NUL
5614#else
5615 && aff_entry->ae_chop[1] == NUL
5616#endif
5617 )
5618 {
5619 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005620
Bram Moolenaar53805d12005-08-01 07:08:33 +00005621 c = PTR2CHAR(aff_entry->ae_chop);
5622 c_up = SPELL_TOUPPER(c);
5623 if (c_up != c
5624 && (aff_entry->ae_cond == NULL
5625 || PTR2CHAR(aff_entry->ae_cond) == c))
5626 {
5627 p = aff_entry->ae_add
5628 + STRLEN(aff_entry->ae_add);
5629 mb_ptr_back(aff_entry->ae_add, p);
5630 if (PTR2CHAR(p) == c_up)
5631 {
5632 upper = TRUE;
5633 aff_entry->ae_chop = NULL;
5634 *p = NUL;
5635
5636 /* The condition is matched with the
5637 * actual word, thus must check for the
5638 * upper-case letter. */
5639 if (aff_entry->ae_cond != NULL)
5640 {
5641 char_u buf[MAXLINELEN];
5642#ifdef FEAT_MBYTE
5643 if (has_mbyte)
5644 {
5645 onecap_copy(items[4], buf, TRUE);
5646 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005647 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005648 }
5649 else
5650#endif
5651 *aff_entry->ae_cond = c_up;
5652 if (aff_entry->ae_cond != NULL)
5653 {
5654 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005655 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005656 vim_free(aff_entry->ae_prog);
5657 aff_entry->ae_prog = vim_regcomp(
5658 buf, RE_MAGIC + RE_STRING);
5659 }
5660 }
5661 }
5662 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005663 }
5664
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005665 if (aff_entry->ae_chop == NULL
5666 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005667 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005668 int idx;
5669 char_u **pp;
5670 int n;
5671
Bram Moolenaar6de68532005-08-24 22:08:48 +00005672 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005673 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5674 --idx)
5675 {
5676 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5677 if (str_equal(p, aff_entry->ae_cond))
5678 break;
5679 }
5680 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5681 {
5682 /* Not found, add a new condition. */
5683 idx = spin->si_prefcond.ga_len++;
5684 pp = ((char_u **)spin->si_prefcond.ga_data)
5685 + idx;
5686 if (aff_entry->ae_cond == NULL)
5687 *pp = NULL;
5688 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005689 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005690 aff_entry->ae_cond);
5691 }
5692
5693 /* Add the prefix to the prefix tree. */
5694 if (aff_entry->ae_add == NULL)
5695 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005696 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005697 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005698
Bram Moolenaar53805d12005-08-01 07:08:33 +00005699 /* PFX_FLAGS is a negative number, so that
5700 * tree_add_word() knows this is the prefix tree. */
5701 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005702 if (!cur_aff->ah_combine)
5703 n |= WFP_NC;
5704 if (upper)
5705 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005706 if (aff_entry->ae_comppermit)
5707 n |= WFP_COMPPERMIT;
5708 if (aff_entry->ae_compforbid)
5709 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005710 tree_add_word(spin, p, spin->si_prefroot, n,
5711 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005712 did_postpone_prefix = TRUE;
5713 }
5714
5715 /* Didn't actually use ah_newID, backup si_newprefID. */
5716 if (aff_todo == 0 && !did_postpone_prefix)
5717 {
5718 --spin->si_newprefID;
5719 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005720 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005721 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005722 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005723 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005724 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5725 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005726 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005727 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005728 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005729 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5730 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005731 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005732 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005733 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005734 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5735 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005736 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005737 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005738 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005739 else if ((STRCMP(items[0], "REP") == 0
5740 || STRCMP(items[0], "REPSAL") == 0)
5741 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005742 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005743 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005744 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005745 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005746 fname, lnum);
5747 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005748 else if ((STRCMP(items[0], "REP") == 0
5749 || STRCMP(items[0], "REPSAL") == 0)
5750 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005751 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005752 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005753 /* Myspell ignores extra arguments, we require it starts with
5754 * # to detect mistakes. */
5755 if (itemcnt > 3 && items[3][0] != '#')
5756 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005757 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005758 {
5759 /* Replace underscore with space (can't include a space
5760 * directly). */
5761 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5762 if (*p == '_')
5763 *p = ' ';
5764 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5765 if (*p == '_')
5766 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005767 add_fromto(spin, items[0][3] == 'S'
5768 ? &spin->si_repsal
5769 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005771 }
5772 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5773 {
5774 /* MAP item or count */
5775 if (!found_map)
5776 {
5777 /* First line contains the count. */
5778 found_map = TRUE;
5779 if (!isdigit(*items[1]))
5780 smsg((char_u *)_("Expected MAP count in %s line %d"),
5781 fname, lnum);
5782 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005783 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005784 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005785 int c;
5786
5787 /* Check that every character appears only once. */
5788 for (p = items[1]; *p != NUL; )
5789 {
5790#ifdef FEAT_MBYTE
5791 c = mb_ptr2char_adv(&p);
5792#else
5793 c = *p++;
5794#endif
5795 if ((spin->si_map.ga_len > 0
5796 && vim_strchr(spin->si_map.ga_data, c)
5797 != NULL)
5798 || vim_strchr(p, c) != NULL)
5799 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5800 fname, lnum);
5801 }
5802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005803 /* We simply concatenate all the MAP strings, separated by
5804 * slashes. */
5805 ga_concat(&spin->si_map, items[1]);
5806 ga_append(&spin->si_map, '/');
5807 }
5808 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005809 /* Accept "SAL from to" and "SAL from to # comment". */
5810 else if (STRCMP(items[0], "SAL") == 0
5811 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005812 {
5813 if (do_sal)
5814 {
5815 /* SAL item (sounds-a-like)
5816 * Either one of the known keys or a from-to pair. */
5817 if (STRCMP(items[1], "followup") == 0)
5818 spin->si_followup = sal_to_bool(items[2]);
5819 else if (STRCMP(items[1], "collapse_result") == 0)
5820 spin->si_collapse = sal_to_bool(items[2]);
5821 else if (STRCMP(items[1], "remove_accents") == 0)
5822 spin->si_rem_accents = sal_to_bool(items[2]);
5823 else
5824 /* when "to" is "_" it means empty */
5825 add_fromto(spin, &spin->si_sal, items[1],
5826 STRCMP(items[2], "_") == 0 ? (char_u *)""
5827 : items[2]);
5828 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005829 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005830 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005831 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005832 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005833 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005834 }
5835 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005836 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005837 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005838 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005839 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005840 else if (STRCMP(items[0], "COMMON") == 0)
5841 {
5842 int i;
5843
5844 for (i = 1; i < itemcnt; ++i)
5845 {
5846 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5847 items[i])))
5848 {
5849 p = vim_strsave(items[i]);
5850 if (p == NULL)
5851 break;
5852 hash_add(&spin->si_commonwords, p);
5853 }
5854 }
5855 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005856 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005857 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005858 fname, lnum, items[0]);
5859 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005860 }
5861
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005862 if (fol != NULL || low != NULL || upp != NULL)
5863 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005864 if (spin->si_clear_chartab)
5865 {
5866 /* Clear the char type tables, don't want to use any of the
5867 * currently used spell properties. */
5868 init_spell_chartab();
5869 spin->si_clear_chartab = FALSE;
5870 }
5871
Bram Moolenaar3982c542005-06-08 21:56:31 +00005872 /*
5873 * Don't write a word table for an ASCII file, so that we don't check
5874 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005875 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005876 * mb_get_class(), the list of chars in the file will be incomplete.
5877 */
5878 if (!spin->si_ascii
5879#ifdef FEAT_MBYTE
5880 && !enc_utf8
5881#endif
5882 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005883 {
5884 if (fol == NULL || low == NULL || upp == NULL)
5885 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5886 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005887 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005888 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005889
5890 vim_free(fol);
5891 vim_free(low);
5892 vim_free(upp);
5893 }
5894
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005895 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005896 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005897 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005898 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005899 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005900 }
5901
Bram Moolenaar6de68532005-08-24 22:08:48 +00005902 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005903 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005904 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5905 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005906 }
5907
Bram Moolenaar6de68532005-08-24 22:08:48 +00005908 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005909 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005910 if (syllable == NULL)
5911 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5912 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5913 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005914 }
5915
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005916 if (compoptions != 0)
5917 {
5918 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5919 spin->si_compoptions |= compoptions;
5920 }
5921
Bram Moolenaar6de68532005-08-24 22:08:48 +00005922 if (compflags != NULL)
5923 process_compflags(spin, aff, compflags);
5924
5925 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005926 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005927 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005928 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005929 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005930 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005931 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005932 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005933 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005934 }
5935
Bram Moolenaar6de68532005-08-24 22:08:48 +00005936 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005937 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005938 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5939 spin->si_syllable = syllable;
5940 }
5941
5942 if (sofofrom != NULL || sofoto != NULL)
5943 {
5944 if (sofofrom == NULL || sofoto == NULL)
5945 smsg((char_u *)_("Missing SOFO%s line in %s"),
5946 sofofrom == NULL ? "FROM" : "TO", fname);
5947 else if (spin->si_sal.ga_len > 0)
5948 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005949 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005950 {
5951 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5952 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5953 spin->si_sofofr = sofofrom;
5954 spin->si_sofoto = sofoto;
5955 }
5956 }
5957
5958 if (midword != NULL)
5959 {
5960 aff_check_string(spin->si_midword, midword, "MIDWORD");
5961 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005962 }
5963
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005964 vim_free(pc);
5965 fclose(fd);
5966 return aff;
5967}
5968
5969/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005970 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
5971 * ae_flags to ae_comppermit and ae_compforbid.
5972 */
5973 static void
5974aff_process_flags(affile, entry)
5975 afffile_T *affile;
5976 affentry_T *entry;
5977{
5978 char_u *p;
5979 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005980 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005981
5982 if (entry->ae_flags != NULL
5983 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
5984 {
5985 for (p = entry->ae_flags; *p != NUL; )
5986 {
5987 prevp = p;
5988 flag = get_affitem(affile->af_flagtype, &p);
5989 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
5990 {
5991 mch_memmove(prevp, p, STRLEN(p) + 1);
5992 p = prevp;
5993 if (flag == affile->af_comppermit)
5994 entry->ae_comppermit = TRUE;
5995 else
5996 entry->ae_compforbid = TRUE;
5997 }
5998 if (affile->af_flagtype == AFT_NUM && *p == ',')
5999 ++p;
6000 }
6001 if (*entry->ae_flags == NUL)
6002 entry->ae_flags = NULL; /* nothing left */
6003 }
6004}
6005
6006/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006007 * Return TRUE if "s" is the name of an info item in the affix file.
6008 */
6009 static int
6010spell_info_item(s)
6011 char_u *s;
6012{
6013 return STRCMP(s, "NAME") == 0
6014 || STRCMP(s, "HOME") == 0
6015 || STRCMP(s, "VERSION") == 0
6016 || STRCMP(s, "AUTHOR") == 0
6017 || STRCMP(s, "EMAIL") == 0
6018 || STRCMP(s, "COPYRIGHT") == 0;
6019}
6020
6021/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006022 * Turn an affix flag name into a number, according to the FLAG type.
6023 * returns zero for failure.
6024 */
6025 static unsigned
6026affitem2flag(flagtype, item, fname, lnum)
6027 int flagtype;
6028 char_u *item;
6029 char_u *fname;
6030 int lnum;
6031{
6032 unsigned res;
6033 char_u *p = item;
6034
6035 res = get_affitem(flagtype, &p);
6036 if (res == 0)
6037 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006038 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006039 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6040 fname, lnum, item);
6041 else
6042 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6043 fname, lnum, item);
6044 }
6045 if (*p != NUL)
6046 {
6047 smsg((char_u *)_(e_affname), fname, lnum, item);
6048 return 0;
6049 }
6050
6051 return res;
6052}
6053
6054/*
6055 * Get one affix name from "*pp" and advance the pointer.
6056 * Returns zero for an error, still advances the pointer then.
6057 */
6058 static unsigned
6059get_affitem(flagtype, pp)
6060 int flagtype;
6061 char_u **pp;
6062{
6063 int res;
6064
Bram Moolenaar95529562005-08-25 21:21:38 +00006065 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006066 {
6067 if (!VIM_ISDIGIT(**pp))
6068 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006069 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006070 return 0;
6071 }
6072 res = getdigits(pp);
6073 }
6074 else
6075 {
6076#ifdef FEAT_MBYTE
6077 res = mb_ptr2char_adv(pp);
6078#else
6079 res = *(*pp)++;
6080#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006081 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006082 && res >= 'A' && res <= 'Z'))
6083 {
6084 if (**pp == NUL)
6085 return 0;
6086#ifdef FEAT_MBYTE
6087 res = mb_ptr2char_adv(pp) + (res << 16);
6088#else
6089 res = *(*pp)++ + (res << 16);
6090#endif
6091 }
6092 }
6093 return res;
6094}
6095
6096/*
6097 * Process the "compflags" string used in an affix file and append it to
6098 * spin->si_compflags.
6099 * The processing involves changing the affix names to ID numbers, so that
6100 * they fit in one byte.
6101 */
6102 static void
6103process_compflags(spin, aff, compflags)
6104 spellinfo_T *spin;
6105 afffile_T *aff;
6106 char_u *compflags;
6107{
6108 char_u *p;
6109 char_u *prevp;
6110 unsigned flag;
6111 compitem_T *ci;
6112 int id;
6113 int len;
6114 char_u *tp;
6115 char_u key[AH_KEY_LEN];
6116 hashitem_T *hi;
6117
6118 /* Make room for the old and the new compflags, concatenated with a / in
6119 * between. Processing it makes it shorter, but we don't know by how
6120 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006121 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006122 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006123 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006124 p = getroom(spin, len, FALSE);
6125 if (p == NULL)
6126 return;
6127 if (spin->si_compflags != NULL)
6128 {
6129 STRCPY(p, spin->si_compflags);
6130 STRCAT(p, "/");
6131 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006132 spin->si_compflags = p;
6133 tp = p + STRLEN(p);
6134
6135 for (p = compflags; *p != NUL; )
6136 {
6137 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6138 /* Copy non-flag characters directly. */
6139 *tp++ = *p++;
6140 else
6141 {
6142 /* First get the flag number, also checks validity. */
6143 prevp = p;
6144 flag = get_affitem(aff->af_flagtype, &p);
6145 if (flag != 0)
6146 {
6147 /* Find the flag in the hashtable. If it was used before, use
6148 * the existing ID. Otherwise add a new entry. */
6149 vim_strncpy(key, prevp, p - prevp);
6150 hi = hash_find(&aff->af_comp, key);
6151 if (!HASHITEM_EMPTY(hi))
6152 id = HI2CI(hi)->ci_newID;
6153 else
6154 {
6155 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6156 if (ci == NULL)
6157 break;
6158 STRCPY(ci->ci_key, key);
6159 ci->ci_flag = flag;
6160 /* Avoid using a flag ID that has a special meaning in a
6161 * regexp (also inside []). */
6162 do
6163 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006164 check_renumber(spin);
6165 id = spin->si_newcompID--;
6166 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006167 ci->ci_newID = id;
6168 hash_add(&aff->af_comp, ci->ci_key);
6169 }
6170 *tp++ = id;
6171 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006172 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006173 ++p;
6174 }
6175 }
6176
6177 *tp = NUL;
6178}
6179
6180/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006181 * Check that the new IDs for postponed affixes and compounding don't overrun
6182 * each other. We have almost 255 available, but start at 0-127 to avoid
6183 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6184 * When that is used up an error message is given.
6185 */
6186 static void
6187check_renumber(spin)
6188 spellinfo_T *spin;
6189{
6190 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6191 {
6192 spin->si_newprefID = 127;
6193 spin->si_newcompID = 255;
6194 }
6195}
6196
6197/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006198 * Return TRUE if flag "flag" appears in affix list "afflist".
6199 */
6200 static int
6201flag_in_afflist(flagtype, afflist, flag)
6202 int flagtype;
6203 char_u *afflist;
6204 unsigned flag;
6205{
6206 char_u *p;
6207 unsigned n;
6208
6209 switch (flagtype)
6210 {
6211 case AFT_CHAR:
6212 return vim_strchr(afflist, flag) != NULL;
6213
Bram Moolenaar95529562005-08-25 21:21:38 +00006214 case AFT_CAPLONG:
6215 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006216 for (p = afflist; *p != NUL; )
6217 {
6218#ifdef FEAT_MBYTE
6219 n = mb_ptr2char_adv(&p);
6220#else
6221 n = *p++;
6222#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006223 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006224 && *p != NUL)
6225#ifdef FEAT_MBYTE
6226 n = mb_ptr2char_adv(&p) + (n << 16);
6227#else
6228 n = *p++ + (n << 16);
6229#endif
6230 if (n == flag)
6231 return TRUE;
6232 }
6233 break;
6234
Bram Moolenaar95529562005-08-25 21:21:38 +00006235 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006236 for (p = afflist; *p != NUL; )
6237 {
6238 n = getdigits(&p);
6239 if (n == flag)
6240 return TRUE;
6241 if (*p != NUL) /* skip over comma */
6242 ++p;
6243 }
6244 break;
6245 }
6246 return FALSE;
6247}
6248
6249/*
6250 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6251 */
6252 static void
6253aff_check_number(spinval, affval, name)
6254 int spinval;
6255 int affval;
6256 char *name;
6257{
6258 if (spinval != 0 && spinval != affval)
6259 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6260}
6261
6262/*
6263 * Give a warning when "spinval" and "affval" strings are set and not the same.
6264 */
6265 static void
6266aff_check_string(spinval, affval, name)
6267 char_u *spinval;
6268 char_u *affval;
6269 char *name;
6270{
6271 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6272 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6273}
6274
6275/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006276 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6277 * NULL as equal.
6278 */
6279 static int
6280str_equal(s1, s2)
6281 char_u *s1;
6282 char_u *s2;
6283{
6284 if (s1 == NULL || s2 == NULL)
6285 return s1 == s2;
6286 return STRCMP(s1, s2) == 0;
6287}
6288
6289/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006290 * Add a from-to item to "gap". Used for REP and SAL items.
6291 * They are stored case-folded.
6292 */
6293 static void
6294add_fromto(spin, gap, from, to)
6295 spellinfo_T *spin;
6296 garray_T *gap;
6297 char_u *from;
6298 char_u *to;
6299{
6300 fromto_T *ftp;
6301 char_u word[MAXWLEN];
6302
6303 if (ga_grow(gap, 1) == OK)
6304 {
6305 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006306 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006307 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006308 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006309 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006310 ++gap->ga_len;
6311 }
6312}
6313
6314/*
6315 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6316 */
6317 static int
6318sal_to_bool(s)
6319 char_u *s;
6320{
6321 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6322}
6323
6324/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006325 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6326 * When "s" is NULL FALSE is returned.
6327 */
6328 static int
6329has_non_ascii(s)
6330 char_u *s;
6331{
6332 char_u *p;
6333
6334 if (s != NULL)
6335 for (p = s; *p != NUL; ++p)
6336 if (*p >= 128)
6337 return TRUE;
6338 return FALSE;
6339}
6340
6341/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006342 * Free the structure filled by spell_read_aff().
6343 */
6344 static void
6345spell_free_aff(aff)
6346 afffile_T *aff;
6347{
6348 hashtab_T *ht;
6349 hashitem_T *hi;
6350 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006351 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006352 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006353
6354 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006355
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006356 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006357 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6358 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006359 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006360 for (hi = ht->ht_array; todo > 0; ++hi)
6361 {
6362 if (!HASHITEM_EMPTY(hi))
6363 {
6364 --todo;
6365 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006366 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6367 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006368 }
6369 }
6370 if (ht == &aff->af_suff)
6371 break;
6372 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006373
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006374 hash_clear(&aff->af_pref);
6375 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006376 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006377}
6378
6379/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006380 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006381 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006382 */
6383 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006384spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006385 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006386 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006387 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006388{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006389 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006390 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006391 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006392 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006393 char_u store_afflist[MAXWLEN];
6394 int pfxlen;
6395 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006396 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006397 char_u *pc;
6398 char_u *w;
6399 int l;
6400 hash_T hash;
6401 hashitem_T *hi;
6402 FILE *fd;
6403 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006404 int non_ascii = 0;
6405 int retval = OK;
6406 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006407 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006408 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006409
Bram Moolenaar51485f02005-06-04 21:55:20 +00006410 /*
6411 * Open the file.
6412 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006413 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006414 if (fd == NULL)
6415 {
6416 EMSG2(_(e_notopen), fname);
6417 return FAIL;
6418 }
6419
Bram Moolenaar51485f02005-06-04 21:55:20 +00006420 /* The hashtable is only used to detect duplicated words. */
6421 hash_init(&ht);
6422
Bram Moolenaar4770d092006-01-12 23:22:24 +00006423 vim_snprintf((char *)IObuff, IOSIZE,
6424 _("Reading dictionary file %s ..."), fname);
6425 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006426
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006427 /* start with a message for the first line */
6428 spin->si_msg_count = 999999;
6429
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006430 /* Read and ignore the first line: word count. */
6431 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006432 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006433 EMSG2(_("E760: No word count in %s"), fname);
6434
6435 /*
6436 * Read all the lines in the file one by one.
6437 * The words are converted to 'encoding' here, before being added to
6438 * the hashtable.
6439 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006440 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006441 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006442 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006443 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006444 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006445 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006446
Bram Moolenaar51485f02005-06-04 21:55:20 +00006447 /* Remove CR, LF and white space from the end. White space halfway
6448 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006449 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006450 while (l > 0 && line[l - 1] <= ' ')
6451 --l;
6452 if (l == 0)
6453 continue; /* empty line */
6454 line[l] = NUL;
6455
Bram Moolenaarb765d632005-06-07 21:00:02 +00006456#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006457 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006458 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006460 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006461 if (pc == NULL)
6462 {
6463 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6464 fname, lnum, line);
6465 continue;
6466 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006467 w = pc;
6468 }
6469 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006470#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006471 {
6472 pc = NULL;
6473 w = line;
6474 }
6475
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006476 /* Truncate the word at the "/", set "afflist" to what follows.
6477 * Replace "\/" by "/" and "\\" by "\". */
6478 afflist = NULL;
6479 for (p = w; *p != NUL; mb_ptr_adv(p))
6480 {
6481 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6482 mch_memmove(p, p + 1, STRLEN(p));
6483 else if (*p == '/')
6484 {
6485 *p = NUL;
6486 afflist = p + 1;
6487 break;
6488 }
6489 }
6490
6491 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6492 if (spin->si_ascii && has_non_ascii(w))
6493 {
6494 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006495 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006496 continue;
6497 }
6498
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006499 /* This takes time, print a message every 10000 words. */
6500 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006501 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006502 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006503 vim_snprintf((char *)message, sizeof(message),
6504 _("line %6d, word %6d - %s"),
6505 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6506 msg_start();
6507 msg_puts_long_attr(message, 0);
6508 msg_clr_eos();
6509 msg_didout = FALSE;
6510 msg_col = 0;
6511 out_flush();
6512 }
6513
Bram Moolenaar51485f02005-06-04 21:55:20 +00006514 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006515 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006517 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006518 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006519 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006520 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006521 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006522
Bram Moolenaar51485f02005-06-04 21:55:20 +00006523 hash = hash_hash(dw);
6524 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006525 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006526 {
6527 if (p_verbose > 0)
6528 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006529 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006530 else if (duplicate == 0)
6531 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6532 fname, lnum, dw);
6533 ++duplicate;
6534 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006535 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006537
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006538 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006539 store_afflist[0] = NUL;
6540 pfxlen = 0;
6541 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006542 if (afflist != NULL)
6543 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006544 /* Extract flags from the affix list. */
6545 flags |= get_affix_flags(affile, afflist);
6546
Bram Moolenaar6de68532005-08-24 22:08:48 +00006547 if (affile->af_needaffix != 0 && flag_in_afflist(
6548 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006549 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006550
6551 if (affile->af_pfxpostpone)
6552 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006553 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006554
Bram Moolenaar5195e452005-08-19 20:32:47 +00006555 if (spin->si_compflags != NULL)
6556 /* Need to store the list of compound flags with the word.
6557 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006558 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006559 }
6560
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006562 if (store_word(spin, dw, flags, spin->si_region,
6563 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006564 retval = FAIL;
6565
6566 if (afflist != NULL)
6567 {
6568 /* Find all matching suffixes and add the resulting words.
6569 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006570 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006571 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006572 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006573 retval = FAIL;
6574
6575 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006576 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006577 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006578 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006579 retval = FAIL;
6580 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006581
6582 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006583 }
6584
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006585 if (duplicate > 0)
6586 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006587 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006588 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6589 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006590 hash_clear(&ht);
6591
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006592 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006593 return retval;
6594}
6595
6596/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006597 * Check for affix flags in "afflist" that are turned into word flags.
6598 * Return WF_ flags.
6599 */
6600 static int
6601get_affix_flags(affile, afflist)
6602 afffile_T *affile;
6603 char_u *afflist;
6604{
6605 int flags = 0;
6606
6607 if (affile->af_keepcase != 0 && flag_in_afflist(
6608 affile->af_flagtype, afflist, affile->af_keepcase))
6609 flags |= WF_KEEPCAP | WF_FIXCAP;
6610 if (affile->af_rare != 0 && flag_in_afflist(
6611 affile->af_flagtype, afflist, affile->af_rare))
6612 flags |= WF_RARE;
6613 if (affile->af_bad != 0 && flag_in_afflist(
6614 affile->af_flagtype, afflist, affile->af_bad))
6615 flags |= WF_BANNED;
6616 if (affile->af_needcomp != 0 && flag_in_afflist(
6617 affile->af_flagtype, afflist, affile->af_needcomp))
6618 flags |= WF_NEEDCOMP;
6619 if (affile->af_comproot != 0 && flag_in_afflist(
6620 affile->af_flagtype, afflist, affile->af_comproot))
6621 flags |= WF_COMPROOT;
6622 if (affile->af_nosuggest != 0 && flag_in_afflist(
6623 affile->af_flagtype, afflist, affile->af_nosuggest))
6624 flags |= WF_NOSUGGEST;
6625 return flags;
6626}
6627
6628/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006629 * Get the list of prefix IDs from the affix list "afflist".
6630 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006631 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6632 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006633 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006634 static int
6635get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006636 afffile_T *affile;
6637 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006638 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006639{
6640 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006641 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006642 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006643 int id;
6644 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006645 hashitem_T *hi;
6646
Bram Moolenaar6de68532005-08-24 22:08:48 +00006647 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006648 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006649 prevp = p;
6650 if (get_affitem(affile->af_flagtype, &p) != 0)
6651 {
6652 /* A flag is a postponed prefix flag if it appears in "af_pref"
6653 * and it's ID is not zero. */
6654 vim_strncpy(key, prevp, p - prevp);
6655 hi = hash_find(&affile->af_pref, key);
6656 if (!HASHITEM_EMPTY(hi))
6657 {
6658 id = HI2AH(hi)->ah_newID;
6659 if (id != 0)
6660 store_afflist[cnt++] = id;
6661 }
6662 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006663 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006664 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006665 }
6666
Bram Moolenaar5195e452005-08-19 20:32:47 +00006667 store_afflist[cnt] = NUL;
6668 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006669}
6670
6671/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006672 * Get the list of compound IDs from the affix list "afflist" that are used
6673 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006674 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006675 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006676 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006677get_compflags(affile, afflist, store_afflist)
6678 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006679 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006680 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006681{
6682 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006683 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006684 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006685 char_u key[AH_KEY_LEN];
6686 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006687
Bram Moolenaar6de68532005-08-24 22:08:48 +00006688 for (p = afflist; *p != NUL; )
6689 {
6690 prevp = p;
6691 if (get_affitem(affile->af_flagtype, &p) != 0)
6692 {
6693 /* A flag is a compound flag if it appears in "af_comp". */
6694 vim_strncpy(key, prevp, p - prevp);
6695 hi = hash_find(&affile->af_comp, key);
6696 if (!HASHITEM_EMPTY(hi))
6697 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6698 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006699 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006700 ++p;
6701 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006702
Bram Moolenaar5195e452005-08-19 20:32:47 +00006703 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006704}
6705
6706/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006707 * Apply affixes to a word and store the resulting words.
6708 * "ht" is the hashtable with affentry_T that need to be applied, either
6709 * prefixes or suffixes.
6710 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6711 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006712 *
6713 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006714 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006715 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006716store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006717 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006718 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006719 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006720 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006721 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006722 hashtab_T *ht;
6723 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006724 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006725 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006726 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006727 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6728 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006729{
6730 int todo;
6731 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006732 affheader_T *ah;
6733 affentry_T *ae;
6734 regmatch_T regmatch;
6735 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006736 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006737 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006738 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006739 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006740 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006741 int use_pfxlen;
6742 int need_affix;
6743 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006744 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006745 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006746 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006747
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006748 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006749 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006750 {
6751 if (!HASHITEM_EMPTY(hi))
6752 {
6753 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006754 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006755
Bram Moolenaar51485f02005-06-04 21:55:20 +00006756 /* Check that the affix combines, if required, and that the word
6757 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006758 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6759 && flag_in_afflist(affile->af_flagtype, afflist,
6760 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006761 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006762 /* Loop over all affix entries with this name. */
6763 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006764 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006765 /* Check the condition. It's not logical to match case
6766 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006767 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006768 * Another requirement from Myspell is that the chop
6769 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006770 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006771 * prefixes with a chop string and/or flags.
6772 * When a previously added affix had CIRCUMFIX this one
6773 * must have it too, if it had not then this one must not
6774 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006775 regmatch.regprog = ae->ae_prog;
6776 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006777 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006778 || ae->ae_chop != NULL
6779 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006780 && (ae->ae_chop == NULL
6781 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006782 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006783 || vim_regexec(&regmatch, word, (colnr_T)0))
6784 && (((condit & CONDIT_CFIX) == 0)
6785 == ((condit & CONDIT_AFF) == 0
6786 || ae->ae_flags == NULL
6787 || !flag_in_afflist(affile->af_flagtype,
6788 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006790 /* Match. Remove the chop and add the affix. */
6791 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006792 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006793 /* prefix: chop/add at the start of the word */
6794 if (ae->ae_add == NULL)
6795 *newword = NUL;
6796 else
6797 STRCPY(newword, ae->ae_add);
6798 p = word;
6799 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006800 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006801 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006802#ifdef FEAT_MBYTE
6803 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006804 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006805 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006806 for ( ; i > 0; --i)
6807 mb_ptr_adv(p);
6808 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006809 else
6810#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006811 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006812 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006813 STRCAT(newword, p);
6814 }
6815 else
6816 {
6817 /* suffix: chop/add at the end of the word */
6818 STRCPY(newword, word);
6819 if (ae->ae_chop != NULL)
6820 {
6821 /* Remove chop string. */
6822 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006823 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006824 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006825 mb_ptr_back(newword, p);
6826 *p = NUL;
6827 }
6828 if (ae->ae_add != NULL)
6829 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006830 }
6831
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006832 use_flags = flags;
6833 use_pfxlist = pfxlist;
6834 use_pfxlen = pfxlen;
6835 need_affix = FALSE;
6836 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6837 if (ae->ae_flags != NULL)
6838 {
6839 /* Extract flags from the affix list. */
6840 use_flags |= get_affix_flags(affile, ae->ae_flags);
6841
6842 if (affile->af_needaffix != 0 && flag_in_afflist(
6843 affile->af_flagtype, ae->ae_flags,
6844 affile->af_needaffix))
6845 need_affix = TRUE;
6846
6847 /* When there is a CIRCUMFIX flag the other affix
6848 * must also have it and we don't add the word
6849 * with one affix. */
6850 if (affile->af_circumfix != 0 && flag_in_afflist(
6851 affile->af_flagtype, ae->ae_flags,
6852 affile->af_circumfix))
6853 {
6854 use_condit |= CONDIT_CFIX;
6855 if ((condit & CONDIT_CFIX) == 0)
6856 need_affix = TRUE;
6857 }
6858
6859 if (affile->af_pfxpostpone
6860 || spin->si_compflags != NULL)
6861 {
6862 if (affile->af_pfxpostpone)
6863 /* Get prefix IDS from the affix list. */
6864 use_pfxlen = get_pfxlist(affile,
6865 ae->ae_flags, store_afflist);
6866 else
6867 use_pfxlen = 0;
6868 use_pfxlist = store_afflist;
6869
6870 /* Combine the prefix IDs. Avoid adding the
6871 * same ID twice. */
6872 for (i = 0; i < pfxlen; ++i)
6873 {
6874 for (j = 0; j < use_pfxlen; ++j)
6875 if (pfxlist[i] == use_pfxlist[j])
6876 break;
6877 if (j == use_pfxlen)
6878 use_pfxlist[use_pfxlen++] = pfxlist[i];
6879 }
6880
6881 if (spin->si_compflags != NULL)
6882 /* Get compound IDS from the affix list. */
6883 get_compflags(affile, ae->ae_flags,
6884 use_pfxlist + use_pfxlen);
6885
6886 /* Combine the list of compound flags.
6887 * Concatenate them to the prefix IDs list.
6888 * Avoid adding the same ID twice. */
6889 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6890 {
6891 for (j = use_pfxlen;
6892 use_pfxlist[j] != NUL; ++j)
6893 if (pfxlist[i] == use_pfxlist[j])
6894 break;
6895 if (use_pfxlist[j] == NUL)
6896 {
6897 use_pfxlist[j++] = pfxlist[i];
6898 use_pfxlist[j] = NUL;
6899 }
6900 }
6901 }
6902 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006903
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006904 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006905 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006906 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006907 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006908 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006909 use_pfxlist = pfx_pfxlist;
6910 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006911
6912 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006913 if (spin->si_prefroot != NULL
6914 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006915 {
6916 /* ... add a flag to indicate an affix was used. */
6917 use_flags |= WF_HAS_AFF;
6918
6919 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006920 * affixes is not allowed. But do use the
6921 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006922 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006923 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006924 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006925
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926 /* When compounding is supported and there is no
6927 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6928 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006929 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006930 {
6931 if (xht != NULL)
6932 use_flags |= WF_NOCOMPAFT;
6933 else
6934 use_flags |= WF_NOCOMPBEF;
6935 }
6936
Bram Moolenaar51485f02005-06-04 21:55:20 +00006937 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006938 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006939 spin->si_region, use_pfxlist,
6940 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006941 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006942
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006943 /* When added a prefix or a first suffix and the affix
6944 * has flags may add a(nother) suffix. RECURSIVE! */
6945 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6946 if (store_aff_word(spin, newword, ae->ae_flags,
6947 affile, &affile->af_suff, xht,
6948 use_condit & (xht == NULL
6949 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006950 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006951 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006952
6953 /* When added a suffix and combining is allowed also
6954 * try adding a prefix additionally. Both for the
6955 * word flags and for the affix flags. RECURSIVE! */
6956 if (xht != NULL && ah->ah_combine)
6957 {
6958 if (store_aff_word(spin, newword,
6959 afflist, affile,
6960 xht, NULL, use_condit,
6961 use_flags, use_pfxlist,
6962 pfxlen) == FAIL
6963 || (ae->ae_flags != NULL
6964 && store_aff_word(spin, newword,
6965 ae->ae_flags, affile,
6966 xht, NULL, use_condit,
6967 use_flags, use_pfxlist,
6968 pfxlen) == FAIL))
6969 retval = FAIL;
6970 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006971 }
6972 }
6973 }
6974 }
6975 }
6976
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006977 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006978}
6979
6980/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006981 * Read a file with a list of words.
6982 */
6983 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006984spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006985 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006986 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006987{
6988 FILE *fd;
6989 long lnum = 0;
6990 char_u rline[MAXLINELEN];
6991 char_u *line;
6992 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006993 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006994 int l;
6995 int retval = OK;
6996 int did_word = FALSE;
6997 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006998 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006999 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007000
7001 /*
7002 * Open the file.
7003 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007004 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007005 if (fd == NULL)
7006 {
7007 EMSG2(_(e_notopen), fname);
7008 return FAIL;
7009 }
7010
Bram Moolenaar4770d092006-01-12 23:22:24 +00007011 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7012 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007013
7014 /*
7015 * Read all the lines in the file one by one.
7016 */
7017 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7018 {
7019 line_breakcheck();
7020 ++lnum;
7021
7022 /* Skip comment lines. */
7023 if (*rline == '#')
7024 continue;
7025
7026 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007027 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007028 while (l > 0 && rline[l - 1] <= ' ')
7029 --l;
7030 if (l == 0)
7031 continue; /* empty or blank line */
7032 rline[l] = NUL;
7033
Bram Moolenaar9c102382006-05-03 21:26:49 +00007034 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007035 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007036#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007037 if (spin->si_conv.vc_type != CONV_NONE)
7038 {
7039 pc = string_convert(&spin->si_conv, rline, NULL);
7040 if (pc == NULL)
7041 {
7042 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7043 fname, lnum, rline);
7044 continue;
7045 }
7046 line = pc;
7047 }
7048 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007049#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007050 {
7051 pc = NULL;
7052 line = rline;
7053 }
7054
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007055 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007057 ++line;
7058 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007059 {
7060 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007061 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7062 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007063 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007064 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7065 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007066 else
7067 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007068#ifdef FEAT_MBYTE
7069 char_u *enc;
7070
Bram Moolenaar51485f02005-06-04 21:55:20 +00007071 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007072 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007073 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007074 if (enc != NULL && !spin->si_ascii
7075 && convert_setup(&spin->si_conv, enc,
7076 p_enc) == FAIL)
7077 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007078 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007079 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007080 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007081#else
7082 smsg((char_u *)_("Conversion in %s not supported"), fname);
7083#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007084 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007085 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007086 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007087
Bram Moolenaar3982c542005-06-08 21:56:31 +00007088 if (STRNCMP(line, "regions=", 8) == 0)
7089 {
7090 if (spin->si_region_count > 1)
7091 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7092 fname, lnum, line);
7093 else
7094 {
7095 line += 8;
7096 if (STRLEN(line) > 16)
7097 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7098 fname, lnum, line);
7099 else
7100 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007101 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007102 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007103
7104 /* Adjust the mask for a word valid in all regions. */
7105 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007106 }
7107 }
7108 continue;
7109 }
7110
Bram Moolenaar7887d882005-07-01 22:33:52 +00007111 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7112 fname, lnum, line - 1);
7113 continue;
7114 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007115
Bram Moolenaar7887d882005-07-01 22:33:52 +00007116 flags = 0;
7117 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007118
Bram Moolenaar7887d882005-07-01 22:33:52 +00007119 /* Check for flags and region after a slash. */
7120 p = vim_strchr(line, '/');
7121 if (p != NULL)
7122 {
7123 *p++ = NUL;
7124 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007125 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007126 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007127 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007128 else if (*p == '!') /* Bad, bad, wicked word. */
7129 flags |= WF_BANNED;
7130 else if (*p == '?') /* Rare word. */
7131 flags |= WF_RARE;
7132 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007133 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007134 if ((flags & WF_REGION) == 0) /* first one */
7135 regionmask = 0;
7136 flags |= WF_REGION;
7137
7138 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007139 if (l > spin->si_region_count)
7140 {
7141 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007142 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007143 break;
7144 }
7145 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007146 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007147 else
7148 {
7149 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7150 fname, lnum, p);
7151 break;
7152 }
7153 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007154 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007155 }
7156
7157 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7158 if (spin->si_ascii && has_non_ascii(line))
7159 {
7160 ++non_ascii;
7161 continue;
7162 }
7163
7164 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007165 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007166 {
7167 retval = FAIL;
7168 break;
7169 }
7170 did_word = TRUE;
7171 }
7172
7173 vim_free(pc);
7174 fclose(fd);
7175
Bram Moolenaar4770d092006-01-12 23:22:24 +00007176 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007177 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007178 vim_snprintf((char *)IObuff, IOSIZE,
7179 _("Ignored %d words with non-ASCII characters"), non_ascii);
7180 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007181 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007182
Bram Moolenaar51485f02005-06-04 21:55:20 +00007183 return retval;
7184}
7185
7186/*
7187 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007188 * This avoids calling free() for every little struct we use (and keeping
7189 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007190 * The memory is cleared to all zeros.
7191 * Returns NULL when out of memory.
7192 */
7193 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007194getroom(spin, len, align)
7195 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007196 size_t len; /* length needed */
7197 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198{
7199 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007200 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007201
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007202 if (align && bl != NULL)
7203 /* Round size up for alignment. On some systems structures need to be
7204 * aligned to the size of a pointer (e.g., SPARC). */
7205 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7206 & ~(sizeof(char *) - 1);
7207
Bram Moolenaar51485f02005-06-04 21:55:20 +00007208 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7209 {
7210 /* Allocate a block of memory. This is not freed until much later. */
7211 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7212 if (bl == NULL)
7213 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007214 bl->sb_next = spin->si_blocks;
7215 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007216 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007217 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007218 }
7219
7220 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007221 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007222
7223 return p;
7224}
7225
7226/*
7227 * Make a copy of a string into memory allocated with getroom().
7228 */
7229 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007230getroom_save(spin, s)
7231 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007232 char_u *s;
7233{
7234 char_u *sc;
7235
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007236 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007237 if (sc != NULL)
7238 STRCPY(sc, s);
7239 return sc;
7240}
7241
7242
7243/*
7244 * Free the list of allocated sblock_T.
7245 */
7246 static void
7247free_blocks(bl)
7248 sblock_T *bl;
7249{
7250 sblock_T *next;
7251
7252 while (bl != NULL)
7253 {
7254 next = bl->sb_next;
7255 vim_free(bl);
7256 bl = next;
7257 }
7258}
7259
7260/*
7261 * Allocate the root of a word tree.
7262 */
7263 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007264wordtree_alloc(spin)
7265 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007266{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007267 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007268}
7269
7270/*
7271 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007272 * Always store it in the case-folded tree. For a keep-case word this is
7273 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7274 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007275 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007276 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7277 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007278 */
7279 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007280store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007281 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007282 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007283 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007284 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007285 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007286 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007287{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007288 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007289 int ct = captype(word, word + len);
7290 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007291 int res = OK;
7292 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007294 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007295 for (p = pfxlist; res == OK; ++p)
7296 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007297 if (!need_affix || (p != NULL && *p != NUL))
7298 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007299 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007300 if (p == NULL || *p == NUL)
7301 break;
7302 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007303 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007304
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007305 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007306 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007307 for (p = pfxlist; res == OK; ++p)
7308 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007309 if (!need_affix || (p != NULL && *p != NUL))
7310 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007311 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007312 if (p == NULL || *p == NUL)
7313 break;
7314 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007315 ++spin->si_keepwcount;
7316 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007317 return res;
7318}
7319
7320/*
7321 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007322 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007323 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007324 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007325 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007326 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007327tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007328 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007329 char_u *word;
7330 wordnode_T *root;
7331 int flags;
7332 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007333 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007334{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007335 wordnode_T *node = root;
7336 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007337 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007338 wordnode_T **prev = NULL;
7339 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007340
Bram Moolenaar51485f02005-06-04 21:55:20 +00007341 /* Add each byte of the word to the tree, including the NUL at the end. */
7342 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007343 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007344 /* When there is more than one reference to this node we need to make
7345 * a copy, so that we can modify it. Copy the whole list of siblings
7346 * (we don't optimize for a partly shared list of siblings). */
7347 if (node != NULL && node->wn_refs > 1)
7348 {
7349 --node->wn_refs;
7350 copyprev = prev;
7351 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7352 {
7353 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007354 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007355 if (np == NULL)
7356 return FAIL;
7357 np->wn_child = copyp->wn_child;
7358 if (np->wn_child != NULL)
7359 ++np->wn_child->wn_refs; /* child gets extra ref */
7360 np->wn_byte = copyp->wn_byte;
7361 if (np->wn_byte == NUL)
7362 {
7363 np->wn_flags = copyp->wn_flags;
7364 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007365 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007366 }
7367
7368 /* Link the new node in the list, there will be one ref. */
7369 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007370 if (copyprev != NULL)
7371 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007372 copyprev = &np->wn_sibling;
7373
7374 /* Let "node" point to the head of the copied list. */
7375 if (copyp == node)
7376 node = np;
7377 }
7378 }
7379
Bram Moolenaar51485f02005-06-04 21:55:20 +00007380 /* Look for the sibling that has the same character. They are sorted
7381 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007382 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007383 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007384 while (node != NULL
7385 && (node->wn_byte < word[i]
7386 || (node->wn_byte == NUL
7387 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007388 ? node->wn_affixID < (unsigned)affixID
7389 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007390 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007391 && (spin->si_sugtree
7392 ? (node->wn_region & 0xffff) < region
7393 : node->wn_affixID
7394 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007395 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007396 prev = &node->wn_sibling;
7397 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007398 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007399 if (node == NULL
7400 || node->wn_byte != word[i]
7401 || (word[i] == NUL
7402 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007403 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007404 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007405 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007406 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007407 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007408 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007409 if (np == NULL)
7410 return FAIL;
7411 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007412
7413 /* If "node" is NULL this is a new child or the end of the sibling
7414 * list: ref count is one. Otherwise use ref count of sibling and
7415 * make ref count of sibling one (matters when inserting in front
7416 * of the list of siblings). */
7417 if (node == NULL)
7418 np->wn_refs = 1;
7419 else
7420 {
7421 np->wn_refs = node->wn_refs;
7422 node->wn_refs = 1;
7423 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007424 *prev = np;
7425 np->wn_sibling = node;
7426 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007427 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007428
Bram Moolenaar51485f02005-06-04 21:55:20 +00007429 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007430 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007431 node->wn_flags = flags;
7432 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007433 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007434 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007435 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007436 prev = &node->wn_child;
7437 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007438 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007439#ifdef SPELL_PRINTTREE
7440 smsg("Added \"%s\"", word);
7441 spell_print_tree(root->wn_sibling);
7442#endif
7443
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007444 /* count nr of words added since last message */
7445 ++spin->si_msg_count;
7446
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007447 if (spin->si_compress_cnt > 1)
7448 {
7449 if (--spin->si_compress_cnt == 1)
7450 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007451 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007452 }
7453
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007454 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007455 * When we have allocated lots of memory we need to compress the word tree
7456 * to free up some room. But compression is slow, and we might actually
7457 * need that room, thus only compress in the following situations:
7458 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007459 * "compress_start" blocks.
7460 * 2. When compressed before and used "compress_inc" blocks before
7461 * adding "compress_added" words (si_compress_cnt > 1).
7462 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007463 * (si_compress_cnt == 1) and the number of free nodes drops below the
7464 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007465 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007466#ifndef SPELL_PRINTTREE
7467 if (spin->si_compress_cnt == 1
7468 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007469 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007470#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007471 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007472 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007473 * when the freed up room has been used and another "compress_inc"
7474 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007475 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007476 spin->si_blocks_cnt -= compress_inc;
7477 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007478
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007479 if (spin->si_verbose)
7480 {
7481 msg_start();
7482 msg_puts((char_u *)_(msg_compressing));
7483 msg_clr_eos();
7484 msg_didout = FALSE;
7485 msg_col = 0;
7486 out_flush();
7487 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007488
7489 /* Compress both trees. Either they both have many nodes, which makes
7490 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007491 * compression goes fast. But when filling the souldfold word tree
7492 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007493 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007494 if (affixID >= 0)
7495 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007496 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007497
7498 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007499}
7500
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007501/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007502 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7503 * Sets "sps_flags".
7504 */
7505 int
7506spell_check_msm()
7507{
7508 char_u *p = p_msm;
7509 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007510 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007511 long added = 0;
7512
7513 if (!VIM_ISDIGIT(*p))
7514 return FAIL;
7515 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7516 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7517 if (*p != ',')
7518 return FAIL;
7519 ++p;
7520 if (!VIM_ISDIGIT(*p))
7521 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007522 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007523 if (*p != ',')
7524 return FAIL;
7525 ++p;
7526 if (!VIM_ISDIGIT(*p))
7527 return FAIL;
7528 added = getdigits(&p) * 1024;
7529 if (*p != NUL)
7530 return FAIL;
7531
Bram Moolenaar89d40322006-08-29 15:30:07 +00007532 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007533 return FAIL;
7534
7535 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007536 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007537 compress_added = added;
7538 return OK;
7539}
7540
7541
7542/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007543 * Get a wordnode_T, either from the list of previously freed nodes or
7544 * allocate a new one.
7545 */
7546 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007547get_wordnode(spin)
7548 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007549{
7550 wordnode_T *n;
7551
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007552 if (spin->si_first_free == NULL)
7553 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007554 else
7555 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007556 n = spin->si_first_free;
7557 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007558 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007559 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007560 }
7561#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007562 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007563#endif
7564 return n;
7565}
7566
7567/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007568 * Decrement the reference count on a node (which is the head of a list of
7569 * siblings). If the reference count becomes zero free the node and its
7570 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007571 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007572 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007573 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007574deref_wordnode(spin, node)
7575 spellinfo_T *spin;
7576 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007577{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007578 wordnode_T *np;
7579 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007580
7581 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007582 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007583 for (np = node; np != NULL; np = np->wn_sibling)
7584 {
7585 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007586 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007587 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007588 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007589 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007590 ++cnt; /* length field */
7591 }
7592 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007593}
7594
7595/*
7596 * Free a wordnode_T for re-use later.
7597 * Only the "wn_child" field becomes invalid.
7598 */
7599 static void
7600free_wordnode(spin, n)
7601 spellinfo_T *spin;
7602 wordnode_T *n;
7603{
7604 n->wn_child = spin->si_first_free;
7605 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007606 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007607}
7608
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007609/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007610 * Compress a tree: find tails that are identical and can be shared.
7611 */
7612 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007613wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007614 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007615 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007616{
7617 hashtab_T ht;
7618 int n;
7619 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007620 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007621
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007622 /* Skip the root itself, it's not actually used. The first sibling is the
7623 * start of the tree. */
7624 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007625 {
7626 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007627 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007628
7629#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007630 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007631#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007632 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007633 if (tot > 1000000)
7634 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007635 else if (tot == 0)
7636 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007637 else
7638 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007639 vim_snprintf((char *)IObuff, IOSIZE,
7640 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7641 n, tot, tot - n, perc);
7642 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007643 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007644#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007645 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007646#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007647 hash_clear(&ht);
7648 }
7649}
7650
7651/*
7652 * Compress a node, its siblings and its children, depth first.
7653 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007654 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007655 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007656node_compress(spin, node, ht, tot)
7657 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007658 wordnode_T *node;
7659 hashtab_T *ht;
7660 int *tot; /* total count of nodes before compressing,
7661 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007662{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007663 wordnode_T *np;
7664 wordnode_T *tp;
7665 wordnode_T *child;
7666 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007667 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007668 int len = 0;
7669 unsigned nr, n;
7670 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007671
Bram Moolenaar51485f02005-06-04 21:55:20 +00007672 /*
7673 * Go through the list of siblings. Compress each child and then try
7674 * finding an identical child to replace it.
7675 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007676 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007677 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007678 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007679 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007680 ++len;
7681 if ((child = np->wn_child) != NULL)
7682 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007683 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007684 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007685
7686 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007687 hash = hash_hash(child->wn_u1.hashkey);
7688 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007689 if (!HASHITEM_EMPTY(hi))
7690 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007691 /* There are children we encountered before with a hash value
7692 * identical to the current child. Now check if there is one
7693 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007694 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007695 if (node_equal(child, tp))
7696 {
7697 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007698 * current one. This means the current child and all
7699 * its siblings is unlinked from the tree. */
7700 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007701 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007702 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007703 break;
7704 }
7705 if (tp == NULL)
7706 {
7707 /* No other child with this hash value equals the child of
7708 * the node, add it to the linked list after the first
7709 * item. */
7710 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007711 child->wn_u2.next = tp->wn_u2.next;
7712 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007713 }
7714 }
7715 else
7716 /* No other child has this hash value, add it to the
7717 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007718 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007719 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007720 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007721 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007722
7723 /*
7724 * Make a hash key for the node and its siblings, so that we can quickly
7725 * find a lookalike node. This must be done after compressing the sibling
7726 * list, otherwise the hash key would become invalid by the compression.
7727 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007728 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007729 nr = 0;
7730 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007731 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007732 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007733 /* end node: use wn_flags, wn_region and wn_affixID */
7734 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007735 else
7736 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007737 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007738 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007739 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007740
7741 /* Avoid NUL bytes, it terminates the hash key. */
7742 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007743 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007744 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007745 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007746 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007747 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007748 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007749 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7750 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007751
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007752 /* Check for CTRL-C pressed now and then. */
7753 fast_breakcheck();
7754
Bram Moolenaar51485f02005-06-04 21:55:20 +00007755 return compressed;
7756}
7757
7758/*
7759 * Return TRUE when two nodes have identical siblings and children.
7760 */
7761 static int
7762node_equal(n1, n2)
7763 wordnode_T *n1;
7764 wordnode_T *n2;
7765{
7766 wordnode_T *p1;
7767 wordnode_T *p2;
7768
7769 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7770 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7771 if (p1->wn_byte != p2->wn_byte
7772 || (p1->wn_byte == NUL
7773 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007774 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007775 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007776 : (p1->wn_child != p2->wn_child)))
7777 break;
7778
7779 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007780}
7781
7782/*
7783 * Write a number to file "fd", MSB first, in "len" bytes.
7784 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007785 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007786put_bytes(fd, nr, len)
7787 FILE *fd;
7788 long_u nr;
7789 int len;
7790{
7791 int i;
7792
7793 for (i = len - 1; i >= 0; --i)
7794 putc((int)(nr >> (i * 8)), fd);
7795}
7796
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007797#ifdef _MSC_VER
7798# if (_MSC_VER <= 1200)
7799/* This line is required for VC6 without the service pack. Also see the
7800 * matching #pragma below. */
7801/* # pragma optimize("", off) */
7802# endif
7803#endif
7804
Bram Moolenaar4770d092006-01-12 23:22:24 +00007805/*
7806 * Write spin->si_sugtime to file "fd".
7807 */
7808 static void
7809put_sugtime(spin, fd)
7810 spellinfo_T *spin;
7811 FILE *fd;
7812{
7813 int c;
7814 int i;
7815
7816 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7817 * can't use put_bytes() here. */
7818 for (i = 7; i >= 0; --i)
7819 if (i + 1 > sizeof(time_t))
7820 /* ">>" doesn't work well when shifting more bits than avail */
7821 putc(0, fd);
7822 else
7823 {
7824 c = (unsigned)spin->si_sugtime >> (i * 8);
7825 putc(c, fd);
7826 }
7827}
7828
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007829#ifdef _MSC_VER
7830# if (_MSC_VER <= 1200)
7831/* # pragma optimize("", on) */
7832# endif
7833#endif
7834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007835static int
7836#ifdef __BORLANDC__
7837_RTLENTRYF
7838#endif
7839rep_compare __ARGS((const void *s1, const void *s2));
7840
7841/*
7842 * Function given to qsort() to sort the REP items on "from" string.
7843 */
7844 static int
7845#ifdef __BORLANDC__
7846_RTLENTRYF
7847#endif
7848rep_compare(s1, s2)
7849 const void *s1;
7850 const void *s2;
7851{
7852 fromto_T *p1 = (fromto_T *)s1;
7853 fromto_T *p2 = (fromto_T *)s2;
7854
7855 return STRCMP(p1->ft_from, p2->ft_from);
7856}
7857
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007858/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007859 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007860 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007861 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007862 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007863write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007864 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007865 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007866{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007867 FILE *fd;
7868 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007869 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007870 wordnode_T *tree;
7871 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 int i;
7873 int l;
7874 garray_T *gap;
7875 fromto_T *ftp;
7876 char_u *p;
7877 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007878 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007879
Bram Moolenaarb765d632005-06-07 21:00:02 +00007880 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007881 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007882 {
7883 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007884 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007885 }
7886
Bram Moolenaar5195e452005-08-19 20:32:47 +00007887 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007888 /* <fileID> */
7889 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007890 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007891 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007892 retval = FAIL;
7893 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007894 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007895
Bram Moolenaar5195e452005-08-19 20:32:47 +00007896 /*
7897 * <SECTIONS>: <section> ... <sectionend>
7898 */
7899
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007900 /* SN_INFO: <infotext> */
7901 if (spin->si_info != NULL)
7902 {
7903 putc(SN_INFO, fd); /* <sectionID> */
7904 putc(0, fd); /* <sectionflags> */
7905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007906 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007907 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7908 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7909 }
7910
Bram Moolenaar5195e452005-08-19 20:32:47 +00007911 /* SN_REGION: <regionname> ...
7912 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007913 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007914 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007915 putc(SN_REGION, fd); /* <sectionID> */
7916 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7917 l = spin->si_region_count * 2;
7918 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7919 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7920 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007921 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007922 }
7923 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007924 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007925
Bram Moolenaar5195e452005-08-19 20:32:47 +00007926 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7927 *
7928 * The table with character flags and the table for case folding.
7929 * This makes sure the same characters are recognized as word characters
7930 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007931 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007932 * 'encoding'.
7933 * Also skip this for an .add.spl file, the main spell file must contain
7934 * the table (avoids that it conflicts). File is shorter too.
7935 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007936 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007937 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007938 char_u folchars[128 * 8];
7939 int flags;
7940
Bram Moolenaard12a1322005-08-21 22:08:24 +00007941 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007942 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7943
7944 /* Form the <folchars> string first, we need to know its length. */
7945 l = 0;
7946 for (i = 128; i < 256; ++i)
7947 {
7948#ifdef FEAT_MBYTE
7949 if (has_mbyte)
7950 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7951 else
7952#endif
7953 folchars[l++] = spelltab.st_fold[i];
7954 }
7955 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7956
7957 fputc(128, fd); /* <charflagslen> */
7958 for (i = 128; i < 256; ++i)
7959 {
7960 flags = 0;
7961 if (spelltab.st_isw[i])
7962 flags |= CF_WORD;
7963 if (spelltab.st_isu[i])
7964 flags |= CF_UPPER;
7965 fputc(flags, fd); /* <charflags> */
7966 }
7967
7968 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7969 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007970 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007971
Bram Moolenaar5195e452005-08-19 20:32:47 +00007972 /* SN_MIDWORD: <midword> */
7973 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007974 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007975 putc(SN_MIDWORD, fd); /* <sectionID> */
7976 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7977
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007978 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007979 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007980 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7981 }
7982
Bram Moolenaar5195e452005-08-19 20:32:47 +00007983 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7984 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007985 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007986 putc(SN_PREFCOND, fd); /* <sectionID> */
7987 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7988
7989 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7990 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7991
7992 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007993 }
7994
Bram Moolenaar5195e452005-08-19 20:32:47 +00007995 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007996 * SN_SAL: <salflags> <salcount> <sal> ...
7997 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007998
Bram Moolenaar5195e452005-08-19 20:32:47 +00007999 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008000 * round 2: SN_SAL section (unless SN_SOFO is used)
8001 * round 3: SN_REPSAL section */
8002 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008003 {
8004 if (round == 1)
8005 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008006 else if (round == 2)
8007 {
8008 /* Don't write SN_SAL when using a SN_SOFO section */
8009 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8010 continue;
8011 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008012 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008014 gap = &spin->si_repsal;
8015
8016 /* Don't write the section if there are no items. */
8017 if (gap->ga_len == 0)
8018 continue;
8019
8020 /* Sort the REP/REPSAL items. */
8021 if (round != 2)
8022 qsort(gap->ga_data, (size_t)gap->ga_len,
8023 sizeof(fromto_T), rep_compare);
8024
8025 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8026 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008027
Bram Moolenaar5195e452005-08-19 20:32:47 +00008028 /* This is for making suggestions, section is not required. */
8029 putc(0, fd); /* <sectionflags> */
8030
8031 /* Compute the length of what follows. */
8032 l = 2; /* count <repcount> or <salcount> */
8033 for (i = 0; i < gap->ga_len; ++i)
8034 {
8035 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008036 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8037 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008038 }
8039 if (round == 2)
8040 ++l; /* count <salflags> */
8041 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8042
8043 if (round == 2)
8044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008045 i = 0;
8046 if (spin->si_followup)
8047 i |= SAL_F0LLOWUP;
8048 if (spin->si_collapse)
8049 i |= SAL_COLLAPSE;
8050 if (spin->si_rem_accents)
8051 i |= SAL_REM_ACCENTS;
8052 putc(i, fd); /* <salflags> */
8053 }
8054
8055 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8056 for (i = 0; i < gap->ga_len; ++i)
8057 {
8058 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8059 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8060 ftp = &((fromto_T *)gap->ga_data)[i];
8061 for (rr = 1; rr <= 2; ++rr)
8062 {
8063 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008064 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008065 putc(l, fd);
8066 fwrite(p, l, (size_t)1, fd);
8067 }
8068 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008070 }
8071
Bram Moolenaar5195e452005-08-19 20:32:47 +00008072 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8073 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008074 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8075 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008076 putc(SN_SOFO, fd); /* <sectionID> */
8077 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008079 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008080 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8081 /* <sectionlen> */
8082
8083 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8084 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008086 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008087 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8088 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008089 }
8090
Bram Moolenaar4770d092006-01-12 23:22:24 +00008091 /* SN_WORDS: <word> ...
8092 * This is for making suggestions, section is not required. */
8093 if (spin->si_commonwords.ht_used > 0)
8094 {
8095 putc(SN_WORDS, fd); /* <sectionID> */
8096 putc(0, fd); /* <sectionflags> */
8097
8098 /* round 1: count the bytes
8099 * round 2: write the bytes */
8100 for (round = 1; round <= 2; ++round)
8101 {
8102 int todo;
8103 int len = 0;
8104 hashitem_T *hi;
8105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008106 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008107 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8108 if (!HASHITEM_EMPTY(hi))
8109 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008110 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008111 len += l;
8112 if (round == 2) /* <word> */
8113 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8114 --todo;
8115 }
8116 if (round == 1)
8117 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8118 }
8119 }
8120
Bram Moolenaar5195e452005-08-19 20:32:47 +00008121 /* SN_MAP: <mapstr>
8122 * This is for making suggestions, section is not required. */
8123 if (spin->si_map.ga_len > 0)
8124 {
8125 putc(SN_MAP, fd); /* <sectionID> */
8126 putc(0, fd); /* <sectionflags> */
8127 l = spin->si_map.ga_len;
8128 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8129 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8130 /* <mapstr> */
8131 }
8132
Bram Moolenaar4770d092006-01-12 23:22:24 +00008133 /* SN_SUGFILE: <timestamp>
8134 * This is used to notify that a .sug file may be available and at the
8135 * same time allows for checking that a .sug file that is found matches
8136 * with this .spl file. That's because the word numbers must be exactly
8137 * right. */
8138 if (!spin->si_nosugfile
8139 && (spin->si_sal.ga_len > 0
8140 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8141 {
8142 putc(SN_SUGFILE, fd); /* <sectionID> */
8143 putc(0, fd); /* <sectionflags> */
8144 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8145
8146 /* Set si_sugtime and write it to the file. */
8147 spin->si_sugtime = time(NULL);
8148 put_sugtime(spin, fd); /* <timestamp> */
8149 }
8150
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008151 /* SN_NOSPLITSUGS: nothing
8152 * This is used to notify that no suggestions with word splits are to be
8153 * made. */
8154 if (spin->si_nosplitsugs)
8155 {
8156 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8157 putc(0, fd); /* <sectionflags> */
8158 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8159 }
8160
Bram Moolenaar5195e452005-08-19 20:32:47 +00008161 /* SN_COMPOUND: compound info.
8162 * We don't mark it required, when not supported all compound words will
8163 * be bad words. */
8164 if (spin->si_compflags != NULL)
8165 {
8166 putc(SN_COMPOUND, fd); /* <sectionID> */
8167 putc(0, fd); /* <sectionflags> */
8168
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008169 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008170 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008171 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008172 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8173
Bram Moolenaar5195e452005-08-19 20:32:47 +00008174 putc(spin->si_compmax, fd); /* <compmax> */
8175 putc(spin->si_compminlen, fd); /* <compminlen> */
8176 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008177 putc(0, fd); /* for Vim 7.0b compatibility */
8178 putc(spin->si_compoptions, fd); /* <compoptions> */
8179 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8180 /* <comppatcount> */
8181 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8182 {
8183 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008184 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008185 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8186 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008187 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008188 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8189 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008190 }
8191
Bram Moolenaar78622822005-08-23 21:00:13 +00008192 /* SN_NOBREAK: NOBREAK flag */
8193 if (spin->si_nobreak)
8194 {
8195 putc(SN_NOBREAK, fd); /* <sectionID> */
8196 putc(0, fd); /* <sectionflags> */
8197
8198 /* It's empty, the precense of the section flags the feature. */
8199 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8200 }
8201
Bram Moolenaar5195e452005-08-19 20:32:47 +00008202 /* SN_SYLLABLE: syllable info.
8203 * We don't mark it required, when not supported syllables will not be
8204 * counted. */
8205 if (spin->si_syllable != NULL)
8206 {
8207 putc(SN_SYLLABLE, fd); /* <sectionID> */
8208 putc(0, fd); /* <sectionflags> */
8209
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008210 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008211 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8212 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8213 }
8214
8215 /* end of <SECTIONS> */
8216 putc(SN_END, fd); /* <sectionend> */
8217
Bram Moolenaar50cde822005-06-05 21:54:54 +00008218
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008219 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008220 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008221 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008222 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008223 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008224 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008225 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008226 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008227 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008228 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008229 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008230 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008231
Bram Moolenaar0c405862005-06-22 22:26:26 +00008232 /* Clear the index and wnode fields in the tree. */
8233 clear_node(tree);
8234
Bram Moolenaar51485f02005-06-04 21:55:20 +00008235 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008236 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008237 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008238 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008239
Bram Moolenaar51485f02005-06-04 21:55:20 +00008240 /* number of nodes in 4 bytes */
8241 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008242 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008243
Bram Moolenaar51485f02005-06-04 21:55:20 +00008244 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008245 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008246 }
8247
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008248 /* Write another byte to check for errors. */
8249 if (putc(0, fd) == EOF)
8250 retval = FAIL;
8251
8252 if (fclose(fd) == EOF)
8253 retval = FAIL;
8254
8255 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008256}
8257
8258/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008259 * Clear the index and wnode fields of "node", it siblings and its
8260 * children. This is needed because they are a union with other items to save
8261 * space.
8262 */
8263 static void
8264clear_node(node)
8265 wordnode_T *node;
8266{
8267 wordnode_T *np;
8268
8269 if (node != NULL)
8270 for (np = node; np != NULL; np = np->wn_sibling)
8271 {
8272 np->wn_u1.index = 0;
8273 np->wn_u2.wnode = NULL;
8274
8275 if (np->wn_byte != NUL)
8276 clear_node(np->wn_child);
8277 }
8278}
8279
8280
8281/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008282 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008283 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008284 * This first writes the list of possible bytes (siblings). Then for each
8285 * byte recursively write the children.
8286 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008287 * NOTE: The code here must match the code in read_tree_node(), since
8288 * assumptions are made about the indexes (so that we don't have to write them
8289 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008290 *
8291 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008292 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008293 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008294put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008295 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008296 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008297 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008298 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008299 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008300{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008301 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008302 int siblingcount = 0;
8303 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008304 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008305
Bram Moolenaar51485f02005-06-04 21:55:20 +00008306 /* If "node" is zero the tree is empty. */
8307 if (node == NULL)
8308 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008309
Bram Moolenaar51485f02005-06-04 21:55:20 +00008310 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008311 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008312
8313 /* Count the number of siblings. */
8314 for (np = node; np != NULL; np = np->wn_sibling)
8315 ++siblingcount;
8316
8317 /* Write the sibling count. */
8318 if (fd != NULL)
8319 putc(siblingcount, fd); /* <siblingcount> */
8320
8321 /* Write each sibling byte and optionally extra info. */
8322 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008323 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008324 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008325 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008326 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008327 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008328 /* For a NUL byte (end of word) write the flags etc. */
8329 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008330 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008331 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008332 * associated condition nr (stored in wn_region). The
8333 * byte value is misused to store the "rare" and "not
8334 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008335 if (np->wn_flags == (short_u)PFX_FLAGS)
8336 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008337 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008338 {
8339 putc(BY_FLAGS, fd); /* <byte> */
8340 putc(np->wn_flags, fd); /* <pflags> */
8341 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008342 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008343 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008344 }
8345 else
8346 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008347 /* For word trees we write the flag/region items. */
8348 flags = np->wn_flags;
8349 if (regionmask != 0 && np->wn_region != regionmask)
8350 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008351 if (np->wn_affixID != 0)
8352 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008353 if (flags == 0)
8354 {
8355 /* word without flags or region */
8356 putc(BY_NOFLAGS, fd); /* <byte> */
8357 }
8358 else
8359 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008360 if (np->wn_flags >= 0x100)
8361 {
8362 putc(BY_FLAGS2, fd); /* <byte> */
8363 putc(flags, fd); /* <flags> */
8364 putc((unsigned)flags >> 8, fd); /* <flags2> */
8365 }
8366 else
8367 {
8368 putc(BY_FLAGS, fd); /* <byte> */
8369 putc(flags, fd); /* <flags> */
8370 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008371 if (flags & WF_REGION)
8372 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008373 if (flags & WF_AFX)
8374 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008375 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008376 }
8377 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008378 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008379 else
8380 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008381 if (np->wn_child->wn_u1.index != 0
8382 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008383 {
8384 /* The child is written elsewhere, write the reference. */
8385 if (fd != NULL)
8386 {
8387 putc(BY_INDEX, fd); /* <byte> */
8388 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008389 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008390 }
8391 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008392 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008393 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008394 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008395
Bram Moolenaar51485f02005-06-04 21:55:20 +00008396 if (fd != NULL)
8397 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8398 {
8399 EMSG(_(e_write));
8400 return 0;
8401 }
8402 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008403 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008404
8405 /* Space used in the array when reading: one for each sibling and one for
8406 * the count. */
8407 newindex += siblingcount + 1;
8408
8409 /* Recursively dump the children of each sibling. */
8410 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008411 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8412 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008413 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008414
8415 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008416}
8417
8418
8419/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008420 * ":mkspell [-ascii] outfile infile ..."
8421 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008422 */
8423 void
8424ex_mkspell(eap)
8425 exarg_T *eap;
8426{
8427 int fcount;
8428 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008429 char_u *arg = eap->arg;
8430 int ascii = FALSE;
8431
8432 if (STRNCMP(arg, "-ascii", 6) == 0)
8433 {
8434 ascii = TRUE;
8435 arg = skipwhite(arg + 6);
8436 }
8437
8438 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8439 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008442 FreeWild(fcount, fnames);
8443 }
8444}
8445
8446/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008447 * Create the .sug file.
8448 * Uses the soundfold info in "spin".
8449 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8450 */
8451 static void
8452spell_make_sugfile(spin, wfname)
8453 spellinfo_T *spin;
8454 char_u *wfname;
8455{
8456 char_u fname[MAXPATHL];
8457 int len;
8458 slang_T *slang;
8459 int free_slang = FALSE;
8460
8461 /*
8462 * Read back the .spl file that was written. This fills the required
8463 * info for soundfolding. This also uses less memory than the
8464 * pointer-linked version of the trie. And it avoids having two versions
8465 * of the code for the soundfolding stuff.
8466 * It might have been done already by spell_reload_one().
8467 */
8468 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8469 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8470 break;
8471 if (slang == NULL)
8472 {
8473 spell_message(spin, (char_u *)_("Reading back spell file..."));
8474 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8475 if (slang == NULL)
8476 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008477 free_slang = TRUE;
8478 }
8479
8480 /*
8481 * Clear the info in "spin" that is used.
8482 */
8483 spin->si_blocks = NULL;
8484 spin->si_blocks_cnt = 0;
8485 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8486 spin->si_free_count = 0;
8487 spin->si_first_free = NULL;
8488 spin->si_foldwcount = 0;
8489
8490 /*
8491 * Go through the trie of good words, soundfold each word and add it to
8492 * the soundfold trie.
8493 */
8494 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8495 if (sug_filltree(spin, slang) == FAIL)
8496 goto theend;
8497
8498 /*
8499 * Create the table which links each soundfold word with a list of the
8500 * good words it may come from. Creates buffer "spin->si_spellbuf".
8501 * This also removes the wordnr from the NUL byte entries to make
8502 * compression possible.
8503 */
8504 if (sug_maketable(spin) == FAIL)
8505 goto theend;
8506
8507 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8508 (long)spin->si_spellbuf->b_ml.ml_line_count);
8509
8510 /*
8511 * Compress the soundfold trie.
8512 */
8513 spell_message(spin, (char_u *)_(msg_compressing));
8514 wordtree_compress(spin, spin->si_foldroot);
8515
8516 /*
8517 * Write the .sug file.
8518 * Make the file name by changing ".spl" to ".sug".
8519 */
8520 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008521 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008522 fname[len - 2] = 'u';
8523 fname[len - 1] = 'g';
8524 sug_write(spin, fname);
8525
8526theend:
8527 if (free_slang)
8528 slang_free(slang);
8529 free_blocks(spin->si_blocks);
8530 close_spellbuf(spin->si_spellbuf);
8531}
8532
8533/*
8534 * Build the soundfold trie for language "slang".
8535 */
8536 static int
8537sug_filltree(spin, slang)
8538 spellinfo_T *spin;
8539 slang_T *slang;
8540{
8541 char_u *byts;
8542 idx_T *idxs;
8543 int depth;
8544 idx_T arridx[MAXWLEN];
8545 int curi[MAXWLEN];
8546 char_u tword[MAXWLEN];
8547 char_u tsalword[MAXWLEN];
8548 int c;
8549 idx_T n;
8550 unsigned words_done = 0;
8551 int wordcount[MAXWLEN];
8552
8553 /* We use si_foldroot for the souldfolded trie. */
8554 spin->si_foldroot = wordtree_alloc(spin);
8555 if (spin->si_foldroot == NULL)
8556 return FAIL;
8557
8558 /* let tree_add_word() know we're adding to the soundfolded tree */
8559 spin->si_sugtree = TRUE;
8560
8561 /*
8562 * Go through the whole case-folded tree, soundfold each word and put it
8563 * in the trie.
8564 */
8565 byts = slang->sl_fbyts;
8566 idxs = slang->sl_fidxs;
8567
8568 arridx[0] = 0;
8569 curi[0] = 1;
8570 wordcount[0] = 0;
8571
8572 depth = 0;
8573 while (depth >= 0 && !got_int)
8574 {
8575 if (curi[depth] > byts[arridx[depth]])
8576 {
8577 /* Done all bytes at this node, go up one level. */
8578 idxs[arridx[depth]] = wordcount[depth];
8579 if (depth > 0)
8580 wordcount[depth - 1] += wordcount[depth];
8581
8582 --depth;
8583 line_breakcheck();
8584 }
8585 else
8586 {
8587
8588 /* Do one more byte at this node. */
8589 n = arridx[depth] + curi[depth];
8590 ++curi[depth];
8591
8592 c = byts[n];
8593 if (c == 0)
8594 {
8595 /* Sound-fold the word. */
8596 tword[depth] = NUL;
8597 spell_soundfold(slang, tword, TRUE, tsalword);
8598
8599 /* We use the "flags" field for the MSB of the wordnr,
8600 * "region" for the LSB of the wordnr. */
8601 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8602 words_done >> 16, words_done & 0xffff,
8603 0) == FAIL)
8604 return FAIL;
8605
8606 ++words_done;
8607 ++wordcount[depth];
8608
8609 /* Reset the block count each time to avoid compression
8610 * kicking in. */
8611 spin->si_blocks_cnt = 0;
8612
8613 /* Skip over any other NUL bytes (same word with different
8614 * flags). */
8615 while (byts[n + 1] == 0)
8616 {
8617 ++n;
8618 ++curi[depth];
8619 }
8620 }
8621 else
8622 {
8623 /* Normal char, go one level deeper. */
8624 tword[depth++] = c;
8625 arridx[depth] = idxs[n];
8626 curi[depth] = 1;
8627 wordcount[depth] = 0;
8628 }
8629 }
8630 }
8631
8632 smsg((char_u *)_("Total number of words: %d"), words_done);
8633
8634 return OK;
8635}
8636
8637/*
8638 * Make the table that links each word in the soundfold trie to the words it
8639 * can be produced from.
8640 * This is not unlike lines in a file, thus use a memfile to be able to access
8641 * the table efficiently.
8642 * Returns FAIL when out of memory.
8643 */
8644 static int
8645sug_maketable(spin)
8646 spellinfo_T *spin;
8647{
8648 garray_T ga;
8649 int res = OK;
8650
8651 /* Allocate a buffer, open a memline for it and create the swap file
8652 * (uses a temp file, not a .swp file). */
8653 spin->si_spellbuf = open_spellbuf();
8654 if (spin->si_spellbuf == NULL)
8655 return FAIL;
8656
8657 /* Use a buffer to store the line info, avoids allocating many small
8658 * pieces of memory. */
8659 ga_init2(&ga, 1, 100);
8660
8661 /* recursively go through the tree */
8662 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8663 res = FAIL;
8664
8665 ga_clear(&ga);
8666 return res;
8667}
8668
8669/*
8670 * Fill the table for one node and its children.
8671 * Returns the wordnr at the start of the node.
8672 * Returns -1 when out of memory.
8673 */
8674 static int
8675sug_filltable(spin, node, startwordnr, gap)
8676 spellinfo_T *spin;
8677 wordnode_T *node;
8678 int startwordnr;
8679 garray_T *gap; /* place to store line of numbers */
8680{
8681 wordnode_T *p, *np;
8682 int wordnr = startwordnr;
8683 int nr;
8684 int prev_nr;
8685
8686 for (p = node; p != NULL; p = p->wn_sibling)
8687 {
8688 if (p->wn_byte == NUL)
8689 {
8690 gap->ga_len = 0;
8691 prev_nr = 0;
8692 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8693 {
8694 if (ga_grow(gap, 10) == FAIL)
8695 return -1;
8696
8697 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8698 /* Compute the offset from the previous nr and store the
8699 * offset in a way that it takes a minimum number of bytes.
8700 * It's a bit like utf-8, but without the need to mark
8701 * following bytes. */
8702 nr -= prev_nr;
8703 prev_nr += nr;
8704 gap->ga_len += offset2bytes(nr,
8705 (char_u *)gap->ga_data + gap->ga_len);
8706 }
8707
8708 /* add the NUL byte */
8709 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8710
8711 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8712 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8713 return -1;
8714 ++wordnr;
8715
8716 /* Remove extra NUL entries, we no longer need them. We don't
8717 * bother freeing the nodes, the won't be reused anyway. */
8718 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8719 p->wn_sibling = p->wn_sibling->wn_sibling;
8720
8721 /* Clear the flags on the remaining NUL node, so that compression
8722 * works a lot better. */
8723 p->wn_flags = 0;
8724 p->wn_region = 0;
8725 }
8726 else
8727 {
8728 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8729 if (wordnr == -1)
8730 return -1;
8731 }
8732 }
8733 return wordnr;
8734}
8735
8736/*
8737 * Convert an offset into a minimal number of bytes.
8738 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8739 * bytes.
8740 */
8741 static int
8742offset2bytes(nr, buf)
8743 int nr;
8744 char_u *buf;
8745{
8746 int rem;
8747 int b1, b2, b3, b4;
8748
8749 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8750 b1 = nr % 255 + 1;
8751 rem = nr / 255;
8752 b2 = rem % 255 + 1;
8753 rem = rem / 255;
8754 b3 = rem % 255 + 1;
8755 b4 = rem / 255 + 1;
8756
8757 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8758 {
8759 buf[0] = 0xe0 + b4;
8760 buf[1] = b3;
8761 buf[2] = b2;
8762 buf[3] = b1;
8763 return 4;
8764 }
8765 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8766 {
8767 buf[0] = 0xc0 + b3;
8768 buf[1] = b2;
8769 buf[2] = b1;
8770 return 3;
8771 }
8772 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8773 {
8774 buf[0] = 0x80 + b2;
8775 buf[1] = b1;
8776 return 2;
8777 }
8778 /* 1 byte */
8779 buf[0] = b1;
8780 return 1;
8781}
8782
8783/*
8784 * Opposite of offset2bytes().
8785 * "pp" points to the bytes and is advanced over it.
8786 * Returns the offset.
8787 */
8788 static int
8789bytes2offset(pp)
8790 char_u **pp;
8791{
8792 char_u *p = *pp;
8793 int nr;
8794 int c;
8795
8796 c = *p++;
8797 if ((c & 0x80) == 0x00) /* 1 byte */
8798 {
8799 nr = c - 1;
8800 }
8801 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8802 {
8803 nr = (c & 0x3f) - 1;
8804 nr = nr * 255 + (*p++ - 1);
8805 }
8806 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8807 {
8808 nr = (c & 0x1f) - 1;
8809 nr = nr * 255 + (*p++ - 1);
8810 nr = nr * 255 + (*p++ - 1);
8811 }
8812 else /* 4 bytes */
8813 {
8814 nr = (c & 0x0f) - 1;
8815 nr = nr * 255 + (*p++ - 1);
8816 nr = nr * 255 + (*p++ - 1);
8817 nr = nr * 255 + (*p++ - 1);
8818 }
8819
8820 *pp = p;
8821 return nr;
8822}
8823
8824/*
8825 * Write the .sug file in "fname".
8826 */
8827 static void
8828sug_write(spin, fname)
8829 spellinfo_T *spin;
8830 char_u *fname;
8831{
8832 FILE *fd;
8833 wordnode_T *tree;
8834 int nodecount;
8835 int wcount;
8836 char_u *line;
8837 linenr_T lnum;
8838 int len;
8839
8840 /* Create the file. Note that an existing file is silently overwritten! */
8841 fd = mch_fopen((char *)fname, "w");
8842 if (fd == NULL)
8843 {
8844 EMSG2(_(e_notopen), fname);
8845 return;
8846 }
8847
8848 vim_snprintf((char *)IObuff, IOSIZE,
8849 _("Writing suggestion file %s ..."), fname);
8850 spell_message(spin, IObuff);
8851
8852 /*
8853 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8854 */
8855 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8856 {
8857 EMSG(_(e_write));
8858 goto theend;
8859 }
8860 putc(VIMSUGVERSION, fd); /* <versionnr> */
8861
8862 /* Write si_sugtime to the file. */
8863 put_sugtime(spin, fd); /* <timestamp> */
8864
8865 /*
8866 * <SUGWORDTREE>
8867 */
8868 spin->si_memtot = 0;
8869 tree = spin->si_foldroot->wn_sibling;
8870
8871 /* Clear the index and wnode fields in the tree. */
8872 clear_node(tree);
8873
8874 /* Count the number of nodes. Needed to be able to allocate the
8875 * memory when reading the nodes. Also fills in index for shared
8876 * nodes. */
8877 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8878
8879 /* number of nodes in 4 bytes */
8880 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8881 spin->si_memtot += nodecount + nodecount * sizeof(int);
8882
8883 /* Write the nodes. */
8884 (void)put_node(fd, tree, 0, 0, FALSE);
8885
8886 /*
8887 * <SUGTABLE>: <sugwcount> <sugline> ...
8888 */
8889 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8890 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8891
8892 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8893 {
8894 /* <sugline>: <sugnr> ... NUL */
8895 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008896 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008897 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8898 {
8899 EMSG(_(e_write));
8900 goto theend;
8901 }
8902 spin->si_memtot += len;
8903 }
8904
8905 /* Write another byte to check for errors. */
8906 if (putc(0, fd) == EOF)
8907 EMSG(_(e_write));
8908
8909 vim_snprintf((char *)IObuff, IOSIZE,
8910 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8911 spell_message(spin, IObuff);
8912
8913theend:
8914 /* close the file */
8915 fclose(fd);
8916}
8917
8918/*
8919 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8920 * list and only contains text lines. Can use a swapfile to reduce memory
8921 * use.
8922 * Most other fields are invalid! Esp. watch out for string options being
8923 * NULL and there is no undo info.
8924 * Returns NULL when out of memory.
8925 */
8926 static buf_T *
8927open_spellbuf()
8928{
8929 buf_T *buf;
8930
8931 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8932 if (buf != NULL)
8933 {
8934 buf->b_spell = TRUE;
8935 buf->b_p_swf = TRUE; /* may create a swap file */
8936 ml_open(buf);
8937 ml_open_file(buf); /* create swap file now */
8938 }
8939 return buf;
8940}
8941
8942/*
8943 * Close the buffer used for spell info.
8944 */
8945 static void
8946close_spellbuf(buf)
8947 buf_T *buf;
8948{
8949 if (buf != NULL)
8950 {
8951 ml_close(buf, TRUE);
8952 vim_free(buf);
8953 }
8954}
8955
8956
8957/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008958 * Create a Vim spell file from one or more word lists.
8959 * "fnames[0]" is the output file name.
8960 * "fnames[fcount - 1]" is the last input file name.
8961 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8962 * and ".spl" is appended to make the output file name.
8963 */
8964 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008966 int fcount;
8967 char_u **fnames;
8968 int ascii; /* -ascii argument given */
8969 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008971{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008972 char_u fname[MAXPATHL];
8973 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008974 char_u **innames;
8975 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008976 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008977 int i;
8978 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008979 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008980 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008981 spellinfo_T spin;
8982
8983 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008985 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 spin.si_followup = TRUE;
8987 spin.si_rem_accents = TRUE;
8988 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008989 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8991 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008992 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008993 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008994 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008995 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008996
Bram Moolenaarb765d632005-06-07 21:00:02 +00008997 /* default: fnames[0] is output file, following are input files */
8998 innames = &fnames[1];
8999 incount = fcount - 1;
9000
9001 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009002 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009003 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009004 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9005 {
9006 /* For ":mkspell path/en.latin1.add" output file is
9007 * "path/en.latin1.add.spl". */
9008 innames = &fnames[0];
9009 incount = 1;
9010 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9011 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009012 else if (fcount == 1)
9013 {
9014 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9015 innames = &fnames[0];
9016 incount = 1;
9017 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9018 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9019 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009020 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9021 {
9022 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009024 }
9025 else
9026 /* Name should be language, make the file name from it. */
9027 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9028 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9029
9030 /* Check for .ascii.spl. */
9031 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9032 spin.si_ascii = TRUE;
9033
9034 /* Check for .add.spl. */
9035 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9036 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009037 }
9038
Bram Moolenaarb765d632005-06-07 21:00:02 +00009039 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009040 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009041 else if (vim_strchr(gettail(wfname), '_') != NULL)
9042 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009043 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009044 EMSG(_("E754: Only up to 8 regions supported"));
9045 else
9046 {
9047 /* Check for overwriting before doing things that may take a lot of
9048 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009049 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009050 {
9051 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009052 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009053 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009054 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009055 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009056 EMSG2(_(e_isadir2), wfname);
9057 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009058 }
9059
9060 /*
9061 * Init the aff and dic pointers.
9062 * Get the region names if there are more than 2 arguments.
9063 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009064 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009065 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009066 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009067
Bram Moolenaar3982c542005-06-08 21:56:31 +00009068 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009069 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009070 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009071 if (STRLEN(gettail(innames[i])) < 5
9072 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009073 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009074 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9075 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009076 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009077 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9078 spin.si_region_name[i * 2 + 1] =
9079 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009080 }
9081 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009082 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009083
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009084 spin.si_foldroot = wordtree_alloc(&spin);
9085 spin.si_keeproot = wordtree_alloc(&spin);
9086 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009087 if (spin.si_foldroot == NULL
9088 || spin.si_keeproot == NULL
9089 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009090 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009091 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009092 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009093 }
9094
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009095 /* When not producing a .add.spl file clear the character table when
9096 * we encounter one in the .aff file. This means we dump the current
9097 * one in the .spl file if the .aff file doesn't define one. That's
9098 * better than guessing the contents, the table will match a
9099 * previously loaded spell file. */
9100 if (!spin.si_add)
9101 spin.si_clear_chartab = TRUE;
9102
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009103 /*
9104 * Read all the .aff and .dic files.
9105 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009106 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009107 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009108 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009110 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009111 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009112
Bram Moolenaarb765d632005-06-07 21:00:02 +00009113 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009114 if (mch_stat((char *)fname, &st) >= 0)
9115 {
9116 /* Read the .aff file. Will init "spin->si_conv" based on the
9117 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009118 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009119 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009120 error = TRUE;
9121 else
9122 {
9123 /* Read the .dic file and store the words in the trees. */
9124 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009125 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009126 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009127 error = TRUE;
9128 }
9129 }
9130 else
9131 {
9132 /* No .aff file, try reading the file as a word list. Store
9133 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009134 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009135 error = TRUE;
9136 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009137
Bram Moolenaarb765d632005-06-07 21:00:02 +00009138#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009139 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009140 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009141#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009142 }
9143
Bram Moolenaar78622822005-08-23 21:00:13 +00009144 if (spin.si_compflags != NULL && spin.si_nobreak)
9145 MSG(_("Warning: both compounding and NOBREAK specified"));
9146
Bram Moolenaar4770d092006-01-12 23:22:24 +00009147 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009148 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009149 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009150 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009151 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009152 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009153 wordtree_compress(&spin, spin.si_foldroot);
9154 wordtree_compress(&spin, spin.si_keeproot);
9155 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009156 }
9157
Bram Moolenaar4770d092006-01-12 23:22:24 +00009158 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009159 {
9160 /*
9161 * Write the info in the spell file.
9162 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009163 vim_snprintf((char *)IObuff, IOSIZE,
9164 _("Writing spell file %s ..."), wfname);
9165 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009166
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009167 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009168
Bram Moolenaar4770d092006-01-12 23:22:24 +00009169 spell_message(&spin, (char_u *)_("Done!"));
9170 vim_snprintf((char *)IObuff, IOSIZE,
9171 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9172 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009173
Bram Moolenaar4770d092006-01-12 23:22:24 +00009174 /*
9175 * If the file is loaded need to reload it.
9176 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009177 if (!error)
9178 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009179 }
9180
9181 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009183 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009184 ga_clear(&spin.si_sal);
9185 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009186 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009187 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009188 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009189
9190 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009191 for (i = 0; i < incount; ++i)
9192 if (afile[i] != NULL)
9193 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009194
9195 /* Free all the bits and pieces at once. */
9196 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009197
9198 /*
9199 * If there is soundfolding info and no NOSUGFILE item create the
9200 * .sug file with the soundfolded word trie.
9201 */
9202 if (spin.si_sugtime != 0 && !error && !got_int)
9203 spell_make_sugfile(&spin, wfname);
9204
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009205 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009206}
9207
Bram Moolenaar4770d092006-01-12 23:22:24 +00009208/*
9209 * Display a message for spell file processing when 'verbose' is set or using
9210 * ":mkspell". "str" can be IObuff.
9211 */
9212 static void
9213spell_message(spin, str)
9214 spellinfo_T *spin;
9215 char_u *str;
9216{
9217 if (spin->si_verbose || p_verbose > 2)
9218 {
9219 if (!spin->si_verbose)
9220 verbose_enter();
9221 MSG(str);
9222 out_flush();
9223 if (!spin->si_verbose)
9224 verbose_leave();
9225 }
9226}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009227
9228/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009229 * ":[count]spellgood {word}"
9230 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009231 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009232 */
9233 void
9234ex_spell(eap)
9235 exarg_T *eap;
9236{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009237 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009238 eap->forceit ? 0 : (int)eap->line2,
9239 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009240}
9241
9242/*
9243 * Add "word[len]" to 'spellfile' as a good or bad word.
9244 */
9245 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009246spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009247 char_u *word;
9248 int len;
9249 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009250 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009251 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009252 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009253{
Bram Moolenaara3917072006-09-14 08:48:14 +00009254 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009255 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009256 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009257 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009258 char_u fnamebuf[MAXPATHL];
9259 char_u line[MAXWLEN * 2];
9260 long fpos, fpos_next = 0;
9261 int i;
9262 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009263
Bram Moolenaar89d40322006-08-29 15:30:07 +00009264 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009265 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009266 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009267 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009268 int_wordlist = vim_tempname('s');
9269 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009270 return;
9271 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009272 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009273 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009274 else
9275 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009276 /* If 'spellfile' isn't set figure out a good default value. */
9277 if (*curbuf->b_p_spf == NUL)
9278 {
9279 init_spellfile();
9280 new_spf = TRUE;
9281 }
9282
9283 if (*curbuf->b_p_spf == NUL)
9284 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009285 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009286 return;
9287 }
9288
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009289 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9290 {
9291 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009292 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009293 break;
9294 if (*spf == NUL)
9295 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009296 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009297 return;
9298 }
9299 }
9300
Bram Moolenaarb765d632005-06-07 21:00:02 +00009301 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009302 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009303 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9304 buf = NULL;
9305 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009306 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009307 EMSG(_(e_bufloaded));
9308 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009309 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009310
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009311 fname = fnamebuf;
9312 }
9313
Bram Moolenaard0131a82006-03-04 21:46:13 +00009314 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009315 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009316 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009317 * since its flags sort before the one with WF_BANNED. */
9318 fd = mch_fopen((char *)fname, "r");
9319 if (fd != NULL)
9320 {
9321 while (!vim_fgets(line, MAXWLEN * 2, fd))
9322 {
9323 fpos = fpos_next;
9324 fpos_next = ftell(fd);
9325 if (STRNCMP(word, line, len) == 0
9326 && (line[len] == '/' || line[len] < ' '))
9327 {
9328 /* Found duplicate word. Remove it by writing a '#' at
9329 * the start of the line. Mixing reading and writing
9330 * doesn't work for all systems, close the file first. */
9331 fclose(fd);
9332 fd = mch_fopen((char *)fname, "r+");
9333 if (fd == NULL)
9334 break;
9335 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009336 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009337 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009338 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009339 {
9340 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009341 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009342 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009343 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009344 fseek(fd, fpos_next, SEEK_SET);
9345 }
9346 }
9347 fclose(fd);
9348 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009349 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009350
9351 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009352 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009353 fd = mch_fopen((char *)fname, "a");
9354 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009355 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009356 char_u *p;
9357
Bram Moolenaard0131a82006-03-04 21:46:13 +00009358 /* We just initialized the 'spellfile' option and can't open the
9359 * file. We may need to create the "spell" directory first. We
9360 * already checked the runtime directory is writable in
9361 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009362 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009363 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009364 int c = *p;
9365
Bram Moolenaard0131a82006-03-04 21:46:13 +00009366 /* The directory doesn't exist. Try creating it and opening
9367 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009368 *p = NUL;
9369 vim_mkdir(fname, 0755);
9370 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009371 fd = mch_fopen((char *)fname, "a");
9372 }
9373 }
9374
9375 if (fd == NULL)
9376 EMSG2(_(e_notopen), fname);
9377 else
9378 {
9379 if (bad)
9380 fprintf(fd, "%.*s/!\n", len, word);
9381 else
9382 fprintf(fd, "%.*s\n", len, word);
9383 fclose(fd);
9384
9385 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9386 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009387 }
9388 }
9389
Bram Moolenaard0131a82006-03-04 21:46:13 +00009390 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009391 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009392 /* Update the .add.spl file. */
9393 mkspell(1, &fname, FALSE, TRUE, TRUE);
9394
9395 /* If the .add file is edited somewhere, reload it. */
9396 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009397 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009398
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009399 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009400 }
9401}
9402
9403/*
9404 * Initialize 'spellfile' for the current buffer.
9405 */
9406 static void
9407init_spellfile()
9408{
9409 char_u buf[MAXPATHL];
9410 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009411 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009412 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009413 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009414 int aspath = FALSE;
9415 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009416
9417 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9418 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009419 /* Find the end of the language name. Exclude the region. If there
9420 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009421 for (lend = curbuf->b_p_spl; *lend != NUL
9422 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009423 if (vim_ispathsep(*lend))
9424 {
9425 aspath = TRUE;
9426 lstart = lend + 1;
9427 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009428
9429 /* Loop over all entries in 'runtimepath'. Use the first one where we
9430 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009431 rtp = p_rtp;
9432 while (*rtp != NUL)
9433 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009434 if (aspath)
9435 /* Use directory of an entry with path, e.g., for
9436 * "/dir/lg.utf-8.spl" use "/dir". */
9437 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9438 else
9439 /* Copy the path from 'runtimepath' to buf[]. */
9440 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009441 if (filewritable(buf) == 2)
9442 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009443 /* Use the first language name from 'spelllang' and the
9444 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009445 if (aspath)
9446 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9447 else
9448 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009449 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009450 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009451 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9452 if (!filewritable(buf) != 2)
9453 vim_mkdir(buf, 0755);
9454
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009455 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009456 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009457 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009458 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009459 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009460 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9461 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9462 fname != NULL
9463 && strstr((char *)gettail(fname), ".ascii.") != NULL
9464 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009465 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9466 break;
9467 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009468 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009469 }
9470 }
9471}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009472
Bram Moolenaar51485f02005-06-04 21:55:20 +00009473
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009474/*
9475 * Init the chartab used for spelling for ASCII.
9476 * EBCDIC is not supported!
9477 */
9478 static void
9479clear_spell_chartab(sp)
9480 spelltab_T *sp;
9481{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009482 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009483
9484 /* Init everything to FALSE. */
9485 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9486 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9487 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009488 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009489 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009490 sp->st_upper[i] = i;
9491 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009492
9493 /* We include digits. A word shouldn't start with a digit, but handling
9494 * that is done separately. */
9495 for (i = '0'; i <= '9'; ++i)
9496 sp->st_isw[i] = TRUE;
9497 for (i = 'A'; i <= 'Z'; ++i)
9498 {
9499 sp->st_isw[i] = TRUE;
9500 sp->st_isu[i] = TRUE;
9501 sp->st_fold[i] = i + 0x20;
9502 }
9503 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009504 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009505 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009506 sp->st_upper[i] = i - 0x20;
9507 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009508}
9509
9510/*
9511 * Init the chartab used for spelling. Only depends on 'encoding'.
9512 * Called once while starting up and when 'encoding' changes.
9513 * The default is to use isalpha(), but the spell file should define the word
9514 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009515 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009516 */
9517 void
9518init_spell_chartab()
9519{
9520 int i;
9521
9522 did_set_spelltab = FALSE;
9523 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009524#ifdef FEAT_MBYTE
9525 if (enc_dbcs)
9526 {
9527 /* DBCS: assume double-wide characters are word characters. */
9528 for (i = 128; i <= 255; ++i)
9529 if (MB_BYTE2LEN(i) == 2)
9530 spelltab.st_isw[i] = TRUE;
9531 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009532 else if (enc_utf8)
9533 {
9534 for (i = 128; i < 256; ++i)
9535 {
9536 spelltab.st_isu[i] = utf_isupper(i);
9537 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9538 spelltab.st_fold[i] = utf_fold(i);
9539 spelltab.st_upper[i] = utf_toupper(i);
9540 }
9541 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009542 else
9543#endif
9544 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009545 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009546 for (i = 128; i < 256; ++i)
9547 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009548 if (MB_ISUPPER(i))
9549 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009550 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009551 spelltab.st_isu[i] = TRUE;
9552 spelltab.st_fold[i] = MB_TOLOWER(i);
9553 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009554 else if (MB_ISLOWER(i))
9555 {
9556 spelltab.st_isw[i] = TRUE;
9557 spelltab.st_upper[i] = MB_TOUPPER(i);
9558 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009559 }
9560 }
9561}
9562
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009563/*
9564 * Set the spell character tables from strings in the affix file.
9565 */
9566 static int
9567set_spell_chartab(fol, low, upp)
9568 char_u *fol;
9569 char_u *low;
9570 char_u *upp;
9571{
9572 /* We build the new tables here first, so that we can compare with the
9573 * previous one. */
9574 spelltab_T new_st;
9575 char_u *pf = fol, *pl = low, *pu = upp;
9576 int f, l, u;
9577
9578 clear_spell_chartab(&new_st);
9579
9580 while (*pf != NUL)
9581 {
9582 if (*pl == NUL || *pu == NUL)
9583 {
9584 EMSG(_(e_affform));
9585 return FAIL;
9586 }
9587#ifdef FEAT_MBYTE
9588 f = mb_ptr2char_adv(&pf);
9589 l = mb_ptr2char_adv(&pl);
9590 u = mb_ptr2char_adv(&pu);
9591#else
9592 f = *pf++;
9593 l = *pl++;
9594 u = *pu++;
9595#endif
9596 /* Every character that appears is a word character. */
9597 if (f < 256)
9598 new_st.st_isw[f] = TRUE;
9599 if (l < 256)
9600 new_st.st_isw[l] = TRUE;
9601 if (u < 256)
9602 new_st.st_isw[u] = TRUE;
9603
9604 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9605 * case-folding */
9606 if (l < 256 && l != f)
9607 {
9608 if (f >= 256)
9609 {
9610 EMSG(_(e_affrange));
9611 return FAIL;
9612 }
9613 new_st.st_fold[l] = f;
9614 }
9615
9616 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009617 * case-folding, it's upper case and the "UPP" is the upper case of
9618 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009619 if (u < 256 && u != f)
9620 {
9621 if (f >= 256)
9622 {
9623 EMSG(_(e_affrange));
9624 return FAIL;
9625 }
9626 new_st.st_fold[u] = f;
9627 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009628 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009629 }
9630 }
9631
9632 if (*pl != NUL || *pu != NUL)
9633 {
9634 EMSG(_(e_affform));
9635 return FAIL;
9636 }
9637
9638 return set_spell_finish(&new_st);
9639}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009640
9641/*
9642 * Set the spell character tables from strings in the .spl file.
9643 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009644 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009645set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009646 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009647 int cnt; /* length of "flags" */
9648 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009649{
9650 /* We build the new tables here first, so that we can compare with the
9651 * previous one. */
9652 spelltab_T new_st;
9653 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009654 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009655 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009656
9657 clear_spell_chartab(&new_st);
9658
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009659 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009661 if (i < cnt)
9662 {
9663 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9664 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9665 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009666
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009667 if (*p != NUL)
9668 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009669#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009670 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009671#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009672 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009673#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009674 new_st.st_fold[i + 128] = c;
9675 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9676 new_st.st_upper[c] = i + 128;
9677 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009678 }
9679
Bram Moolenaar5195e452005-08-19 20:32:47 +00009680 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009681}
9682
9683 static int
9684set_spell_finish(new_st)
9685 spelltab_T *new_st;
9686{
9687 int i;
9688
9689 if (did_set_spelltab)
9690 {
9691 /* check that it's the same table */
9692 for (i = 0; i < 256; ++i)
9693 {
9694 if (spelltab.st_isw[i] != new_st->st_isw[i]
9695 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009696 || spelltab.st_fold[i] != new_st->st_fold[i]
9697 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009698 {
9699 EMSG(_("E763: Word characters differ between spell files"));
9700 return FAIL;
9701 }
9702 }
9703 }
9704 else
9705 {
9706 /* copy the new spelltab into the one being used */
9707 spelltab = *new_st;
9708 did_set_spelltab = TRUE;
9709 }
9710
9711 return OK;
9712}
9713
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009714/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009715 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009716 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009717 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009718 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009719 */
9720 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009721spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009722 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009723 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009724{
Bram Moolenaarea408852005-06-25 22:49:46 +00009725#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009726 char_u *s;
9727 int l;
9728 int c;
9729
9730 if (has_mbyte)
9731 {
9732 l = MB_BYTE2LEN(*p);
9733 s = p;
9734 if (l == 1)
9735 {
9736 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009737 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009738 {
9739 s = p + 1; /* skip a mid-word character */
9740 l = MB_BYTE2LEN(*s);
9741 }
9742 }
9743 else
9744 {
9745 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009746 if (c < 256 ? buf->b_spell_ismw[c]
9747 : (buf->b_spell_ismw_mb != NULL
9748 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009749 {
9750 s = p + l;
9751 l = MB_BYTE2LEN(*s);
9752 }
9753 }
9754
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009755 c = mb_ptr2char(s);
9756 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009757 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009758 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009759 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009760#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009761
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009762 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9763}
9764
9765/*
9766 * Return TRUE if "p" points to a word character.
9767 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9768 */
9769 static int
9770spell_iswordp_nmw(p)
9771 char_u *p;
9772{
9773#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009774 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009775
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009776 if (has_mbyte)
9777 {
9778 c = mb_ptr2char(p);
9779 if (c > 255)
9780 return mb_get_class(p) >= 2;
9781 return spelltab.st_isw[c];
9782 }
9783#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009784 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009785}
9786
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009787#ifdef FEAT_MBYTE
9788/*
9789 * Return TRUE if "p" points to a word character.
9790 * Wide version of spell_iswordp().
9791 */
9792 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009793spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009794 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009795 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009796{
9797 int *s;
9798
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009799 if (*p < 256 ? buf->b_spell_ismw[*p]
9800 : (buf->b_spell_ismw_mb != NULL
9801 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009802 s = p + 1;
9803 else
9804 s = p;
9805
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009806 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009807 {
9808 if (enc_utf8)
9809 return utf_class(*s) >= 2;
9810 if (enc_dbcs)
9811 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9812 return 0;
9813 }
9814 return spelltab.st_isw[*s];
9815}
9816#endif
9817
Bram Moolenaarea408852005-06-25 22:49:46 +00009818/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009819 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009820 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009821 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009822 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009823write_spell_prefcond(fd, gap)
9824 FILE *fd;
9825 garray_T *gap;
9826{
9827 int i;
9828 char_u *p;
9829 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009830 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009831
Bram Moolenaar5195e452005-08-19 20:32:47 +00009832 if (fd != NULL)
9833 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9834
9835 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009836
9837 for (i = 0; i < gap->ga_len; ++i)
9838 {
9839 /* <prefcond> : <condlen> <condstr> */
9840 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009841 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009842 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009843 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009844 if (fd != NULL)
9845 {
9846 fputc(len, fd);
9847 fwrite(p, (size_t)len, (size_t)1, fd);
9848 }
9849 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009850 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009851 else if (fd != NULL)
9852 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009853 }
9854
Bram Moolenaar5195e452005-08-19 20:32:47 +00009855 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009856}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009857
9858/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009859 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9860 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009861 * When using a multi-byte 'encoding' the length may change!
9862 * Returns FAIL when something wrong.
9863 */
9864 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009865spell_casefold(str, len, buf, buflen)
9866 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009867 int len;
9868 char_u *buf;
9869 int buflen;
9870{
9871 int i;
9872
9873 if (len >= buflen)
9874 {
9875 buf[0] = NUL;
9876 return FAIL; /* result will not fit */
9877 }
9878
9879#ifdef FEAT_MBYTE
9880 if (has_mbyte)
9881 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009882 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009883 char_u *p;
9884 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009885
9886 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009887 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009888 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009889 if (outi + MB_MAXBYTES > buflen)
9890 {
9891 buf[outi] = NUL;
9892 return FAIL;
9893 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009894 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009895 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009896 }
9897 buf[outi] = NUL;
9898 }
9899 else
9900#endif
9901 {
9902 /* Be quick for non-multibyte encodings. */
9903 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009904 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009905 buf[i] = NUL;
9906 }
9907
9908 return OK;
9909}
9910
Bram Moolenaar4770d092006-01-12 23:22:24 +00009911/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009912#define SPS_BEST 1
9913#define SPS_FAST 2
9914#define SPS_DOUBLE 4
9915
Bram Moolenaar4770d092006-01-12 23:22:24 +00009916static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9917static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009918
9919/*
9920 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009921 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009922 */
9923 int
9924spell_check_sps()
9925{
9926 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009927 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009928 char_u buf[MAXPATHL];
9929 int f;
9930
9931 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009932 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009933
9934 for (p = p_sps; *p != NUL; )
9935 {
9936 copy_option_part(&p, buf, MAXPATHL, ",");
9937
9938 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009939 if (VIM_ISDIGIT(*buf))
9940 {
9941 s = buf;
9942 sps_limit = getdigits(&s);
9943 if (*s != NUL && !VIM_ISDIGIT(*s))
9944 f = -1;
9945 }
9946 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009947 f = SPS_BEST;
9948 else if (STRCMP(buf, "fast") == 0)
9949 f = SPS_FAST;
9950 else if (STRCMP(buf, "double") == 0)
9951 f = SPS_DOUBLE;
9952 else if (STRNCMP(buf, "expr:", 5) != 0
9953 && STRNCMP(buf, "file:", 5) != 0)
9954 f = -1;
9955
9956 if (f == -1 || (sps_flags != 0 && f != 0))
9957 {
9958 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009959 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009960 return FAIL;
9961 }
9962 if (f != 0)
9963 sps_flags = f;
9964 }
9965
9966 if (sps_flags == 0)
9967 sps_flags = SPS_BEST;
9968
9969 return OK;
9970}
9971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972/*
9973 * "z?": Find badly spelled word under or after the cursor.
9974 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009975 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009976 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009977 */
9978 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009979spell_suggest(count)
9980 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981{
9982 char_u *line;
9983 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 char_u wcopy[MAXWLEN + 2];
9985 char_u *p;
9986 int i;
9987 int c;
9988 suginfo_T sug;
9989 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009990 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009991 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009992 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009993 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009994 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009995
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009996 if (no_spell_checking(curwin))
9997 return;
9998
9999#ifdef FEAT_VISUAL
10000 if (VIsual_active)
10001 {
10002 /* Use the Visually selected text as the bad word. But reject
10003 * a multi-line selection. */
10004 if (curwin->w_cursor.lnum != VIsual.lnum)
10005 {
10006 vim_beep();
10007 return;
10008 }
10009 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10010 if (badlen < 0)
10011 badlen = -badlen;
10012 else
10013 curwin->w_cursor.col = VIsual.col;
10014 ++badlen;
10015 end_visual_mode();
10016 }
10017 else
10018#endif
10019 /* Find the start of the badly spelled word. */
10020 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010021 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010022 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010023 /* No bad word or it starts after the cursor: use the word under the
10024 * cursor. */
10025 curwin->w_cursor = prev_cursor;
10026 line = ml_get_curline();
10027 p = line + curwin->w_cursor.col;
10028 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010029 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010030 mb_ptr_back(line, p);
10031 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010032 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010033 mb_ptr_adv(p);
10034
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010035 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010036 {
10037 beep_flush();
10038 return;
10039 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010040 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 }
10042
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010043 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010045 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010046 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010047
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010048 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010049
Bram Moolenaar5195e452005-08-19 20:32:47 +000010050 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10051 * 'spellsuggest', whatever is smaller. */
10052 if (sps_limit > (int)Rows - 2)
10053 limit = (int)Rows - 2;
10054 else
10055 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010056 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010057 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010058
10059 if (sug.su_ga.ga_len == 0)
10060 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010061 else if (count > 0)
10062 {
10063 if (count > sug.su_ga.ga_len)
10064 smsg((char_u *)_("Sorry, only %ld suggestions"),
10065 (long)sug.su_ga.ga_len);
10066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010067 else
10068 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010069 vim_free(repl_from);
10070 repl_from = NULL;
10071 vim_free(repl_to);
10072 repl_to = NULL;
10073
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010074#ifdef FEAT_RIGHTLEFT
10075 /* When 'rightleft' is set the list is drawn right-left. */
10076 cmdmsg_rl = curwin->w_p_rl;
10077 if (cmdmsg_rl)
10078 msg_col = Columns - 1;
10079#endif
10080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 /* List the suggestions. */
10082 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010083 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010084 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10086 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010087#ifdef FEAT_RIGHTLEFT
10088 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10089 {
10090 /* And now the rabbit from the high hat: Avoid showing the
10091 * untranslated message rightleft. */
10092 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10093 sug.su_badlen, sug.su_badptr);
10094 }
10095#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 msg_puts(IObuff);
10097 msg_clr_eos();
10098 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 msg_scroll = TRUE;
10101 for (i = 0; i < sug.su_ga.ga_len; ++i)
10102 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010103 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010104
10105 /* The suggested word may replace only part of the bad word, add
10106 * the not replaced part. */
10107 STRCPY(wcopy, stp->st_word);
10108 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010109 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 sug.su_badptr + stp->st_orglen,
10111 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010112 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10113#ifdef FEAT_RIGHTLEFT
10114 if (cmdmsg_rl)
10115 rl_mirror(IObuff);
10116#endif
10117 msg_puts(IObuff);
10118
10119 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010120 msg_puts(IObuff);
10121
10122 /* The word may replace more than "su_badlen". */
10123 if (sug.su_badlen < stp->st_orglen)
10124 {
10125 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10126 stp->st_orglen, sug.su_badptr);
10127 msg_puts(IObuff);
10128 }
10129
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010130 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010131 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010132 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010133 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010134 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010135 stp->st_salscore ? "s " : "",
10136 stp->st_score, stp->st_altscore);
10137 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010138 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010139 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010140#ifdef FEAT_RIGHTLEFT
10141 if (cmdmsg_rl)
10142 /* Mirror the numbers, but keep the leading space. */
10143 rl_mirror(IObuff + 1);
10144#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010145 msg_advance(30);
10146 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010148 msg_putchar('\n');
10149 }
10150
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010151#ifdef FEAT_RIGHTLEFT
10152 cmdmsg_rl = FALSE;
10153 msg_col = 0;
10154#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010155 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010156 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010157 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010158 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010159 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 }
10161
Bram Moolenaard12a1322005-08-21 22:08:24 +000010162 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10163 {
10164 /* Save the from and to text for :spellrepall. */
10165 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010166 if (sug.su_badlen > stp->st_orglen)
10167 {
10168 /* Replacing less than "su_badlen", append the remainder to
10169 * repl_to. */
10170 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10171 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10172 sug.su_badlen - stp->st_orglen,
10173 sug.su_badptr + stp->st_orglen);
10174 repl_to = vim_strsave(IObuff);
10175 }
10176 else
10177 {
10178 /* Replacing su_badlen or more, use the whole word. */
10179 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10180 repl_to = vim_strsave(stp->st_word);
10181 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010182
10183 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010184 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010185 if (p != NULL)
10186 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010187 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010188 mch_memmove(p, line, c);
10189 STRCPY(p + c, stp->st_word);
10190 STRCAT(p, sug.su_badptr + stp->st_orglen);
10191 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10192 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010193
10194 /* For redo we use a change-word command. */
10195 ResetRedobuff();
10196 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010197 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010198 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010199 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010200
10201 /* After this "p" may be invalid. */
10202 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010203 }
10204 }
10205 else
10206 curwin->w_cursor = prev_cursor;
10207
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010208 spell_find_cleanup(&sug);
10209}
10210
10211/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010212 * Check if the word at line "lnum" column "col" is required to start with a
10213 * capital. This uses 'spellcapcheck' of the current buffer.
10214 */
10215 static int
10216check_need_cap(lnum, col)
10217 linenr_T lnum;
10218 colnr_T col;
10219{
10220 int need_cap = FALSE;
10221 char_u *line;
10222 char_u *line_copy = NULL;
10223 char_u *p;
10224 colnr_T endcol;
10225 regmatch_T regmatch;
10226
10227 if (curbuf->b_cap_prog == NULL)
10228 return FALSE;
10229
10230 line = ml_get_curline();
10231 endcol = 0;
10232 if ((int)(skipwhite(line) - line) >= (int)col)
10233 {
10234 /* At start of line, check if previous line is empty or sentence
10235 * ends there. */
10236 if (lnum == 1)
10237 need_cap = TRUE;
10238 else
10239 {
10240 line = ml_get(lnum - 1);
10241 if (*skipwhite(line) == NUL)
10242 need_cap = TRUE;
10243 else
10244 {
10245 /* Append a space in place of the line break. */
10246 line_copy = concat_str(line, (char_u *)" ");
10247 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010248 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010249 }
10250 }
10251 }
10252 else
10253 endcol = col;
10254
10255 if (endcol > 0)
10256 {
10257 /* Check if sentence ends before the bad word. */
10258 regmatch.regprog = curbuf->b_cap_prog;
10259 regmatch.rm_ic = FALSE;
10260 p = line + endcol;
10261 for (;;)
10262 {
10263 mb_ptr_back(line, p);
10264 if (p == line || spell_iswordp_nmw(p))
10265 break;
10266 if (vim_regexec(&regmatch, p, 0)
10267 && regmatch.endp[0] == line + endcol)
10268 {
10269 need_cap = TRUE;
10270 break;
10271 }
10272 }
10273 }
10274
10275 vim_free(line_copy);
10276
10277 return need_cap;
10278}
10279
10280
10281/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010282 * ":spellrepall"
10283 */
10284/*ARGSUSED*/
10285 void
10286ex_spellrepall(eap)
10287 exarg_T *eap;
10288{
10289 pos_T pos = curwin->w_cursor;
10290 char_u *frompat;
10291 int addlen;
10292 char_u *line;
10293 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010294 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010295 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010296
10297 if (repl_from == NULL || repl_to == NULL)
10298 {
10299 EMSG(_("E752: No previous spell replacement"));
10300 return;
10301 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010302 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010304 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010305 if (frompat == NULL)
10306 return;
10307 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10308 p_ws = FALSE;
10309
Bram Moolenaar5195e452005-08-19 20:32:47 +000010310 sub_nsubs = 0;
10311 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010312 curwin->w_cursor.lnum = 0;
10313 while (!got_int)
10314 {
10315 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10316 || u_save_cursor() == FAIL)
10317 break;
10318
10319 /* Only replace when the right word isn't there yet. This happens
10320 * when changing "etc" to "etc.". */
10321 line = ml_get_curline();
10322 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10323 repl_to, STRLEN(repl_to)) != 0)
10324 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010325 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010326 if (p == NULL)
10327 break;
10328 mch_memmove(p, line, curwin->w_cursor.col);
10329 STRCPY(p + curwin->w_cursor.col, repl_to);
10330 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10331 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10332 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010333
10334 if (curwin->w_cursor.lnum != prev_lnum)
10335 {
10336 ++sub_nlines;
10337 prev_lnum = curwin->w_cursor.lnum;
10338 }
10339 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010340 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010341 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010342 }
10343
10344 p_ws = save_ws;
10345 curwin->w_cursor = pos;
10346 vim_free(frompat);
10347
Bram Moolenaar5195e452005-08-19 20:32:47 +000010348 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010349 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010350 else
10351 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010352}
10353
10354/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010355 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10356 * a list of allocated strings.
10357 */
10358 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010359spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010360 garray_T *gap;
10361 char_u *word;
10362 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010363 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010364 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010365{
10366 suginfo_T sug;
10367 int i;
10368 suggest_T *stp;
10369 char_u *wcopy;
10370
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010371 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010372
10373 /* Make room in "gap". */
10374 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010375 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010376 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010377 for (i = 0; i < sug.su_ga.ga_len; ++i)
10378 {
10379 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010380
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010381 /* The suggested word may replace only part of "word", add the not
10382 * replaced part. */
10383 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010384 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010385 if (wcopy == NULL)
10386 break;
10387 STRCPY(wcopy, stp->st_word);
10388 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10389 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10390 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010391 }
10392
10393 spell_find_cleanup(&sug);
10394}
10395
10396/*
10397 * Find spell suggestions for the word at the start of "badptr".
10398 * Return the suggestions in "su->su_ga".
10399 * The maximum number of suggestions is "maxcount".
10400 * Note: does use info for the current window.
10401 * This is based on the mechanisms of Aspell, but completely reimplemented.
10402 */
10403 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010404spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010405 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010406 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010407 suginfo_T *su;
10408 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010409 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010410 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010411 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010412{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010413 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010414 char_u buf[MAXPATHL];
10415 char_u *p;
10416 int do_combine = FALSE;
10417 char_u *sps_copy;
10418#ifdef FEAT_EVAL
10419 static int expr_busy = FALSE;
10420#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010421 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010422 int i;
10423 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010424
10425 /*
10426 * Set the info in "*su".
10427 */
10428 vim_memset(su, 0, sizeof(suginfo_T));
10429 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10430 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010431 if (*badptr == NUL)
10432 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010433 hash_init(&su->su_banned);
10434
10435 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010436 if (badlen != 0)
10437 su->su_badlen = badlen;
10438 else
10439 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010440 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010441 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010442
10443 if (su->su_badlen >= MAXWLEN)
10444 su->su_badlen = MAXWLEN - 1; /* just in case */
10445 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10446 (void)spell_casefold(su->su_badptr, su->su_badlen,
10447 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010448 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010449 su->su_badflags = badword_captype(su->su_badptr,
10450 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010451 if (need_cap)
10452 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010453
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010454 /* Find the default language for sound folding. We simply use the first
10455 * one in 'spelllang' that supports sound folding. That's good for when
10456 * using multiple files for one language, it's not that bad when mixing
10457 * languages (e.g., "pl,en"). */
10458 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10459 {
10460 lp = LANGP_ENTRY(curbuf->b_langp, i);
10461 if (lp->lp_sallang != NULL)
10462 {
10463 su->su_sallang = lp->lp_sallang;
10464 break;
10465 }
10466 }
10467
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010468 /* Soundfold the bad word with the default sound folding, so that we don't
10469 * have to do this many times. */
10470 if (su->su_sallang != NULL)
10471 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10472 su->su_sal_badword);
10473
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010474 /* If the word is not capitalised and spell_check() doesn't consider the
10475 * word to be bad then it might need to be capitalised. Add a suggestion
10476 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010477 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010478 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010479 {
10480 make_case_word(su->su_badword, buf, WF_ONECAP);
10481 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010482 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010483 }
10484
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010485 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010486 if (banbadword)
10487 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010488
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010489 /* Make a copy of 'spellsuggest', because the expression may change it. */
10490 sps_copy = vim_strsave(p_sps);
10491 if (sps_copy == NULL)
10492 return;
10493
10494 /* Loop over the items in 'spellsuggest'. */
10495 for (p = sps_copy; *p != NUL; )
10496 {
10497 copy_option_part(&p, buf, MAXPATHL, ",");
10498
10499 if (STRNCMP(buf, "expr:", 5) == 0)
10500 {
10501#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010502 /* Evaluate an expression. Skip this when called recursively,
10503 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010504 if (!expr_busy)
10505 {
10506 expr_busy = TRUE;
10507 spell_suggest_expr(su, buf + 5);
10508 expr_busy = FALSE;
10509 }
10510#endif
10511 }
10512 else if (STRNCMP(buf, "file:", 5) == 0)
10513 /* Use list of suggestions in a file. */
10514 spell_suggest_file(su, buf + 5);
10515 else
10516 {
10517 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010518 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010519 if (sps_flags & SPS_DOUBLE)
10520 do_combine = TRUE;
10521 }
10522 }
10523
10524 vim_free(sps_copy);
10525
10526 if (do_combine)
10527 /* Combine the two list of suggestions. This must be done last,
10528 * because sorting changes the order again. */
10529 score_combine(su);
10530}
10531
10532#ifdef FEAT_EVAL
10533/*
10534 * Find suggestions by evaluating expression "expr".
10535 */
10536 static void
10537spell_suggest_expr(su, expr)
10538 suginfo_T *su;
10539 char_u *expr;
10540{
10541 list_T *list;
10542 listitem_T *li;
10543 int score;
10544 char_u *p;
10545
10546 /* The work is split up in a few parts to avoid having to export
10547 * suginfo_T.
10548 * First evaluate the expression and get the resulting list. */
10549 list = eval_spell_expr(su->su_badword, expr);
10550 if (list != NULL)
10551 {
10552 /* Loop over the items in the list. */
10553 for (li = list->lv_first; li != NULL; li = li->li_next)
10554 if (li->li_tv.v_type == VAR_LIST)
10555 {
10556 /* Get the word and the score from the items. */
10557 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010558 if (score >= 0 && score <= su->su_maxscore)
10559 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10560 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010561 }
10562 list_unref(list);
10563 }
10564
Bram Moolenaar4770d092006-01-12 23:22:24 +000010565 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10566 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010567 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10568}
10569#endif
10570
10571/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010572 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010573 */
10574 static void
10575spell_suggest_file(su, fname)
10576 suginfo_T *su;
10577 char_u *fname;
10578{
10579 FILE *fd;
10580 char_u line[MAXWLEN * 2];
10581 char_u *p;
10582 int len;
10583 char_u cword[MAXWLEN];
10584
10585 /* Open the file. */
10586 fd = mch_fopen((char *)fname, "r");
10587 if (fd == NULL)
10588 {
10589 EMSG2(_(e_notopen), fname);
10590 return;
10591 }
10592
10593 /* Read it line by line. */
10594 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10595 {
10596 line_breakcheck();
10597
10598 p = vim_strchr(line, '/');
10599 if (p == NULL)
10600 continue; /* No Tab found, just skip the line. */
10601 *p++ = NUL;
10602 if (STRICMP(su->su_badword, line) == 0)
10603 {
10604 /* Match! Isolate the good word, until CR or NL. */
10605 for (len = 0; p[len] >= ' '; ++len)
10606 ;
10607 p[len] = NUL;
10608
10609 /* If the suggestion doesn't have specific case duplicate the case
10610 * of the bad word. */
10611 if (captype(p, NULL) == 0)
10612 {
10613 make_case_word(p, cword, su->su_badflags);
10614 p = cword;
10615 }
10616
10617 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010618 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010619 }
10620 }
10621
10622 fclose(fd);
10623
Bram Moolenaar4770d092006-01-12 23:22:24 +000010624 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10625 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010626 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10627}
10628
10629/*
10630 * Find suggestions for the internal method indicated by "sps_flags".
10631 */
10632 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010633spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010634 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010635 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010636{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010637 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010638 * Load the .sug file(s) that are available and not done yet.
10639 */
10640 suggest_load_files();
10641
10642 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010643 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010644 *
10645 * Set a maximum score to limit the combination of operations that is
10646 * tried.
10647 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010648 suggest_try_special(su);
10649
10650 /*
10651 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10652 * from the .aff file and inserting a space (split the word).
10653 */
10654 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010655
10656 /* For the resulting top-scorers compute the sound-a-like score. */
10657 if (sps_flags & SPS_DOUBLE)
10658 score_comp_sal(su);
10659
10660 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010661 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010662 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010663 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010664 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010665 if (sps_flags & SPS_BEST)
10666 /* Adjust the word score for the suggestions found so far for how
10667 * they sounds like. */
10668 rescore_suggestions(su);
10669
10670 /*
10671 * While going throught the soundfold tree "su_maxscore" is the score
10672 * for the soundfold word, limits the changes that are being tried,
10673 * and "su_sfmaxscore" the rescored score, which is set by
10674 * cleanup_suggestions().
10675 * First find words with a small edit distance, because this is much
10676 * faster and often already finds the top-N suggestions. If we didn't
10677 * find many suggestions try again with a higher edit distance.
10678 * "sl_sounddone" is used to avoid doing the same word twice.
10679 */
10680 suggest_try_soundalike_prep();
10681 su->su_maxscore = SCORE_SFMAX1;
10682 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010683 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010684 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10685 {
10686 /* We didn't find enough matches, try again, allowing more
10687 * changes to the soundfold word. */
10688 su->su_maxscore = SCORE_SFMAX2;
10689 suggest_try_soundalike(su);
10690 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10691 {
10692 /* Still didn't find enough matches, try again, allowing even
10693 * more changes to the soundfold word. */
10694 su->su_maxscore = SCORE_SFMAX3;
10695 suggest_try_soundalike(su);
10696 }
10697 }
10698 su->su_maxscore = su->su_sfmaxscore;
10699 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010700 }
10701
Bram Moolenaar4770d092006-01-12 23:22:24 +000010702 /* When CTRL-C was hit while searching do show the results. Only clear
10703 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010704 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010705 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010706 {
10707 (void)vgetc();
10708 got_int = FALSE;
10709 }
10710
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010711 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010712 {
10713 if (sps_flags & SPS_BEST)
10714 /* Adjust the word score for how it sounds like. */
10715 rescore_suggestions(su);
10716
Bram Moolenaar4770d092006-01-12 23:22:24 +000010717 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10718 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010719 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010720 }
10721}
10722
10723/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010724 * Load the .sug files for languages that have one and weren't loaded yet.
10725 */
10726 static void
10727suggest_load_files()
10728{
10729 langp_T *lp;
10730 int lpi;
10731 slang_T *slang;
10732 char_u *dotp;
10733 FILE *fd;
10734 char_u buf[MAXWLEN];
10735 int i;
10736 time_t timestamp;
10737 int wcount;
10738 int wordnr;
10739 garray_T ga;
10740 int c;
10741
10742 /* Do this for all languages that support sound folding. */
10743 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10744 {
10745 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10746 slang = lp->lp_slang;
10747 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10748 {
10749 /* Change ".spl" to ".sug" and open the file. When the file isn't
10750 * found silently skip it. Do set "sl_sugloaded" so that we
10751 * don't try again and again. */
10752 slang->sl_sugloaded = TRUE;
10753
10754 dotp = vim_strrchr(slang->sl_fname, '.');
10755 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10756 continue;
10757 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010758 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010759 if (fd == NULL)
10760 goto nextone;
10761
10762 /*
10763 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10764 */
10765 for (i = 0; i < VIMSUGMAGICL; ++i)
10766 buf[i] = getc(fd); /* <fileID> */
10767 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10768 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010769 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010770 slang->sl_fname);
10771 goto nextone;
10772 }
10773 c = getc(fd); /* <versionnr> */
10774 if (c < VIMSUGVERSION)
10775 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010776 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010777 slang->sl_fname);
10778 goto nextone;
10779 }
10780 else if (c > VIMSUGVERSION)
10781 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010782 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010783 slang->sl_fname);
10784 goto nextone;
10785 }
10786
10787 /* Check the timestamp, it must be exactly the same as the one in
10788 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010789 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010790 if (timestamp != slang->sl_sugtime)
10791 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010792 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010793 slang->sl_fname);
10794 goto nextone;
10795 }
10796
10797 /*
10798 * <SUGWORDTREE>: <wordtree>
10799 * Read the trie with the soundfolded words.
10800 */
10801 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10802 FALSE, 0) != 0)
10803 {
10804someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010805 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010806 slang->sl_fname);
10807 slang_clear_sug(slang);
10808 goto nextone;
10809 }
10810
10811 /*
10812 * <SUGTABLE>: <sugwcount> <sugline> ...
10813 *
10814 * Read the table with word numbers. We use a file buffer for
10815 * this, because it's so much like a file with lines. Makes it
10816 * possible to swap the info and save on memory use.
10817 */
10818 slang->sl_sugbuf = open_spellbuf();
10819 if (slang->sl_sugbuf == NULL)
10820 goto someerror;
10821 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010822 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010823 if (wcount < 0)
10824 goto someerror;
10825
10826 /* Read all the wordnr lists into the buffer, one NUL terminated
10827 * list per line. */
10828 ga_init2(&ga, 1, 100);
10829 for (wordnr = 0; wordnr < wcount; ++wordnr)
10830 {
10831 ga.ga_len = 0;
10832 for (;;)
10833 {
10834 c = getc(fd); /* <sugline> */
10835 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10836 goto someerror;
10837 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10838 if (c == NUL)
10839 break;
10840 }
10841 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10842 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10843 goto someerror;
10844 }
10845 ga_clear(&ga);
10846
10847 /*
10848 * Need to put word counts in the word tries, so that we can find
10849 * a word by its number.
10850 */
10851 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10852 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10853
10854nextone:
10855 if (fd != NULL)
10856 fclose(fd);
10857 STRCPY(dotp, ".spl");
10858 }
10859 }
10860}
10861
10862
10863/*
10864 * Fill in the wordcount fields for a trie.
10865 * Returns the total number of words.
10866 */
10867 static void
10868tree_count_words(byts, idxs)
10869 char_u *byts;
10870 idx_T *idxs;
10871{
10872 int depth;
10873 idx_T arridx[MAXWLEN];
10874 int curi[MAXWLEN];
10875 int c;
10876 idx_T n;
10877 int wordcount[MAXWLEN];
10878
10879 arridx[0] = 0;
10880 curi[0] = 1;
10881 wordcount[0] = 0;
10882 depth = 0;
10883 while (depth >= 0 && !got_int)
10884 {
10885 if (curi[depth] > byts[arridx[depth]])
10886 {
10887 /* Done all bytes at this node, go up one level. */
10888 idxs[arridx[depth]] = wordcount[depth];
10889 if (depth > 0)
10890 wordcount[depth - 1] += wordcount[depth];
10891
10892 --depth;
10893 fast_breakcheck();
10894 }
10895 else
10896 {
10897 /* Do one more byte at this node. */
10898 n = arridx[depth] + curi[depth];
10899 ++curi[depth];
10900
10901 c = byts[n];
10902 if (c == 0)
10903 {
10904 /* End of word, count it. */
10905 ++wordcount[depth];
10906
10907 /* Skip over any other NUL bytes (same word with different
10908 * flags). */
10909 while (byts[n + 1] == 0)
10910 {
10911 ++n;
10912 ++curi[depth];
10913 }
10914 }
10915 else
10916 {
10917 /* Normal char, go one level deeper to count the words. */
10918 ++depth;
10919 arridx[depth] = idxs[n];
10920 curi[depth] = 1;
10921 wordcount[depth] = 0;
10922 }
10923 }
10924 }
10925}
10926
10927/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010928 * Free the info put in "*su" by spell_find_suggest().
10929 */
10930 static void
10931spell_find_cleanup(su)
10932 suginfo_T *su;
10933{
10934 int i;
10935
10936 /* Free the suggestions. */
10937 for (i = 0; i < su->su_ga.ga_len; ++i)
10938 vim_free(SUG(su->su_ga, i).st_word);
10939 ga_clear(&su->su_ga);
10940 for (i = 0; i < su->su_sga.ga_len; ++i)
10941 vim_free(SUG(su->su_sga, i).st_word);
10942 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943
10944 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010945 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946}
10947
10948/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010949 * Make a copy of "word", with the first letter upper or lower cased, to
10950 * "wcopy[MAXWLEN]". "word" must not be empty.
10951 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 */
10953 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010954onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010956 char_u *wcopy;
10957 int upper; /* TRUE: first letter made upper case */
10958{
10959 char_u *p;
10960 int c;
10961 int l;
10962
10963 p = word;
10964#ifdef FEAT_MBYTE
10965 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010966 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 else
10968#endif
10969 c = *p++;
10970 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010971 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010973 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974#ifdef FEAT_MBYTE
10975 if (has_mbyte)
10976 l = mb_char2bytes(c, wcopy);
10977 else
10978#endif
10979 {
10980 l = 1;
10981 wcopy[0] = c;
10982 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010983 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010984}
10985
10986/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010987 * Make a copy of "word" with all the letters upper cased into
10988 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 */
10990 static void
10991allcap_copy(word, wcopy)
10992 char_u *word;
10993 char_u *wcopy;
10994{
10995 char_u *s;
10996 char_u *d;
10997 int c;
10998
10999 d = wcopy;
11000 for (s = word; *s != NUL; )
11001 {
11002#ifdef FEAT_MBYTE
11003 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011004 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 else
11006#endif
11007 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011008
11009#ifdef FEAT_MBYTE
11010 /* We only change ß to SS when we are certain latin1 is used. It
11011 * would cause weird errors in other 8-bit encodings. */
11012 if (enc_latin1like && c == 0xdf)
11013 {
11014 c = 'S';
11015 if (d - wcopy >= MAXWLEN - 1)
11016 break;
11017 *d++ = c;
11018 }
11019 else
11020#endif
11021 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011022
11023#ifdef FEAT_MBYTE
11024 if (has_mbyte)
11025 {
11026 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11027 break;
11028 d += mb_char2bytes(c, d);
11029 }
11030 else
11031#endif
11032 {
11033 if (d - wcopy >= MAXWLEN - 1)
11034 break;
11035 *d++ = c;
11036 }
11037 }
11038 *d = NUL;
11039}
11040
11041/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011042 * Try finding suggestions by recognizing specific situations.
11043 */
11044 static void
11045suggest_try_special(su)
11046 suginfo_T *su;
11047{
11048 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011049 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011050 int c;
11051 char_u word[MAXWLEN];
11052
11053 /*
11054 * Recognize a word that is repeated: "the the".
11055 */
11056 p = skiptowhite(su->su_fbadword);
11057 len = p - su->su_fbadword;
11058 p = skipwhite(p);
11059 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11060 {
11061 /* Include badflags: if the badword is onecap or allcap
11062 * use that for the goodword too: "The the" -> "The". */
11063 c = su->su_fbadword[len];
11064 su->su_fbadword[len] = NUL;
11065 make_case_word(su->su_fbadword, word, su->su_badflags);
11066 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011067
11068 /* Give a soundalike score of 0, compute the score as if deleting one
11069 * character. */
11070 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011071 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011072 }
11073}
11074
11075/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 * Try finding suggestions by adding/removing/swapping letters.
11077 */
11078 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011079suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 suginfo_T *su;
11081{
11082 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011083 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011085 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011086 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087
11088 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011089 * to find matches (esp. REP items). Append some more text, changing
11090 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011092 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011093 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011094 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011096 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011098 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011099
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011100 /* If reloading a spell file fails it's still in the list but
11101 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011102 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011103 continue;
11104
Bram Moolenaar4770d092006-01-12 23:22:24 +000011105 /* Try it for this language. Will add possible suggestions. */
11106 suggest_trie_walk(su, lp, fword, FALSE);
11107 }
11108}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109
Bram Moolenaar4770d092006-01-12 23:22:24 +000011110/* Check the maximum score, if we go over it we won't try this change. */
11111#define TRY_DEEPER(su, stack, depth, add) \
11112 (stack[depth].ts_score + (add) < su->su_maxscore)
11113
11114/*
11115 * Try finding suggestions by adding/removing/swapping letters.
11116 *
11117 * This uses a state machine. At each node in the tree we try various
11118 * operations. When trying if an operation works "depth" is increased and the
11119 * stack[] is used to store info. This allows combinations, thus insert one
11120 * character, replace one and delete another. The number of changes is
11121 * limited by su->su_maxscore.
11122 *
11123 * After implementing this I noticed an article by Kemal Oflazer that
11124 * describes something similar: "Error-tolerant Finite State Recognition with
11125 * Applications to Morphological Analysis and Spelling Correction" (1996).
11126 * The implementation in the article is simplified and requires a stack of
11127 * unknown depth. The implementation here only needs a stack depth equal to
11128 * the length of the word.
11129 *
11130 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11131 * The mechanism is the same, but we find a match with a sound-folded word
11132 * that comes from one or more original words. Each of these words may be
11133 * added, this is done by add_sound_suggest().
11134 * Don't use:
11135 * the prefix tree or the keep-case tree
11136 * "su->su_badlen"
11137 * anything to do with upper and lower case
11138 * anything to do with word or non-word characters ("spell_iswordp()")
11139 * banned words
11140 * word flags (rare, region, compounding)
11141 * word splitting for now
11142 * "similar_chars()"
11143 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11144 */
11145 static void
11146suggest_trie_walk(su, lp, fword, soundfold)
11147 suginfo_T *su;
11148 langp_T *lp;
11149 char_u *fword;
11150 int soundfold;
11151{
11152 char_u tword[MAXWLEN]; /* good word collected so far */
11153 trystate_T stack[MAXWLEN];
11154 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11155 * concatanation of prefix compound
11156 * words and split word. NUL terminated
11157 * when going deeper but not when coming
11158 * back. */
11159 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11160 trystate_T *sp;
11161 int newscore;
11162 int score;
11163 char_u *byts, *fbyts, *pbyts;
11164 idx_T *idxs, *fidxs, *pidxs;
11165 int depth;
11166 int c, c2, c3;
11167 int n = 0;
11168 int flags;
11169 garray_T *gap;
11170 idx_T arridx;
11171 int len;
11172 char_u *p;
11173 fromto_T *ftp;
11174 int fl = 0, tl;
11175 int repextra = 0; /* extra bytes in fword[] from REP item */
11176 slang_T *slang = lp->lp_slang;
11177 int fword_ends;
11178 int goodword_ends;
11179#ifdef DEBUG_TRIEWALK
11180 /* Stores the name of the change made at each level. */
11181 char_u changename[MAXWLEN][80];
11182#endif
11183 int breakcheckcount = 1000;
11184 int compound_ok;
11185
11186 /*
11187 * Go through the whole case-fold tree, try changes at each node.
11188 * "tword[]" contains the word collected from nodes in the tree.
11189 * "fword[]" the word we are trying to match with (initially the bad
11190 * word).
11191 */
11192 depth = 0;
11193 sp = &stack[0];
11194 vim_memset(sp, 0, sizeof(trystate_T));
11195 sp->ts_curi = 1;
11196
11197 if (soundfold)
11198 {
11199 /* Going through the soundfold tree. */
11200 byts = fbyts = slang->sl_sbyts;
11201 idxs = fidxs = slang->sl_sidxs;
11202 pbyts = NULL;
11203 pidxs = NULL;
11204 sp->ts_prefixdepth = PFD_NOPREFIX;
11205 sp->ts_state = STATE_START;
11206 }
11207 else
11208 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011209 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011210 * When there are postponed prefixes we need to use these first. At
11211 * the end of the prefix we continue in the case-fold tree.
11212 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011213 fbyts = slang->sl_fbyts;
11214 fidxs = slang->sl_fidxs;
11215 pbyts = slang->sl_pbyts;
11216 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011217 if (pbyts != NULL)
11218 {
11219 byts = pbyts;
11220 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011221 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011222 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11223 }
11224 else
11225 {
11226 byts = fbyts;
11227 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011228 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011229 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011230 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011231 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011232
Bram Moolenaar4770d092006-01-12 23:22:24 +000011233 /*
11234 * Loop to find all suggestions. At each round we either:
11235 * - For the current state try one operation, advance "ts_curi",
11236 * increase "depth".
11237 * - When a state is done go to the next, set "ts_state".
11238 * - When all states are tried decrease "depth".
11239 */
11240 while (depth >= 0 && !got_int)
11241 {
11242 sp = &stack[depth];
11243 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011245 case STATE_START:
11246 case STATE_NOPREFIX:
11247 /*
11248 * Start of node: Deal with NUL bytes, which means
11249 * tword[] may end here.
11250 */
11251 arridx = sp->ts_arridx; /* current node in the tree */
11252 len = byts[arridx]; /* bytes in this node */
11253 arridx += sp->ts_curi; /* index of current byte */
11254
11255 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011257 /* Skip over the NUL bytes, we use them later. */
11258 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11259 ;
11260 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261
Bram Moolenaar4770d092006-01-12 23:22:24 +000011262 /* Always past NUL bytes now. */
11263 n = (int)sp->ts_state;
11264 sp->ts_state = STATE_ENDNUL;
11265 sp->ts_save_badflags = su->su_badflags;
11266
11267 /* At end of a prefix or at start of prefixtree: check for
11268 * following word. */
11269 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011270 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011271 /* Set su->su_badflags to the caps type at this position.
11272 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011273#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011274 if (has_mbyte)
11275 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11276 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011277#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011278 n = sp->ts_fidx;
11279 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11280 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011281 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011282#ifdef DEBUG_TRIEWALK
11283 sprintf(changename[depth], "prefix");
11284#endif
11285 go_deeper(stack, depth, 0);
11286 ++depth;
11287 sp = &stack[depth];
11288 sp->ts_prefixdepth = depth - 1;
11289 byts = fbyts;
11290 idxs = fidxs;
11291 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011292
Bram Moolenaar4770d092006-01-12 23:22:24 +000011293 /* Move the prefix to preword[] with the right case
11294 * and make find_keepcap_word() works. */
11295 tword[sp->ts_twordlen] = NUL;
11296 make_case_word(tword + sp->ts_splitoff,
11297 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011298 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011299 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011300 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011301 break;
11302 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011303
Bram Moolenaar4770d092006-01-12 23:22:24 +000011304 if (sp->ts_curi > len || byts[arridx] != 0)
11305 {
11306 /* Past bytes in node and/or past NUL bytes. */
11307 sp->ts_state = STATE_ENDNUL;
11308 sp->ts_save_badflags = su->su_badflags;
11309 break;
11310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311
Bram Moolenaar4770d092006-01-12 23:22:24 +000011312 /*
11313 * End of word in tree.
11314 */
11315 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316
Bram Moolenaar4770d092006-01-12 23:22:24 +000011317 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011318
11319 /* Skip words with the NOSUGGEST flag. */
11320 if (flags & WF_NOSUGGEST)
11321 break;
11322
Bram Moolenaar4770d092006-01-12 23:22:24 +000011323 fword_ends = (fword[sp->ts_fidx] == NUL
11324 || (soundfold
11325 ? vim_iswhite(fword[sp->ts_fidx])
11326 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11327 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328
Bram Moolenaar4770d092006-01-12 23:22:24 +000011329 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011330 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011331 {
11332 /* There was a prefix before the word. Check that the prefix
11333 * can be used with this word. */
11334 /* Count the length of the NULs in the prefix. If there are
11335 * none this must be the first try without a prefix. */
11336 n = stack[sp->ts_prefixdepth].ts_arridx;
11337 len = pbyts[n++];
11338 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11339 ;
11340 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011341 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011342 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011343 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011344 if (c == 0)
11345 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011346
Bram Moolenaar4770d092006-01-12 23:22:24 +000011347 /* Use the WF_RARE flag for a rare prefix. */
11348 if (c & WF_RAREPFX)
11349 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011350
Bram Moolenaar4770d092006-01-12 23:22:24 +000011351 /* Tricky: when checking for both prefix and compounding
11352 * we run into the prefix flag first.
11353 * Remember that it's OK, so that we accept the prefix
11354 * when arriving at a compound flag. */
11355 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011356 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011357 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011358
Bram Moolenaar4770d092006-01-12 23:22:24 +000011359 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11360 * appending another compound word below. */
11361 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011362 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011363 goodword_ends = FALSE;
11364 else
11365 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011366
Bram Moolenaar4770d092006-01-12 23:22:24 +000011367 p = NULL;
11368 compound_ok = TRUE;
11369 if (sp->ts_complen > sp->ts_compsplit)
11370 {
11371 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011372 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011373 /* There was a word before this word. When there was no
11374 * change in this word (it was correct) add the first word
11375 * as a suggestion. If this word was corrected too, we
11376 * need to check if a correct word follows. */
11377 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011378 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 && STRNCMP(fword + sp->ts_splitfidx,
11380 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011381 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011382 {
11383 preword[sp->ts_prewordlen] = NUL;
11384 newscore = score_wordcount_adj(slang, sp->ts_score,
11385 preword + sp->ts_prewordlen,
11386 sp->ts_prewordlen > 0);
11387 /* Add the suggestion if the score isn't too bad. */
11388 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011389 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011390 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011391 newscore, 0, FALSE,
11392 lp->lp_sallang, FALSE);
11393 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011394 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011395 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011396 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011397 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011398 /* There was a compound word before this word. If this
11399 * word does not support compounding then give up
11400 * (splitting is tried for the word without compound
11401 * flag). */
11402 if (((unsigned)flags >> 24) == 0
11403 || sp->ts_twordlen - sp->ts_splitoff
11404 < slang->sl_compminlen)
11405 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011406#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011407 /* For multi-byte chars check character length against
11408 * COMPOUNDMIN. */
11409 if (has_mbyte
11410 && slang->sl_compminlen > 0
11411 && mb_charlen(tword + sp->ts_splitoff)
11412 < slang->sl_compminlen)
11413 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011414#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011415
Bram Moolenaar4770d092006-01-12 23:22:24 +000011416 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11417 compflags[sp->ts_complen + 1] = NUL;
11418 vim_strncpy(preword + sp->ts_prewordlen,
11419 tword + sp->ts_splitoff,
11420 sp->ts_twordlen - sp->ts_splitoff);
11421 p = preword;
11422 while (*skiptowhite(p) != NUL)
11423 p = skipwhite(skiptowhite(p));
11424 if (fword_ends && !can_compound(slang, p,
11425 compflags + sp->ts_compsplit))
11426 /* Compound is not allowed. But it may still be
11427 * possible if we add another (short) word. */
11428 compound_ok = FALSE;
11429
11430 /* Get pointer to last char of previous word. */
11431 p = preword + sp->ts_prewordlen;
11432 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011433 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435
Bram Moolenaar4770d092006-01-12 23:22:24 +000011436 /*
11437 * Form the word with proper case in preword.
11438 * If there is a word from a previous split, append.
11439 * For the soundfold tree don't change the case, simply append.
11440 */
11441 if (soundfold)
11442 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11443 else if (flags & WF_KEEPCAP)
11444 /* Must find the word in the keep-case tree. */
11445 find_keepcap_word(slang, tword + sp->ts_splitoff,
11446 preword + sp->ts_prewordlen);
11447 else
11448 {
11449 /* Include badflags: If the badword is onecap or allcap
11450 * use that for the goodword too. But if the badword is
11451 * allcap and it's only one char long use onecap. */
11452 c = su->su_badflags;
11453 if ((c & WF_ALLCAP)
11454#ifdef FEAT_MBYTE
11455 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11456#else
11457 && su->su_badlen == 1
11458#endif
11459 )
11460 c = WF_ONECAP;
11461 c |= flags;
11462
11463 /* When appending a compound word after a word character don't
11464 * use Onecap. */
11465 if (p != NULL && spell_iswordp_nmw(p))
11466 c &= ~WF_ONECAP;
11467 make_case_word(tword + sp->ts_splitoff,
11468 preword + sp->ts_prewordlen, c);
11469 }
11470
11471 if (!soundfold)
11472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473 /* Don't use a banned word. It may appear again as a good
11474 * word, thus remember it. */
11475 if (flags & WF_BANNED)
11476 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011477 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 break;
11479 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011480 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011481 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11482 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011483 {
11484 if (slang->sl_compprog == NULL)
11485 break;
11486 /* the word so far was banned but we may try compounding */
11487 goodword_ends = FALSE;
11488 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490
Bram Moolenaar4770d092006-01-12 23:22:24 +000011491 newscore = 0;
11492 if (!soundfold) /* soundfold words don't have flags */
11493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011495 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 newscore += SCORE_REGION;
11497 if (flags & WF_RARE)
11498 newscore += SCORE_RARE;
11499
Bram Moolenaar0c405862005-06-22 22:26:26 +000011500 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011501 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 /* TODO: how about splitting in the soundfold tree? */
11506 if (fword_ends
11507 && goodword_ends
11508 && sp->ts_fidx >= sp->ts_fidxtry
11509 && compound_ok)
11510 {
11511 /* The badword also ends: add suggestions. */
11512#ifdef DEBUG_TRIEWALK
11513 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011515 int j;
11516
11517 /* print the stack of changes that brought us here */
11518 smsg("------ %s -------", fword);
11519 for (j = 0; j < depth; ++j)
11520 smsg("%s", changename[j]);
11521 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011522#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011523 if (soundfold)
11524 {
11525 /* For soundfolded words we need to find the original
11526 * words, the edit distrance and then add them. */
11527 add_sound_suggest(su, preword, sp->ts_score, lp);
11528 }
11529 else
11530 {
11531 /* Give a penalty when changing non-word char to word
11532 * char, e.g., "thes," -> "these". */
11533 p = fword + sp->ts_fidx;
11534 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011535 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011536 {
11537 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011538 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011539 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011540 newscore += SCORE_NONWORD;
11541 }
11542
Bram Moolenaar4770d092006-01-12 23:22:24 +000011543 /* Give a bonus to words seen before. */
11544 score = score_wordcount_adj(slang,
11545 sp->ts_score + newscore,
11546 preword + sp->ts_prewordlen,
11547 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011548
Bram Moolenaar4770d092006-01-12 23:22:24 +000011549 /* Add the suggestion if the score isn't too bad. */
11550 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011551 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011552 add_suggestion(su, &su->su_ga, preword,
11553 sp->ts_fidx - repextra,
11554 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011555
11556 if (su->su_badflags & WF_MIXCAP)
11557 {
11558 /* We really don't know if the word should be
11559 * upper or lower case, add both. */
11560 c = captype(preword, NULL);
11561 if (c == 0 || c == WF_ALLCAP)
11562 {
11563 make_case_word(tword + sp->ts_splitoff,
11564 preword + sp->ts_prewordlen,
11565 c == 0 ? WF_ALLCAP : 0);
11566
11567 add_suggestion(su, &su->su_ga, preword,
11568 sp->ts_fidx - repextra,
11569 score + SCORE_ICASE, 0, FALSE,
11570 lp->lp_sallang, FALSE);
11571 }
11572 }
11573 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011575 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011576
Bram Moolenaar4770d092006-01-12 23:22:24 +000011577 /*
11578 * Try word split and/or compounding.
11579 */
11580 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011581#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011582 /* Don't split halfway a character. */
11583 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011584#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011585 )
11586 {
11587 int try_compound;
11588 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011589
Bram Moolenaar4770d092006-01-12 23:22:24 +000011590 /* If past the end of the bad word don't try a split.
11591 * Otherwise try changing the next word. E.g., find
11592 * suggestions for "the the" where the second "the" is
11593 * different. It's done like a split.
11594 * TODO: word split for soundfold words */
11595 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11596 && !soundfold;
11597
11598 /* Get here in several situations:
11599 * 1. The word in the tree ends:
11600 * If the word allows compounding try that. Otherwise try
11601 * a split by inserting a space. For both check that a
11602 * valid words starts at fword[sp->ts_fidx].
11603 * For NOBREAK do like compounding to be able to check if
11604 * the next word is valid.
11605 * 2. The badword does end, but it was due to a change (e.g.,
11606 * a swap). No need to split, but do check that the
11607 * following word is valid.
11608 * 3. The badword and the word in the tree end. It may still
11609 * be possible to compound another (short) word.
11610 */
11611 try_compound = FALSE;
11612 if (!soundfold
11613 && slang->sl_compprog != NULL
11614 && ((unsigned)flags >> 24) != 0
11615 && sp->ts_twordlen - sp->ts_splitoff
11616 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011617#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011618 && (!has_mbyte
11619 || slang->sl_compminlen == 0
11620 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011621 >= slang->sl_compminlen)
11622#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011623 && (slang->sl_compsylmax < MAXWLEN
11624 || sp->ts_complen + 1 - sp->ts_compsplit
11625 < slang->sl_compmax)
11626 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11627 ? slang->sl_compstartflags
11628 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011629 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011630 {
11631 try_compound = TRUE;
11632 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11633 compflags[sp->ts_complen + 1] = NUL;
11634 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011635
Bram Moolenaar4770d092006-01-12 23:22:24 +000011636 /* For NOBREAK we never try splitting, it won't make any word
11637 * valid. */
11638 if (slang->sl_nobreak)
11639 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011640
Bram Moolenaar4770d092006-01-12 23:22:24 +000011641 /* If we could add a compound word, and it's also possible to
11642 * split at this point, do the split first and set
11643 * TSF_DIDSPLIT to avoid doing it again. */
11644 else if (!fword_ends
11645 && try_compound
11646 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11647 {
11648 try_compound = FALSE;
11649 sp->ts_flags |= TSF_DIDSPLIT;
11650 --sp->ts_curi; /* do the same NUL again */
11651 compflags[sp->ts_complen] = NUL;
11652 }
11653 else
11654 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011655
Bram Moolenaar4770d092006-01-12 23:22:24 +000011656 if (try_split || try_compound)
11657 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011658 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011659 {
11660 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011661 * words so far are valid for compounding. If there
11662 * is only one word it must not have the NEEDCOMPOUND
11663 * flag. */
11664 if (sp->ts_complen == sp->ts_compsplit
11665 && (flags & WF_NEEDCOMP))
11666 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011667 p = preword;
11668 while (*skiptowhite(p) != NUL)
11669 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011670 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011671 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011672 compflags + sp->ts_compsplit))
11673 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011674
11675 if (slang->sl_nosplitsugs)
11676 newscore += SCORE_SPLIT_NO;
11677 else
11678 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011679
11680 /* Give a bonus to words seen before. */
11681 newscore = score_wordcount_adj(slang, newscore,
11682 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011683 }
11684
Bram Moolenaar4770d092006-01-12 23:22:24 +000011685 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011687 go_deeper(stack, depth, newscore);
11688#ifdef DEBUG_TRIEWALK
11689 if (!try_compound && !fword_ends)
11690 sprintf(changename[depth], "%.*s-%s: split",
11691 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11692 else
11693 sprintf(changename[depth], "%.*s-%s: compound",
11694 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11695#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011697 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011698 sp->ts_state = STATE_SPLITUNDO;
11699
11700 ++depth;
11701 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011703 /* Append a space to preword when splitting. */
11704 if (!try_compound && !fword_ends)
11705 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011706 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011707 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011708 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011709
11710 /* If the badword has a non-word character at this
11711 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011712 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011713 * character when the word ends. But only when the
11714 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011715 if (((!try_compound && !spell_iswordp_nmw(fword
11716 + sp->ts_fidx))
11717 || fword_ends)
11718 && fword[sp->ts_fidx] != NUL
11719 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011720 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011721 int l;
11722
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011723#ifdef FEAT_MBYTE
11724 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011725 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011726 else
11727#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011728 l = 1;
11729 if (fword_ends)
11730 {
11731 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011732 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011733 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011734 sp->ts_prewordlen += l;
11735 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011736 }
11737 else
11738 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11739 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011740 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011741
Bram Moolenaard12a1322005-08-21 22:08:24 +000011742 /* When compounding include compound flag in
11743 * compflags[] (already set above). When splitting we
11744 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011745 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011746 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011747 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011748 sp->ts_compsplit = sp->ts_complen;
11749 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011750
Bram Moolenaar53805d12005-08-01 07:08:33 +000011751 /* set su->su_badflags to the caps type at this
11752 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011753#ifdef FEAT_MBYTE
11754 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011755 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011756 else
11757#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011758 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011759 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011760 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011763 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011764
11765 /* If there are postponed prefixes, try these too. */
11766 if (pbyts != NULL)
11767 {
11768 byts = pbyts;
11769 idxs = pidxs;
11770 sp->ts_prefixdepth = PFD_PREFIXTREE;
11771 sp->ts_state = STATE_NOPREFIX;
11772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 }
11774 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011775 }
11776 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777
Bram Moolenaar4770d092006-01-12 23:22:24 +000011778 case STATE_SPLITUNDO:
11779 /* Undo the changes done for word split or compound word. */
11780 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011781
Bram Moolenaar4770d092006-01-12 23:22:24 +000011782 /* Continue looking for NUL bytes. */
11783 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011784
Bram Moolenaar4770d092006-01-12 23:22:24 +000011785 /* In case we went into the prefix tree. */
11786 byts = fbyts;
11787 idxs = fidxs;
11788 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789
Bram Moolenaar4770d092006-01-12 23:22:24 +000011790 case STATE_ENDNUL:
11791 /* Past the NUL bytes in the node. */
11792 su->su_badflags = sp->ts_save_badflags;
11793 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011794#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011795 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011796#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011797 )
11798 {
11799 /* The badword ends, can't use STATE_PLAIN. */
11800 sp->ts_state = STATE_DEL;
11801 break;
11802 }
11803 sp->ts_state = STATE_PLAIN;
11804 /*FALLTHROUGH*/
11805
11806 case STATE_PLAIN:
11807 /*
11808 * Go over all possible bytes at this node, add each to tword[]
11809 * and use child node. "ts_curi" is the index.
11810 */
11811 arridx = sp->ts_arridx;
11812 if (sp->ts_curi > byts[arridx])
11813 {
11814 /* Done all bytes at this node, do next state. When still at
11815 * already changed bytes skip the other tricks. */
11816 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011819 sp->ts_state = STATE_FINAL;
11820 }
11821 else
11822 {
11823 arridx += sp->ts_curi++;
11824 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825
Bram Moolenaar4770d092006-01-12 23:22:24 +000011826 /* Normal byte, go one level deeper. If it's not equal to the
11827 * byte in the bad word adjust the score. But don't even try
11828 * when the byte was already changed. And don't try when we
11829 * just deleted this byte, accepting it is always cheaper then
11830 * delete + substitute. */
11831 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011832#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011833 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011834#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011835 )
11836 newscore = 0;
11837 else
11838 newscore = SCORE_SUBST;
11839 if ((newscore == 0
11840 || (sp->ts_fidx >= sp->ts_fidxtry
11841 && ((sp->ts_flags & TSF_DIDDEL) == 0
11842 || c != fword[sp->ts_delidx])))
11843 && TRY_DEEPER(su, stack, depth, newscore))
11844 {
11845 go_deeper(stack, depth, newscore);
11846#ifdef DEBUG_TRIEWALK
11847 if (newscore > 0)
11848 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11849 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11850 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011852 sprintf(changename[depth], "%.*s-%s: accept %c",
11853 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11854 fword[sp->ts_fidx]);
11855#endif
11856 ++depth;
11857 sp = &stack[depth];
11858 ++sp->ts_fidx;
11859 tword[sp->ts_twordlen++] = c;
11860 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011861#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011862 if (newscore == SCORE_SUBST)
11863 sp->ts_isdiff = DIFF_YES;
11864 if (has_mbyte)
11865 {
11866 /* Multi-byte characters are a bit complicated to
11867 * handle: They differ when any of the bytes differ
11868 * and then their length may also differ. */
11869 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011870 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011871 /* First byte. */
11872 sp->ts_tcharidx = 0;
11873 sp->ts_tcharlen = MB_BYTE2LEN(c);
11874 sp->ts_fcharstart = sp->ts_fidx - 1;
11875 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011876 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011877 }
11878 else if (sp->ts_isdiff == DIFF_INSERT)
11879 /* When inserting trail bytes don't advance in the
11880 * bad word. */
11881 --sp->ts_fidx;
11882 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11883 {
11884 /* Last byte of character. */
11885 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011886 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011887 /* Correct ts_fidx for the byte length of the
11888 * character (we didn't check that before). */
11889 sp->ts_fidx = sp->ts_fcharstart
11890 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011891 fword[sp->ts_fcharstart]);
11892
Bram Moolenaar4770d092006-01-12 23:22:24 +000011893 /* For changing a composing character adjust
11894 * the score from SCORE_SUBST to
11895 * SCORE_SUBCOMP. */
11896 if (enc_utf8
11897 && utf_iscomposing(
11898 mb_ptr2char(tword
11899 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011900 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011901 && utf_iscomposing(
11902 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011903 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011904 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011905 SCORE_SUBST - SCORE_SUBCOMP;
11906
Bram Moolenaar4770d092006-01-12 23:22:24 +000011907 /* For a similar character adjust score from
11908 * SCORE_SUBST to SCORE_SIMILAR. */
11909 else if (!soundfold
11910 && slang->sl_has_map
11911 && similar_chars(slang,
11912 mb_ptr2char(tword
11913 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011914 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011915 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011916 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011917 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011918 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011919 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011920 else if (sp->ts_isdiff == DIFF_INSERT
11921 && sp->ts_twordlen > sp->ts_tcharlen)
11922 {
11923 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11924 c = mb_ptr2char(p);
11925 if (enc_utf8 && utf_iscomposing(c))
11926 {
11927 /* Inserting a composing char doesn't
11928 * count that much. */
11929 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11930 }
11931 else
11932 {
11933 /* If the previous character was the same,
11934 * thus doubling a character, give a bonus
11935 * to the score. Also for the soundfold
11936 * tree (might seem illogical but does
11937 * give better scores). */
11938 mb_ptr_back(tword, p);
11939 if (c == mb_ptr2char(p))
11940 sp->ts_score -= SCORE_INS
11941 - SCORE_INSDUP;
11942 }
11943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944
Bram Moolenaar4770d092006-01-12 23:22:24 +000011945 /* Starting a new char, reset the length. */
11946 sp->ts_tcharlen = 0;
11947 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011948 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011949 else
11950#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011951 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011952 /* If we found a similar char adjust the score.
11953 * We do this after calling go_deeper() because
11954 * it's slow. */
11955 if (newscore != 0
11956 && !soundfold
11957 && slang->sl_has_map
11958 && similar_chars(slang,
11959 c, fword[sp->ts_fidx - 1]))
11960 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011961 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011962 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011963 }
11964 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965
Bram Moolenaar4770d092006-01-12 23:22:24 +000011966 case STATE_DEL:
11967#ifdef FEAT_MBYTE
11968 /* When past the first byte of a multi-byte char don't try
11969 * delete/insert/swap a character. */
11970 if (has_mbyte && sp->ts_tcharlen > 0)
11971 {
11972 sp->ts_state = STATE_FINAL;
11973 break;
11974 }
11975#endif
11976 /*
11977 * Try skipping one character in the bad word (delete it).
11978 */
11979 sp->ts_state = STATE_INS_PREP;
11980 sp->ts_curi = 1;
11981 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11982 /* Deleting a vowel at the start of a word counts less, see
11983 * soundalike_score(). */
11984 newscore = 2 * SCORE_DEL / 3;
11985 else
11986 newscore = SCORE_DEL;
11987 if (fword[sp->ts_fidx] != NUL
11988 && TRY_DEEPER(su, stack, depth, newscore))
11989 {
11990 go_deeper(stack, depth, newscore);
11991#ifdef DEBUG_TRIEWALK
11992 sprintf(changename[depth], "%.*s-%s: delete %c",
11993 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11994 fword[sp->ts_fidx]);
11995#endif
11996 ++depth;
11997
11998 /* Remember what character we deleted, so that we can avoid
11999 * inserting it again. */
12000 stack[depth].ts_flags |= TSF_DIDDEL;
12001 stack[depth].ts_delidx = sp->ts_fidx;
12002
12003 /* Advance over the character in fword[]. Give a bonus to the
12004 * score if the same character is following "nn" -> "n". It's
12005 * a bit illogical for soundfold tree but it does give better
12006 * results. */
12007#ifdef FEAT_MBYTE
12008 if (has_mbyte)
12009 {
12010 c = mb_ptr2char(fword + sp->ts_fidx);
12011 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12012 if (enc_utf8 && utf_iscomposing(c))
12013 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12014 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12015 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12016 }
12017 else
12018#endif
12019 {
12020 ++stack[depth].ts_fidx;
12021 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12022 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12023 }
12024 break;
12025 }
12026 /*FALLTHROUGH*/
12027
12028 case STATE_INS_PREP:
12029 if (sp->ts_flags & TSF_DIDDEL)
12030 {
12031 /* If we just deleted a byte then inserting won't make sense,
12032 * a substitute is always cheaper. */
12033 sp->ts_state = STATE_SWAP;
12034 break;
12035 }
12036
12037 /* skip over NUL bytes */
12038 n = sp->ts_arridx;
12039 for (;;)
12040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 if (sp->ts_curi > byts[n])
12042 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012043 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012044 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012045 break;
12046 }
12047 if (byts[n + sp->ts_curi] != NUL)
12048 {
12049 /* Found a byte to insert. */
12050 sp->ts_state = STATE_INS;
12051 break;
12052 }
12053 ++sp->ts_curi;
12054 }
12055 break;
12056
12057 /*FALLTHROUGH*/
12058
12059 case STATE_INS:
12060 /* Insert one byte. Repeat this for each possible byte at this
12061 * node. */
12062 n = sp->ts_arridx;
12063 if (sp->ts_curi > byts[n])
12064 {
12065 /* Done all bytes at this node, go to next state. */
12066 sp->ts_state = STATE_SWAP;
12067 break;
12068 }
12069
12070 /* Do one more byte at this node, but:
12071 * - Skip NUL bytes.
12072 * - Skip the byte if it's equal to the byte in the word,
12073 * accepting that byte is always better.
12074 */
12075 n += sp->ts_curi++;
12076 c = byts[n];
12077 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12078 /* Inserting a vowel at the start of a word counts less,
12079 * see soundalike_score(). */
12080 newscore = 2 * SCORE_INS / 3;
12081 else
12082 newscore = SCORE_INS;
12083 if (c != fword[sp->ts_fidx]
12084 && TRY_DEEPER(su, stack, depth, newscore))
12085 {
12086 go_deeper(stack, depth, newscore);
12087#ifdef DEBUG_TRIEWALK
12088 sprintf(changename[depth], "%.*s-%s: insert %c",
12089 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12090 c);
12091#endif
12092 ++depth;
12093 sp = &stack[depth];
12094 tword[sp->ts_twordlen++] = c;
12095 sp->ts_arridx = idxs[n];
12096#ifdef FEAT_MBYTE
12097 if (has_mbyte)
12098 {
12099 fl = MB_BYTE2LEN(c);
12100 if (fl > 1)
12101 {
12102 /* There are following bytes for the same character.
12103 * We must find all bytes before trying
12104 * delete/insert/swap/etc. */
12105 sp->ts_tcharlen = fl;
12106 sp->ts_tcharidx = 1;
12107 sp->ts_isdiff = DIFF_INSERT;
12108 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012109 }
12110 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012111 fl = 1;
12112 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012113#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012114 {
12115 /* If the previous character was the same, thus doubling a
12116 * character, give a bonus to the score. Also for
12117 * soundfold words (illogical but does give a better
12118 * score). */
12119 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012120 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012121 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012122 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012123 }
12124 break;
12125
12126 case STATE_SWAP:
12127 /*
12128 * Swap two bytes in the bad word: "12" -> "21".
12129 * We change "fword" here, it's changed back afterwards at
12130 * STATE_UNSWAP.
12131 */
12132 p = fword + sp->ts_fidx;
12133 c = *p;
12134 if (c == NUL)
12135 {
12136 /* End of word, can't swap or replace. */
12137 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012138 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012140
Bram Moolenaar4770d092006-01-12 23:22:24 +000012141 /* Don't swap if the first character is not a word character.
12142 * SWAP3 etc. also don't make sense then. */
12143 if (!soundfold && !spell_iswordp(p, curbuf))
12144 {
12145 sp->ts_state = STATE_REP_INI;
12146 break;
12147 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012148
Bram Moolenaar4770d092006-01-12 23:22:24 +000012149#ifdef FEAT_MBYTE
12150 if (has_mbyte)
12151 {
12152 n = mb_cptr2len(p);
12153 c = mb_ptr2char(p);
12154 if (!soundfold && !spell_iswordp(p + n, curbuf))
12155 c2 = c; /* don't swap non-word char */
12156 else
12157 c2 = mb_ptr2char(p + n);
12158 }
12159 else
12160#endif
12161 {
12162 if (!soundfold && !spell_iswordp(p + 1, curbuf))
12163 c2 = c; /* don't swap non-word char */
12164 else
12165 c2 = p[1];
12166 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012167
Bram Moolenaar4770d092006-01-12 23:22:24 +000012168 /* When characters are identical, swap won't do anything.
12169 * Also get here if the second char is not a word character. */
12170 if (c == c2)
12171 {
12172 sp->ts_state = STATE_SWAP3;
12173 break;
12174 }
12175 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12176 {
12177 go_deeper(stack, depth, SCORE_SWAP);
12178#ifdef DEBUG_TRIEWALK
12179 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12180 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12181 c, c2);
12182#endif
12183 sp->ts_state = STATE_UNSWAP;
12184 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012185#ifdef FEAT_MBYTE
12186 if (has_mbyte)
12187 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012188 fl = mb_char2len(c2);
12189 mch_memmove(p, p + n, fl);
12190 mb_char2bytes(c, p + fl);
12191 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012192 }
12193 else
12194#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012195 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012196 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012197 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012198 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012199 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012200 }
12201 else
12202 /* If this swap doesn't work then SWAP3 won't either. */
12203 sp->ts_state = STATE_REP_INI;
12204 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012205
Bram Moolenaar4770d092006-01-12 23:22:24 +000012206 case STATE_UNSWAP:
12207 /* Undo the STATE_SWAP swap: "21" -> "12". */
12208 p = fword + sp->ts_fidx;
12209#ifdef FEAT_MBYTE
12210 if (has_mbyte)
12211 {
12212 n = MB_BYTE2LEN(*p);
12213 c = mb_ptr2char(p + n);
12214 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12215 mb_char2bytes(c, p);
12216 }
12217 else
12218#endif
12219 {
12220 c = *p;
12221 *p = p[1];
12222 p[1] = c;
12223 }
12224 /*FALLTHROUGH*/
12225
12226 case STATE_SWAP3:
12227 /* Swap two bytes, skipping one: "123" -> "321". We change
12228 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12229 p = fword + sp->ts_fidx;
12230#ifdef FEAT_MBYTE
12231 if (has_mbyte)
12232 {
12233 n = mb_cptr2len(p);
12234 c = mb_ptr2char(p);
12235 fl = mb_cptr2len(p + n);
12236 c2 = mb_ptr2char(p + n);
12237 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12238 c3 = c; /* don't swap non-word char */
12239 else
12240 c3 = mb_ptr2char(p + n + fl);
12241 }
12242 else
12243#endif
12244 {
12245 c = *p;
12246 c2 = p[1];
12247 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12248 c3 = c; /* don't swap non-word char */
12249 else
12250 c3 = p[2];
12251 }
12252
12253 /* When characters are identical: "121" then SWAP3 result is
12254 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12255 * same as SWAP on next char: "112". Thus skip all swapping.
12256 * Also skip when c3 is NUL.
12257 * Also get here when the third character is not a word character.
12258 * Second character may any char: "a.b" -> "b.a" */
12259 if (c == c3 || c3 == NUL)
12260 {
12261 sp->ts_state = STATE_REP_INI;
12262 break;
12263 }
12264 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12265 {
12266 go_deeper(stack, depth, SCORE_SWAP3);
12267#ifdef DEBUG_TRIEWALK
12268 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12269 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12270 c, c3);
12271#endif
12272 sp->ts_state = STATE_UNSWAP3;
12273 ++depth;
12274#ifdef FEAT_MBYTE
12275 if (has_mbyte)
12276 {
12277 tl = mb_char2len(c3);
12278 mch_memmove(p, p + n + fl, tl);
12279 mb_char2bytes(c2, p + tl);
12280 mb_char2bytes(c, p + fl + tl);
12281 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12282 }
12283 else
12284#endif
12285 {
12286 p[0] = p[2];
12287 p[2] = c;
12288 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12289 }
12290 }
12291 else
12292 sp->ts_state = STATE_REP_INI;
12293 break;
12294
12295 case STATE_UNSWAP3:
12296 /* Undo STATE_SWAP3: "321" -> "123" */
12297 p = fword + sp->ts_fidx;
12298#ifdef FEAT_MBYTE
12299 if (has_mbyte)
12300 {
12301 n = MB_BYTE2LEN(*p);
12302 c2 = mb_ptr2char(p + n);
12303 fl = MB_BYTE2LEN(p[n]);
12304 c = mb_ptr2char(p + n + fl);
12305 tl = MB_BYTE2LEN(p[n + fl]);
12306 mch_memmove(p + fl + tl, p, n);
12307 mb_char2bytes(c, p);
12308 mb_char2bytes(c2, p + tl);
12309 p = p + tl;
12310 }
12311 else
12312#endif
12313 {
12314 c = *p;
12315 *p = p[2];
12316 p[2] = c;
12317 ++p;
12318 }
12319
12320 if (!soundfold && !spell_iswordp(p, curbuf))
12321 {
12322 /* Middle char is not a word char, skip the rotate. First and
12323 * third char were already checked at swap and swap3. */
12324 sp->ts_state = STATE_REP_INI;
12325 break;
12326 }
12327
12328 /* Rotate three characters left: "123" -> "231". We change
12329 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12330 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12331 {
12332 go_deeper(stack, depth, SCORE_SWAP3);
12333#ifdef DEBUG_TRIEWALK
12334 p = fword + sp->ts_fidx;
12335 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12336 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12337 p[0], p[1], p[2]);
12338#endif
12339 sp->ts_state = STATE_UNROT3L;
12340 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012341 p = fword + sp->ts_fidx;
12342#ifdef FEAT_MBYTE
12343 if (has_mbyte)
12344 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012345 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012346 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012347 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012348 fl += mb_cptr2len(p + n + fl);
12349 mch_memmove(p, p + n, fl);
12350 mb_char2bytes(c, p + fl);
12351 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012352 }
12353 else
12354#endif
12355 {
12356 c = *p;
12357 *p = p[1];
12358 p[1] = p[2];
12359 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012360 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012361 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012362 }
12363 else
12364 sp->ts_state = STATE_REP_INI;
12365 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012366
Bram Moolenaar4770d092006-01-12 23:22:24 +000012367 case STATE_UNROT3L:
12368 /* Undo ROT3L: "231" -> "123" */
12369 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012370#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012371 if (has_mbyte)
12372 {
12373 n = MB_BYTE2LEN(*p);
12374 n += MB_BYTE2LEN(p[n]);
12375 c = mb_ptr2char(p + n);
12376 tl = MB_BYTE2LEN(p[n]);
12377 mch_memmove(p + tl, p, n);
12378 mb_char2bytes(c, p);
12379 }
12380 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012381#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012382 {
12383 c = p[2];
12384 p[2] = p[1];
12385 p[1] = *p;
12386 *p = c;
12387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388
Bram Moolenaar4770d092006-01-12 23:22:24 +000012389 /* Rotate three bytes right: "123" -> "312". We change "fword"
12390 * here, it's changed back afterwards at STATE_UNROT3R. */
12391 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12392 {
12393 go_deeper(stack, depth, SCORE_SWAP3);
12394#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012395 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012396 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12397 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12398 p[0], p[1], p[2]);
12399#endif
12400 sp->ts_state = STATE_UNROT3R;
12401 ++depth;
12402 p = fword + sp->ts_fidx;
12403#ifdef FEAT_MBYTE
12404 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012405 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012406 n = mb_cptr2len(p);
12407 n += mb_cptr2len(p + n);
12408 c = mb_ptr2char(p + n);
12409 tl = mb_cptr2len(p + n);
12410 mch_memmove(p + tl, p, n);
12411 mb_char2bytes(c, p);
12412 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012413 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012414 else
12415#endif
12416 {
12417 c = p[2];
12418 p[2] = p[1];
12419 p[1] = *p;
12420 *p = c;
12421 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12422 }
12423 }
12424 else
12425 sp->ts_state = STATE_REP_INI;
12426 break;
12427
12428 case STATE_UNROT3R:
12429 /* Undo ROT3R: "312" -> "123" */
12430 p = fword + sp->ts_fidx;
12431#ifdef FEAT_MBYTE
12432 if (has_mbyte)
12433 {
12434 c = mb_ptr2char(p);
12435 tl = MB_BYTE2LEN(*p);
12436 n = MB_BYTE2LEN(p[tl]);
12437 n += MB_BYTE2LEN(p[tl + n]);
12438 mch_memmove(p, p + tl, n);
12439 mb_char2bytes(c, p + n);
12440 }
12441 else
12442#endif
12443 {
12444 c = *p;
12445 *p = p[1];
12446 p[1] = p[2];
12447 p[2] = c;
12448 }
12449 /*FALLTHROUGH*/
12450
12451 case STATE_REP_INI:
12452 /* Check if matching with REP items from the .aff file would work.
12453 * Quickly skip if:
12454 * - there are no REP items and we are not in the soundfold trie
12455 * - the score is going to be too high anyway
12456 * - already applied a REP item or swapped here */
12457 if ((lp->lp_replang == NULL && !soundfold)
12458 || sp->ts_score + SCORE_REP >= su->su_maxscore
12459 || sp->ts_fidx < sp->ts_fidxtry)
12460 {
12461 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012462 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012464
Bram Moolenaar4770d092006-01-12 23:22:24 +000012465 /* Use the first byte to quickly find the first entry that may
12466 * match. If the index is -1 there is none. */
12467 if (soundfold)
12468 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12469 else
12470 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471
Bram Moolenaar4770d092006-01-12 23:22:24 +000012472 if (sp->ts_curi < 0)
12473 {
12474 sp->ts_state = STATE_FINAL;
12475 break;
12476 }
12477
12478 sp->ts_state = STATE_REP;
12479 /*FALLTHROUGH*/
12480
12481 case STATE_REP:
12482 /* Try matching with REP items from the .aff file. For each match
12483 * replace the characters and check if the resulting word is
12484 * valid. */
12485 p = fword + sp->ts_fidx;
12486
12487 if (soundfold)
12488 gap = &slang->sl_repsal;
12489 else
12490 gap = &lp->lp_replang->sl_rep;
12491 while (sp->ts_curi < gap->ga_len)
12492 {
12493 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12494 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012495 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012496 /* past possible matching entries */
12497 sp->ts_curi = gap->ga_len;
12498 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012499 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012500 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12501 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12502 {
12503 go_deeper(stack, depth, SCORE_REP);
12504#ifdef DEBUG_TRIEWALK
12505 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12506 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12507 ftp->ft_from, ftp->ft_to);
12508#endif
12509 /* Need to undo this afterwards. */
12510 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012511
Bram Moolenaar4770d092006-01-12 23:22:24 +000012512 /* Change the "from" to the "to" string. */
12513 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012514 fl = (int)STRLEN(ftp->ft_from);
12515 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012516 if (fl != tl)
12517 {
12518 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12519 repextra += tl - fl;
12520 }
12521 mch_memmove(p, ftp->ft_to, tl);
12522 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12523#ifdef FEAT_MBYTE
12524 stack[depth].ts_tcharlen = 0;
12525#endif
12526 break;
12527 }
12528 }
12529
12530 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12531 /* No (more) matches. */
12532 sp->ts_state = STATE_FINAL;
12533
12534 break;
12535
12536 case STATE_REP_UNDO:
12537 /* Undo a REP replacement and continue with the next one. */
12538 if (soundfold)
12539 gap = &slang->sl_repsal;
12540 else
12541 gap = &lp->lp_replang->sl_rep;
12542 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012543 fl = (int)STRLEN(ftp->ft_from);
12544 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012545 p = fword + sp->ts_fidx;
12546 if (fl != tl)
12547 {
12548 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12549 repextra -= tl - fl;
12550 }
12551 mch_memmove(p, ftp->ft_from, fl);
12552 sp->ts_state = STATE_REP;
12553 break;
12554
12555 default:
12556 /* Did all possible states at this level, go up one level. */
12557 --depth;
12558
12559 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12560 {
12561 /* Continue in or go back to the prefix tree. */
12562 byts = pbyts;
12563 idxs = pidxs;
12564 }
12565
12566 /* Don't check for CTRL-C too often, it takes time. */
12567 if (--breakcheckcount == 0)
12568 {
12569 ui_breakcheck();
12570 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012571 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012572 }
12573 }
12574}
12575
Bram Moolenaar4770d092006-01-12 23:22:24 +000012576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012578 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012579 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012580 static void
12581go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012582 trystate_T *stack;
12583 int depth;
12584 int score_add;
12585{
Bram Moolenaarea424162005-06-16 21:51:00 +000012586 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012587 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012588 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012589 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012590 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012591}
12592
Bram Moolenaar53805d12005-08-01 07:08:33 +000012593#ifdef FEAT_MBYTE
12594/*
12595 * Case-folding may change the number of bytes: Count nr of chars in
12596 * fword[flen] and return the byte length of that many chars in "word".
12597 */
12598 static int
12599nofold_len(fword, flen, word)
12600 char_u *fword;
12601 int flen;
12602 char_u *word;
12603{
12604 char_u *p;
12605 int i = 0;
12606
12607 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12608 ++i;
12609 for (p = word; i > 0; mb_ptr_adv(p))
12610 --i;
12611 return (int)(p - word);
12612}
12613#endif
12614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615/*
12616 * "fword" is a good word with case folded. Find the matching keep-case
12617 * words and put it in "kword".
12618 * Theoretically there could be several keep-case words that result in the
12619 * same case-folded word, but we only find one...
12620 */
12621 static void
12622find_keepcap_word(slang, fword, kword)
12623 slang_T *slang;
12624 char_u *fword;
12625 char_u *kword;
12626{
12627 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12628 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012629 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012630
12631 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012632 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 int round[MAXWLEN];
12634 int fwordidx[MAXWLEN];
12635 int uwordidx[MAXWLEN];
12636 int kwordlen[MAXWLEN];
12637
12638 int flen, ulen;
12639 int l;
12640 int len;
12641 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012642 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 char_u *p;
12644 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012645 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646
12647 if (byts == NULL)
12648 {
12649 /* array is empty: "cannot happen" */
12650 *kword = NUL;
12651 return;
12652 }
12653
12654 /* Make an all-cap version of "fword". */
12655 allcap_copy(fword, uword);
12656
12657 /*
12658 * Each character needs to be tried both case-folded and upper-case.
12659 * All this gets very complicated if we keep in mind that changing case
12660 * may change the byte length of a multi-byte character...
12661 */
12662 depth = 0;
12663 arridx[0] = 0;
12664 round[0] = 0;
12665 fwordidx[0] = 0;
12666 uwordidx[0] = 0;
12667 kwordlen[0] = 0;
12668 while (depth >= 0)
12669 {
12670 if (fword[fwordidx[depth]] == NUL)
12671 {
12672 /* We are at the end of "fword". If the tree allows a word to end
12673 * here we have found a match. */
12674 if (byts[arridx[depth] + 1] == 0)
12675 {
12676 kword[kwordlen[depth]] = NUL;
12677 return;
12678 }
12679
12680 /* kword is getting too long, continue one level up */
12681 --depth;
12682 }
12683 else if (++round[depth] > 2)
12684 {
12685 /* tried both fold-case and upper-case character, continue one
12686 * level up */
12687 --depth;
12688 }
12689 else
12690 {
12691 /*
12692 * round[depth] == 1: Try using the folded-case character.
12693 * round[depth] == 2: Try using the upper-case character.
12694 */
12695#ifdef FEAT_MBYTE
12696 if (has_mbyte)
12697 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012698 flen = mb_cptr2len(fword + fwordidx[depth]);
12699 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 }
12701 else
12702#endif
12703 ulen = flen = 1;
12704 if (round[depth] == 1)
12705 {
12706 p = fword + fwordidx[depth];
12707 l = flen;
12708 }
12709 else
12710 {
12711 p = uword + uwordidx[depth];
12712 l = ulen;
12713 }
12714
12715 for (tryidx = arridx[depth]; l > 0; --l)
12716 {
12717 /* Perform a binary search in the list of accepted bytes. */
12718 len = byts[tryidx++];
12719 c = *p++;
12720 lo = tryidx;
12721 hi = tryidx + len - 1;
12722 while (lo < hi)
12723 {
12724 m = (lo + hi) / 2;
12725 if (byts[m] > c)
12726 hi = m - 1;
12727 else if (byts[m] < c)
12728 lo = m + 1;
12729 else
12730 {
12731 lo = hi = m;
12732 break;
12733 }
12734 }
12735
12736 /* Stop if there is no matching byte. */
12737 if (hi < lo || byts[lo] != c)
12738 break;
12739
12740 /* Continue at the child (if there is one). */
12741 tryidx = idxs[lo];
12742 }
12743
12744 if (l == 0)
12745 {
12746 /*
12747 * Found the matching char. Copy it to "kword" and go a
12748 * level deeper.
12749 */
12750 if (round[depth] == 1)
12751 {
12752 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12753 flen);
12754 kwordlen[depth + 1] = kwordlen[depth] + flen;
12755 }
12756 else
12757 {
12758 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12759 ulen);
12760 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12761 }
12762 fwordidx[depth + 1] = fwordidx[depth] + flen;
12763 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12764
12765 ++depth;
12766 arridx[depth] = tryidx;
12767 round[depth] = 0;
12768 }
12769 }
12770 }
12771
12772 /* Didn't find it: "cannot happen". */
12773 *kword = NUL;
12774}
12775
12776/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012777 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12778 * su->su_sga.
12779 */
12780 static void
12781score_comp_sal(su)
12782 suginfo_T *su;
12783{
12784 langp_T *lp;
12785 char_u badsound[MAXWLEN];
12786 int i;
12787 suggest_T *stp;
12788 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012789 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012790 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012791
12792 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12793 return;
12794
12795 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012796 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012797 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012798 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012799 if (lp->lp_slang->sl_sal.ga_len > 0)
12800 {
12801 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012802 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012803
12804 for (i = 0; i < su->su_ga.ga_len; ++i)
12805 {
12806 stp = &SUG(su->su_ga, i);
12807
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012808 /* Case-fold the suggested word, sound-fold it and compute the
12809 * sound-a-like score. */
12810 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012811 if (score < SCORE_MAXMAX)
12812 {
12813 /* Add the suggestion. */
12814 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12815 sstp->st_word = vim_strsave(stp->st_word);
12816 if (sstp->st_word != NULL)
12817 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012818 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012819 sstp->st_score = score;
12820 sstp->st_altscore = 0;
12821 sstp->st_orglen = stp->st_orglen;
12822 ++su->su_sga.ga_len;
12823 }
12824 }
12825 }
12826 break;
12827 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012828 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012829}
12830
12831/*
12832 * Combine the list of suggestions in su->su_ga and su->su_sga.
12833 * They are intwined.
12834 */
12835 static void
12836score_combine(su)
12837 suginfo_T *su;
12838{
12839 int i;
12840 int j;
12841 garray_T ga;
12842 garray_T *gap;
12843 langp_T *lp;
12844 suggest_T *stp;
12845 char_u *p;
12846 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012847 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012848 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012849 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012850
12851 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012852 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012853 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012854 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012855 if (lp->lp_slang->sl_sal.ga_len > 0)
12856 {
12857 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012858 slang = lp->lp_slang;
12859 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012860
12861 for (i = 0; i < su->su_ga.ga_len; ++i)
12862 {
12863 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012864 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012865 if (stp->st_altscore == SCORE_MAXMAX)
12866 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12867 else
12868 stp->st_score = (stp->st_score * 3
12869 + stp->st_altscore) / 4;
12870 stp->st_salscore = FALSE;
12871 }
12872 break;
12873 }
12874 }
12875
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012876 if (slang == NULL) /* Using "double" without sound folding. */
12877 {
12878 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12879 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012880 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012881 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012882
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012883 /* Add the alternate score to su_sga. */
12884 for (i = 0; i < su->su_sga.ga_len; ++i)
12885 {
12886 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012887 stp->st_altscore = spell_edit_score(slang,
12888 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012889 if (stp->st_score == SCORE_MAXMAX)
12890 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12891 else
12892 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12893 stp->st_salscore = TRUE;
12894 }
12895
Bram Moolenaar4770d092006-01-12 23:22:24 +000012896 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12897 * for both lists. */
12898 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012899 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012900 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012901 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12902
12903 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12904 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12905 return;
12906
12907 stp = &SUG(ga, 0);
12908 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12909 {
12910 /* round 1: get a suggestion from su_ga
12911 * round 2: get a suggestion from su_sga */
12912 for (round = 1; round <= 2; ++round)
12913 {
12914 gap = round == 1 ? &su->su_ga : &su->su_sga;
12915 if (i < gap->ga_len)
12916 {
12917 /* Don't add a word if it's already there. */
12918 p = SUG(*gap, i).st_word;
12919 for (j = 0; j < ga.ga_len; ++j)
12920 if (STRCMP(stp[j].st_word, p) == 0)
12921 break;
12922 if (j == ga.ga_len)
12923 stp[ga.ga_len++] = SUG(*gap, i);
12924 else
12925 vim_free(p);
12926 }
12927 }
12928 }
12929
12930 ga_clear(&su->su_ga);
12931 ga_clear(&su->su_sga);
12932
12933 /* Truncate the list to the number of suggestions that will be displayed. */
12934 if (ga.ga_len > su->su_maxcount)
12935 {
12936 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12937 vim_free(stp[i].st_word);
12938 ga.ga_len = su->su_maxcount;
12939 }
12940
12941 su->su_ga = ga;
12942}
12943
12944/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012945 * For the goodword in "stp" compute the soundalike score compared to the
12946 * badword.
12947 */
12948 static int
12949stp_sal_score(stp, su, slang, badsound)
12950 suggest_T *stp;
12951 suginfo_T *su;
12952 slang_T *slang;
12953 char_u *badsound; /* sound-folded badword */
12954{
12955 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012956 char_u *pbad;
12957 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012958 char_u badsound2[MAXWLEN];
12959 char_u fword[MAXWLEN];
12960 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012961 char_u goodword[MAXWLEN];
12962 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012963
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012964 lendiff = (int)(su->su_badlen - stp->st_orglen);
12965 if (lendiff >= 0)
12966 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012967 else
12968 {
12969 /* soundfold the bad word with more characters following */
12970 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12971
12972 /* When joining two words the sound often changes a lot. E.g., "t he"
12973 * sounds like "t h" while "the" sounds like "@". Avoid that by
12974 * removing the space. Don't do it when the good word also contains a
12975 * space. */
12976 if (vim_iswhite(su->su_badptr[su->su_badlen])
12977 && *skiptowhite(stp->st_word) == NUL)
12978 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12979 mch_memmove(p, p + 1, STRLEN(p));
12980
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012981 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012982 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012983 }
12984
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012985 if (lendiff > 0)
12986 {
12987 /* Add part of the bad word to the good word, so that we soundfold
12988 * what replaces the bad word. */
12989 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012990 vim_strncpy(goodword + stp->st_wordlen,
12991 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012992 pgood = goodword;
12993 }
12994 else
12995 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012996
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012997 /* Sound-fold the word and compute the score for the difference. */
12998 spell_soundfold(slang, pgood, FALSE, goodsound);
12999
13000 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013001}
13002
Bram Moolenaar4770d092006-01-12 23:22:24 +000013003/* structure used to store soundfolded words that add_sound_suggest() has
13004 * handled already. */
13005typedef struct
13006{
13007 short sft_score; /* lowest score used */
13008 char_u sft_word[1]; /* soundfolded word, actually longer */
13009} sftword_T;
13010
13011static sftword_T dumsft;
13012#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13013#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13014
13015/*
13016 * Prepare for calling suggest_try_soundalike().
13017 */
13018 static void
13019suggest_try_soundalike_prep()
13020{
13021 langp_T *lp;
13022 int lpi;
13023 slang_T *slang;
13024
13025 /* Do this for all languages that support sound folding and for which a
13026 * .sug file has been loaded. */
13027 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13028 {
13029 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13030 slang = lp->lp_slang;
13031 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13032 /* prepare the hashtable used by add_sound_suggest() */
13033 hash_init(&slang->sl_sounddone);
13034 }
13035}
13036
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013037/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013039 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 */
13041 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013042suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 suginfo_T *su;
13044{
13045 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013047 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013048 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049
Bram Moolenaar4770d092006-01-12 23:22:24 +000013050 /* Do this for all languages that support sound folding and for which a
13051 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013052 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013053 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013054 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13055 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013056 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 {
13058 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013059 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060
Bram Moolenaar4770d092006-01-12 23:22:24 +000013061 /* try all kinds of inserts/deletes/swaps/etc. */
13062 /* TODO: also soundfold the next words, so that we can try joining
13063 * and splitting */
13064 suggest_trie_walk(su, lp, salword, TRUE);
13065 }
13066 }
13067}
13068
13069/*
13070 * Finish up after calling suggest_try_soundalike().
13071 */
13072 static void
13073suggest_try_soundalike_finish()
13074{
13075 langp_T *lp;
13076 int lpi;
13077 slang_T *slang;
13078 int todo;
13079 hashitem_T *hi;
13080
13081 /* Do this for all languages that support sound folding and for which a
13082 * .sug file has been loaded. */
13083 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13084 {
13085 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13086 slang = lp->lp_slang;
13087 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13088 {
13089 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013090 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013091 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13092 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013094 vim_free(HI2SFT(hi));
13095 --todo;
13096 }
13097 hash_clear(&slang->sl_sounddone);
13098 }
13099 }
13100}
13101
13102/*
13103 * A match with a soundfolded word is found. Add the good word(s) that
13104 * produce this soundfolded word.
13105 */
13106 static void
13107add_sound_suggest(su, goodword, score, lp)
13108 suginfo_T *su;
13109 char_u *goodword;
13110 int score; /* soundfold score */
13111 langp_T *lp;
13112{
13113 slang_T *slang = lp->lp_slang; /* language for sound folding */
13114 int sfwordnr;
13115 char_u *nrline;
13116 int orgnr;
13117 char_u theword[MAXWLEN];
13118 int i;
13119 int wlen;
13120 char_u *byts;
13121 idx_T *idxs;
13122 int n;
13123 int wordcount;
13124 int wc;
13125 int goodscore;
13126 hash_T hash;
13127 hashitem_T *hi;
13128 sftword_T *sft;
13129 int bc, gc;
13130 int limit;
13131
13132 /*
13133 * It's very well possible that the same soundfold word is found several
13134 * times with different scores. Since the following is quite slow only do
13135 * the words that have a better score than before. Use a hashtable to
13136 * remember the words that have been done.
13137 */
13138 hash = hash_hash(goodword);
13139 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13140 if (HASHITEM_EMPTY(hi))
13141 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013142 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13143 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013144 if (sft != NULL)
13145 {
13146 sft->sft_score = score;
13147 STRCPY(sft->sft_word, goodword);
13148 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13149 }
13150 }
13151 else
13152 {
13153 sft = HI2SFT(hi);
13154 if (score >= sft->sft_score)
13155 return;
13156 sft->sft_score = score;
13157 }
13158
13159 /*
13160 * Find the word nr in the soundfold tree.
13161 */
13162 sfwordnr = soundfold_find(slang, goodword);
13163 if (sfwordnr < 0)
13164 {
13165 EMSG2(_(e_intern2), "add_sound_suggest()");
13166 return;
13167 }
13168
13169 /*
13170 * go over the list of good words that produce this soundfold word
13171 */
13172 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13173 orgnr = 0;
13174 while (*nrline != NUL)
13175 {
13176 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13177 * previous wordnr. */
13178 orgnr += bytes2offset(&nrline);
13179
13180 byts = slang->sl_fbyts;
13181 idxs = slang->sl_fidxs;
13182
13183 /* Lookup the word "orgnr" one of the two tries. */
13184 n = 0;
13185 wlen = 0;
13186 wordcount = 0;
13187 for (;;)
13188 {
13189 i = 1;
13190 if (wordcount == orgnr && byts[n + 1] == NUL)
13191 break; /* found end of word */
13192
13193 if (byts[n + 1] == NUL)
13194 ++wordcount;
13195
13196 /* skip over the NUL bytes */
13197 for ( ; byts[n + i] == NUL; ++i)
13198 if (i > byts[n]) /* safety check */
13199 {
13200 STRCPY(theword + wlen, "BAD");
13201 goto badword;
13202 }
13203
13204 /* One of the siblings must have the word. */
13205 for ( ; i < byts[n]; ++i)
13206 {
13207 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13208 if (wordcount + wc > orgnr)
13209 break;
13210 wordcount += wc;
13211 }
13212
13213 theword[wlen++] = byts[n + i];
13214 n = idxs[n + i];
13215 }
13216badword:
13217 theword[wlen] = NUL;
13218
13219 /* Go over the possible flags and regions. */
13220 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13221 {
13222 char_u cword[MAXWLEN];
13223 char_u *p;
13224 int flags = (int)idxs[n + i];
13225
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013226 /* Skip words with the NOSUGGEST flag */
13227 if (flags & WF_NOSUGGEST)
13228 continue;
13229
Bram Moolenaar4770d092006-01-12 23:22:24 +000013230 if (flags & WF_KEEPCAP)
13231 {
13232 /* Must find the word in the keep-case tree. */
13233 find_keepcap_word(slang, theword, cword);
13234 p = cword;
13235 }
13236 else
13237 {
13238 flags |= su->su_badflags;
13239 if ((flags & WF_CAPMASK) != 0)
13240 {
13241 /* Need to fix case according to "flags". */
13242 make_case_word(theword, cword, flags);
13243 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 }
13245 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013246 p = theword;
13247 }
13248
13249 /* Add the suggestion. */
13250 if (sps_flags & SPS_DOUBLE)
13251 {
13252 /* Add the suggestion if the score isn't too bad. */
13253 if (score <= su->su_maxscore)
13254 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13255 score, 0, FALSE, slang, FALSE);
13256 }
13257 else
13258 {
13259 /* Add a penalty for words in another region. */
13260 if ((flags & WF_REGION)
13261 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13262 goodscore = SCORE_REGION;
13263 else
13264 goodscore = 0;
13265
13266 /* Add a small penalty for changing the first letter from
13267 * lower to upper case. Helps for "tath" -> "Kath", which is
13268 * less common thatn "tath" -> "path". Don't do it when the
13269 * letter is the same, that has already been counted. */
13270 gc = PTR2CHAR(p);
13271 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013273 bc = PTR2CHAR(su->su_badword);
13274 if (!SPELL_ISUPPER(bc)
13275 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13276 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 }
13278
Bram Moolenaar4770d092006-01-12 23:22:24 +000013279 /* Compute the score for the good word. This only does letter
13280 * insert/delete/swap/replace. REP items are not considered,
13281 * which may make the score a bit higher.
13282 * Use a limit for the score to make it work faster. Use
13283 * MAXSCORE(), because RESCORE() will change the score.
13284 * If the limit is very high then the iterative method is
13285 * inefficient, using an array is quicker. */
13286 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13287 if (limit > SCORE_LIMITMAX)
13288 goodscore += spell_edit_score(slang, su->su_badword, p);
13289 else
13290 goodscore += spell_edit_score_limit(slang, su->su_badword,
13291 p, limit);
13292
13293 /* When going over the limit don't bother to do the rest. */
13294 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013296 /* Give a bonus to words seen before. */
13297 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013298
Bram Moolenaar4770d092006-01-12 23:22:24 +000013299 /* Add the suggestion if the score isn't too bad. */
13300 goodscore = RESCORE(goodscore, score);
13301 if (goodscore <= su->su_sfmaxscore)
13302 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13303 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 }
13306 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013307 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013308 }
13309}
13310
13311/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013312 * Find word "word" in fold-case tree for "slang" and return the word number.
13313 */
13314 static int
13315soundfold_find(slang, word)
13316 slang_T *slang;
13317 char_u *word;
13318{
13319 idx_T arridx = 0;
13320 int len;
13321 int wlen = 0;
13322 int c;
13323 char_u *ptr = word;
13324 char_u *byts;
13325 idx_T *idxs;
13326 int wordnr = 0;
13327
13328 byts = slang->sl_sbyts;
13329 idxs = slang->sl_sidxs;
13330
13331 for (;;)
13332 {
13333 /* First byte is the number of possible bytes. */
13334 len = byts[arridx++];
13335
13336 /* If the first possible byte is a zero the word could end here.
13337 * If the word ends we found the word. If not skip the NUL bytes. */
13338 c = ptr[wlen];
13339 if (byts[arridx] == NUL)
13340 {
13341 if (c == NUL)
13342 break;
13343
13344 /* Skip over the zeros, there can be several. */
13345 while (len > 0 && byts[arridx] == NUL)
13346 {
13347 ++arridx;
13348 --len;
13349 }
13350 if (len == 0)
13351 return -1; /* no children, word should have ended here */
13352 ++wordnr;
13353 }
13354
13355 /* If the word ends we didn't find it. */
13356 if (c == NUL)
13357 return -1;
13358
13359 /* Perform a binary search in the list of accepted bytes. */
13360 if (c == TAB) /* <Tab> is handled like <Space> */
13361 c = ' ';
13362 while (byts[arridx] < c)
13363 {
13364 /* The word count is in the first idxs[] entry of the child. */
13365 wordnr += idxs[idxs[arridx]];
13366 ++arridx;
13367 if (--len == 0) /* end of the bytes, didn't find it */
13368 return -1;
13369 }
13370 if (byts[arridx] != c) /* didn't find the byte */
13371 return -1;
13372
13373 /* Continue at the child (if there is one). */
13374 arridx = idxs[arridx];
13375 ++wlen;
13376
13377 /* One space in the good word may stand for several spaces in the
13378 * checked word. */
13379 if (c == ' ')
13380 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13381 ++wlen;
13382 }
13383
13384 return wordnr;
13385}
13386
13387/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013388 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 */
13390 static void
13391make_case_word(fword, cword, flags)
13392 char_u *fword;
13393 char_u *cword;
13394 int flags;
13395{
13396 if (flags & WF_ALLCAP)
13397 /* Make it all upper-case */
13398 allcap_copy(fword, cword);
13399 else if (flags & WF_ONECAP)
13400 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013401 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 else
13403 /* Use goodword as-is. */
13404 STRCPY(cword, fword);
13405}
13406
Bram Moolenaarea424162005-06-16 21:51:00 +000013407/*
13408 * Use map string "map" for languages "lp".
13409 */
13410 static void
13411set_map_str(lp, map)
13412 slang_T *lp;
13413 char_u *map;
13414{
13415 char_u *p;
13416 int headc = 0;
13417 int c;
13418 int i;
13419
13420 if (*map == NUL)
13421 {
13422 lp->sl_has_map = FALSE;
13423 return;
13424 }
13425 lp->sl_has_map = TRUE;
13426
Bram Moolenaar4770d092006-01-12 23:22:24 +000013427 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013428 for (i = 0; i < 256; ++i)
13429 lp->sl_map_array[i] = 0;
13430#ifdef FEAT_MBYTE
13431 hash_init(&lp->sl_map_hash);
13432#endif
13433
13434 /*
13435 * The similar characters are stored separated with slashes:
13436 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13437 * before the same slash. For characters above 255 sl_map_hash is used.
13438 */
13439 for (p = map; *p != NUL; )
13440 {
13441#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013442 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013443#else
13444 c = *p++;
13445#endif
13446 if (c == '/')
13447 headc = 0;
13448 else
13449 {
13450 if (headc == 0)
13451 headc = c;
13452
13453#ifdef FEAT_MBYTE
13454 /* Characters above 255 don't fit in sl_map_array[], put them in
13455 * the hash table. Each entry is the char, a NUL the headchar and
13456 * a NUL. */
13457 if (c >= 256)
13458 {
13459 int cl = mb_char2len(c);
13460 int headcl = mb_char2len(headc);
13461 char_u *b;
13462 hash_T hash;
13463 hashitem_T *hi;
13464
13465 b = alloc((unsigned)(cl + headcl + 2));
13466 if (b == NULL)
13467 return;
13468 mb_char2bytes(c, b);
13469 b[cl] = NUL;
13470 mb_char2bytes(headc, b + cl + 1);
13471 b[cl + 1 + headcl] = NUL;
13472 hash = hash_hash(b);
13473 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13474 if (HASHITEM_EMPTY(hi))
13475 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13476 else
13477 {
13478 /* This should have been checked when generating the .spl
13479 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013480 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013481 vim_free(b);
13482 }
13483 }
13484 else
13485#endif
13486 lp->sl_map_array[c] = headc;
13487 }
13488 }
13489}
13490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491/*
13492 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13493 * lines in the .aff file.
13494 */
13495 static int
13496similar_chars(slang, c1, c2)
13497 slang_T *slang;
13498 int c1;
13499 int c2;
13500{
Bram Moolenaarea424162005-06-16 21:51:00 +000013501 int m1, m2;
13502#ifdef FEAT_MBYTE
13503 char_u buf[MB_MAXBYTES];
13504 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505
Bram Moolenaarea424162005-06-16 21:51:00 +000013506 if (c1 >= 256)
13507 {
13508 buf[mb_char2bytes(c1, buf)] = 0;
13509 hi = hash_find(&slang->sl_map_hash, buf);
13510 if (HASHITEM_EMPTY(hi))
13511 m1 = 0;
13512 else
13513 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13514 }
13515 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013516#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013517 m1 = slang->sl_map_array[c1];
13518 if (m1 == 0)
13519 return FALSE;
13520
13521
13522#ifdef FEAT_MBYTE
13523 if (c2 >= 256)
13524 {
13525 buf[mb_char2bytes(c2, buf)] = 0;
13526 hi = hash_find(&slang->sl_map_hash, buf);
13527 if (HASHITEM_EMPTY(hi))
13528 m2 = 0;
13529 else
13530 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13531 }
13532 else
13533#endif
13534 m2 = slang->sl_map_array[c2];
13535
13536 return m1 == m2;
13537}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538
13539/*
13540 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013541 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542 */
13543 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013544add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13545 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013547 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013549 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013550 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013551 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013552 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013553 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013554 int maxsf; /* su_maxscore applies to soundfold score,
13555 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013557 int goodlen; /* len of goodword changed */
13558 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013559 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013560 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013562 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013564 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13565 * "thee the" is added next to changing the first "the" the "thee". */
13566 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013567 pbad = su->su_badptr + badlenarg;
13568 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013569 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013570 goodlen = (int)(pgood - goodword);
13571 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013572 if (goodlen <= 0 || badlen <= 0)
13573 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013574 mb_ptr_back(goodword, pgood);
13575 mb_ptr_back(su->su_badptr, pbad);
13576#ifdef FEAT_MBYTE
13577 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013578 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013579 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13580 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013581 }
13582 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013583#endif
13584 if (*pgood != *pbad)
13585 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013586 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013587
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013588 if (badlen == 0 && goodlen == 0)
13589 /* goodword doesn't change anything; may happen for "the the" changing
13590 * the first "the" to itself. */
13591 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013592
Bram Moolenaar89d40322006-08-29 15:30:07 +000013593 if (gap->ga_len == 0)
13594 i = -1;
13595 else
13596 {
13597 /* Check if the word is already there. Also check the length that is
13598 * being replaced "thes," -> "these" is a different suggestion from
13599 * "thes" -> "these". */
13600 stp = &SUG(*gap, 0);
13601 for (i = gap->ga_len; --i >= 0; ++stp)
13602 if (stp->st_wordlen == goodlen
13603 && stp->st_orglen == badlen
13604 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013605 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013606 /*
13607 * Found it. Remember the word with the lowest score.
13608 */
13609 if (stp->st_slang == NULL)
13610 stp->st_slang = slang;
13611
13612 new_sug.st_score = score;
13613 new_sug.st_altscore = altscore;
13614 new_sug.st_had_bonus = had_bonus;
13615
13616 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013617 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013618 /* Only one of the two had the soundalike score computed.
13619 * Need to do that for the other one now, otherwise the
13620 * scores can't be compared. This happens because
13621 * suggest_try_change() doesn't compute the soundalike
13622 * word to keep it fast, while some special methods set
13623 * the soundalike score to zero. */
13624 if (had_bonus)
13625 rescore_one(su, stp);
13626 else
13627 {
13628 new_sug.st_word = stp->st_word;
13629 new_sug.st_wordlen = stp->st_wordlen;
13630 new_sug.st_slang = stp->st_slang;
13631 new_sug.st_orglen = badlen;
13632 rescore_one(su, &new_sug);
13633 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635
Bram Moolenaar89d40322006-08-29 15:30:07 +000013636 if (stp->st_score > new_sug.st_score)
13637 {
13638 stp->st_score = new_sug.st_score;
13639 stp->st_altscore = new_sug.st_altscore;
13640 stp->st_had_bonus = new_sug.st_had_bonus;
13641 }
13642 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013643 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645
Bram Moolenaar4770d092006-01-12 23:22:24 +000013646 if (i < 0 && ga_grow(gap, 1) == OK)
13647 {
13648 /* Add a suggestion. */
13649 stp = &SUG(*gap, gap->ga_len);
13650 stp->st_word = vim_strnsave(goodword, goodlen);
13651 if (stp->st_word != NULL)
13652 {
13653 stp->st_wordlen = goodlen;
13654 stp->st_score = score;
13655 stp->st_altscore = altscore;
13656 stp->st_had_bonus = had_bonus;
13657 stp->st_orglen = badlen;
13658 stp->st_slang = slang;
13659 ++gap->ga_len;
13660
13661 /* If we have too many suggestions now, sort the list and keep
13662 * the best suggestions. */
13663 if (gap->ga_len > SUG_MAX_COUNT(su))
13664 {
13665 if (maxsf)
13666 su->su_sfmaxscore = cleanup_suggestions(gap,
13667 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13668 else
13669 {
13670 i = su->su_maxscore;
13671 su->su_maxscore = cleanup_suggestions(gap,
13672 su->su_maxscore, SUG_CLEAN_COUNT(su));
13673 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 }
13675 }
13676 }
13677}
13678
13679/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013680 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13681 * for split words, such as "the the". Remove these from the list here.
13682 */
13683 static void
13684check_suggestions(su, gap)
13685 suginfo_T *su;
13686 garray_T *gap; /* either su_ga or su_sga */
13687{
13688 suggest_T *stp;
13689 int i;
13690 char_u longword[MAXWLEN + 1];
13691 int len;
13692 hlf_T attr;
13693
13694 stp = &SUG(*gap, 0);
13695 for (i = gap->ga_len - 1; i >= 0; --i)
13696 {
13697 /* Need to append what follows to check for "the the". */
13698 STRCPY(longword, stp[i].st_word);
13699 len = stp[i].st_wordlen;
13700 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13701 MAXWLEN - len);
13702 attr = HLF_COUNT;
13703 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13704 if (attr != HLF_COUNT)
13705 {
13706 /* Remove this entry. */
13707 vim_free(stp[i].st_word);
13708 --gap->ga_len;
13709 if (i < gap->ga_len)
13710 mch_memmove(stp + i, stp + i + 1,
13711 sizeof(suggest_T) * (gap->ga_len - i));
13712 }
13713 }
13714}
13715
13716
13717/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013718 * Add a word to be banned.
13719 */
13720 static void
13721add_banned(su, word)
13722 suginfo_T *su;
13723 char_u *word;
13724{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013725 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013726 hash_T hash;
13727 hashitem_T *hi;
13728
Bram Moolenaar4770d092006-01-12 23:22:24 +000013729 hash = hash_hash(word);
13730 hi = hash_lookup(&su->su_banned, word, hash);
13731 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013732 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013733 s = vim_strsave(word);
13734 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013735 hash_add_item(&su->su_banned, hi, s, hash);
13736 }
13737}
13738
13739/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013740 * Recompute the score for all suggestions if sound-folding is possible. This
13741 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013742 */
13743 static void
13744rescore_suggestions(su)
13745 suginfo_T *su;
13746{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013747 int i;
13748
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013749 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013750 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013751 rescore_one(su, &SUG(su->su_ga, i));
13752}
13753
13754/*
13755 * Recompute the score for one suggestion if sound-folding is possible.
13756 */
13757 static void
13758rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013759 suginfo_T *su;
13760 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013761{
13762 slang_T *slang = stp->st_slang;
13763 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013764 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013765
13766 /* Only rescore suggestions that have no sal score yet and do have a
13767 * language. */
13768 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13769 {
13770 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013771 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013772 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013773 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013774 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013775 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013776 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013777
13778 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013779 if (stp->st_altscore == SCORE_MAXMAX)
13780 stp->st_altscore = SCORE_BIG;
13781 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13782 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013783 }
13784}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786static int
13787#ifdef __BORLANDC__
13788_RTLENTRYF
13789#endif
13790sug_compare __ARGS((const void *s1, const void *s2));
13791
13792/*
13793 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013794 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 */
13796 static int
13797#ifdef __BORLANDC__
13798_RTLENTRYF
13799#endif
13800sug_compare(s1, s2)
13801 const void *s1;
13802 const void *s2;
13803{
13804 suggest_T *p1 = (suggest_T *)s1;
13805 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013806 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013807
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013808 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013809 {
13810 n = p1->st_altscore - p2->st_altscore;
13811 if (n == 0)
13812 n = STRICMP(p1->st_word, p2->st_word);
13813 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013814 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815}
13816
13817/*
13818 * Cleanup the suggestions:
13819 * - Sort on score.
13820 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013821 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013823 static int
13824cleanup_suggestions(gap, maxscore, keep)
13825 garray_T *gap;
13826 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013827 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013829 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 int i;
13831
13832 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013833 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013834
13835 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013836 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013838 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013840 gap->ga_len = keep;
13841 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013843 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844}
13845
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013846#if defined(FEAT_EVAL) || defined(PROTO)
13847/*
13848 * Soundfold a string, for soundfold().
13849 * Result is in allocated memory, NULL for an error.
13850 */
13851 char_u *
13852eval_soundfold(word)
13853 char_u *word;
13854{
13855 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013856 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013857 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013858
13859 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13860 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013861 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013862 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013863 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013864 if (lp->lp_slang->sl_sal.ga_len > 0)
13865 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013866 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013867 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013868 return vim_strsave(sound);
13869 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013870 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013871
13872 /* No language with sound folding, return word as-is. */
13873 return vim_strsave(word);
13874}
13875#endif
13876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877/*
13878 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013879 *
13880 * There are many ways to turn a word into a sound-a-like representation. The
13881 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13882 * swedish name matching - survey and test of different algorithms" by Klas
13883 * Erikson.
13884 *
13885 * We support two methods:
13886 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13887 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013888 */
13889 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013890spell_soundfold(slang, inword, folded, res)
13891 slang_T *slang;
13892 char_u *inword;
13893 int folded; /* "inword" is already case-folded */
13894 char_u *res;
13895{
13896 char_u fword[MAXWLEN];
13897 char_u *word;
13898
13899 if (slang->sl_sofo)
13900 /* SOFOFROM and SOFOTO used */
13901 spell_soundfold_sofo(slang, inword, res);
13902 else
13903 {
13904 /* SAL items used. Requires the word to be case-folded. */
13905 if (folded)
13906 word = inword;
13907 else
13908 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013909 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013910 word = fword;
13911 }
13912
13913#ifdef FEAT_MBYTE
13914 if (has_mbyte)
13915 spell_soundfold_wsal(slang, word, res);
13916 else
13917#endif
13918 spell_soundfold_sal(slang, word, res);
13919 }
13920}
13921
13922/*
13923 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13924 * SOFOTO lines.
13925 */
13926 static void
13927spell_soundfold_sofo(slang, inword, res)
13928 slang_T *slang;
13929 char_u *inword;
13930 char_u *res;
13931{
13932 char_u *s;
13933 int ri = 0;
13934 int c;
13935
13936#ifdef FEAT_MBYTE
13937 if (has_mbyte)
13938 {
13939 int prevc = 0;
13940 int *ip;
13941
13942 /* The sl_sal_first[] table contains the translation for chars up to
13943 * 255, sl_sal the rest. */
13944 for (s = inword; *s != NUL; )
13945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013946 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013947 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13948 c = ' ';
13949 else if (c < 256)
13950 c = slang->sl_sal_first[c];
13951 else
13952 {
13953 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13954 if (ip == NULL) /* empty list, can't match */
13955 c = NUL;
13956 else
13957 for (;;) /* find "c" in the list */
13958 {
13959 if (*ip == 0) /* not found */
13960 {
13961 c = NUL;
13962 break;
13963 }
13964 if (*ip == c) /* match! */
13965 {
13966 c = ip[1];
13967 break;
13968 }
13969 ip += 2;
13970 }
13971 }
13972
13973 if (c != NUL && c != prevc)
13974 {
13975 ri += mb_char2bytes(c, res + ri);
13976 if (ri + MB_MAXBYTES > MAXWLEN)
13977 break;
13978 prevc = c;
13979 }
13980 }
13981 }
13982 else
13983#endif
13984 {
13985 /* The sl_sal_first[] table contains the translation. */
13986 for (s = inword; (c = *s) != NUL; ++s)
13987 {
13988 if (vim_iswhite(c))
13989 c = ' ';
13990 else
13991 c = slang->sl_sal_first[c];
13992 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13993 res[ri++] = c;
13994 }
13995 }
13996
13997 res[ri] = NUL;
13998}
13999
14000 static void
14001spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 slang_T *slang;
14003 char_u *inword;
14004 char_u *res;
14005{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014006 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014008 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014010 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014012 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014013 int n, k = 0;
14014 int z0;
14015 int k0;
14016 int n0;
14017 int c;
14018 int pri;
14019 int p0 = -333;
14020 int c0;
14021
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014022 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014023 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 if (slang->sl_rem_accents)
14025 {
14026 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014027 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014028 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014029 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014030 {
14031 *t++ = ' ';
14032 s = skipwhite(s);
14033 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014034 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014036 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 *t++ = *s;
14038 ++s;
14039 }
14040 }
14041 *t = NUL;
14042 }
14043 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014044 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014046 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047
14048 /*
14049 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014050 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014052 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014053 while ((c = word[i]) != NUL)
14054 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014055 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 n = slang->sl_sal_first[c];
14057 z0 = 0;
14058
14059 if (n >= 0)
14060 {
14061 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014062 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014064 /* Quickly skip entries that don't match the word. Most
14065 * entries are less then three chars, optimize for that. */
14066 k = smp[n].sm_leadlen;
14067 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014069 if (word[i + 1] != s[1])
14070 continue;
14071 if (k > 2)
14072 {
14073 for (j = 2; j < k; ++j)
14074 if (word[i + j] != s[j])
14075 break;
14076 if (j < k)
14077 continue;
14078 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 }
14080
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014081 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014083 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014084 while (*pf != NUL && *pf != word[i + k])
14085 ++pf;
14086 if (*pf == NUL)
14087 continue;
14088 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014090 s = smp[n].sm_rules;
14091 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092
14093 p0 = *s;
14094 k0 = k;
14095 while (*s == '-' && k > 1)
14096 {
14097 k--;
14098 s++;
14099 }
14100 if (*s == '<')
14101 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014102 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 {
14104 /* determine priority */
14105 pri = *s - '0';
14106 s++;
14107 }
14108 if (*s == '^' && *(s + 1) == '^')
14109 s++;
14110
14111 if (*s == NUL
14112 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014113 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014114 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014116 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014118 && spell_iswordp(word + i - 1, curbuf)
14119 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 {
14121 /* search for followup rules, if: */
14122 /* followup and k > 1 and NO '-' in searchstring */
14123 c0 = word[i + k - 1];
14124 n0 = slang->sl_sal_first[c0];
14125
14126 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014127 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 {
14129 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014130 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014132 /* Quickly skip entries that don't match the word.
14133 * */
14134 k0 = smp[n0].sm_leadlen;
14135 if (k0 > 1)
14136 {
14137 if (word[i + k] != s[1])
14138 continue;
14139 if (k0 > 2)
14140 {
14141 pf = word + i + k + 1;
14142 for (j = 2; j < k0; ++j)
14143 if (*pf++ != s[j])
14144 break;
14145 if (j < k0)
14146 continue;
14147 }
14148 }
14149 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014151 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014152 {
14153 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014154 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014155 while (*pf != NUL && *pf != word[i + k0])
14156 ++pf;
14157 if (*pf == NUL)
14158 continue;
14159 ++k0;
14160 }
14161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014163 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 while (*s == '-')
14165 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014166 /* "k0" gets NOT reduced because
14167 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 s++;
14169 }
14170 if (*s == '<')
14171 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014172 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 {
14174 p0 = *s - '0';
14175 s++;
14176 }
14177
14178 if (*s == NUL
14179 /* *s == '^' cuts */
14180 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014181 && !spell_iswordp(word + i + k0,
14182 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 {
14184 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187
14188 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 /* rule fits; stop search */
14192 break;
14193 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 }
14195
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014196 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 }
14199
14200 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014201 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014202 if (s == NULL)
14203 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014204 pf = smp[n].sm_rules;
14205 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 if (p0 == 1 && z == 0)
14207 {
14208 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014209 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14210 || res[reslen - 1] == *s))
14211 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 z0 = 1;
14213 z = 1;
14214 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014215 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 {
14217 word[i + k0] = *s;
14218 k0++;
14219 s++;
14220 }
14221 if (k > k0)
14222 mch_memmove(word + i + k0, word + i + k,
14223 STRLEN(word + i + k) + 1);
14224
14225 /* new "actual letter" */
14226 c = word[i];
14227 }
14228 else
14229 {
14230 /* no '<' rule used */
14231 i += k - 1;
14232 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014233 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014235 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014236 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 s++;
14238 }
14239 /* new "actual letter" */
14240 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014241 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 {
14243 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014244 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 mch_memmove(word, word + i + 1,
14246 STRLEN(word + i + 1) + 1);
14247 i = 0;
14248 z0 = 1;
14249 }
14250 }
14251 break;
14252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253 }
14254 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014255 else if (vim_iswhite(c))
14256 {
14257 c = ' ';
14258 k = 1;
14259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260
14261 if (z0 == 0)
14262 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014263 if (k && !p0 && reslen < MAXWLEN && c != NUL
14264 && (!slang->sl_collapse || reslen == 0
14265 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014267 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268
14269 i++;
14270 z = 0;
14271 k = 0;
14272 }
14273 }
14274
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014275 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276}
14277
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014278#ifdef FEAT_MBYTE
14279/*
14280 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14281 * Multi-byte version of spell_soundfold().
14282 */
14283 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014284spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014285 slang_T *slang;
14286 char_u *inword;
14287 char_u *res;
14288{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014289 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014290 int word[MAXWLEN];
14291 int wres[MAXWLEN];
14292 int l;
14293 char_u *s;
14294 int *ws;
14295 char_u *t;
14296 int *pf;
14297 int i, j, z;
14298 int reslen;
14299 int n, k = 0;
14300 int z0;
14301 int k0;
14302 int n0;
14303 int c;
14304 int pri;
14305 int p0 = -333;
14306 int c0;
14307 int did_white = FALSE;
14308
14309 /*
14310 * Convert the multi-byte string to a wide-character string.
14311 * Remove accents, if wanted. We actually remove all non-word characters.
14312 * But keep white space.
14313 */
14314 n = 0;
14315 for (s = inword; *s != NUL; )
14316 {
14317 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014318 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014319 if (slang->sl_rem_accents)
14320 {
14321 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14322 {
14323 if (did_white)
14324 continue;
14325 c = ' ';
14326 did_white = TRUE;
14327 }
14328 else
14329 {
14330 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014331 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014332 continue;
14333 }
14334 }
14335 word[n++] = c;
14336 }
14337 word[n] = NUL;
14338
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014339 /*
14340 * This comes from Aspell phonet.cpp.
14341 * Converted from C++ to C. Added support for multi-byte chars.
14342 * Changed to keep spaces.
14343 */
14344 i = reslen = z = 0;
14345 while ((c = word[i]) != NUL)
14346 {
14347 /* Start with the first rule that has the character in the word. */
14348 n = slang->sl_sal_first[c & 0xff];
14349 z0 = 0;
14350
14351 if (n >= 0)
14352 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014353 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014354 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14355 {
14356 /* Quickly skip entries that don't match the word. Most
14357 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014358 if (c != ws[0])
14359 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014360 k = smp[n].sm_leadlen;
14361 if (k > 1)
14362 {
14363 if (word[i + 1] != ws[1])
14364 continue;
14365 if (k > 2)
14366 {
14367 for (j = 2; j < k; ++j)
14368 if (word[i + j] != ws[j])
14369 break;
14370 if (j < k)
14371 continue;
14372 }
14373 }
14374
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014375 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014376 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014377 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014378 while (*pf != NUL && *pf != word[i + k])
14379 ++pf;
14380 if (*pf == NUL)
14381 continue;
14382 ++k;
14383 }
14384 s = smp[n].sm_rules;
14385 pri = 5; /* default priority */
14386
14387 p0 = *s;
14388 k0 = k;
14389 while (*s == '-' && k > 1)
14390 {
14391 k--;
14392 s++;
14393 }
14394 if (*s == '<')
14395 s++;
14396 if (VIM_ISDIGIT(*s))
14397 {
14398 /* determine priority */
14399 pri = *s - '0';
14400 s++;
14401 }
14402 if (*s == '^' && *(s + 1) == '^')
14403 s++;
14404
14405 if (*s == NUL
14406 || (*s == '^'
14407 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014408 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014409 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014410 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014411 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014412 && spell_iswordp_w(word + i - 1, curbuf)
14413 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014414 {
14415 /* search for followup rules, if: */
14416 /* followup and k > 1 and NO '-' in searchstring */
14417 c0 = word[i + k - 1];
14418 n0 = slang->sl_sal_first[c0 & 0xff];
14419
14420 if (slang->sl_followup && k > 1 && n0 >= 0
14421 && p0 != '-' && word[i + k] != NUL)
14422 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014423 /* Test follow-up rule for "word[i + k]"; loop over
14424 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014425 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14426 == (c0 & 0xff); ++n0)
14427 {
14428 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014429 */
14430 if (c0 != ws[0])
14431 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014432 k0 = smp[n0].sm_leadlen;
14433 if (k0 > 1)
14434 {
14435 if (word[i + k] != ws[1])
14436 continue;
14437 if (k0 > 2)
14438 {
14439 pf = word + i + k + 1;
14440 for (j = 2; j < k0; ++j)
14441 if (*pf++ != ws[j])
14442 break;
14443 if (j < k0)
14444 continue;
14445 }
14446 }
14447 k0 += k - 1;
14448
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014449 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014450 {
14451 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014452 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014453 while (*pf != NUL && *pf != word[i + k0])
14454 ++pf;
14455 if (*pf == NUL)
14456 continue;
14457 ++k0;
14458 }
14459
14460 p0 = 5;
14461 s = smp[n0].sm_rules;
14462 while (*s == '-')
14463 {
14464 /* "k0" gets NOT reduced because
14465 * "if (k0 == k)" */
14466 s++;
14467 }
14468 if (*s == '<')
14469 s++;
14470 if (VIM_ISDIGIT(*s))
14471 {
14472 p0 = *s - '0';
14473 s++;
14474 }
14475
14476 if (*s == NUL
14477 /* *s == '^' cuts */
14478 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014479 && !spell_iswordp_w(word + i + k0,
14480 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014481 {
14482 if (k0 == k)
14483 /* this is just a piece of the string */
14484 continue;
14485
14486 if (p0 < pri)
14487 /* priority too low */
14488 continue;
14489 /* rule fits; stop search */
14490 break;
14491 }
14492 }
14493
14494 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14495 == (c0 & 0xff))
14496 continue;
14497 }
14498
14499 /* replace string */
14500 ws = smp[n].sm_to_w;
14501 s = smp[n].sm_rules;
14502 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14503 if (p0 == 1 && z == 0)
14504 {
14505 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014506 if (reslen > 0 && ws != NULL && *ws != NUL
14507 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014508 || wres[reslen - 1] == *ws))
14509 reslen--;
14510 z0 = 1;
14511 z = 1;
14512 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014513 if (ws != NULL)
14514 while (*ws != NUL && word[i + k0] != NUL)
14515 {
14516 word[i + k0] = *ws;
14517 k0++;
14518 ws++;
14519 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014520 if (k > k0)
14521 mch_memmove(word + i + k0, word + i + k,
14522 sizeof(int) * (STRLEN(word + i + k) + 1));
14523
14524 /* new "actual letter" */
14525 c = word[i];
14526 }
14527 else
14528 {
14529 /* no '<' rule used */
14530 i += k - 1;
14531 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014532 if (ws != NULL)
14533 while (*ws != NUL && ws[1] != NUL
14534 && reslen < MAXWLEN)
14535 {
14536 if (reslen == 0 || wres[reslen - 1] != *ws)
14537 wres[reslen++] = *ws;
14538 ws++;
14539 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014540 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014541 if (ws == NULL)
14542 c = NUL;
14543 else
14544 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014545 if (strstr((char *)s, "^^") != NULL)
14546 {
14547 if (c != NUL)
14548 wres[reslen++] = c;
14549 mch_memmove(word, word + i + 1,
14550 sizeof(int) * (STRLEN(word + i + 1) + 1));
14551 i = 0;
14552 z0 = 1;
14553 }
14554 }
14555 break;
14556 }
14557 }
14558 }
14559 else if (vim_iswhite(c))
14560 {
14561 c = ' ';
14562 k = 1;
14563 }
14564
14565 if (z0 == 0)
14566 {
14567 if (k && !p0 && reslen < MAXWLEN && c != NUL
14568 && (!slang->sl_collapse || reslen == 0
14569 || wres[reslen - 1] != c))
14570 /* condense only double letters */
14571 wres[reslen++] = c;
14572
14573 i++;
14574 z = 0;
14575 k = 0;
14576 }
14577 }
14578
14579 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14580 l = 0;
14581 for (n = 0; n < reslen; ++n)
14582 {
14583 l += mb_char2bytes(wres[n], res + l);
14584 if (l + MB_MAXBYTES > MAXWLEN)
14585 break;
14586 }
14587 res[l] = NUL;
14588}
14589#endif
14590
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014591/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014592 * Compute a score for two sound-a-like words.
14593 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14594 * Instead of a generic loop we write out the code. That keeps it fast by
14595 * avoiding checks that will not be possible.
14596 */
14597 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014598soundalike_score(goodstart, badstart)
14599 char_u *goodstart; /* sound-folded good word */
14600 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014601{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014602 char_u *goodsound = goodstart;
14603 char_u *badsound = badstart;
14604 int goodlen;
14605 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014606 int n;
14607 char_u *pl, *ps;
14608 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014609 int score = 0;
14610
14611 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14612 * counted so much, vowels halfway the word aren't counted at all. */
14613 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14614 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014615 if (badsound[1] == goodsound[1]
14616 || (badsound[1] != NUL
14617 && goodsound[1] != NUL
14618 && badsound[2] == goodsound[2]))
14619 {
14620 /* handle like a substitute */
14621 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014622 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014623 {
14624 score = 2 * SCORE_DEL / 3;
14625 if (*badsound == '*')
14626 ++badsound;
14627 else
14628 ++goodsound;
14629 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014630 }
14631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014632 goodlen = (int)STRLEN(goodsound);
14633 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014634
14635 /* Return quickly if the lenghts are too different to be fixed by two
14636 * changes. */
14637 n = goodlen - badlen;
14638 if (n < -2 || n > 2)
14639 return SCORE_MAXMAX;
14640
14641 if (n > 0)
14642 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014643 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014644 ps = badsound;
14645 }
14646 else
14647 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014648 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014649 ps = goodsound;
14650 }
14651
14652 /* Skip over the identical part. */
14653 while (*pl == *ps && *pl != NUL)
14654 {
14655 ++pl;
14656 ++ps;
14657 }
14658
14659 switch (n)
14660 {
14661 case -2:
14662 case 2:
14663 /*
14664 * Must delete two characters from "pl".
14665 */
14666 ++pl; /* first delete */
14667 while (*pl == *ps)
14668 {
14669 ++pl;
14670 ++ps;
14671 }
14672 /* strings must be equal after second delete */
14673 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014674 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014675
14676 /* Failed to compare. */
14677 break;
14678
14679 case -1:
14680 case 1:
14681 /*
14682 * Minimal one delete from "pl" required.
14683 */
14684
14685 /* 1: delete */
14686 pl2 = pl + 1;
14687 ps2 = ps;
14688 while (*pl2 == *ps2)
14689 {
14690 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014691 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014692 ++pl2;
14693 ++ps2;
14694 }
14695
14696 /* 2: delete then swap, then rest must be equal */
14697 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14698 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014699 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014700
14701 /* 3: delete then substitute, then the rest must be equal */
14702 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014703 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014704
14705 /* 4: first swap then delete */
14706 if (pl[0] == ps[1] && pl[1] == ps[0])
14707 {
14708 pl2 = pl + 2; /* swap, skip two chars */
14709 ps2 = ps + 2;
14710 while (*pl2 == *ps2)
14711 {
14712 ++pl2;
14713 ++ps2;
14714 }
14715 /* delete a char and then strings must be equal */
14716 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014717 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014718 }
14719
14720 /* 5: first substitute then delete */
14721 pl2 = pl + 1; /* substitute, skip one char */
14722 ps2 = ps + 1;
14723 while (*pl2 == *ps2)
14724 {
14725 ++pl2;
14726 ++ps2;
14727 }
14728 /* delete a char and then strings must be equal */
14729 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014730 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014731
14732 /* Failed to compare. */
14733 break;
14734
14735 case 0:
14736 /*
14737 * Lenghts are equal, thus changes must result in same length: An
14738 * insert is only possible in combination with a delete.
14739 * 1: check if for identical strings
14740 */
14741 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014742 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014743
14744 /* 2: swap */
14745 if (pl[0] == ps[1] && pl[1] == ps[0])
14746 {
14747 pl2 = pl + 2; /* swap, skip two chars */
14748 ps2 = ps + 2;
14749 while (*pl2 == *ps2)
14750 {
14751 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014752 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014753 ++pl2;
14754 ++ps2;
14755 }
14756 /* 3: swap and swap again */
14757 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14758 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014759 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014760
14761 /* 4: swap and substitute */
14762 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014763 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014764 }
14765
14766 /* 5: substitute */
14767 pl2 = pl + 1;
14768 ps2 = ps + 1;
14769 while (*pl2 == *ps2)
14770 {
14771 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014772 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014773 ++pl2;
14774 ++ps2;
14775 }
14776
14777 /* 6: substitute and swap */
14778 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14779 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014780 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781
14782 /* 7: substitute and substitute */
14783 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014784 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014785
14786 /* 8: insert then delete */
14787 pl2 = pl;
14788 ps2 = ps + 1;
14789 while (*pl2 == *ps2)
14790 {
14791 ++pl2;
14792 ++ps2;
14793 }
14794 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014795 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014796
14797 /* 9: delete then insert */
14798 pl2 = pl + 1;
14799 ps2 = ps;
14800 while (*pl2 == *ps2)
14801 {
14802 ++pl2;
14803 ++ps2;
14804 }
14805 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014806 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014807
14808 /* Failed to compare. */
14809 break;
14810 }
14811
14812 return SCORE_MAXMAX;
14813}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014815/*
14816 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014817 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014818 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014819 * The algorithm is described by Du and Chang, 1992.
14820 * The implementation of the algorithm comes from Aspell editdist.cpp,
14821 * edit_distance(). It has been converted from C++ to C and modified to
14822 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 */
14824 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014825spell_edit_score(slang, badword, goodword)
14826 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014827 char_u *badword;
14828 char_u *goodword;
14829{
14830 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014831 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014832 int j, i;
14833 int t;
14834 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014835 int pbc, pgc;
14836#ifdef FEAT_MBYTE
14837 char_u *p;
14838 int wbadword[MAXWLEN];
14839 int wgoodword[MAXWLEN];
14840
14841 if (has_mbyte)
14842 {
14843 /* Get the characters from the multi-byte strings and put them in an
14844 * int array for easy access. */
14845 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014846 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014847 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014848 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014849 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014850 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014851 }
14852 else
14853#endif
14854 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014855 badlen = (int)STRLEN(badword) + 1;
14856 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014857 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858
14859 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14860#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014861 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14862 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014863 if (cnt == NULL)
14864 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865
14866 CNT(0, 0) = 0;
14867 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014868 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014869
14870 for (i = 1; i <= badlen; ++i)
14871 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014872 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014873 for (j = 1; j <= goodlen; ++j)
14874 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014875#ifdef FEAT_MBYTE
14876 if (has_mbyte)
14877 {
14878 bc = wbadword[i - 1];
14879 gc = wgoodword[j - 1];
14880 }
14881 else
14882#endif
14883 {
14884 bc = badword[i - 1];
14885 gc = goodword[j - 1];
14886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 if (bc == gc)
14888 CNT(i, j) = CNT(i - 1, j - 1);
14889 else
14890 {
14891 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014892 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014893 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14894 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014895 {
14896 /* For a similar character use SCORE_SIMILAR. */
14897 if (slang != NULL
14898 && slang->sl_has_map
14899 && similar_chars(slang, gc, bc))
14900 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14901 else
14902 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014905 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014907#ifdef FEAT_MBYTE
14908 if (has_mbyte)
14909 {
14910 pbc = wbadword[i - 2];
14911 pgc = wgoodword[j - 2];
14912 }
14913 else
14914#endif
14915 {
14916 pbc = badword[i - 2];
14917 pgc = goodword[j - 2];
14918 }
14919 if (bc == pgc && pbc == gc)
14920 {
14921 t = SCORE_SWAP + CNT(i - 2, j - 2);
14922 if (t < CNT(i, j))
14923 CNT(i, j) = t;
14924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 }
14926 t = SCORE_DEL + CNT(i - 1, j);
14927 if (t < CNT(i, j))
14928 CNT(i, j) = t;
14929 t = SCORE_INS + CNT(i, j - 1);
14930 if (t < CNT(i, j))
14931 CNT(i, j) = t;
14932 }
14933 }
14934 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014935
14936 i = CNT(badlen - 1, goodlen - 1);
14937 vim_free(cnt);
14938 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014939}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014940
Bram Moolenaar4770d092006-01-12 23:22:24 +000014941typedef struct
14942{
14943 int badi;
14944 int goodi;
14945 int score;
14946} limitscore_T;
14947
14948/*
14949 * Like spell_edit_score(), but with a limit on the score to make it faster.
14950 * May return SCORE_MAXMAX when the score is higher than "limit".
14951 *
14952 * This uses a stack for the edits still to be tried.
14953 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14954 * for multi-byte characters.
14955 */
14956 static int
14957spell_edit_score_limit(slang, badword, goodword, limit)
14958 slang_T *slang;
14959 char_u *badword;
14960 char_u *goodword;
14961 int limit;
14962{
14963 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14964 int stackidx;
14965 int bi, gi;
14966 int bi2, gi2;
14967 int bc, gc;
14968 int score;
14969 int score_off;
14970 int minscore;
14971 int round;
14972
14973#ifdef FEAT_MBYTE
14974 /* Multi-byte characters require a bit more work, use a different function
14975 * to avoid testing "has_mbyte" quite often. */
14976 if (has_mbyte)
14977 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14978#endif
14979
14980 /*
14981 * The idea is to go from start to end over the words. So long as
14982 * characters are equal just continue, this always gives the lowest score.
14983 * When there is a difference try several alternatives. Each alternative
14984 * increases "score" for the edit distance. Some of the alternatives are
14985 * pushed unto a stack and tried later, some are tried right away. At the
14986 * end of the word the score for one alternative is known. The lowest
14987 * possible score is stored in "minscore".
14988 */
14989 stackidx = 0;
14990 bi = 0;
14991 gi = 0;
14992 score = 0;
14993 minscore = limit + 1;
14994
14995 for (;;)
14996 {
14997 /* Skip over an equal part, score remains the same. */
14998 for (;;)
14999 {
15000 bc = badword[bi];
15001 gc = goodword[gi];
15002 if (bc != gc) /* stop at a char that's different */
15003 break;
15004 if (bc == NUL) /* both words end */
15005 {
15006 if (score < minscore)
15007 minscore = score;
15008 goto pop; /* do next alternative */
15009 }
15010 ++bi;
15011 ++gi;
15012 }
15013
15014 if (gc == NUL) /* goodword ends, delete badword chars */
15015 {
15016 do
15017 {
15018 if ((score += SCORE_DEL) >= minscore)
15019 goto pop; /* do next alternative */
15020 } while (badword[++bi] != NUL);
15021 minscore = score;
15022 }
15023 else if (bc == NUL) /* badword ends, insert badword chars */
15024 {
15025 do
15026 {
15027 if ((score += SCORE_INS) >= minscore)
15028 goto pop; /* do next alternative */
15029 } while (goodword[++gi] != NUL);
15030 minscore = score;
15031 }
15032 else /* both words continue */
15033 {
15034 /* If not close to the limit, perform a change. Only try changes
15035 * that may lead to a lower score than "minscore".
15036 * round 0: try deleting a char from badword
15037 * round 1: try inserting a char in badword */
15038 for (round = 0; round <= 1; ++round)
15039 {
15040 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15041 if (score_off < minscore)
15042 {
15043 if (score_off + SCORE_EDIT_MIN >= minscore)
15044 {
15045 /* Near the limit, rest of the words must match. We
15046 * can check that right now, no need to push an item
15047 * onto the stack. */
15048 bi2 = bi + 1 - round;
15049 gi2 = gi + round;
15050 while (goodword[gi2] == badword[bi2])
15051 {
15052 if (goodword[gi2] == NUL)
15053 {
15054 minscore = score_off;
15055 break;
15056 }
15057 ++bi2;
15058 ++gi2;
15059 }
15060 }
15061 else
15062 {
15063 /* try deleting/inserting a character later */
15064 stack[stackidx].badi = bi + 1 - round;
15065 stack[stackidx].goodi = gi + round;
15066 stack[stackidx].score = score_off;
15067 ++stackidx;
15068 }
15069 }
15070 }
15071
15072 if (score + SCORE_SWAP < minscore)
15073 {
15074 /* If swapping two characters makes a match then the
15075 * substitution is more expensive, thus there is no need to
15076 * try both. */
15077 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15078 {
15079 /* Swap two characters, that is: skip them. */
15080 gi += 2;
15081 bi += 2;
15082 score += SCORE_SWAP;
15083 continue;
15084 }
15085 }
15086
15087 /* Substitute one character for another which is the same
15088 * thing as deleting a character from both goodword and badword.
15089 * Use a better score when there is only a case difference. */
15090 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15091 score += SCORE_ICASE;
15092 else
15093 {
15094 /* For a similar character use SCORE_SIMILAR. */
15095 if (slang != NULL
15096 && slang->sl_has_map
15097 && similar_chars(slang, gc, bc))
15098 score += SCORE_SIMILAR;
15099 else
15100 score += SCORE_SUBST;
15101 }
15102
15103 if (score < minscore)
15104 {
15105 /* Do the substitution. */
15106 ++gi;
15107 ++bi;
15108 continue;
15109 }
15110 }
15111pop:
15112 /*
15113 * Get here to try the next alternative, pop it from the stack.
15114 */
15115 if (stackidx == 0) /* stack is empty, finished */
15116 break;
15117
15118 /* pop an item from the stack */
15119 --stackidx;
15120 gi = stack[stackidx].goodi;
15121 bi = stack[stackidx].badi;
15122 score = stack[stackidx].score;
15123 }
15124
15125 /* When the score goes over "limit" it may actually be much higher.
15126 * Return a very large number to avoid going below the limit when giving a
15127 * bonus. */
15128 if (minscore > limit)
15129 return SCORE_MAXMAX;
15130 return minscore;
15131}
15132
15133#ifdef FEAT_MBYTE
15134/*
15135 * Multi-byte version of spell_edit_score_limit().
15136 * Keep it in sync with the above!
15137 */
15138 static int
15139spell_edit_score_limit_w(slang, badword, goodword, limit)
15140 slang_T *slang;
15141 char_u *badword;
15142 char_u *goodword;
15143 int limit;
15144{
15145 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15146 int stackidx;
15147 int bi, gi;
15148 int bi2, gi2;
15149 int bc, gc;
15150 int score;
15151 int score_off;
15152 int minscore;
15153 int round;
15154 char_u *p;
15155 int wbadword[MAXWLEN];
15156 int wgoodword[MAXWLEN];
15157
15158 /* Get the characters from the multi-byte strings and put them in an
15159 * int array for easy access. */
15160 bi = 0;
15161 for (p = badword; *p != NUL; )
15162 wbadword[bi++] = mb_cptr2char_adv(&p);
15163 wbadword[bi++] = 0;
15164 gi = 0;
15165 for (p = goodword; *p != NUL; )
15166 wgoodword[gi++] = mb_cptr2char_adv(&p);
15167 wgoodword[gi++] = 0;
15168
15169 /*
15170 * The idea is to go from start to end over the words. So long as
15171 * characters are equal just continue, this always gives the lowest score.
15172 * When there is a difference try several alternatives. Each alternative
15173 * increases "score" for the edit distance. Some of the alternatives are
15174 * pushed unto a stack and tried later, some are tried right away. At the
15175 * end of the word the score for one alternative is known. The lowest
15176 * possible score is stored in "minscore".
15177 */
15178 stackidx = 0;
15179 bi = 0;
15180 gi = 0;
15181 score = 0;
15182 minscore = limit + 1;
15183
15184 for (;;)
15185 {
15186 /* Skip over an equal part, score remains the same. */
15187 for (;;)
15188 {
15189 bc = wbadword[bi];
15190 gc = wgoodword[gi];
15191
15192 if (bc != gc) /* stop at a char that's different */
15193 break;
15194 if (bc == NUL) /* both words end */
15195 {
15196 if (score < minscore)
15197 minscore = score;
15198 goto pop; /* do next alternative */
15199 }
15200 ++bi;
15201 ++gi;
15202 }
15203
15204 if (gc == NUL) /* goodword ends, delete badword chars */
15205 {
15206 do
15207 {
15208 if ((score += SCORE_DEL) >= minscore)
15209 goto pop; /* do next alternative */
15210 } while (wbadword[++bi] != NUL);
15211 minscore = score;
15212 }
15213 else if (bc == NUL) /* badword ends, insert badword chars */
15214 {
15215 do
15216 {
15217 if ((score += SCORE_INS) >= minscore)
15218 goto pop; /* do next alternative */
15219 } while (wgoodword[++gi] != NUL);
15220 minscore = score;
15221 }
15222 else /* both words continue */
15223 {
15224 /* If not close to the limit, perform a change. Only try changes
15225 * that may lead to a lower score than "minscore".
15226 * round 0: try deleting a char from badword
15227 * round 1: try inserting a char in badword */
15228 for (round = 0; round <= 1; ++round)
15229 {
15230 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15231 if (score_off < minscore)
15232 {
15233 if (score_off + SCORE_EDIT_MIN >= minscore)
15234 {
15235 /* Near the limit, rest of the words must match. We
15236 * can check that right now, no need to push an item
15237 * onto the stack. */
15238 bi2 = bi + 1 - round;
15239 gi2 = gi + round;
15240 while (wgoodword[gi2] == wbadword[bi2])
15241 {
15242 if (wgoodword[gi2] == NUL)
15243 {
15244 minscore = score_off;
15245 break;
15246 }
15247 ++bi2;
15248 ++gi2;
15249 }
15250 }
15251 else
15252 {
15253 /* try deleting a character from badword later */
15254 stack[stackidx].badi = bi + 1 - round;
15255 stack[stackidx].goodi = gi + round;
15256 stack[stackidx].score = score_off;
15257 ++stackidx;
15258 }
15259 }
15260 }
15261
15262 if (score + SCORE_SWAP < minscore)
15263 {
15264 /* If swapping two characters makes a match then the
15265 * substitution is more expensive, thus there is no need to
15266 * try both. */
15267 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15268 {
15269 /* Swap two characters, that is: skip them. */
15270 gi += 2;
15271 bi += 2;
15272 score += SCORE_SWAP;
15273 continue;
15274 }
15275 }
15276
15277 /* Substitute one character for another which is the same
15278 * thing as deleting a character from both goodword and badword.
15279 * Use a better score when there is only a case difference. */
15280 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15281 score += SCORE_ICASE;
15282 else
15283 {
15284 /* For a similar character use SCORE_SIMILAR. */
15285 if (slang != NULL
15286 && slang->sl_has_map
15287 && similar_chars(slang, gc, bc))
15288 score += SCORE_SIMILAR;
15289 else
15290 score += SCORE_SUBST;
15291 }
15292
15293 if (score < minscore)
15294 {
15295 /* Do the substitution. */
15296 ++gi;
15297 ++bi;
15298 continue;
15299 }
15300 }
15301pop:
15302 /*
15303 * Get here to try the next alternative, pop it from the stack.
15304 */
15305 if (stackidx == 0) /* stack is empty, finished */
15306 break;
15307
15308 /* pop an item from the stack */
15309 --stackidx;
15310 gi = stack[stackidx].goodi;
15311 bi = stack[stackidx].badi;
15312 score = stack[stackidx].score;
15313 }
15314
15315 /* When the score goes over "limit" it may actually be much higher.
15316 * Return a very large number to avoid going below the limit when giving a
15317 * bonus. */
15318 if (minscore > limit)
15319 return SCORE_MAXMAX;
15320 return minscore;
15321}
15322#endif
15323
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015324/*
15325 * ":spellinfo"
15326 */
15327/*ARGSUSED*/
15328 void
15329ex_spellinfo(eap)
15330 exarg_T *eap;
15331{
15332 int lpi;
15333 langp_T *lp;
15334 char_u *p;
15335
15336 if (no_spell_checking(curwin))
15337 return;
15338
15339 msg_start();
15340 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15341 {
15342 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15343 msg_puts((char_u *)"file: ");
15344 msg_puts(lp->lp_slang->sl_fname);
15345 msg_putchar('\n');
15346 p = lp->lp_slang->sl_info;
15347 if (p != NULL)
15348 {
15349 msg_puts(p);
15350 msg_putchar('\n');
15351 }
15352 }
15353 msg_end();
15354}
15355
Bram Moolenaar4770d092006-01-12 23:22:24 +000015356#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15357#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015358#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015359#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15360#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015361
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015362/*
15363 * ":spelldump"
15364 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015365 void
15366ex_spelldump(eap)
15367 exarg_T *eap;
15368{
15369 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015370
15371 if (no_spell_checking(curwin))
15372 return;
15373
15374 /* Create a new empty buffer by splitting the window. */
15375 do_cmdline_cmd((char_u *)"new");
15376 if (!bufempty() || !buf_valid(buf))
15377 return;
15378
15379 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15380
15381 /* Delete the empty line that we started with. */
15382 if (curbuf->b_ml.ml_line_count > 1)
15383 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15384
15385 redraw_later(NOT_VALID);
15386}
15387
15388/*
15389 * Go through all possible words and:
15390 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15391 * "ic" and "dir" are not used.
15392 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15393 */
15394 void
15395spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15396 buf_T *buf; /* buffer with spell checking */
15397 char_u *pat; /* leading part of the word */
15398 int ic; /* ignore case */
15399 int *dir; /* direction for adding matches */
15400 int dumpflags_arg; /* DUMPFLAG_* */
15401{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015402 langp_T *lp;
15403 slang_T *slang;
15404 idx_T arridx[MAXWLEN];
15405 int curi[MAXWLEN];
15406 char_u word[MAXWLEN];
15407 int c;
15408 char_u *byts;
15409 idx_T *idxs;
15410 linenr_T lnum = 0;
15411 int round;
15412 int depth;
15413 int n;
15414 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015415 char_u *region_names = NULL; /* region names being used */
15416 int do_region = TRUE; /* dump region names and numbers */
15417 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015418 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015419 int dumpflags = dumpflags_arg;
15420 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015421
Bram Moolenaard0131a82006-03-04 21:46:13 +000015422 /* When ignoring case or when the pattern starts with capital pass this on
15423 * to dump_word(). */
15424 if (pat != NULL)
15425 {
15426 if (ic)
15427 dumpflags |= DUMPFLAG_ICASE;
15428 else
15429 {
15430 n = captype(pat, NULL);
15431 if (n == WF_ONECAP)
15432 dumpflags |= DUMPFLAG_ONECAP;
15433 else if (n == WF_ALLCAP
15434#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015435 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015436#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015437 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015438#endif
15439 )
15440 dumpflags |= DUMPFLAG_ALLCAP;
15441 }
15442 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015443
Bram Moolenaar7887d882005-07-01 22:33:52 +000015444 /* Find out if we can support regions: All languages must support the same
15445 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015446 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015447 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015448 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015449 p = lp->lp_slang->sl_regions;
15450 if (p[0] != 0)
15451 {
15452 if (region_names == NULL) /* first language with regions */
15453 region_names = p;
15454 else if (STRCMP(region_names, p) != 0)
15455 {
15456 do_region = FALSE; /* region names are different */
15457 break;
15458 }
15459 }
15460 }
15461
15462 if (do_region && region_names != NULL)
15463 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015464 if (pat == NULL)
15465 {
15466 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15467 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15468 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015469 }
15470 else
15471 do_region = FALSE;
15472
15473 /*
15474 * Loop over all files loaded for the entries in 'spelllang'.
15475 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015476 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015477 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015478 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015479 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015480 if (slang->sl_fbyts == NULL) /* reloading failed */
15481 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015482
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015483 if (pat == NULL)
15484 {
15485 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15486 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15487 }
15488
15489 /* When matching with a pattern and there are no prefixes only use
15490 * parts of the tree that match "pat". */
15491 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015492 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015493 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015494 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015495
15496 /* round 1: case-folded tree
15497 * round 2: keep-case tree */
15498 for (round = 1; round <= 2; ++round)
15499 {
15500 if (round == 1)
15501 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015502 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015503 byts = slang->sl_fbyts;
15504 idxs = slang->sl_fidxs;
15505 }
15506 else
15507 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015508 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015509 byts = slang->sl_kbyts;
15510 idxs = slang->sl_kidxs;
15511 }
15512 if (byts == NULL)
15513 continue; /* array is empty */
15514
15515 depth = 0;
15516 arridx[0] = 0;
15517 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015518 while (depth >= 0 && !got_int
15519 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015520 {
15521 if (curi[depth] > byts[arridx[depth]])
15522 {
15523 /* Done all bytes at this node, go up one level. */
15524 --depth;
15525 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015526 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015527 }
15528 else
15529 {
15530 /* Do one more byte at this node. */
15531 n = arridx[depth] + curi[depth];
15532 ++curi[depth];
15533 c = byts[n];
15534 if (c == 0)
15535 {
15536 /* End of word, deal with the word.
15537 * Don't use keep-case words in the fold-case tree,
15538 * they will appear in the keep-case tree.
15539 * Only use the word when the region matches. */
15540 flags = (int)idxs[n];
15541 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015542 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015543 && (do_region
15544 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015545 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015546 & lp->lp_region) != 0))
15547 {
15548 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015549 if (!do_region)
15550 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015551
15552 /* Dump the basic word if there is no prefix or
15553 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015554 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015555 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015556 {
15557 dump_word(slang, word, pat, dir,
15558 dumpflags, flags, lnum);
15559 if (pat == NULL)
15560 ++lnum;
15561 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015562
15563 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015564 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015565 lnum = dump_prefixes(slang, word, pat, dir,
15566 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015567 }
15568 }
15569 else
15570 {
15571 /* Normal char, go one level deeper. */
15572 word[depth++] = c;
15573 arridx[depth] = idxs[n];
15574 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015575
15576 /* Check if this characters matches with the pattern.
15577 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015578 * Always ignore case here, dump_word() will check
15579 * proper case later. This isn't exactly right when
15580 * length changes for multi-byte characters with
15581 * ignore case... */
15582 if (depth <= patlen
15583 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015584 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015585 }
15586 }
15587 }
15588 }
15589 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015590}
15591
15592/*
15593 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015594 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015595 */
15596 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015597dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015598 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015599 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015600 char_u *pat;
15601 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015602 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015603 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015604 linenr_T lnum;
15605{
15606 int keepcap = FALSE;
15607 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015608 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015609 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015610 char_u badword[MAXWLEN + 10];
15611 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015612 int flags = wordflags;
15613
15614 if (dumpflags & DUMPFLAG_ONECAP)
15615 flags |= WF_ONECAP;
15616 if (dumpflags & DUMPFLAG_ALLCAP)
15617 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015618
Bram Moolenaar4770d092006-01-12 23:22:24 +000015619 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015620 {
15621 /* Need to fix case according to "flags". */
15622 make_case_word(word, cword, flags);
15623 p = cword;
15624 }
15625 else
15626 {
15627 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015628 if ((dumpflags & DUMPFLAG_KEEPCASE)
15629 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015630 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015631 keepcap = TRUE;
15632 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015633 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015634
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015635 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015636 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015637 /* Add flags and regions after a slash. */
15638 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015639 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015640 STRCPY(badword, p);
15641 STRCAT(badword, "/");
15642 if (keepcap)
15643 STRCAT(badword, "=");
15644 if (flags & WF_BANNED)
15645 STRCAT(badword, "!");
15646 else if (flags & WF_RARE)
15647 STRCAT(badword, "?");
15648 if (flags & WF_REGION)
15649 for (i = 0; i < 7; ++i)
15650 if (flags & (0x10000 << i))
15651 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15652 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015653 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015654
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015655 if (dumpflags & DUMPFLAG_COUNT)
15656 {
15657 hashitem_T *hi;
15658
15659 /* Include the word count for ":spelldump!". */
15660 hi = hash_find(&slang->sl_wordcount, tw);
15661 if (!HASHITEM_EMPTY(hi))
15662 {
15663 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15664 tw, HI2WC(hi)->wc_count);
15665 p = IObuff;
15666 }
15667 }
15668
15669 ml_append(lnum, p, (colnr_T)0, FALSE);
15670 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015671 else if (((dumpflags & DUMPFLAG_ICASE)
15672 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15673 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015674 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015675 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015676 /* if dir was BACKWARD then honor it just once */
15677 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015678}
15679
15680/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015681 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15682 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015683 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015684 * Return the updated line number.
15685 */
15686 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015687dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015688 slang_T *slang;
15689 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015690 char_u *pat;
15691 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015692 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015693 int flags; /* flags with prefix ID */
15694 linenr_T startlnum;
15695{
15696 idx_T arridx[MAXWLEN];
15697 int curi[MAXWLEN];
15698 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015699 char_u word_up[MAXWLEN];
15700 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015701 int c;
15702 char_u *byts;
15703 idx_T *idxs;
15704 linenr_T lnum = startlnum;
15705 int depth;
15706 int n;
15707 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015708 int i;
15709
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015710 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015711 * upper-case letter in word_up[]. */
15712 c = PTR2CHAR(word);
15713 if (SPELL_TOUPPER(c) != c)
15714 {
15715 onecap_copy(word, word_up, TRUE);
15716 has_word_up = TRUE;
15717 }
15718
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015719 byts = slang->sl_pbyts;
15720 idxs = slang->sl_pidxs;
15721 if (byts != NULL) /* array not is empty */
15722 {
15723 /*
15724 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015725 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015726 */
15727 depth = 0;
15728 arridx[0] = 0;
15729 curi[0] = 1;
15730 while (depth >= 0 && !got_int)
15731 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015732 n = arridx[depth];
15733 len = byts[n];
15734 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015735 {
15736 /* Done all bytes at this node, go up one level. */
15737 --depth;
15738 line_breakcheck();
15739 }
15740 else
15741 {
15742 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015743 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015744 ++curi[depth];
15745 c = byts[n];
15746 if (c == 0)
15747 {
15748 /* End of prefix, find out how many IDs there are. */
15749 for (i = 1; i < len; ++i)
15750 if (byts[n + i] != 0)
15751 break;
15752 curi[depth] += i - 1;
15753
Bram Moolenaar53805d12005-08-01 07:08:33 +000015754 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15755 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015756 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015757 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015758 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015759 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015760 : flags, lnum);
15761 if (lnum != 0)
15762 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015763 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015764
15765 /* Check for prefix that matches the word when the
15766 * first letter is upper-case, but only if the prefix has
15767 * a condition. */
15768 if (has_word_up)
15769 {
15770 c = valid_word_prefix(i, n, flags, word_up, slang,
15771 TRUE);
15772 if (c != 0)
15773 {
15774 vim_strncpy(prefix + depth, word_up,
15775 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015776 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015777 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015778 : flags, lnum);
15779 if (lnum != 0)
15780 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015781 }
15782 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015783 }
15784 else
15785 {
15786 /* Normal char, go one level deeper. */
15787 prefix[depth++] = c;
15788 arridx[depth] = idxs[n];
15789 curi[depth] = 1;
15790 }
15791 }
15792 }
15793 }
15794
15795 return lnum;
15796}
15797
Bram Moolenaar95529562005-08-25 21:21:38 +000015798/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015799 * Move "p" to the end of word "start".
15800 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015801 */
15802 char_u *
15803spell_to_word_end(start, buf)
15804 char_u *start;
15805 buf_T *buf;
15806{
15807 char_u *p = start;
15808
15809 while (*p != NUL && spell_iswordp(p, buf))
15810 mb_ptr_adv(p);
15811 return p;
15812}
15813
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015814#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015815/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015816 * For Insert mode completion CTRL-X s:
15817 * Find start of the word in front of column "startcol".
15818 * We don't check if it is badly spelled, with completion we can only change
15819 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015820 * Returns the column number of the word.
15821 */
15822 int
15823spell_word_start(startcol)
15824 int startcol;
15825{
15826 char_u *line;
15827 char_u *p;
15828 int col = 0;
15829
Bram Moolenaar95529562005-08-25 21:21:38 +000015830 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015831 return startcol;
15832
15833 /* Find a word character before "startcol". */
15834 line = ml_get_curline();
15835 for (p = line + startcol; p > line; )
15836 {
15837 mb_ptr_back(line, p);
15838 if (spell_iswordp_nmw(p))
15839 break;
15840 }
15841
15842 /* Go back to start of the word. */
15843 while (p > line)
15844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015845 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015846 mb_ptr_back(line, p);
15847 if (!spell_iswordp(p, curbuf))
15848 break;
15849 col = 0;
15850 }
15851
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015852 return col;
15853}
15854
15855/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015856 * Need to check for 'spellcapcheck' now, the word is removed before
15857 * expand_spelling() is called. Therefore the ugly global variable.
15858 */
15859static int spell_expand_need_cap;
15860
15861 void
15862spell_expand_check_cap(col)
15863 colnr_T col;
15864{
15865 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15866}
15867
15868/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015869 * Get list of spelling suggestions.
15870 * Used for Insert mode completion CTRL-X ?.
15871 * Returns the number of matches. The matches are in "matchp[]", array of
15872 * allocated strings.
15873 */
15874/*ARGSUSED*/
15875 int
15876expand_spelling(lnum, col, pat, matchp)
15877 linenr_T lnum;
15878 int col;
15879 char_u *pat;
15880 char_u ***matchp;
15881{
15882 garray_T ga;
15883
Bram Moolenaar4770d092006-01-12 23:22:24 +000015884 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015885 *matchp = ga.ga_data;
15886 return ga.ga_len;
15887}
15888#endif
15889
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015890#endif /* FEAT_SPELL */